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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 // Project the clip rect to the original video frame, obtains the | 102 // Project the clip rect to the original video frame, obtains the |
103 // dimensions of the projected clip rect, "left" and "top" of the rect. | 103 // 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 | 104 // The math here are all integer math so we won't have rounding error and |
105 // write outside of the canvas. | 105 // write outside of the canvas. |
106 // We have the assumptions of dest_rect.width() and dest_rect.height() | 106 // We have the assumptions of dest_rect.width() and dest_rect.height() |
107 // being non-zero, these are valid assumptions since finding intersection | 107 // being non-zero, these are valid assumptions since finding intersection |
108 // above rejects empty rectangle so we just do a DCHECK here. | 108 // above rejects empty rectangle so we just do a DCHECK here. |
109 DCHECK_NE(0, dest_rect.width()); | 109 DCHECK_NE(0, dest_rect.width()); |
110 DCHECK_NE(0, dest_rect.height()); | 110 DCHECK_NE(0, dest_rect.height()); |
111 size_t frame_clip_width = local_dest_irect.width() * | 111 size_t frame_clip_width = local_dest_irect.width() * |
112 video_frame->data_size().width() / local_dest_irect_saved.width(); | 112 video_frame->visible_rect().width() / local_dest_irect_saved.width(); |
113 size_t frame_clip_height = local_dest_irect.height() * | 113 size_t frame_clip_height = local_dest_irect.height() * |
114 video_frame->data_size().height() / local_dest_irect_saved.height(); | 114 video_frame->visible_rect().height() / local_dest_irect_saved.height(); |
115 | 115 |
116 // Project the "left" and "top" of the final destination rect to local | 116 // 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 | 117 // coordinates of the video frame, use these values to find the offsets |
118 // in the video frame to start reading. | 118 // in the video frame to start reading. |
119 size_t frame_clip_left = | 119 size_t frame_clip_left = |
| 120 video_frame->visible_rect().x() + |
120 (local_dest_irect.fLeft - local_dest_irect_saved.fLeft) * | 121 (local_dest_irect.fLeft - local_dest_irect_saved.fLeft) * |
121 video_frame->data_size().width() / local_dest_irect_saved.width(); | 122 video_frame->visible_rect().width() / local_dest_irect_saved.width(); |
122 size_t frame_clip_top = | 123 size_t frame_clip_top = |
| 124 video_frame->visible_rect().y() + |
123 (local_dest_irect.fTop - local_dest_irect_saved.fTop) * | 125 (local_dest_irect.fTop - local_dest_irect_saved.fTop) * |
124 video_frame->data_size().height() / local_dest_irect_saved.height(); | 126 video_frame->visible_rect().height() / local_dest_irect_saved.height(); |
125 | 127 |
126 // Use the "left" and "top" of the destination rect to locate the offset | 128 // Use the "left" and "top" of the destination rect to locate the offset |
127 // in Y, U and V planes. | 129 // in Y, U and V planes. |
128 size_t y_offset = video_frame->stride(media::VideoFrame::kYPlane) * | 130 size_t y_offset = (video_frame->stride(media::VideoFrame::kYPlane) * |
129 frame_clip_top + frame_clip_left; | 131 frame_clip_top) + frame_clip_left; |
130 | 132 |
131 // For format YV12, there is one U, V value per 2x2 block. | 133 // 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. | 134 // For format YV16, there is one u, V value per 2x1 block. |
133 size_t uv_offset = (video_frame->stride(media::VideoFrame::kUPlane) * | 135 size_t uv_offset = (video_frame->stride(media::VideoFrame::kUPlane) * |
134 (frame_clip_top >> y_shift)) + (frame_clip_left >> 1); | 136 (frame_clip_top >> y_shift)) + (frame_clip_left >> 1); |
135 uint8* frame_clip_y = | 137 uint8* frame_clip_y = |
136 video_frame->data(media::VideoFrame::kYPlane) + y_offset; | 138 video_frame->data(media::VideoFrame::kYPlane) + y_offset; |
137 uint8* frame_clip_u = | 139 uint8* frame_clip_u = |
138 video_frame->data(media::VideoFrame::kUPlane) + uv_offset; | 140 video_frame->data(media::VideoFrame::kUPlane) + uv_offset; |
139 uint8* frame_clip_v = | 141 uint8* frame_clip_v = |
(...skipping 27 matching lines...) Expand all Loading... |
167 SkBitmap* bitmap) { | 169 SkBitmap* bitmap) { |
168 DCHECK(IsEitherYV12OrYV16OrNative(video_frame->format())) | 170 DCHECK(IsEitherYV12OrYV16OrNative(video_frame->format())) |
169 << video_frame->format(); | 171 << video_frame->format(); |
170 if (IsEitherYV12OrYV16(video_frame->format())) { | 172 if (IsEitherYV12OrYV16(video_frame->format())) { |
171 DCHECK_EQ(video_frame->stride(media::VideoFrame::kUPlane), | 173 DCHECK_EQ(video_frame->stride(media::VideoFrame::kUPlane), |
172 video_frame->stride(media::VideoFrame::kVPlane)); | 174 video_frame->stride(media::VideoFrame::kVPlane)); |
173 } | 175 } |
174 | 176 |
175 // Check if |bitmap| needs to be (re)allocated. | 177 // Check if |bitmap| needs to be (re)allocated. |
176 if (bitmap->isNull() || | 178 if (bitmap->isNull() || |
177 bitmap->width() != video_frame->data_size().width() || | 179 bitmap->width() != video_frame->visible_rect().width() || |
178 bitmap->height() != video_frame->data_size().height()) { | 180 bitmap->height() != video_frame->visible_rect().height()) { |
179 bitmap->setConfig(SkBitmap::kARGB_8888_Config, | 181 bitmap->setConfig(SkBitmap::kARGB_8888_Config, |
180 video_frame->data_size().width(), | 182 video_frame->visible_rect().width(), |
181 video_frame->data_size().height()); | 183 video_frame->visible_rect().height()); |
182 bitmap->allocPixels(); | 184 bitmap->allocPixels(); |
183 bitmap->setIsVolatile(true); | 185 bitmap->setIsVolatile(true); |
184 } | 186 } |
185 | 187 |
186 bitmap->lockPixels(); | 188 bitmap->lockPixels(); |
187 if (IsEitherYV12OrYV16(video_frame->format())) { | 189 if (IsEitherYV12OrYV16(video_frame->format())) { |
188 media::YUVType yuv_type = | 190 media::YUVType yuv_type = |
189 (video_frame->format() == media::VideoFrame::YV12) ? | 191 (video_frame->format() == media::VideoFrame::YV12) ? |
190 media::YV12 : media::YV16; | 192 media::YV12 : media::YV16; |
191 media::ConvertYUVToRGB32(video_frame->data(media::VideoFrame::kYPlane), | 193 int y_shift = yuv_type; |
192 video_frame->data(media::VideoFrame::kUPlane), | 194 |
193 video_frame->data(media::VideoFrame::kVPlane), | 195 // Use the "left" and "top" of the destination rect to locate the offset |
| 196 // in Y, U and V planes. |
| 197 size_t y_offset = (video_frame->stride(media::VideoFrame::kYPlane) * |
| 198 video_frame->visible_rect().y()) + |
| 199 video_frame->visible_rect().x(); |
| 200 |
| 201 // For format YV12, there is one U, V value per 2x2 block. |
| 202 // For format YV16, there is one u, V value per 2x1 block. |
| 203 size_t uv_offset = (video_frame->stride(media::VideoFrame::kUPlane) * |
| 204 (video_frame->visible_rect().y() >> y_shift)) + |
| 205 (video_frame->visible_rect().x() >> 1); |
| 206 uint8* frame_clip_y = |
| 207 video_frame->data(media::VideoFrame::kYPlane) + y_offset; |
| 208 uint8* frame_clip_u = |
| 209 video_frame->data(media::VideoFrame::kUPlane) + uv_offset; |
| 210 uint8* frame_clip_v = |
| 211 video_frame->data(media::VideoFrame::kVPlane) + uv_offset; |
| 212 |
| 213 media::ConvertYUVToRGB32(frame_clip_y, |
| 214 frame_clip_u, |
| 215 frame_clip_v, |
194 static_cast<uint8*>(bitmap->getPixels()), | 216 static_cast<uint8*>(bitmap->getPixels()), |
195 video_frame->data_size().width(), | 217 video_frame->visible_rect().width(), |
196 video_frame->data_size().height(), | 218 video_frame->visible_rect().height(), |
197 video_frame->stride(media::VideoFrame::kYPlane), | 219 video_frame->stride(media::VideoFrame::kYPlane), |
198 video_frame->stride(media::VideoFrame::kUPlane), | 220 video_frame->stride(media::VideoFrame::kUPlane), |
199 bitmap->rowBytes(), | 221 bitmap->rowBytes(), |
200 yuv_type); | 222 yuv_type); |
201 } else { | 223 } else { |
202 DCHECK_EQ(video_frame->format(), media::VideoFrame::NATIVE_TEXTURE); | 224 DCHECK_EQ(video_frame->format(), media::VideoFrame::NATIVE_TEXTURE); |
203 video_frame->ReadPixelsFromNativeTexture(bitmap->getPixels()); | 225 video_frame->ReadPixelsFromNativeTexture(bitmap->getPixels()); |
204 } | 226 } |
205 bitmap->notifyPixelsChanged(); | 227 bitmap->notifyPixelsChanged(); |
206 bitmap->unlockPixels(); | 228 bitmap->unlockPixels(); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 ConvertVideoFrameToBitmap(video_frame, &last_frame_); | 267 ConvertVideoFrameToBitmap(video_frame, &last_frame_); |
246 last_frame_timestamp_ = video_frame->GetTimestamp(); | 268 last_frame_timestamp_ = video_frame->GetTimestamp(); |
247 } | 269 } |
248 | 270 |
249 // Do a slower paint using |last_frame_|. | 271 // Do a slower paint using |last_frame_|. |
250 paint.setFilterBitmap(true); | 272 paint.setFilterBitmap(true); |
251 canvas->drawBitmapRect(last_frame_, NULL, dest, &paint); | 273 canvas->drawBitmapRect(last_frame_, NULL, dest, &paint); |
252 } | 274 } |
253 | 275 |
254 } // namespace media | 276 } // namespace media |
OLD | NEW |