Disable flaky SelectFileDialogExtensionBrowserTest tests
[chromium-blink-merge.git] / cc / layers / video_layer_impl.cc
blob5c36e9945eff01a25fb73d46648579a216c3dada
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"
7 #include "base/bind.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)
25 namespace cc {
27 // static
28 scoped_ptr<VideoLayerImpl> VideoLayerImpl::Create(
29 LayerTreeImpl* tree_impl,
30 int id,
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,
46 int id,
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),
51 frame_(nullptr),
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)
81 return false;
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();
92 if (!frame_.get()) {
93 // Drop any resources used by the updater if there is no frame to display.
94 updater_ = nullptr;
96 provider_client_impl_->ReleaseLock();
97 return false;
100 if (!LayerImpl::WillDraw(draw_mode, resource_provider))
101 return false;
103 if (!updater_) {
104 updater_.reset(
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;
118 return true;
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);
131 return true;
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());
146 break;
147 case media::VIDEO_ROTATION_180:
148 transform.Rotate(180.0);
149 transform.Translate(-rotated_size.width(), -rotated_size.height());
150 break;
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:
156 break;
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 =
174 draw_properties()
175 .occlusion_in_content_space.GetOcclusionWithGivenDrawTransform(
176 transform);
177 gfx::Rect visible_quad_rect =
178 occlusion_in_video_space.GetUnoccludedContentRect(quad_rect);
179 if (visible_quad_rect.IsEmpty())
180 return;
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)
198 break;
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,
211 quad_rect,
212 opaque_rect,
213 visible_quad_rect,
214 software_resources_[0],
215 premultiplied_alpha,
216 uv_top_left,
217 uv_bottom_right,
218 SK_ColorTRANSPARENT,
219 opacity,
220 flipped,
221 nearest_neighbor);
222 break;
224 case VideoFrameExternalResources::YUV_RESOURCE: {
225 DCHECK_GE(frame_resources_.size(), 3u);
226 if (frame_resources_.size() < 3u)
227 break;
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);
267 break;
269 case VideoFrameExternalResources::RGB_RESOURCE: {
270 DCHECK_EQ(frame_resources_.size(), 1u);
271 if (frame_resources_.size() < 1u)
272 break;
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,
285 quad_rect,
286 opaque_rect,
287 visible_quad_rect,
288 frame_resources_[0],
289 premultiplied_alpha,
290 uv_top_left,
291 uv_bottom_right,
292 SK_ColorTRANSPARENT,
293 opacity,
294 flipped,
295 nearest_neighbor);
296 break;
298 case VideoFrameExternalResources::STREAM_TEXTURE_RESOURCE: {
299 DCHECK_EQ(frame_resources_.size(), 1u);
300 if (frame_resources_.size() < 1u)
301 break;
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,
311 frame_resources_[0],
312 scale * provider_client_impl_->StreamTextureMatrix());
313 break;
315 case VideoFrameExternalResources::IO_SURFACE: {
316 DCHECK_EQ(frame_resources_.size(), 1u);
317 if (frame_resources_.size() < 1u)
318 break;
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,
325 quad_rect,
326 opaque_rect,
327 visible_quad_rect,
328 visible_rect.size(),
329 frame_resources_[0],
330 IOSurfaceDrawQuad::UNFLIPPED);
331 break;
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
336 // people instead:
338 // wonsik@chromium.org
339 // lcwu@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,
349 quad_rect,
350 opaque_rect,
351 visible_quad_rect,
352 false,
353 SK_ColorTRANSPARENT,
354 true);
355 break;
357 #endif // defined(VIDEO_HOLE)
358 case VideoFrameExternalResources::NONE:
359 NOTIMPLEMENTED();
360 break;
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();
378 } else {
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();
385 frame_ = nullptr;
387 provider_client_impl_->ReleaseLock();
390 void VideoLayerImpl::ReleaseResources() {
391 updater_ = nullptr;
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";
403 } // namespace cc