OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/filters/skcanvas_video_renderer.h" | 5 #include "media/filters/skcanvas_video_renderer.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "media/base/video_frame.h" | 8 #include "media/base/video_frame.h" |
9 #include "media/base/yuv_convert.h" | 9 #include "media/base/yuv_convert.h" |
10 #include "third_party/skia/include/core/SkCanvas.h" | 10 #include "third_party/skia/include/core/SkCanvas.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 // CanFastPaint() is used to determine the conditions. | 61 // CanFastPaint() is used to determine the conditions. |
62 static void FastPaint( | 62 static void FastPaint( |
63 const scoped_refptr<media::VideoFrame>& video_frame, | 63 const scoped_refptr<media::VideoFrame>& video_frame, |
64 SkCanvas* canvas, | 64 SkCanvas* canvas, |
65 const SkRect& dest_rect) { | 65 const SkRect& dest_rect) { |
66 DCHECK(IsEitherYV12OrYV16(video_frame->format())) << video_frame->format(); | 66 DCHECK(IsEitherYV12OrYV16(video_frame->format())) << video_frame->format(); |
67 DCHECK_EQ(video_frame->stride(media::VideoFrame::kUPlane), | 67 DCHECK_EQ(video_frame->stride(media::VideoFrame::kUPlane), |
68 video_frame->stride(media::VideoFrame::kVPlane)); | 68 video_frame->stride(media::VideoFrame::kVPlane)); |
69 | 69 |
70 const SkBitmap& bitmap = canvas->getDevice()->accessBitmap(true); | 70 const SkBitmap& bitmap = canvas->getDevice()->accessBitmap(true); |
71 media::YUVType yuv_type = (video_frame->format() == media::VideoFrame::YV12) ? | 71 media::YUVType yuv_type = media::YV16; |
72 media::YV12 : media::YV16; | 72 int y_shift = 0; |
73 int y_shift = yuv_type; // 1 for YV12, 0 for YV16. | 73 if (video_frame->format() == media::VideoFrame::YV12) { |
| 74 yuv_type = media::YV12; |
| 75 y_shift = 1; |
| 76 } |
74 | 77 |
75 // Transform the destination rectangle to local coordinates. | 78 // Transform the destination rectangle to local coordinates. |
76 const SkMatrix& local_matrix = canvas->getTotalMatrix(); | 79 const SkMatrix& local_matrix = canvas->getTotalMatrix(); |
77 SkRect local_dest_rect; | 80 SkRect local_dest_rect; |
78 local_matrix.mapRect(&local_dest_rect, dest_rect); | 81 local_matrix.mapRect(&local_dest_rect, dest_rect); |
79 | 82 |
80 // After projecting the destination rectangle to local coordinates, round | 83 // After projecting the destination rectangle to local coordinates, round |
81 // the projected rectangle to integer values, this will give us pixel values | 84 // the projected rectangle to integer values, this will give us pixel values |
82 // of the rectangle. | 85 // of the rectangle. |
83 SkIRect local_dest_irect, local_dest_irect_saved; | 86 SkIRect local_dest_irect, local_dest_irect_saved; |
(...skipping 18 matching lines...) Expand all Loading... |
102 // Project the clip rect to the original video frame, obtains the | 105 // Project the clip rect to the original video frame, obtains the |
103 // dimensions of the projected clip rect, "left" and "top" of the rect. | 106 // dimensions of the projected clip rect, "left" and "top" of the rect. |
104 // The math here are all integer math so we won't have rounding error and | 107 // The math here are all integer math so we won't have rounding error and |
105 // write outside of the canvas. | 108 // write outside of the canvas. |
106 // We have the assumptions of dest_rect.width() and dest_rect.height() | 109 // We have the assumptions of dest_rect.width() and dest_rect.height() |
107 // being non-zero, these are valid assumptions since finding intersection | 110 // being non-zero, these are valid assumptions since finding intersection |
108 // above rejects empty rectangle so we just do a DCHECK here. | 111 // above rejects empty rectangle so we just do a DCHECK here. |
109 DCHECK_NE(0, dest_rect.width()); | 112 DCHECK_NE(0, dest_rect.width()); |
110 DCHECK_NE(0, dest_rect.height()); | 113 DCHECK_NE(0, dest_rect.height()); |
111 size_t frame_clip_width = local_dest_irect.width() * | 114 size_t frame_clip_width = local_dest_irect.width() * |
112 video_frame->data_size().width() / local_dest_irect_saved.width(); | 115 video_frame->visible_rect().width() / local_dest_irect_saved.width(); |
113 size_t frame_clip_height = local_dest_irect.height() * | 116 size_t frame_clip_height = local_dest_irect.height() * |
114 video_frame->data_size().height() / local_dest_irect_saved.height(); | 117 video_frame->visible_rect().height() / local_dest_irect_saved.height(); |
115 | 118 |
116 // Project the "left" and "top" of the final destination rect to local | 119 // Project the "left" and "top" of the final destination rect to local |
117 // coordinates of the video frame, use these values to find the offsets | 120 // coordinates of the video frame, use these values to find the offsets |
118 // in the video frame to start reading. | 121 // in the video frame to start reading. |
119 size_t frame_clip_left = | 122 size_t frame_clip_left = |
| 123 video_frame->visible_rect().x() + |
120 (local_dest_irect.fLeft - local_dest_irect_saved.fLeft) * | 124 (local_dest_irect.fLeft - local_dest_irect_saved.fLeft) * |
121 video_frame->data_size().width() / local_dest_irect_saved.width(); | 125 video_frame->visible_rect().width() / local_dest_irect_saved.width(); |
122 size_t frame_clip_top = | 126 size_t frame_clip_top = |
| 127 video_frame->visible_rect().y() + |
123 (local_dest_irect.fTop - local_dest_irect_saved.fTop) * | 128 (local_dest_irect.fTop - local_dest_irect_saved.fTop) * |
124 video_frame->data_size().height() / local_dest_irect_saved.height(); | 129 video_frame->visible_rect().height() / local_dest_irect_saved.height(); |
125 | 130 |
126 // Use the "left" and "top" of the destination rect to locate the offset | 131 // Use the "left" and "top" of the destination rect to locate the offset |
127 // in Y, U and V planes. | 132 // in Y, U and V planes. |
128 size_t y_offset = video_frame->stride(media::VideoFrame::kYPlane) * | 133 size_t y_offset = (video_frame->stride(media::VideoFrame::kYPlane) * |
129 frame_clip_top + frame_clip_left; | 134 frame_clip_top) + frame_clip_left; |
130 | 135 |
131 // For format YV12, there is one U, V value per 2x2 block. | 136 // For format YV12, there is one U, V value per 2x2 block. |
132 // For format YV16, there is one u, V value per 2x1 block. | 137 // For format YV16, there is one U, V value per 2x1 block. |
133 size_t uv_offset = (video_frame->stride(media::VideoFrame::kUPlane) * | 138 size_t uv_offset = (video_frame->stride(media::VideoFrame::kUPlane) * |
134 (frame_clip_top >> y_shift)) + (frame_clip_left >> 1); | 139 (frame_clip_top >> y_shift)) + (frame_clip_left >> 1); |
135 uint8* frame_clip_y = | 140 uint8* frame_clip_y = |
136 video_frame->data(media::VideoFrame::kYPlane) + y_offset; | 141 video_frame->data(media::VideoFrame::kYPlane) + y_offset; |
137 uint8* frame_clip_u = | 142 uint8* frame_clip_u = |
138 video_frame->data(media::VideoFrame::kUPlane) + uv_offset; | 143 video_frame->data(media::VideoFrame::kUPlane) + uv_offset; |
139 uint8* frame_clip_v = | 144 uint8* frame_clip_v = |
140 video_frame->data(media::VideoFrame::kVPlane) + uv_offset; | 145 video_frame->data(media::VideoFrame::kVPlane) + uv_offset; |
141 | 146 |
142 // TODO(hclam): do rotation and mirroring here. | 147 // TODO(hclam): do rotation and mirroring here. |
(...skipping 24 matching lines...) Expand all Loading... |
167 SkBitmap* bitmap) { | 172 SkBitmap* bitmap) { |
168 DCHECK(IsEitherYV12OrYV16OrNative(video_frame->format())) | 173 DCHECK(IsEitherYV12OrYV16OrNative(video_frame->format())) |
169 << video_frame->format(); | 174 << video_frame->format(); |
170 if (IsEitherYV12OrYV16(video_frame->format())) { | 175 if (IsEitherYV12OrYV16(video_frame->format())) { |
171 DCHECK_EQ(video_frame->stride(media::VideoFrame::kUPlane), | 176 DCHECK_EQ(video_frame->stride(media::VideoFrame::kUPlane), |
172 video_frame->stride(media::VideoFrame::kVPlane)); | 177 video_frame->stride(media::VideoFrame::kVPlane)); |
173 } | 178 } |
174 | 179 |
175 // Check if |bitmap| needs to be (re)allocated. | 180 // Check if |bitmap| needs to be (re)allocated. |
176 if (bitmap->isNull() || | 181 if (bitmap->isNull() || |
177 bitmap->width() != video_frame->data_size().width() || | 182 bitmap->width() != video_frame->visible_rect().width() || |
178 bitmap->height() != video_frame->data_size().height()) { | 183 bitmap->height() != video_frame->visible_rect().height()) { |
179 bitmap->setConfig(SkBitmap::kARGB_8888_Config, | 184 bitmap->setConfig(SkBitmap::kARGB_8888_Config, |
180 video_frame->data_size().width(), | 185 video_frame->visible_rect().width(), |
181 video_frame->data_size().height()); | 186 video_frame->visible_rect().height()); |
182 bitmap->allocPixels(); | 187 bitmap->allocPixels(); |
183 bitmap->setIsVolatile(true); | 188 bitmap->setIsVolatile(true); |
184 } | 189 } |
185 | 190 |
186 bitmap->lockPixels(); | 191 bitmap->lockPixels(); |
187 if (IsEitherYV12OrYV16(video_frame->format())) { | 192 if (IsEitherYV12OrYV16(video_frame->format())) { |
188 media::YUVType yuv_type = | 193 media::YUVType yuv_type = media::YV16; |
189 (video_frame->format() == media::VideoFrame::YV12) ? | 194 int y_shift = 0; |
190 media::YV12 : media::YV16; | 195 if (video_frame->format() == media::VideoFrame::YV12) { |
191 media::ConvertYUVToRGB32(video_frame->data(media::VideoFrame::kYPlane), | 196 yuv_type = media::YV12; |
192 video_frame->data(media::VideoFrame::kUPlane), | 197 y_shift = 1; |
193 video_frame->data(media::VideoFrame::kVPlane), | 198 } |
| 199 |
| 200 // Use the "left" and "top" of the destination rect to locate the offset |
| 201 // in Y, U and V planes. |
| 202 size_t y_offset = (video_frame->stride(media::VideoFrame::kYPlane) * |
| 203 video_frame->visible_rect().y()) + |
| 204 video_frame->visible_rect().x(); |
| 205 |
| 206 // For format YV12, there is one U, V value per 2x2 block. |
| 207 // For format YV16, there is one U, V value per 2x1 block. |
| 208 size_t uv_offset = (video_frame->stride(media::VideoFrame::kUPlane) * |
| 209 (video_frame->visible_rect().y() >> y_shift)) + |
| 210 (video_frame->visible_rect().x() >> 1); |
| 211 uint8* frame_clip_y = |
| 212 video_frame->data(media::VideoFrame::kYPlane) + y_offset; |
| 213 uint8* frame_clip_u = |
| 214 video_frame->data(media::VideoFrame::kUPlane) + uv_offset; |
| 215 uint8* frame_clip_v = |
| 216 video_frame->data(media::VideoFrame::kVPlane) + uv_offset; |
| 217 |
| 218 media::ConvertYUVToRGB32(frame_clip_y, |
| 219 frame_clip_u, |
| 220 frame_clip_v, |
194 static_cast<uint8*>(bitmap->getPixels()), | 221 static_cast<uint8*>(bitmap->getPixels()), |
195 video_frame->data_size().width(), | 222 video_frame->visible_rect().width(), |
196 video_frame->data_size().height(), | 223 video_frame->visible_rect().height(), |
197 video_frame->stride(media::VideoFrame::kYPlane), | 224 video_frame->stride(media::VideoFrame::kYPlane), |
198 video_frame->stride(media::VideoFrame::kUPlane), | 225 video_frame->stride(media::VideoFrame::kUPlane), |
199 bitmap->rowBytes(), | 226 bitmap->rowBytes(), |
200 yuv_type); | 227 yuv_type); |
201 } else { | 228 } else { |
202 DCHECK_EQ(video_frame->format(), media::VideoFrame::NATIVE_TEXTURE); | 229 DCHECK_EQ(video_frame->format(), media::VideoFrame::NATIVE_TEXTURE); |
203 video_frame->ReadPixelsFromNativeTexture(bitmap->getPixels()); | 230 video_frame->ReadPixelsFromNativeTexture(bitmap->getPixels()); |
204 } | 231 } |
205 bitmap->notifyPixelsChanged(); | 232 bitmap->notifyPixelsChanged(); |
206 bitmap->unlockPixels(); | 233 bitmap->unlockPixels(); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 ConvertVideoFrameToBitmap(video_frame, &last_frame_); | 272 ConvertVideoFrameToBitmap(video_frame, &last_frame_); |
246 last_frame_timestamp_ = video_frame->GetTimestamp(); | 273 last_frame_timestamp_ = video_frame->GetTimestamp(); |
247 } | 274 } |
248 | 275 |
249 // Do a slower paint using |last_frame_|. | 276 // Do a slower paint using |last_frame_|. |
250 paint.setFilterBitmap(true); | 277 paint.setFilterBitmap(true); |
251 canvas->drawBitmapRect(last_frame_, NULL, dest, &paint); | 278 canvas->drawBitmapRect(last_frame_, NULL, dest, &paint); |
252 } | 279 } |
253 | 280 |
254 } // namespace media | 281 } // namespace media |
OLD | NEW |