Remove INJECT_EVENTS permissions from test APKs.
[chromium-blink-merge.git] / media / blink / skcanvas_video_renderer.cc
blob51168fb0886354cb87f62bf4055ec2a1f814c618
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
3 // found in the LICENSE file.
5 #include "media/blink/skcanvas_video_renderer.h"
7 #include "gpu/GLES2/gl2extchromium.h"
8 #include "gpu/command_buffer/client/gles2_interface.h"
9 #include "gpu/command_buffer/common/mailbox_holder.h"
10 #include "media/base/video_frame.h"
11 #include "media/base/yuv_convert.h"
12 #include "skia/ext/refptr.h"
13 #include "third_party/libyuv/include/libyuv.h"
14 #include "third_party/skia/include/core/SkCanvas.h"
15 #include "third_party/skia/include/core/SkImage.h"
16 #include "third_party/skia/include/core/SkImageGenerator.h"
17 #include "third_party/skia/include/gpu/GrContext.h"
18 #include "third_party/skia/include/gpu/GrPaint.h"
19 #include "third_party/skia/include/gpu/GrTexture.h"
20 #include "third_party/skia/include/gpu/GrTextureProvider.h"
21 #include "third_party/skia/include/gpu/SkGr.h"
22 #include "third_party/skia/include/gpu/SkGrPixelRef.h"
23 #include "ui/gfx/skbitmap_operations.h"
25 // Skia internal format depends on a platform. On Android it is ABGR, on others
26 // it is ARGB.
27 #if SK_B32_SHIFT == 0 && SK_G32_SHIFT == 8 && SK_R32_SHIFT == 16 && \
28 SK_A32_SHIFT == 24
29 #define LIBYUV_I420_TO_ARGB libyuv::I420ToARGB
30 #define LIBYUV_I422_TO_ARGB libyuv::I422ToARGB
31 #elif SK_R32_SHIFT == 0 && SK_G32_SHIFT == 8 && SK_B32_SHIFT == 16 && \
32 SK_A32_SHIFT == 24
33 #define LIBYUV_I420_TO_ARGB libyuv::I420ToABGR
34 #define LIBYUV_I422_TO_ARGB libyuv::I422ToABGR
35 #else
36 #error Unexpected Skia ARGB_8888 layout!
37 #endif
39 namespace media {
41 namespace {
43 // This class keeps two temporary resources; software bitmap, hardware bitmap.
44 // If both bitmap are created and then only software bitmap is updated every
45 // frame, hardware bitmap outlives until the media player dies. So we delete
46 // a temporary resource if it is not used for 3 sec.
47 const int kTemporaryResourceDeletionDelay = 3; // Seconds;
49 bool CheckColorSpace(const scoped_refptr<VideoFrame>& video_frame,
50 VideoFrame::ColorSpace color_space) {
51 int result;
52 return video_frame->metadata()->GetInteger(
53 VideoFrameMetadata::COLOR_SPACE, &result) &&
54 result == color_space;
57 bool IsSkBitmapProperlySizedTexture(const SkBitmap* bitmap,
58 const gfx::Size& size) {
59 return bitmap->getTexture() && bitmap->width() == size.width() &&
60 bitmap->height() == size.height();
63 bool AllocateSkBitmapTexture(GrContext* gr,
64 SkBitmap* bitmap,
65 const gfx::Size& size) {
66 DCHECK(gr);
67 GrTextureDesc desc;
68 // Use kRGBA_8888_GrPixelConfig, not kSkia8888_GrPixelConfig, to avoid
69 // RGBA to BGRA conversion.
70 desc.fConfig = kRGBA_8888_GrPixelConfig;
71 desc.fFlags = kRenderTarget_GrSurfaceFlag;
72 desc.fSampleCnt = 0;
73 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
74 desc.fWidth = size.width();
75 desc.fHeight = size.height();
76 skia::RefPtr<GrTexture> texture = skia::AdoptRef(
77 gr->textureProvider()->refScratchTexture(
78 desc, GrTextureProvider::kExact_ScratchTexMatch));
79 if (!texture.get())
80 return false;
82 SkImageInfo info = SkImageInfo::MakeN32Premul(desc.fWidth, desc.fHeight);
83 SkGrPixelRef* pixel_ref = SkNEW_ARGS(SkGrPixelRef, (info, texture.get()));
84 if (!pixel_ref)
85 return false;
86 bitmap->setInfo(info);
87 bitmap->setPixelRef(pixel_ref)->unref();
88 return true;
91 class SyncPointClientImpl : public VideoFrame::SyncPointClient {
92 public:
93 explicit SyncPointClientImpl(gpu::gles2::GLES2Interface* gl) : gl_(gl) {}
94 ~SyncPointClientImpl() override {}
95 uint32 InsertSyncPoint() override { return gl_->InsertSyncPointCHROMIUM(); }
96 void WaitSyncPoint(uint32 sync_point) override {
97 gl_->WaitSyncPointCHROMIUM(sync_point);
100 private:
101 gpu::gles2::GLES2Interface* gl_;
103 DISALLOW_IMPLICIT_CONSTRUCTORS(SyncPointClientImpl);
106 scoped_ptr<SkImage> CreateSkImageFromVideoFrameYUVTextures(
107 VideoFrame* video_frame,
108 const Context3D& context_3d) {
109 // Support only TEXTURE_YUV_420.
110 DCHECK(video_frame->HasTextures());
111 DCHECK_EQ(media::VideoFrame::I420, video_frame->format());
112 DCHECK_EQ(3u, media::VideoFrame::NumPlanes(video_frame->format()));
114 gpu::gles2::GLES2Interface* gl = context_3d.gl;
115 DCHECK(gl);
116 gfx::Size ya_tex_size = video_frame->coded_size();
117 gfx::Size uv_tex_size((ya_tex_size.width() + 1) / 2,
118 (ya_tex_size.height() + 1) / 2);
120 unsigned source_textures[3] = {0};
121 for (size_t i = 0; i < media::VideoFrame::NumPlanes(video_frame->format());
122 ++i) {
123 // Get the texture from the mailbox and wrap it in a GrTexture.
124 const gpu::MailboxHolder& mailbox_holder = video_frame->mailbox_holder(i);
125 DCHECK(mailbox_holder.texture_target == GL_TEXTURE_2D ||
126 mailbox_holder.texture_target == GL_TEXTURE_EXTERNAL_OES ||
127 mailbox_holder.texture_target == GL_TEXTURE_RECTANGLE_ARB);
128 gl->WaitSyncPointCHROMIUM(mailbox_holder.sync_point);
129 source_textures[i] = gl->CreateAndConsumeTextureCHROMIUM(
130 mailbox_holder.texture_target, mailbox_holder.mailbox.name);
132 // TODO(dcastagna): avoid this copy once Skia supports native textures
133 // with a texture target different than TEXTURE_2D.
134 // crbug.com/505026
135 if (mailbox_holder.texture_target != GL_TEXTURE_2D) {
136 unsigned texture_copy = 0;
137 gl->GenTextures(1, &texture_copy);
138 DCHECK(texture_copy);
139 gl->BindTexture(GL_TEXTURE_2D, texture_copy);
140 gl->CopyTextureCHROMIUM(GL_TEXTURE_2D, source_textures[i], texture_copy,
141 GL_RGB, GL_UNSIGNED_BYTE, false, true, false);
143 gl->DeleteTextures(1, &source_textures[i]);
144 source_textures[i] = texture_copy;
147 GrBackendObject handles[3] = {
148 source_textures[0], source_textures[1], source_textures[2]};
150 SkISize yuvSizes[] = {
151 {ya_tex_size.width(), ya_tex_size.height()},
152 {uv_tex_size.width(), uv_tex_size.height()},
153 {uv_tex_size.width(), uv_tex_size.height()},
156 // TODO(dcastagna): Skia currently doesn't support Rec709 YUV conversion.
157 DCHECK(!CheckColorSpace(video_frame, VideoFrame::COLOR_SPACE_HD_REC709));
158 SkYUVColorSpace color_space = kRec601_SkYUVColorSpace;
159 if (CheckColorSpace(video_frame, VideoFrame::COLOR_SPACE_JPEG))
160 color_space = kJPEG_SkYUVColorSpace;
162 SkImage* img = SkImage::NewFromYUVTexturesCopy(context_3d.gr_context,
163 color_space, handles, yuvSizes,
164 kTopLeft_GrSurfaceOrigin);
165 DCHECK(img);
166 gl->DeleteTextures(3, source_textures);
167 SyncPointClientImpl client(gl);
168 video_frame->UpdateReleaseSyncPoint(&client);
169 return make_scoped_ptr(img);
172 bool CopyVideoFrameSingleTextureToSkBitmap(VideoFrame* video_frame,
173 SkBitmap* bitmap,
174 const Context3D& context_3d) {
175 // Check if we could reuse existing texture based bitmap.
176 // Otherwise, release existing texture based bitmap and allocate
177 // a new one based on video size.
178 if (!IsSkBitmapProperlySizedTexture(bitmap,
179 video_frame->visible_rect().size())) {
180 if (!AllocateSkBitmapTexture(context_3d.gr_context, bitmap,
181 video_frame->visible_rect().size())) {
182 return false;
186 unsigned texture_id =
187 static_cast<unsigned>((bitmap->getTexture())->getTextureHandle());
188 // If CopyVideoFrameSingleTextureToGLTexture() changes the state of the
189 // |texture_id|, it's needed to invalidate the state cached in skia,
190 // but currently the state isn't changed.
192 SkCanvasVideoRenderer::CopyVideoFrameSingleTextureToGLTexture(
193 context_3d.gl, video_frame, texture_id, GL_RGBA, GL_UNSIGNED_BYTE, true,
194 false);
195 bitmap->notifyPixelsChanged();
196 return true;
199 } // anonymous namespace
201 // Generates an RGB image from a VideoFrame. Convert YUV to RGB plain on GPU.
202 class VideoImageGenerator : public SkImageGenerator {
203 public:
204 VideoImageGenerator(const scoped_refptr<VideoFrame>& frame)
205 : SkImageGenerator(
206 SkImageInfo::MakeN32Premul(frame->visible_rect().width(),
207 frame->visible_rect().height()))
208 , frame_(frame) {
209 DCHECK(frame_.get());
211 ~VideoImageGenerator() override {}
213 void set_frame(const scoped_refptr<VideoFrame>& frame) { frame_ = frame; }
215 protected:
216 Result onGetPixels(const SkImageInfo& info,
217 void* pixels,
218 size_t row_bytes,
219 const Options&,
220 SkPMColor ctable[],
221 int* ctable_count) override {
222 if (!frame_.get())
223 return kInvalidInput;
224 // If skia couldn't do the YUV conversion on GPU, we will on CPU.
225 SkCanvasVideoRenderer::ConvertVideoFrameToRGBPixels(
226 frame_, pixels, row_bytes);
227 return kSuccess;
230 bool onGetYUV8Planes(SkISize sizes[3],
231 void* planes[3],
232 size_t row_bytes[3],
233 SkYUVColorSpace* color_space) override {
234 if (!frame_.get() || !VideoFrame::IsYuvPlanar(frame_->format()) ||
235 // TODO(rileya): Skia currently doesn't support Rec709 YUV conversion,
236 // or YUVA conversion. Remove this case once it does. As-is we will
237 // fall back on the pure-software path in this case.
238 CheckColorSpace(frame_, VideoFrame::COLOR_SPACE_HD_REC709) ||
239 frame_->format() == VideoFrame::YV12A) {
240 return false;
243 if (color_space) {
244 if (CheckColorSpace(frame_, VideoFrame::COLOR_SPACE_JPEG))
245 *color_space = kJPEG_SkYUVColorSpace;
246 else
247 *color_space = kRec601_SkYUVColorSpace;
250 for (int plane = VideoFrame::kYPlane; plane <= VideoFrame::kVPlane;
251 ++plane) {
252 if (sizes) {
253 const gfx::Size size =
254 VideoFrame::PlaneSize(frame_->format(), plane,
255 gfx::Size(frame_->visible_rect().width(),
256 frame_->visible_rect().height()));
257 sizes[plane].set(size.width(), size.height());
259 if (row_bytes && planes) {
260 size_t offset;
261 const int y_shift = (frame_->format() == VideoFrame::YV16) ? 0 : 1;
262 if (plane == VideoFrame::kYPlane) {
263 offset = (frame_->stride(VideoFrame::kYPlane) *
264 frame_->visible_rect().y()) +
265 frame_->visible_rect().x();
266 } else {
267 offset = (frame_->stride(VideoFrame::kUPlane) *
268 (frame_->visible_rect().y() >> y_shift)) +
269 (frame_->visible_rect().x() >> 1);
272 // Copy the frame to the supplied memory.
273 // TODO: Find a way (API change?) to avoid this copy.
274 char* out_line = static_cast<char*>(planes[plane]);
275 int out_line_stride = row_bytes[plane];
276 uint8* in_line = frame_->data(plane) + offset;
277 int in_line_stride = frame_->stride(plane);
278 int plane_height = sizes[plane].height();
279 if (in_line_stride == out_line_stride) {
280 memcpy(out_line, in_line, plane_height * in_line_stride);
281 } else {
282 // Different line padding so need to copy one line at a time.
283 int bytes_to_copy_per_line = out_line_stride < in_line_stride
284 ? out_line_stride
285 : in_line_stride;
286 for (int line_no = 0; line_no < plane_height; line_no++) {
287 memcpy(out_line, in_line, bytes_to_copy_per_line);
288 in_line += in_line_stride;
289 out_line += out_line_stride;
294 return true;
297 private:
298 scoped_refptr<VideoFrame> frame_;
300 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoImageGenerator);
303 SkCanvasVideoRenderer::SkCanvasVideoRenderer()
304 : last_frame_timestamp_(media::kNoTimestamp()),
305 frame_deleting_timer_(
306 FROM_HERE,
307 base::TimeDelta::FromSeconds(kTemporaryResourceDeletionDelay),
308 this,
309 &SkCanvasVideoRenderer::ResetLastFrame),
310 accelerated_generator_(nullptr),
311 accelerated_last_frame_timestamp_(media::kNoTimestamp()),
312 accelerated_frame_deleting_timer_(
313 FROM_HERE,
314 base::TimeDelta::FromSeconds(kTemporaryResourceDeletionDelay),
315 this,
316 &SkCanvasVideoRenderer::ResetAcceleratedLastFrame) {
317 last_frame_.setIsVolatile(true);
320 SkCanvasVideoRenderer::~SkCanvasVideoRenderer() {}
322 void SkCanvasVideoRenderer::Paint(const scoped_refptr<VideoFrame>& video_frame,
323 SkCanvas* canvas,
324 const gfx::RectF& dest_rect,
325 uint8 alpha,
326 SkXfermode::Mode mode,
327 VideoRotation video_rotation,
328 const Context3D& context_3d) {
329 if (alpha == 0) {
330 return;
333 SkRect dest;
334 dest.set(dest_rect.x(), dest_rect.y(), dest_rect.right(), dest_rect.bottom());
336 SkPaint paint;
337 paint.setAlpha(alpha);
339 // Paint black rectangle if there isn't a frame available or the
340 // frame has an unexpected format.
341 if (!video_frame.get() || video_frame->natural_size().IsEmpty() ||
342 !(VideoFrame::IsYuvPlanar(video_frame->format()) ||
343 video_frame->HasTextures())) {
344 canvas->drawRect(dest, paint);
345 canvas->flush();
346 return;
349 SkBitmap* target_frame = nullptr;
351 if (video_frame->HasTextures()) {
352 // Draw HW Video on both SW and HW Canvas.
353 // In SW Canvas case, rely on skia drawing Ganesh SkBitmap on SW SkCanvas.
354 if (accelerated_last_frame_.isNull() ||
355 video_frame->timestamp() != accelerated_last_frame_timestamp_) {
356 DCHECK(context_3d.gl);
357 DCHECK(context_3d.gr_context);
358 if (accelerated_generator_) {
359 // Reset SkBitmap used in SWVideo-to-HWCanvas path.
360 accelerated_last_frame_.reset();
361 accelerated_generator_ = nullptr;
364 if (media::VideoFrame::NumPlanes(video_frame->format()) == 1) {
365 accelerated_last_image_.reset();
366 if (!CopyVideoFrameSingleTextureToSkBitmap(
367 video_frame.get(), &accelerated_last_frame_, context_3d)) {
368 NOTREACHED();
369 return;
371 DCHECK(video_frame->visible_rect().width() ==
372 accelerated_last_frame_.width() &&
373 video_frame->visible_rect().height() ==
374 accelerated_last_frame_.height());
375 } else {
376 accelerated_last_image_ = CreateSkImageFromVideoFrameYUVTextures(
377 video_frame.get(), context_3d);
378 DCHECK(accelerated_last_image_);
380 accelerated_last_frame_timestamp_ = video_frame->timestamp();
382 target_frame = &accelerated_last_frame_;
383 accelerated_frame_deleting_timer_.Reset();
384 } else if (canvas->getGrContext()) {
385 if (accelerated_last_frame_.isNull() ||
386 video_frame->timestamp() != accelerated_last_frame_timestamp_) {
387 // Draw SW Video on HW Canvas.
388 if (!accelerated_generator_ && !accelerated_last_frame_.isNull()) {
389 // Reset SkBitmap used in HWVideo-to-HWCanvas path.
390 accelerated_last_frame_.reset();
392 accelerated_generator_ = new VideoImageGenerator(video_frame);
394 // Note: This takes ownership of |accelerated_generator_|.
395 if (!SkInstallDiscardablePixelRef(accelerated_generator_,
396 &accelerated_last_frame_)) {
397 NOTREACHED();
398 return;
400 DCHECK(video_frame->visible_rect().width() ==
401 accelerated_last_frame_.width() &&
402 video_frame->visible_rect().height() ==
403 accelerated_last_frame_.height());
405 accelerated_last_frame_timestamp_ = video_frame->timestamp();
406 } else if (accelerated_generator_) {
407 accelerated_generator_->set_frame(video_frame);
409 target_frame = &accelerated_last_frame_;
410 accelerated_frame_deleting_timer_.Reset();
411 } else {
412 // Draw SW Video on SW Canvas.
413 DCHECK(video_frame->IsMappable());
414 if (last_frame_.isNull() ||
415 video_frame->timestamp() != last_frame_timestamp_) {
416 // Check if |bitmap| needs to be (re)allocated.
417 if (last_frame_.isNull() ||
418 last_frame_.width() != video_frame->visible_rect().width() ||
419 last_frame_.height() != video_frame->visible_rect().height()) {
420 last_frame_.allocN32Pixels(video_frame->visible_rect().width(),
421 video_frame->visible_rect().height());
422 last_frame_.setIsVolatile(true);
424 last_frame_.lockPixels();
425 ConvertVideoFrameToRGBPixels(
426 video_frame, last_frame_.getPixels(), last_frame_.rowBytes());
427 last_frame_.notifyPixelsChanged();
428 last_frame_.unlockPixels();
429 last_frame_timestamp_ = video_frame->timestamp();
431 target_frame = &last_frame_;
432 frame_deleting_timer_.Reset();
435 paint.setXfermodeMode(mode);
436 paint.setFilterQuality(kLow_SkFilterQuality);
438 const bool need_transform =
439 video_rotation != VIDEO_ROTATION_0 ||
440 dest_rect.size() != video_frame->visible_rect().size() ||
441 !dest_rect.origin().IsOrigin();
442 if (need_transform) {
443 canvas->save();
444 canvas->translate(
445 SkFloatToScalar(dest_rect.x() + (dest_rect.width() * 0.5f)),
446 SkFloatToScalar(dest_rect.y() + (dest_rect.height() * 0.5f)));
447 SkScalar angle = SkFloatToScalar(0.0f);
448 switch (video_rotation) {
449 case VIDEO_ROTATION_0:
450 break;
451 case VIDEO_ROTATION_90:
452 angle = SkFloatToScalar(90.0f);
453 break;
454 case VIDEO_ROTATION_180:
455 angle = SkFloatToScalar(180.0f);
456 break;
457 case VIDEO_ROTATION_270:
458 angle = SkFloatToScalar(270.0f);
459 break;
461 canvas->rotate(angle);
463 gfx::SizeF rotated_dest_size = dest_rect.size();
464 if (video_rotation == VIDEO_ROTATION_90 ||
465 video_rotation == VIDEO_ROTATION_270) {
466 rotated_dest_size =
467 gfx::SizeF(rotated_dest_size.height(), rotated_dest_size.width());
469 canvas->scale(
470 SkFloatToScalar(rotated_dest_size.width() / target_frame->width()),
471 SkFloatToScalar(rotated_dest_size.height() / target_frame->height()));
472 canvas->translate(-SkFloatToScalar(target_frame->width() * 0.5f),
473 -SkFloatToScalar(target_frame->height() * 0.5f));
475 if (accelerated_last_image_) {
476 canvas->drawImage(accelerated_last_image_.get(), 0, 0, &paint);
477 } else {
478 canvas->drawBitmap(*target_frame, 0, 0, &paint);
480 if (need_transform)
481 canvas->restore();
482 canvas->flush();
483 // SkCanvas::flush() causes the generator to generate SkImage, so delete
484 // |video_frame| not to be outlived.
485 if (canvas->getGrContext() && accelerated_generator_)
486 accelerated_generator_->set_frame(nullptr);
489 void SkCanvasVideoRenderer::Copy(const scoped_refptr<VideoFrame>& video_frame,
490 SkCanvas* canvas,
491 const Context3D& context_3d) {
492 Paint(video_frame, canvas, video_frame->visible_rect(), 0xff,
493 SkXfermode::kSrc_Mode, media::VIDEO_ROTATION_0, context_3d);
496 // static
497 void SkCanvasVideoRenderer::ConvertVideoFrameToRGBPixels(
498 const scoped_refptr<VideoFrame>& video_frame,
499 void* rgb_pixels,
500 size_t row_bytes) {
501 if (!video_frame->IsMappable()) {
502 NOTREACHED() << "Cannot extract pixels from non-CPU frame formats.";
503 return;
505 if (!VideoFrame::IsYuvPlanar(video_frame->format())) {
506 NOTREACHED() << "Non YUV formats are not supported";
507 return;
510 DCHECK_EQ(video_frame->stride(VideoFrame::kUPlane),
511 video_frame->stride(VideoFrame::kVPlane));
513 const int y_shift =
514 (video_frame->format() == VideoFrame::YV16) ? 0 : 1;
515 // Use the "left" and "top" of the destination rect to locate the offset
516 // in Y, U and V planes.
517 const size_t y_offset = (video_frame->stride(VideoFrame::kYPlane) *
518 video_frame->visible_rect().y()) +
519 video_frame->visible_rect().x();
520 // For format YV12, there is one U, V value per 2x2 block.
521 // For format YV16, there is one U, V value per 2x1 block.
522 const size_t uv_offset = (video_frame->stride(VideoFrame::kUPlane) *
523 (video_frame->visible_rect().y() >> y_shift)) +
524 (video_frame->visible_rect().x() >> 1);
526 switch (video_frame->format()) {
527 case VideoFrame::YV12:
528 case VideoFrame::I420:
529 if (CheckColorSpace(video_frame, VideoFrame::COLOR_SPACE_JPEG)) {
530 ConvertYUVToRGB32(
531 video_frame->data(VideoFrame::kYPlane) + y_offset,
532 video_frame->data(VideoFrame::kUPlane) + uv_offset,
533 video_frame->data(VideoFrame::kVPlane) + uv_offset,
534 static_cast<uint8*>(rgb_pixels),
535 video_frame->visible_rect().width(),
536 video_frame->visible_rect().height(),
537 video_frame->stride(VideoFrame::kYPlane),
538 video_frame->stride(VideoFrame::kUPlane),
539 row_bytes,
540 YV12J);
541 } else if (CheckColorSpace(video_frame,
542 VideoFrame::COLOR_SPACE_HD_REC709)) {
543 ConvertYUVToRGB32(video_frame->data(VideoFrame::kYPlane) + y_offset,
544 video_frame->data(VideoFrame::kUPlane) + uv_offset,
545 video_frame->data(VideoFrame::kVPlane) + uv_offset,
546 static_cast<uint8*>(rgb_pixels),
547 video_frame->visible_rect().width(),
548 video_frame->visible_rect().height(),
549 video_frame->stride(VideoFrame::kYPlane),
550 video_frame->stride(VideoFrame::kUPlane), row_bytes,
551 YV12HD);
552 } else {
553 LIBYUV_I420_TO_ARGB(
554 video_frame->data(VideoFrame::kYPlane) + y_offset,
555 video_frame->stride(VideoFrame::kYPlane),
556 video_frame->data(VideoFrame::kUPlane) + uv_offset,
557 video_frame->stride(VideoFrame::kUPlane),
558 video_frame->data(VideoFrame::kVPlane) + uv_offset,
559 video_frame->stride(VideoFrame::kVPlane),
560 static_cast<uint8*>(rgb_pixels),
561 row_bytes,
562 video_frame->visible_rect().width(),
563 video_frame->visible_rect().height());
565 break;
566 case VideoFrame::YV16:
567 LIBYUV_I422_TO_ARGB(
568 video_frame->data(VideoFrame::kYPlane) + y_offset,
569 video_frame->stride(VideoFrame::kYPlane),
570 video_frame->data(VideoFrame::kUPlane) + uv_offset,
571 video_frame->stride(VideoFrame::kUPlane),
572 video_frame->data(VideoFrame::kVPlane) + uv_offset,
573 video_frame->stride(VideoFrame::kVPlane),
574 static_cast<uint8*>(rgb_pixels),
575 row_bytes,
576 video_frame->visible_rect().width(),
577 video_frame->visible_rect().height());
578 break;
580 case VideoFrame::YV12A:
581 // Since libyuv doesn't support YUVA, fallback to media, which is not ARM
582 // optimized.
583 // TODO(fbarchard, mtomasz): Use libyuv, then copy the alpha channel.
584 ConvertYUVAToARGB(
585 video_frame->data(VideoFrame::kYPlane) + y_offset,
586 video_frame->data(VideoFrame::kUPlane) + uv_offset,
587 video_frame->data(VideoFrame::kVPlane) + uv_offset,
588 video_frame->data(VideoFrame::kAPlane),
589 static_cast<uint8*>(rgb_pixels),
590 video_frame->visible_rect().width(),
591 video_frame->visible_rect().height(),
592 video_frame->stride(VideoFrame::kYPlane),
593 video_frame->stride(VideoFrame::kUPlane),
594 video_frame->stride(VideoFrame::kAPlane),
595 row_bytes,
596 YV12);
597 break;
599 case VideoFrame::YV24:
600 libyuv::I444ToARGB(
601 video_frame->data(VideoFrame::kYPlane) + y_offset,
602 video_frame->stride(VideoFrame::kYPlane),
603 video_frame->data(VideoFrame::kUPlane) + uv_offset,
604 video_frame->stride(VideoFrame::kUPlane),
605 video_frame->data(VideoFrame::kVPlane) + uv_offset,
606 video_frame->stride(VideoFrame::kVPlane),
607 static_cast<uint8*>(rgb_pixels),
608 row_bytes,
609 video_frame->visible_rect().width(),
610 video_frame->visible_rect().height());
611 #if SK_R32_SHIFT == 0 && SK_G32_SHIFT == 8 && SK_B32_SHIFT == 16 && \
612 SK_A32_SHIFT == 24
613 libyuv::ARGBToABGR(static_cast<uint8*>(rgb_pixels),
614 row_bytes,
615 static_cast<uint8*>(rgb_pixels),
616 row_bytes,
617 video_frame->visible_rect().width(),
618 video_frame->visible_rect().height());
619 #endif
620 break;
621 #if defined(OS_MACOSX) || defined(OS_CHROMEOS)
622 case VideoFrame::NV12:
623 #endif
624 case VideoFrame::ARGB:
625 case VideoFrame::XRGB:
626 case VideoFrame::UNKNOWN:
627 NOTREACHED();
631 // static
632 void SkCanvasVideoRenderer::CopyVideoFrameSingleTextureToGLTexture(
633 gpu::gles2::GLES2Interface* gl,
634 VideoFrame* video_frame,
635 unsigned int texture,
636 unsigned int internal_format,
637 unsigned int type,
638 bool premultiply_alpha,
639 bool flip_y) {
640 DCHECK(video_frame);
641 DCHECK(video_frame->HasTextures());
642 DCHECK_EQ(1u, VideoFrame::NumPlanes(video_frame->format()));
644 const gpu::MailboxHolder& mailbox_holder = video_frame->mailbox_holder(0);
645 DCHECK(mailbox_holder.texture_target == GL_TEXTURE_2D ||
646 mailbox_holder.texture_target == GL_TEXTURE_RECTANGLE_ARB ||
647 mailbox_holder.texture_target == GL_TEXTURE_EXTERNAL_OES);
649 gl->WaitSyncPointCHROMIUM(mailbox_holder.sync_point);
650 uint32 source_texture = gl->CreateAndConsumeTextureCHROMIUM(
651 mailbox_holder.texture_target, mailbox_holder.mailbox.name);
653 // The video is stored in a unmultiplied format, so premultiply
654 // if necessary.
655 // Application itself needs to take care of setting the right |flip_y|
656 // value down to get the expected result.
657 // "flip_y == true" means to reverse the video orientation while
658 // "flip_y == false" means to keep the intrinsic orientation.
659 gl->CopyTextureCHROMIUM(GL_TEXTURE_2D, source_texture, texture,
660 internal_format, type,
661 flip_y, premultiply_alpha, false);
663 gl->DeleteTextures(1, &source_texture);
664 gl->Flush();
666 SyncPointClientImpl client(gl);
667 video_frame->UpdateReleaseSyncPoint(&client);
670 void SkCanvasVideoRenderer::ResetLastFrame() {
671 last_frame_.reset();
672 last_frame_timestamp_ = media::kNoTimestamp();
675 void SkCanvasVideoRenderer::ResetAcceleratedLastFrame() {
676 accelerated_last_image_.reset();
677 accelerated_last_frame_.reset();
678 accelerated_generator_ = nullptr;
679 accelerated_last_frame_timestamp_ = media::kNoTimestamp();
682 } // namespace media