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