Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / cc / layers / video_layer_impl.cc
blob51719717e6d49698db972ee9c80580348524d490
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 TextureDrawQuad* texture_quad =
206 render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
207 texture_quad->SetNew(shared_quad_state,
208 quad_rect,
209 opaque_rect,
210 visible_quad_rect,
211 software_resources_[0],
212 premultiplied_alpha,
213 uv_top_left,
214 uv_bottom_right,
215 SK_ColorTRANSPARENT,
216 opacity,
217 flipped,
218 nearest_neighbor);
219 ValidateQuadResources(texture_quad);
220 break;
222 case VideoFrameExternalResources::YUV_RESOURCE: {
223 DCHECK_GE(frame_resources_.size(), 3u);
224 if (frame_resources_.size() < 3u)
225 break;
226 YUVVideoDrawQuad::ColorSpace color_space = YUVVideoDrawQuad::REC_601;
227 if (frame_->format() == media::VideoFrame::YV12J) {
228 color_space = YUVVideoDrawQuad::JPEG;
229 } else if (frame_->format() == media::VideoFrame::YV12HD) {
230 color_space = YUVVideoDrawQuad::REC_709;
233 const gfx::Size ya_tex_size = coded_size;
234 const gfx::Size uv_tex_size = media::VideoFrame::PlaneSize(
235 frame_->format(), media::VideoFrame::kUPlane, coded_size);
236 DCHECK(uv_tex_size ==
237 media::VideoFrame::PlaneSize(
238 frame_->format(), media::VideoFrame::kVPlane, coded_size));
239 if (frame_resources_.size() > 3) {
240 DCHECK(ya_tex_size ==
241 media::VideoFrame::PlaneSize(
242 frame_->format(), media::VideoFrame::kAPlane, coded_size));
245 gfx::RectF tex_coord_rect(
246 tex_x_offset, tex_y_offset, tex_width_scale, tex_height_scale);
247 YUVVideoDrawQuad* yuv_video_quad =
248 render_pass->CreateAndAppendDrawQuad<YUVVideoDrawQuad>();
249 yuv_video_quad->SetNew(
250 shared_quad_state, quad_rect, opaque_rect, visible_quad_rect,
251 tex_coord_rect, ya_tex_size, uv_tex_size, frame_resources_[0],
252 frame_resources_[1], frame_resources_[2],
253 frame_resources_.size() > 3 ? frame_resources_[3] : 0, color_space);
254 ValidateQuadResources(yuv_video_quad);
255 break;
257 case VideoFrameExternalResources::RGB_RESOURCE: {
258 DCHECK_EQ(frame_resources_.size(), 1u);
259 if (frame_resources_.size() < 1u)
260 break;
261 bool premultiplied_alpha = true;
262 gfx::PointF uv_top_left(0.f, 0.f);
263 gfx::PointF uv_bottom_right(tex_width_scale, tex_height_scale);
264 float opacity[] = {1.0f, 1.0f, 1.0f, 1.0f};
265 bool flipped = false;
266 bool nearest_neighbor = false;
267 TextureDrawQuad* texture_quad =
268 render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
269 texture_quad->SetNew(shared_quad_state,
270 quad_rect,
271 opaque_rect,
272 visible_quad_rect,
273 frame_resources_[0],
274 premultiplied_alpha,
275 uv_top_left,
276 uv_bottom_right,
277 SK_ColorTRANSPARENT,
278 opacity,
279 flipped,
280 nearest_neighbor);
281 ValidateQuadResources(texture_quad);
282 break;
284 case VideoFrameExternalResources::STREAM_TEXTURE_RESOURCE: {
285 DCHECK_EQ(frame_resources_.size(), 1u);
286 if (frame_resources_.size() < 1u)
287 break;
288 gfx::Transform scale;
289 scale.Scale(tex_width_scale, tex_height_scale);
290 StreamVideoDrawQuad* stream_video_quad =
291 render_pass->CreateAndAppendDrawQuad<StreamVideoDrawQuad>();
292 stream_video_quad->SetNew(
293 shared_quad_state, quad_rect, opaque_rect, visible_quad_rect,
294 frame_resources_[0],
295 scale * provider_client_impl_->StreamTextureMatrix());
296 ValidateQuadResources(stream_video_quad);
297 break;
299 case VideoFrameExternalResources::IO_SURFACE: {
300 DCHECK_EQ(frame_resources_.size(), 1u);
301 if (frame_resources_.size() < 1u)
302 break;
303 IOSurfaceDrawQuad* io_surface_quad =
304 render_pass->CreateAndAppendDrawQuad<IOSurfaceDrawQuad>();
305 io_surface_quad->SetNew(shared_quad_state,
306 quad_rect,
307 opaque_rect,
308 visible_quad_rect,
309 visible_rect.size(),
310 frame_resources_[0],
311 IOSurfaceDrawQuad::UNFLIPPED);
312 ValidateQuadResources(io_surface_quad);
313 break;
315 #if defined(VIDEO_HOLE)
316 // This block and other blocks wrapped around #if defined(VIDEO_HOLE) is not
317 // maintained by the general compositor team. Please contact the following
318 // people instead:
320 // wonsik@chromium.org
321 // lcwu@chromium.org
322 case VideoFrameExternalResources::HOLE: {
323 DCHECK_EQ(frame_resources_.size(), 0u);
324 SolidColorDrawQuad* solid_color_draw_quad =
325 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
327 // Create a solid color quad with transparent black and force no
328 // blending / no anti-aliasing.
329 gfx::Rect opaque_rect = quad_rect;
330 solid_color_draw_quad->SetAll(shared_quad_state,
331 quad_rect,
332 opaque_rect,
333 visible_quad_rect,
334 false,
335 SK_ColorTRANSPARENT,
336 true);
337 break;
339 #endif // defined(VIDEO_HOLE)
340 case VideoFrameExternalResources::NONE:
341 NOTIMPLEMENTED();
342 break;
346 void VideoLayerImpl::DidDraw(ResourceProvider* resource_provider) {
347 LayerImpl::DidDraw(resource_provider);
349 DCHECK(frame_.get());
351 if (frame_resource_type_ ==
352 VideoFrameExternalResources::SOFTWARE_RESOURCE) {
353 for (size_t i = 0; i < software_resources_.size(); ++i) {
354 software_release_callback_.Run(
355 0, false, layer_tree_impl()->BlockingMainThreadTaskRunner());
358 software_resources_.clear();
359 software_release_callback_.Reset();
360 } else {
361 for (size_t i = 0; i < frame_resources_.size(); ++i)
362 resource_provider->DeleteResource(frame_resources_[i]);
363 frame_resources_.clear();
366 provider_client_impl_->PutCurrentFrame();
367 frame_ = nullptr;
369 provider_client_impl_->ReleaseLock();
372 void VideoLayerImpl::ReleaseResources() {
373 updater_ = nullptr;
376 void VideoLayerImpl::SetNeedsRedraw() {
377 SetUpdateRect(gfx::UnionRects(update_rect(), gfx::Rect(bounds())));
378 layer_tree_impl()->SetNeedsRedraw();
381 const char* VideoLayerImpl::LayerTypeAsString() const {
382 return "cc::VideoLayerImpl";
385 } // namespace cc