1 // Copyright 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/layers/video_layer_impl.h"
8 #include "base/logging.h"
9 #include "cc/layers/video_frame_provider_client_impl.h"
10 #include "cc/quads/io_surface_draw_quad.h"
11 #include "cc/quads/stream_video_draw_quad.h"
12 #include "cc/quads/texture_draw_quad.h"
13 #include "cc/quads/yuv_video_draw_quad.h"
14 #include "cc/resources/resource_provider.h"
15 #include "cc/resources/single_release_callback_impl.h"
16 #include "cc/trees/layer_tree_impl.h"
17 #include "cc/trees/occlusion.h"
18 #include "cc/trees/proxy.h"
19 #include "media/base/video_frame.h"
21 #if defined(VIDEO_HOLE)
22 #include "cc/quads/solid_color_draw_quad.h"
23 #endif // defined(VIDEO_HOLE)
28 scoped_ptr
<VideoLayerImpl
> VideoLayerImpl::Create(
29 LayerTreeImpl
* tree_impl
,
31 VideoFrameProvider
* provider
,
32 media::VideoRotation video_rotation
) {
33 DCHECK(tree_impl
->proxy()->IsMainThreadBlocked());
34 DCHECK(tree_impl
->proxy()->IsImplThread());
36 scoped_refptr
<VideoFrameProviderClientImpl
> provider_client_impl
=
37 VideoFrameProviderClientImpl::Create(
38 provider
, tree_impl
->GetVideoFrameControllerClient());
40 return make_scoped_ptr(
41 new VideoLayerImpl(tree_impl
, id
, provider_client_impl
, video_rotation
));
44 VideoLayerImpl::VideoLayerImpl(
45 LayerTreeImpl
* tree_impl
,
47 const scoped_refptr
<VideoFrameProviderClientImpl
>& provider_client_impl
,
48 media::VideoRotation video_rotation
)
49 : LayerImpl(tree_impl
, id
),
50 provider_client_impl_(provider_client_impl
),
52 video_rotation_(video_rotation
) {
55 VideoLayerImpl::~VideoLayerImpl() {
56 if (!provider_client_impl_
->Stopped()) {
57 // In impl side painting, we may have a pending and active layer
58 // associated with the video provider at the same time. Both have a ref
59 // on the VideoFrameProviderClientImpl, but we stop when the first
60 // LayerImpl (the one on the pending tree) is destroyed since we know
61 // the main thread is blocked for this commit.
62 DCHECK(layer_tree_impl()->proxy()->IsImplThread());
63 DCHECK(layer_tree_impl()->proxy()->IsMainThreadBlocked());
64 provider_client_impl_
->Stop();
68 scoped_ptr
<LayerImpl
> VideoLayerImpl::CreateLayerImpl(
69 LayerTreeImpl
* tree_impl
) {
70 return make_scoped_ptr(new VideoLayerImpl(
71 tree_impl
, id(), provider_client_impl_
, video_rotation_
));
74 void VideoLayerImpl::DidBecomeActive() {
75 provider_client_impl_
->SetActiveVideoLayer(this);
78 bool VideoLayerImpl::WillDraw(DrawMode draw_mode
,
79 ResourceProvider
* resource_provider
) {
80 if (draw_mode
== DRAW_MODE_RESOURCELESS_SOFTWARE
)
83 // Explicitly acquire and release the provider mutex so it can be held from
84 // WillDraw to DidDraw. Since the compositor thread is in the middle of
85 // drawing, the layer will not be destroyed before DidDraw is called.
86 // Therefore, the only thing that will prevent this lock from being released
87 // is the GPU process locking it. As the GPU process can't cause the
88 // destruction of the provider (calling StopUsingProvider), holding this
89 // lock should not cause a deadlock.
90 frame_
= provider_client_impl_
->AcquireLockAndCurrentFrame();
93 // Drop any resources used by the updater if there is no frame to display.
96 provider_client_impl_
->ReleaseLock();
100 if (!LayerImpl::WillDraw(draw_mode
, resource_provider
))
105 new VideoResourceUpdater(layer_tree_impl()->context_provider(),
106 layer_tree_impl()->resource_provider()));
109 VideoFrameExternalResources external_resources
=
110 updater_
->CreateExternalResourcesFromVideoFrame(frame_
);
111 frame_resource_type_
= external_resources
.type
;
113 if (external_resources
.type
==
114 VideoFrameExternalResources::SOFTWARE_RESOURCE
) {
115 software_resources_
= external_resources
.software_resources
;
116 software_release_callback_
=
117 external_resources
.software_release_callback
;
121 DCHECK_EQ(external_resources
.mailboxes
.size(),
122 external_resources
.release_callbacks
.size());
123 for (size_t i
= 0; i
< external_resources
.mailboxes
.size(); ++i
) {
124 unsigned resource_id
= resource_provider
->CreateResourceFromTextureMailbox(
125 external_resources
.mailboxes
[i
],
126 SingleReleaseCallbackImpl::Create(
127 external_resources
.release_callbacks
[i
]));
128 frame_resources_
.push_back(resource_id
);
134 void VideoLayerImpl::AppendQuads(RenderPass
* render_pass
,
135 AppendQuadsData
* append_quads_data
) {
136 DCHECK(frame_
.get());
138 gfx::Transform transform
= draw_transform();
139 gfx::Size rotated_size
= content_bounds();
141 switch (video_rotation_
) {
142 case media::VIDEO_ROTATION_90
:
143 rotated_size
= gfx::Size(rotated_size
.height(), rotated_size
.width());
144 transform
.Rotate(90.0);
145 transform
.Translate(0.0, -rotated_size
.height());
147 case media::VIDEO_ROTATION_180
:
148 transform
.Rotate(180.0);
149 transform
.Translate(-rotated_size
.width(), -rotated_size
.height());
151 case media::VIDEO_ROTATION_270
:
152 rotated_size
= gfx::Size(rotated_size
.height(), rotated_size
.width());
153 transform
.Rotate(270.0);
154 transform
.Translate(-rotated_size
.width(), 0);
155 case media::VIDEO_ROTATION_0
:
159 SharedQuadState
* shared_quad_state
=
160 render_pass
->CreateAndAppendSharedQuadState();
161 shared_quad_state
->SetAll(transform
, rotated_size
, visible_content_rect(),
162 clip_rect(), is_clipped(), draw_opacity(),
163 draw_blend_mode(), sorting_context_id());
165 AppendDebugBorderQuad(
166 render_pass
, rotated_size
, shared_quad_state
, append_quads_data
);
168 gfx::Rect
quad_rect(rotated_size
);
169 gfx::Rect
opaque_rect(contents_opaque() ? quad_rect
: gfx::Rect());
170 gfx::Rect visible_rect
= frame_
->visible_rect();
171 gfx::Size coded_size
= frame_
->coded_size();
173 Occlusion occlusion_in_video_space
=
175 .occlusion_in_content_space
.GetOcclusionWithGivenDrawTransform(
177 gfx::Rect visible_quad_rect
=
178 occlusion_in_video_space
.GetUnoccludedContentRect(quad_rect
);
179 if (visible_quad_rect
.IsEmpty())
182 // Pixels for macroblocked formats.
183 const float tex_width_scale
=
184 static_cast<float>(visible_rect
.width()) / coded_size
.width();
185 const float tex_height_scale
=
186 static_cast<float>(visible_rect
.height()) / coded_size
.height();
187 const float tex_x_offset
=
188 static_cast<float>(visible_rect
.x()) / coded_size
.width();
189 const float tex_y_offset
=
190 static_cast<float>(visible_rect
.y()) / coded_size
.height();
192 switch (frame_resource_type_
) {
193 // TODO(danakj): Remove this, hide it in the hardware path.
194 case VideoFrameExternalResources::SOFTWARE_RESOURCE
: {
195 DCHECK_EQ(frame_resources_
.size(), 0u);
196 DCHECK_EQ(software_resources_
.size(), 1u);
197 if (software_resources_
.size() < 1u)
199 bool premultiplied_alpha
= true;
200 gfx::PointF
uv_top_left(0.f
, 0.f
);
201 gfx::PointF
uv_bottom_right(tex_width_scale
, tex_height_scale
);
202 float opacity
[] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
203 bool flipped
= false;
204 bool nearest_neighbor
= false;
205 // TODO(danakj): crbug.com/455931
206 layer_tree_impl()->resource_provider()->ValidateResource(
207 software_resources_
[0]);
208 TextureDrawQuad
* texture_quad
=
209 render_pass
->CreateAndAppendDrawQuad
<TextureDrawQuad
>();
210 texture_quad
->SetNew(shared_quad_state
,
214 software_resources_
[0],
224 case VideoFrameExternalResources::YUV_RESOURCE
: {
225 DCHECK_GE(frame_resources_
.size(), 3u);
226 if (frame_resources_
.size() < 3u)
228 YUVVideoDrawQuad::ColorSpace color_space
= YUVVideoDrawQuad::REC_601
;
229 if (frame_
->format() == media::VideoFrame::YV12J
) {
230 color_space
= YUVVideoDrawQuad::JPEG
;
231 } else if (frame_
->format() == media::VideoFrame::YV12HD
) {
232 color_space
= YUVVideoDrawQuad::REC_709
;
235 const gfx::Size ya_tex_size
= coded_size
;
236 const gfx::Size uv_tex_size
= media::VideoFrame::PlaneSize(
237 frame_
->format(), media::VideoFrame::kUPlane
, coded_size
);
238 DCHECK(uv_tex_size
==
239 media::VideoFrame::PlaneSize(
240 frame_
->format(), media::VideoFrame::kVPlane
, coded_size
));
241 if (frame_resources_
.size() > 3) {
242 DCHECK(ya_tex_size
==
243 media::VideoFrame::PlaneSize(
244 frame_
->format(), media::VideoFrame::kAPlane
, coded_size
));
247 // TODO(danakj): crbug.com/455931
248 layer_tree_impl()->resource_provider()->ValidateResource(
249 frame_resources_
[0]);
250 layer_tree_impl()->resource_provider()->ValidateResource(
251 frame_resources_
[1]);
252 layer_tree_impl()->resource_provider()->ValidateResource(
253 frame_resources_
[2]);
254 if (frame_resources_
.size() > 3) {
255 layer_tree_impl()->resource_provider()->ValidateResource(
256 frame_resources_
[3]);
258 gfx::RectF
tex_coord_rect(
259 tex_x_offset
, tex_y_offset
, tex_width_scale
, tex_height_scale
);
260 YUVVideoDrawQuad
* yuv_video_quad
=
261 render_pass
->CreateAndAppendDrawQuad
<YUVVideoDrawQuad
>();
262 yuv_video_quad
->SetNew(
263 shared_quad_state
, quad_rect
, opaque_rect
, visible_quad_rect
,
264 tex_coord_rect
, ya_tex_size
, uv_tex_size
, frame_resources_
[0],
265 frame_resources_
[1], frame_resources_
[2],
266 frame_resources_
.size() > 3 ? frame_resources_
[3] : 0, color_space
);
269 case VideoFrameExternalResources::RGB_RESOURCE
: {
270 DCHECK_EQ(frame_resources_
.size(), 1u);
271 if (frame_resources_
.size() < 1u)
273 bool premultiplied_alpha
= true;
274 gfx::PointF
uv_top_left(0.f
, 0.f
);
275 gfx::PointF
uv_bottom_right(tex_width_scale
, tex_height_scale
);
276 float opacity
[] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
277 bool flipped
= false;
278 bool nearest_neighbor
= false;
279 // TODO(danakj): crbug.com/455931
280 layer_tree_impl()->resource_provider()->ValidateResource(
281 frame_resources_
[0]);
282 TextureDrawQuad
* texture_quad
=
283 render_pass
->CreateAndAppendDrawQuad
<TextureDrawQuad
>();
284 texture_quad
->SetNew(shared_quad_state
,
298 case VideoFrameExternalResources::STREAM_TEXTURE_RESOURCE
: {
299 DCHECK_EQ(frame_resources_
.size(), 1u);
300 if (frame_resources_
.size() < 1u)
302 // TODO(danakj): crbug.com/455931
303 layer_tree_impl()->resource_provider()->ValidateResource(
304 frame_resources_
[0]);
305 gfx::Transform scale
;
306 scale
.Scale(tex_width_scale
, tex_height_scale
);
307 StreamVideoDrawQuad
* stream_video_quad
=
308 render_pass
->CreateAndAppendDrawQuad
<StreamVideoDrawQuad
>();
309 stream_video_quad
->SetNew(
310 shared_quad_state
, quad_rect
, opaque_rect
, visible_quad_rect
,
312 scale
* provider_client_impl_
->StreamTextureMatrix());
315 case VideoFrameExternalResources::IO_SURFACE
: {
316 DCHECK_EQ(frame_resources_
.size(), 1u);
317 if (frame_resources_
.size() < 1u)
319 // TODO(danakj): crbug.com/455931
320 layer_tree_impl()->resource_provider()->ValidateResource(
321 frame_resources_
[0]);
322 IOSurfaceDrawQuad
* io_surface_quad
=
323 render_pass
->CreateAndAppendDrawQuad
<IOSurfaceDrawQuad
>();
324 io_surface_quad
->SetNew(shared_quad_state
,
330 IOSurfaceDrawQuad::UNFLIPPED
);
333 #if defined(VIDEO_HOLE)
334 // This block and other blocks wrapped around #if defined(VIDEO_HOLE) is not
335 // maintained by the general compositor team. Please contact the following
338 // wonsik@chromium.org
340 case VideoFrameExternalResources::HOLE
: {
341 DCHECK_EQ(frame_resources_
.size(), 0u);
342 SolidColorDrawQuad
* solid_color_draw_quad
=
343 render_pass
->CreateAndAppendDrawQuad
<SolidColorDrawQuad
>();
345 // Create a solid color quad with transparent black and force no
346 // blending / no anti-aliasing.
347 gfx::Rect opaque_rect
= quad_rect
;
348 solid_color_draw_quad
->SetAll(shared_quad_state
,
357 #endif // defined(VIDEO_HOLE)
358 case VideoFrameExternalResources::NONE
:
364 void VideoLayerImpl::DidDraw(ResourceProvider
* resource_provider
) {
365 LayerImpl::DidDraw(resource_provider
);
367 DCHECK(frame_
.get());
369 if (frame_resource_type_
==
370 VideoFrameExternalResources::SOFTWARE_RESOURCE
) {
371 for (size_t i
= 0; i
< software_resources_
.size(); ++i
) {
372 software_release_callback_
.Run(
373 0, false, layer_tree_impl()->BlockingMainThreadTaskRunner());
376 software_resources_
.clear();
377 software_release_callback_
.Reset();
379 for (size_t i
= 0; i
< frame_resources_
.size(); ++i
)
380 resource_provider
->DeleteResource(frame_resources_
[i
]);
381 frame_resources_
.clear();
384 provider_client_impl_
->PutCurrentFrame();
387 provider_client_impl_
->ReleaseLock();
390 void VideoLayerImpl::ReleaseResources() {
394 void VideoLayerImpl::SetNeedsRedraw() {
395 SetUpdateRect(gfx::UnionRects(update_rect(), gfx::Rect(bounds())));
396 layer_tree_impl()->SetNeedsRedraw();
399 const char* VideoLayerImpl::LayerTypeAsString() const {
400 return "cc::VideoLayerImpl";