Roll src/third_party/WebKit 37b70da:e3be828 (svn 200645:200646)
[chromium-blink-merge.git] / media / blink / video_frame_compositor.cc
blob87d79715f648941793bddd2b7a5a2167ba9a5630
1 // Copyright 2014 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 "media/blink/video_frame_compositor.h"
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/time/default_tick_clock.h"
10 #include "base/trace_event/trace_event.h"
11 #include "media/base/video_frame.h"
13 namespace media {
15 // Amount of time to wait between UpdateCurrentFrame() callbacks before starting
16 // background rendering to keep the Render() callbacks moving.
17 const int kBackgroundRenderingTimeoutMs = 250;
19 // Returns true if the format has no Alpha channel (hence is always opaque).
20 static bool IsOpaque(const scoped_refptr<VideoFrame>& frame) {
21 switch (frame->format()) {
22 case PIXEL_FORMAT_UNKNOWN:
23 case PIXEL_FORMAT_YV12:
24 case PIXEL_FORMAT_I420:
25 case PIXEL_FORMAT_YV16:
26 case PIXEL_FORMAT_YV24:
27 case PIXEL_FORMAT_NV12:
28 case PIXEL_FORMAT_XRGB:
29 return true;
30 case PIXEL_FORMAT_YV12A:
31 case PIXEL_FORMAT_ARGB:
32 break;
34 return false;
37 VideoFrameCompositor::VideoFrameCompositor(
38 const scoped_refptr<base::SingleThreadTaskRunner>& compositor_task_runner,
39 const base::Callback<void(gfx::Size)>& natural_size_changed_cb,
40 const base::Callback<void(bool)>& opacity_changed_cb)
41 : compositor_task_runner_(compositor_task_runner),
42 tick_clock_(new base::DefaultTickClock()),
43 natural_size_changed_cb_(natural_size_changed_cb),
44 opacity_changed_cb_(opacity_changed_cb),
45 background_rendering_enabled_(true),
46 background_rendering_timer_(
47 FROM_HERE,
48 base::TimeDelta::FromMilliseconds(kBackgroundRenderingTimeoutMs),
49 base::Bind(&VideoFrameCompositor::BackgroundRender,
50 base::Unretained(this)),
51 // Task is not repeating, CallRender() will reset the task as needed.
52 false),
53 client_(nullptr),
54 rendering_(false),
55 rendered_last_frame_(false),
56 is_background_rendering_(false),
57 new_background_frame_(false),
58 // Assume 60Hz before the first UpdateCurrentFrame() call.
59 last_interval_(base::TimeDelta::FromSecondsD(1.0 / 60)),
60 callback_(nullptr) {
61 background_rendering_timer_.SetTaskRunner(compositor_task_runner_);
64 VideoFrameCompositor::~VideoFrameCompositor() {
65 DCHECK(compositor_task_runner_->BelongsToCurrentThread());
66 DCHECK(!callback_);
67 DCHECK(!rendering_);
68 if (client_)
69 client_->StopUsingProvider();
72 void VideoFrameCompositor::OnRendererStateUpdate(bool new_state) {
73 DCHECK(compositor_task_runner_->BelongsToCurrentThread());
74 DCHECK_NE(rendering_, new_state);
75 rendering_ = new_state;
77 if (rendering_) {
78 // Always start playback in background rendering mode, if |client_| kicks
79 // in right away it's okay.
80 BackgroundRender();
81 } else if (background_rendering_enabled_) {
82 background_rendering_timer_.Stop();
83 } else {
84 DCHECK(!background_rendering_timer_.IsRunning());
87 if (!client_)
88 return;
90 if (rendering_)
91 client_->StartRendering();
92 else
93 client_->StopRendering();
96 void VideoFrameCompositor::SetVideoFrameProviderClient(
97 cc::VideoFrameProvider::Client* client) {
98 DCHECK(compositor_task_runner_->BelongsToCurrentThread());
99 if (client_)
100 client_->StopUsingProvider();
101 client_ = client;
103 // |client_| may now be null, so verify before calling it.
104 if (rendering_ && client_)
105 client_->StartRendering();
108 scoped_refptr<VideoFrame> VideoFrameCompositor::GetCurrentFrame() {
109 DCHECK(compositor_task_runner_->BelongsToCurrentThread());
110 return current_frame_;
113 void VideoFrameCompositor::PutCurrentFrame() {
114 DCHECK(compositor_task_runner_->BelongsToCurrentThread());
115 rendered_last_frame_ = true;
118 bool VideoFrameCompositor::UpdateCurrentFrame(base::TimeTicks deadline_min,
119 base::TimeTicks deadline_max) {
120 DCHECK(compositor_task_runner_->BelongsToCurrentThread());
121 return CallRender(deadline_min, deadline_max, false);
124 bool VideoFrameCompositor::HasCurrentFrame() {
125 DCHECK(compositor_task_runner_->BelongsToCurrentThread());
126 return current_frame_;
129 void VideoFrameCompositor::Start(RenderCallback* callback) {
130 TRACE_EVENT0("media", "VideoFrameCompositor::Start");
132 // Called from the media thread, so acquire the callback under lock before
133 // returning in case a Stop() call comes in before the PostTask is processed.
134 base::AutoLock lock(lock_);
135 DCHECK(!callback_);
136 callback_ = callback;
137 compositor_task_runner_->PostTask(
138 FROM_HERE, base::Bind(&VideoFrameCompositor::OnRendererStateUpdate,
139 base::Unretained(this), true));
142 void VideoFrameCompositor::Stop() {
143 TRACE_EVENT0("media", "VideoFrameCompositor::Stop");
145 // Called from the media thread, so release the callback under lock before
146 // returning to avoid a pending UpdateCurrentFrame() call occurring before
147 // the PostTask is processed.
148 base::AutoLock lock(lock_);
149 DCHECK(callback_);
150 callback_ = nullptr;
151 compositor_task_runner_->PostTask(
152 FROM_HERE, base::Bind(&VideoFrameCompositor::OnRendererStateUpdate,
153 base::Unretained(this), false));
156 void VideoFrameCompositor::PaintFrameUsingOldRenderingPath(
157 const scoped_refptr<VideoFrame>& frame) {
158 if (!compositor_task_runner_->BelongsToCurrentThread()) {
159 compositor_task_runner_->PostTask(
160 FROM_HERE,
161 base::Bind(&VideoFrameCompositor::PaintFrameUsingOldRenderingPath,
162 base::Unretained(this), frame));
163 return;
166 if (ProcessNewFrame(frame) && client_)
167 client_->DidReceiveFrame();
170 scoped_refptr<VideoFrame>
171 VideoFrameCompositor::GetCurrentFrameAndUpdateIfStale() {
172 DCHECK(compositor_task_runner_->BelongsToCurrentThread());
173 if (client_ || !rendering_ || !is_background_rendering_)
174 return current_frame_;
176 DCHECK(!last_background_render_.is_null());
178 const base::TimeTicks now = tick_clock_->NowTicks();
179 const base::TimeDelta interval = now - last_background_render_;
181 // Cap updates to 250Hz which should be more than enough for everyone.
182 if (interval < base::TimeDelta::FromMilliseconds(4))
183 return current_frame_;
185 // Update the interval based on the time between calls and call background
186 // render which will give this information to the client.
187 last_interval_ = interval;
188 BackgroundRender();
190 return current_frame_;
193 bool VideoFrameCompositor::ProcessNewFrame(
194 const scoped_refptr<VideoFrame>& frame) {
195 DCHECK(compositor_task_runner_->BelongsToCurrentThread());
197 if (frame == current_frame_)
198 return false;
200 // Set the flag indicating that the current frame is unrendered, if we get a
201 // subsequent PutCurrentFrame() call it will mark it as rendered.
202 rendered_last_frame_ = false;
204 if (current_frame_ &&
205 current_frame_->natural_size() != frame->natural_size()) {
206 natural_size_changed_cb_.Run(frame->natural_size());
209 if (!current_frame_ || IsOpaque(current_frame_) != IsOpaque(frame))
210 opacity_changed_cb_.Run(IsOpaque(frame));
212 current_frame_ = frame;
213 return true;
216 void VideoFrameCompositor::BackgroundRender() {
217 DCHECK(compositor_task_runner_->BelongsToCurrentThread());
218 const base::TimeTicks now = tick_clock_->NowTicks();
219 last_background_render_ = now;
220 bool new_frame = CallRender(now, now + last_interval_, true);
221 if (new_frame && client_)
222 client_->DidReceiveFrame();
225 bool VideoFrameCompositor::CallRender(base::TimeTicks deadline_min,
226 base::TimeTicks deadline_max,
227 bool background_rendering) {
228 DCHECK(compositor_task_runner_->BelongsToCurrentThread());
230 base::AutoLock lock(lock_);
231 if (!callback_) {
232 // Even if we no longer have a callback, return true if we have a frame
233 // which |client_| hasn't seen before.
234 return !rendered_last_frame_ && current_frame_;
237 DCHECK(rendering_);
239 // If the previous frame was never rendered and we're not in background
240 // rendering mode (nor have just exited it), let the client know.
241 if (!rendered_last_frame_ && current_frame_ && !background_rendering &&
242 !is_background_rendering_) {
243 callback_->OnFrameDropped();
246 const bool new_frame = ProcessNewFrame(
247 callback_->Render(deadline_min, deadline_max, background_rendering));
249 // We may create a new frame here with background rendering, but the provider
250 // has no way of knowing that a new frame had been processed, so keep track of
251 // the new frame, and return true on the next call to |CallRender|.
252 const bool had_new_background_frame = new_background_frame_;
253 new_background_frame_ = background_rendering && new_frame;
255 is_background_rendering_ = background_rendering;
256 last_interval_ = deadline_max - deadline_min;
258 // Restart the background rendering timer whether we're background rendering
259 // or not; in either case we should wait for |kBackgroundRenderingTimeoutMs|.
260 if (background_rendering_enabled_)
261 background_rendering_timer_.Reset();
262 return new_frame || had_new_background_frame;
265 } // namespace media