1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
gpu / command_buffer / service / shared_image / video_surface_texture_image_backing.cc [blame]
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "gpu/command_buffer/service/shared_image/video_surface_texture_image_backing.h"
#include <utility>
#include "base/task/single_thread_task_runner.h"
#include "components/viz/common/resources/resource_sizes.h"
#include "gpu/command_buffer/common/shared_image_usage.h"
#include "gpu/command_buffer/service/abstract_texture_android.h"
#include "gpu/command_buffer/service/memory_tracking.h"
#include "gpu/command_buffer/service/shared_context_state.h"
#include "gpu/command_buffer/service/shared_image/shared_image_representation.h"
#include "gpu/command_buffer/service/shared_image/skia_gl_image_representation.h"
#include "gpu/command_buffer/service/skia_utils.h"
#include "gpu/command_buffer/service/texture_manager.h"
#include "gpu/command_buffer/service/texture_owner.h"
#include "third_party/skia/include/gpu/ganesh/GrBackendSemaphore.h"
#include "third_party/skia/include/gpu/ganesh/GrBackendSurface.h"
#include "ui/gl/gl_utils.h"
namespace gpu {
VideoSurfaceTextureImageBacking::VideoSurfaceTextureImageBacking(
const Mailbox& mailbox,
const gfx::Size& size,
const gfx::ColorSpace color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
std::string debug_label,
scoped_refptr<StreamTextureSharedImageInterface> stream_texture_sii,
scoped_refptr<SharedContextState> context_state)
: AndroidVideoImageBacking(mailbox,
size,
color_space,
surface_origin,
alpha_type,
std::move(debug_label),
/*is_thread_safe=*/false),
stream_texture_sii_(std::move(stream_texture_sii)),
context_state_(std::move(context_state)),
gpu_main_task_runner_(base::SingleThreadTaskRunner::GetCurrentDefault()) {
DCHECK(stream_texture_sii_);
DCHECK(context_state_);
context_state_->AddContextLostObserver(this);
}
VideoSurfaceTextureImageBacking::~VideoSurfaceTextureImageBacking() {
DCHECK(gpu_main_task_runner_->RunsTasksInCurrentSequence());
if (context_state_)
context_state_->RemoveContextLostObserver(this);
context_state_.reset();
stream_texture_sii_->ReleaseResources();
stream_texture_sii_.reset();
}
void VideoSurfaceTextureImageBacking::OnContextLost() {
DCHECK(gpu_main_task_runner_->RunsTasksInCurrentSequence());
// We release codec buffers when shared image context is lost. This is
// because texture owner's texture was created on shared context. Once
// shared context is lost, no one should try to use that texture.
stream_texture_sii_->ReleaseResources();
context_state_->RemoveContextLostObserver(this);
context_state_ = nullptr;
}
// Representation of VideoSurfaceTextureImageBacking as a GL Texture.
class VideoSurfaceTextureImageBacking::GLTextureVideoImageRepresentation
: public GLTextureImageRepresentation {
public:
GLTextureVideoImageRepresentation(
SharedImageManager* manager,
VideoSurfaceTextureImageBacking* backing,
MemoryTypeTracker* tracker,
std::unique_ptr<AbstractTextureAndroid> texture)
: GLTextureImageRepresentation(manager, backing, tracker),
texture_(std::move(texture)) {}
~GLTextureVideoImageRepresentation() override {
if (!has_context()) {
texture_->NotifyOnContextLost();
}
}
// Disallow copy and assign.
GLTextureVideoImageRepresentation(const GLTextureVideoImageRepresentation&) =
delete;
GLTextureVideoImageRepresentation& operator=(
const GLTextureVideoImageRepresentation&) = delete;
gles2::Texture* GetTexture(int plane_index) override {
DCHECK_EQ(plane_index, 0);
auto* texture = gles2::Texture::CheckedCast(texture_->GetTextureBase());
DCHECK(texture);
return texture;
}
bool BeginAccess(GLenum mode) override {
// This representation should only be called for read.
DCHECK(mode == GL_SHARED_IMAGE_ACCESS_MODE_READ_CHROMIUM);
auto* video_backing =
static_cast<VideoSurfaceTextureImageBacking*>(backing());
video_backing->BeginGLReadAccess(texture_->service_id());
return true;
}
void EndAccess() override {}
private:
std::unique_ptr<AbstractTextureAndroid> texture_;
};
// Representation of VideoSurfaceTextureImageBacking as a GL Texture.
class VideoSurfaceTextureImageBacking::
GLTexturePassthroughVideoImageRepresentation
: public GLTexturePassthroughImageRepresentation {
public:
GLTexturePassthroughVideoImageRepresentation(
SharedImageManager* manager,
VideoSurfaceTextureImageBacking* backing,
MemoryTypeTracker* tracker,
std::unique_ptr<AbstractTextureAndroid> abstract_texture)
: GLTexturePassthroughImageRepresentation(manager, backing, tracker),
abstract_texture_(std::move(abstract_texture)),
passthrough_texture_(gles2::TexturePassthrough::CheckedCast(
abstract_texture_->GetTextureBase())) {
// TODO(crbug.com/40166788): Remove this CHECK.
CHECK(passthrough_texture_);
}
~GLTexturePassthroughVideoImageRepresentation() override {
if (!has_context()) {
abstract_texture_->NotifyOnContextLost();
}
}
// Disallow copy and assign.
GLTexturePassthroughVideoImageRepresentation(
const GLTexturePassthroughVideoImageRepresentation&) = delete;
GLTexturePassthroughVideoImageRepresentation& operator=(
const GLTexturePassthroughVideoImageRepresentation&) = delete;
const scoped_refptr<gles2::TexturePassthrough>& GetTexturePassthrough(
int plane_index) override {
DCHECK_EQ(plane_index, 0);
return passthrough_texture_;
}
bool BeginAccess(GLenum mode) override {
// This representation should only be called for read.
DCHECK(mode == GL_SHARED_IMAGE_ACCESS_MODE_READ_CHROMIUM);
auto* video_backing =
static_cast<VideoSurfaceTextureImageBacking*>(backing());
video_backing->BeginGLReadAccess(passthrough_texture_->service_id());
return true;
}
void EndAccess() override {}
private:
std::unique_ptr<AbstractTextureAndroid> abstract_texture_;
scoped_refptr<gles2::TexturePassthrough> passthrough_texture_;
};
std::unique_ptr<GLTextureImageRepresentation>
VideoSurfaceTextureImageBacking::ProduceGLTexture(SharedImageManager* manager,
MemoryTypeTracker* tracker) {
// Note that for DrDc this method will never be called for
// this(SurfaceTexture) implementation and for Webview, this method is called
// only on gpu main thread.
DCHECK(gpu_main_task_runner_->RunsTasksInCurrentSequence());
// For (old) overlays, we don't have a texture owner, but overlay promotion
// might not happen for some reasons. In that case, it will try to draw
// which should result in no image.
if (!stream_texture_sii_->HasTextureOwner())
return nullptr;
// Generate an abstract texture.
auto texture = GenAbstractTexture(/*passthrough=*/false);
if (!texture)
return nullptr;
// If TextureOwner binds texture implicitly on update, that means it will
// use TextureOwner texture_id to update and bind. Hence use TextureOwner
// texture_id in abstract texture via BindToServiceId().
DCHECK(stream_texture_sii_->TextureOwnerBindsTextureOnUpdate());
texture->BindToServiceId(stream_texture_sii_->GetTextureBase()->service_id());
return std::make_unique<GLTextureVideoImageRepresentation>(
manager, this, tracker, std::move(texture));
}
std::unique_ptr<GLTexturePassthroughImageRepresentation>
VideoSurfaceTextureImageBacking::ProduceGLTexturePassthrough(
SharedImageManager* manager,
MemoryTypeTracker* tracker) {
DCHECK(gpu_main_task_runner_->RunsTasksInCurrentSequence());
// For (old) overlays, we don't have a texture owner, but overlay promotion
// might not happen for some reasons. In that case, it will try to draw
// which should result in no image.
if (!stream_texture_sii_->HasTextureOwner())
return nullptr;
// Generate an abstract texture.
auto texture = GenAbstractTexture(/*passthrough=*/true);
if (!texture)
return nullptr;
// If TextureOwner binds texture implicitly on update, that means it will
// use TextureOwner texture_id to update and bind. Hence use TextureOwner
// texture_id in abstract texture via BindToServiceId().
DCHECK(stream_texture_sii_->TextureOwnerBindsTextureOnUpdate());
texture->BindToServiceId(stream_texture_sii_->GetTextureBase()->service_id());
return std::make_unique<GLTexturePassthroughVideoImageRepresentation>(
manager, this, tracker, std::move(texture));
}
std::unique_ptr<SkiaGaneshImageRepresentation>
VideoSurfaceTextureImageBacking::ProduceSkiaGanesh(
SharedImageManager* manager,
MemoryTypeTracker* tracker,
scoped_refptr<SharedContextState> context_state) {
DCHECK(gpu_main_task_runner_->RunsTasksInCurrentSequence());
DCHECK(context_state);
// For (old) overlays, we don't have a texture owner, but overlay promotion
// might not happen for some reasons. In that case, it will try to draw
// which should result in no image.
if (!stream_texture_sii_->HasTextureOwner())
return nullptr;
if (!context_state->GrContextIsGL()) {
DCHECK(false);
return nullptr;
}
DCHECK(context_state->GrContextIsGL());
auto* texture_base = stream_texture_sii_->GetTextureBase();
DCHECK(texture_base);
const bool passthrough =
(texture_base->GetType() == gpu::TextureBase::Type::kPassthrough);
auto texture = GenAbstractTexture(passthrough);
if (!texture)
return nullptr;
// If TextureOwner binds texture implicitly on update, that means it will
// use TextureOwner texture_id to update and bind. Hence use TextureOwner
// texture_id in abstract texture via BindToServiceId().
DCHECK(stream_texture_sii_->TextureOwnerBindsTextureOnUpdate());
texture->BindToServiceId(stream_texture_sii_->GetTextureBase()->service_id());
std::unique_ptr<gpu::GLTextureImageRepresentationBase> gl_representation;
if (passthrough) {
gl_representation =
std::make_unique<GLTexturePassthroughVideoImageRepresentation>(
manager, this, tracker, std::move(texture));
} else {
gl_representation = std::make_unique<GLTextureVideoImageRepresentation>(
manager, this, tracker, std::move(texture));
}
return SkiaGLImageRepresentation::Create(std::move(gl_representation),
std::move(context_state), manager,
this, tracker);
}
void VideoSurfaceTextureImageBacking::BeginGLReadAccess(
const GLuint service_id) {
stream_texture_sii_->UpdateAndBindTexImage();
}
// Representation of VideoSurfaceTextureImageBacking as an overlay plane.
class VideoSurfaceTextureImageBacking::OverlayVideoImageRepresentation
: public gpu::LegacyOverlayImageRepresentation {
public:
OverlayVideoImageRepresentation(gpu::SharedImageManager* manager,
VideoSurfaceTextureImageBacking* backing,
gpu::MemoryTypeTracker* tracker)
: gpu::LegacyOverlayImageRepresentation(manager, backing, tracker) {}
// Disallow copy and assign.
OverlayVideoImageRepresentation(const OverlayVideoImageRepresentation&) =
delete;
OverlayVideoImageRepresentation& operator=(
const OverlayVideoImageRepresentation&) = delete;
protected:
void RenderToOverlay() override {
DCHECK(!stream_image()->HasTextureOwner())
<< "CodecImage must be already in overlay";
TRACE_EVENT0("media", "OverlayVideoImageRepresentation::RenderToOverlay");
stream_image()->RenderToOverlay();
}
void NotifyOverlayPromotion(bool promotion,
const gfx::Rect& bounds) override {
stream_image()->NotifyOverlayPromotion(promotion, bounds);
}
private:
StreamTextureSharedImageInterface* stream_image() {
auto* video_backing =
static_cast<VideoSurfaceTextureImageBacking*>(backing());
DCHECK(video_backing);
return video_backing->stream_texture_sii_.get();
}
};
std::unique_ptr<gpu::LegacyOverlayImageRepresentation>
VideoSurfaceTextureImageBacking::ProduceLegacyOverlay(
gpu::SharedImageManager* manager,
gpu::MemoryTypeTracker* tracker) {
DCHECK(gpu_main_task_runner_->RunsTasksInCurrentSequence());
return std::make_unique<OverlayVideoImageRepresentation>(manager, this,
tracker);
}
} // namespace gpu