video_renderer_impl.cc revision 731df977c0511bca2206b5f333555b1205ff1f43
1// Copyright (c) 2010 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.
4
5#include "webkit/glue/media/video_renderer_impl.h"
6
7#include "media/base/video_frame.h"
8#include "media/base/yuv_convert.h"
9#include "webkit/glue/webmediaplayer_impl.h"
10
11namespace webkit_glue {
12
13VideoRendererImpl::VideoRendererImpl(WebMediaPlayerImpl::Proxy* proxy,
14                                     bool pts_logging)
15    : proxy_(proxy),
16      last_converted_frame_(NULL),
17      pts_logging_(pts_logging) {
18  // TODO(hclam): decide whether to do the following line in this thread or
19  // in the render thread.
20  proxy_->SetVideoRenderer(this);
21}
22
23VideoRendererImpl::~VideoRendererImpl() {}
24
25// static
26media::FilterFactory* VideoRendererImpl::CreateFactory(
27    WebMediaPlayerImpl::Proxy* proxy,
28    bool pts_logging) {
29  return new media::FilterFactoryImpl2<VideoRendererImpl,
30                                       WebMediaPlayerImpl::Proxy*,
31                                       bool>(proxy, pts_logging);
32}
33
34// static
35bool VideoRendererImpl::IsMediaFormatSupported(
36    const media::MediaFormat& media_format) {
37  return ParseMediaFormat(media_format, NULL, NULL, NULL, NULL);
38}
39
40bool VideoRendererImpl::OnInitialize(media::VideoDecoder* decoder) {
41  video_size_.SetSize(width(), height());
42  bitmap_.setConfig(SkBitmap::kARGB_8888_Config, width(), height());
43  if (bitmap_.allocPixels(NULL, NULL)) {
44    bitmap_.eraseRGB(0x00, 0x00, 0x00);
45    return true;
46  }
47
48  NOTREACHED();
49  return false;
50}
51
52void VideoRendererImpl::OnStop(media::FilterCallback* callback) {
53  if (callback) {
54    callback->Run();
55    delete callback;
56  }
57}
58
59void VideoRendererImpl::OnFrameAvailable() {
60  proxy_->Repaint();
61}
62
63void VideoRendererImpl::SetRect(const gfx::Rect& rect) {
64}
65
66// This method is always called on the renderer's thread.
67void VideoRendererImpl::Paint(skia::PlatformCanvas* canvas,
68                              const gfx::Rect& dest_rect) {
69  scoped_refptr<media::VideoFrame> video_frame;
70  GetCurrentFrame(&video_frame);
71  if (!video_frame) {
72    SkPaint paint;
73    paint.setColor(SK_ColorBLACK);
74    canvas->drawRectCoords(
75        static_cast<float>(dest_rect.x()),
76        static_cast<float>(dest_rect.y()),
77        static_cast<float>(dest_rect.right()),
78        static_cast<float>(dest_rect.bottom()),
79        paint);
80  } else {
81    if (CanFastPaint(canvas, dest_rect)) {
82      FastPaint(video_frame, canvas, dest_rect);
83    } else {
84      SlowPaint(video_frame, canvas, dest_rect);
85    }
86
87    // Presentation timestamp logging is primarily used to measure performance
88    // on low-end devices.  When profiled on an Intel Atom N280 @ 1.66GHz this
89    // code had a ~63 microsecond perf hit when logging to a file (not stdout),
90    // which is neglible enough for measuring playback performance.
91    if (pts_logging_)
92      VLOG(1) << "pts=" << video_frame->GetTimestamp().InMicroseconds();
93  }
94
95  PutCurrentFrame(video_frame);
96}
97
98void VideoRendererImpl::GetCurrentFrame(
99    scoped_refptr<media::VideoFrame>* frame_out) {
100  VideoRendererBase::GetCurrentFrame(frame_out);
101}
102
103void VideoRendererImpl::PutCurrentFrame(
104    scoped_refptr<media::VideoFrame> frame) {
105  VideoRendererBase::PutCurrentFrame(frame);
106}
107
108// CanFastPaint is a helper method to determine the conditions for fast
109// painting. The conditions are:
110// 1. No skew in canvas matrix.
111// 2. No flipping nor mirroring.
112// 3. Canvas has pixel format ARGB8888.
113// 4. Canvas is opaque.
114// TODO(hclam): The fast paint method should support flipping and mirroring.
115// Disable the flipping and mirroring checks once we have it.
116bool VideoRendererImpl::CanFastPaint(skia::PlatformCanvas* canvas,
117                                     const gfx::Rect& dest_rect) {
118  // Fast paint does not handle opacity value other than 1.0. Hence use slow
119  // paint if opacity is not 1.0. Since alpha = opacity * 0xFF, we check that
120  // alpha != 0xFF.
121  //
122  // Additonal notes: If opacity = 0.0, the chrome display engine does not try
123  // to render the video. So, this method is never called. However, if the
124  // opacity = 0.0001, alpha is again 0, but the display engine tries to render
125  // the video. If we use Fast paint, the video shows up with opacity = 1.0.
126  // Hence we use slow paint also in the case where alpha = 0. It would be ideal
127  // if rendering was never called even for cases where alpha is 0. Created
128  // bug 48090 for this.
129  SkCanvas::LayerIter layer_iter(canvas, false);
130  SkColor sk_color = layer_iter.paint().getColor();
131  SkAlpha sk_alpha = SkColorGetA(sk_color);
132  if (sk_alpha != 0xFF) {
133    return false;
134  }
135
136  const SkMatrix& total_matrix = canvas->getTotalMatrix();
137  // Perform the following checks here:
138  // 1. Check for skewing factors of the transformation matrix. They should be
139  //    zero.
140  // 2. Check for mirroring and flipping. Make sure they are greater than zero.
141  if (SkScalarNearlyZero(total_matrix.getSkewX()) &&
142      SkScalarNearlyZero(total_matrix.getSkewY()) &&
143      total_matrix.getScaleX() > 0 &&
144      total_matrix.getScaleY() > 0) {
145    // Get the properties of the SkDevice and the clip rect.
146    SkDevice* device = canvas->getDevice();
147
148    // Get the boundary of the device.
149    SkIRect device_rect;
150    device->getBounds(&device_rect);
151
152    // Get the pixel config of the device.
153    const SkBitmap::Config config = device->config();
154    // Get the total clip rect associated with the canvas.
155    const SkRegion& total_clip = canvas->getTotalClip();
156
157    SkIRect dest_irect;
158    TransformToSkIRect(canvas->getTotalMatrix(), dest_rect, &dest_irect);
159
160    if (config == SkBitmap::kARGB_8888_Config && device->isOpaque() &&
161        device_rect.contains(total_clip.getBounds())) {
162      return true;
163    }
164  }
165
166  return false;
167}
168
169void VideoRendererImpl::SlowPaint(media::VideoFrame* video_frame,
170                                  skia::PlatformCanvas* canvas,
171                                  const gfx::Rect& dest_rect) {
172  // 1. Convert YUV frame to RGB.
173  base::TimeDelta timestamp = video_frame->GetTimestamp();
174  if (video_frame != last_converted_frame_ ||
175      timestamp != last_converted_timestamp_) {
176    last_converted_frame_ = video_frame;
177    last_converted_timestamp_ = timestamp;
178    DCHECK(video_frame->format() == media::VideoFrame::YV12 ||
179           video_frame->format() == media::VideoFrame::YV16);
180    DCHECK(video_frame->stride(media::VideoFrame::kUPlane) ==
181           video_frame->stride(media::VideoFrame::kVPlane));
182    DCHECK(video_frame->planes() == media::VideoFrame::kNumYUVPlanes);
183    bitmap_.lockPixels();
184    media::YUVType yuv_type =
185        (video_frame->format() == media::VideoFrame::YV12) ?
186        media::YV12 : media::YV16;
187    media::ConvertYUVToRGB32(video_frame->data(media::VideoFrame::kYPlane),
188                             video_frame->data(media::VideoFrame::kUPlane),
189                             video_frame->data(media::VideoFrame::kVPlane),
190                             static_cast<uint8*>(bitmap_.getPixels()),
191                             video_frame->width(),
192                             video_frame->height(),
193                             video_frame->stride(media::VideoFrame::kYPlane),
194                             video_frame->stride(media::VideoFrame::kUPlane),
195                             bitmap_.rowBytes(),
196                             yuv_type);
197    bitmap_.unlockPixels();
198  }
199
200  // 2. Paint the bitmap to canvas.
201  SkMatrix matrix;
202  matrix.setTranslate(static_cast<SkScalar>(dest_rect.x()),
203                      static_cast<SkScalar>(dest_rect.y()));
204  if (dest_rect.width()  != video_size_.width() ||
205      dest_rect.height() != video_size_.height()) {
206    matrix.preScale(SkIntToScalar(dest_rect.width()) /
207                    SkIntToScalar(video_size_.width()),
208                    SkIntToScalar(dest_rect.height()) /
209                    SkIntToScalar(video_size_.height()));
210  }
211  SkPaint paint;
212  paint.setFlags(SkPaint::kFilterBitmap_Flag);
213  canvas->drawBitmapMatrix(bitmap_, matrix, &paint);
214}
215
216void VideoRendererImpl::FastPaint(media::VideoFrame* video_frame,
217                                  skia::PlatformCanvas* canvas,
218                                  const gfx::Rect& dest_rect) {
219  DCHECK(video_frame->format() == media::VideoFrame::YV12 ||
220         video_frame->format() == media::VideoFrame::YV16);
221  DCHECK(video_frame->stride(media::VideoFrame::kUPlane) ==
222         video_frame->stride(media::VideoFrame::kVPlane));
223  DCHECK(video_frame->planes() == media::VideoFrame::kNumYUVPlanes);
224  const SkBitmap& bitmap = canvas->getDevice()->accessBitmap(true);
225  media::YUVType yuv_type = (video_frame->format() == media::VideoFrame::YV12) ?
226                            media::YV12 : media::YV16;
227  int y_shift = yuv_type;  // 1 for YV12, 0 for YV16.
228
229  // Create a rectangle backed by SkScalar.
230  SkRect scalar_dest_rect;
231  scalar_dest_rect.iset(dest_rect.x(), dest_rect.y(),
232                        dest_rect.right(), dest_rect.bottom());
233
234  // Transform the destination rectangle to local coordinates.
235  const SkMatrix& local_matrix = canvas->getTotalMatrix();
236  SkRect local_dest_rect;
237  local_matrix.mapRect(&local_dest_rect, scalar_dest_rect);
238
239  // After projecting the destination rectangle to local coordinates, round
240  // the projected rectangle to integer values, this will give us pixel values
241  // of the rectangle.
242  SkIRect local_dest_irect, local_dest_irect_saved;
243  local_dest_rect.round(&local_dest_irect);
244  local_dest_rect.round(&local_dest_irect_saved);
245
246  // Only does the paint if the destination rect intersects with the clip
247  // rect.
248  if (local_dest_irect.intersect(canvas->getTotalClip().getBounds())) {
249    // At this point |local_dest_irect| contains the rect that we should draw
250    // to within the clipping rect.
251
252    // Calculate the address for the top left corner of destination rect in
253    // the canvas that we will draw to. The address is obtained by the base
254    // address of the canvas shifted by "left" and "top" of the rect.
255    uint8* dest_rect_pointer = static_cast<uint8*>(bitmap.getPixels()) +
256        local_dest_irect.fTop * bitmap.rowBytes() +
257        local_dest_irect.fLeft * 4;
258
259    // Project the clip rect to the original video frame, obtains the
260    // dimensions of the projected clip rect, "left" and "top" of the rect.
261    // The math here are all integer math so we won't have rounding error and
262    // write outside of the canvas.
263    // We have the assumptions of dest_rect.width() and dest_rect.height()
264    // being non-zero, these are valid assumptions since finding intersection
265    // above rejects empty rectangle so we just do a DCHECK here.
266    DCHECK_NE(0, dest_rect.width());
267    DCHECK_NE(0, dest_rect.height());
268    size_t frame_clip_width = local_dest_irect.width() *
269        video_frame->width() / local_dest_irect_saved.width();
270    size_t frame_clip_height = local_dest_irect.height() *
271        video_frame->height() / local_dest_irect_saved.height();
272
273    // Project the "left" and "top" of the final destination rect to local
274    // coordinates of the video frame, use these values to find the offsets
275    // in the video frame to start reading.
276    size_t frame_clip_left =
277        (local_dest_irect.fLeft - local_dest_irect_saved.fLeft) *
278        video_frame->width() / local_dest_irect_saved.width();
279    size_t frame_clip_top =
280        (local_dest_irect.fTop - local_dest_irect_saved.fTop) *
281        video_frame->height() / local_dest_irect_saved.height();
282
283    // Use the "left" and "top" of the destination rect to locate the offset
284    // in Y, U and V planes.
285    size_t y_offset = video_frame->stride(media::VideoFrame::kYPlane) *
286        frame_clip_top + frame_clip_left;
287    // For format YV12, there is one U, V value per 2x2 block.
288    // For format YV16, there is one u, V value per 2x1 block.
289    size_t uv_offset = (video_frame->stride(media::VideoFrame::kUPlane) *
290                        (frame_clip_top >> y_shift)) + (frame_clip_left >> 1);
291    uint8* frame_clip_y =
292        video_frame->data(media::VideoFrame::kYPlane) + y_offset;
293    uint8* frame_clip_u =
294        video_frame->data(media::VideoFrame::kUPlane) + uv_offset;
295    uint8* frame_clip_v =
296        video_frame->data(media::VideoFrame::kVPlane) + uv_offset;
297    bitmap.lockPixels();
298
299    // TODO(hclam): do rotation and mirroring here.
300    // TODO(fbarchard): switch filtering based on performance.
301    media::ScaleYUVToRGB32(frame_clip_y,
302                           frame_clip_u,
303                           frame_clip_v,
304                           dest_rect_pointer,
305                           frame_clip_width,
306                           frame_clip_height,
307                           local_dest_irect.width(),
308                           local_dest_irect.height(),
309                           video_frame->stride(media::VideoFrame::kYPlane),
310                           video_frame->stride(media::VideoFrame::kUPlane),
311                           bitmap.rowBytes(),
312                           yuv_type,
313                           media::ROTATE_0,
314                           media::FILTER_BILINEAR);
315    bitmap.unlockPixels();
316  }
317}
318
319void VideoRendererImpl::TransformToSkIRect(const SkMatrix& matrix,
320                                           const gfx::Rect& src_rect,
321                                           SkIRect* dest_rect) {
322    // Transform destination rect to local coordinates.
323    SkRect transformed_rect;
324    SkRect skia_dest_rect;
325    skia_dest_rect.iset(src_rect.x(), src_rect.y(),
326                        src_rect.right(), src_rect.bottom());
327    matrix.mapRect(&transformed_rect, skia_dest_rect);
328    transformed_rect.round(dest_rect);
329}
330
331}  // namespace webkit_glue
332