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
27 #if SK_B32_SHIFT == 0 && SK_G32_SHIFT == 8 && SK_R32_SHIFT == 16 && \
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 && \
33 #define LIBYUV_I420_TO_ARGB libyuv::I420ToABGR
34 #define LIBYUV_I422_TO_ARGB libyuv::I422ToABGR
36 #error Unexpected Skia ARGB_8888 layout!
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
) {
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
,
65 const gfx::Size
& size
) {
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
;
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
));
82 SkImageInfo info
= SkImageInfo::MakeN32Premul(desc
.fWidth
, desc
.fHeight
);
83 SkGrPixelRef
* pixel_ref
= SkNEW_ARGS(SkGrPixelRef
, (info
, texture
.get()));
86 bitmap
->setInfo(info
);
87 bitmap
->setPixelRef(pixel_ref
)->unref();
91 class SyncPointClientImpl
: public VideoFrame::SyncPointClient
{
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
);
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
;
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());
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.
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
);
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
,
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())) {
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,
195 bitmap
->notifyPixelsChanged();
199 } // anonymous namespace
201 // Generates an RGB image from a VideoFrame. Convert YUV to RGB plain on GPU.
202 class VideoImageGenerator
: public SkImageGenerator
{
204 VideoImageGenerator(const scoped_refptr
<VideoFrame
>& frame
)
206 SkImageInfo::MakeN32Premul(frame
->visible_rect().width(),
207 frame
->visible_rect().height()))
209 DCHECK(frame_
.get());
211 ~VideoImageGenerator() override
{}
213 void set_frame(const scoped_refptr
<VideoFrame
>& frame
) { frame_
= frame
; }
216 Result
onGetPixels(const SkImageInfo
& info
,
221 int* ctable_count
) override
{
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
);
230 bool onGetYUV8Planes(SkISize sizes
[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
) {
244 if (CheckColorSpace(frame_
, VideoFrame::COLOR_SPACE_JPEG
))
245 *color_space
= kJPEG_SkYUVColorSpace
;
247 *color_space
= kRec601_SkYUVColorSpace
;
250 for (int plane
= VideoFrame::kYPlane
; plane
<= VideoFrame::kVPlane
;
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
) {
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();
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
);
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
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
;
298 scoped_refptr
<VideoFrame
> frame_
;
300 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoImageGenerator
);
303 SkCanvasVideoRenderer::SkCanvasVideoRenderer()
304 : last_frame_timestamp_(media::kNoTimestamp()),
305 frame_deleting_timer_(
307 base::TimeDelta::FromSeconds(kTemporaryResourceDeletionDelay
),
309 &SkCanvasVideoRenderer::ResetLastFrame
),
310 accelerated_generator_(nullptr),
311 accelerated_last_frame_timestamp_(media::kNoTimestamp()),
312 accelerated_frame_deleting_timer_(
314 base::TimeDelta::FromSeconds(kTemporaryResourceDeletionDelay
),
316 &SkCanvasVideoRenderer::ResetAcceleratedLastFrame
) {
317 last_frame_
.setIsVolatile(true);
320 SkCanvasVideoRenderer::~SkCanvasVideoRenderer() {}
322 void SkCanvasVideoRenderer::Paint(const scoped_refptr
<VideoFrame
>& video_frame
,
324 const gfx::RectF
& dest_rect
,
326 SkXfermode::Mode mode
,
327 VideoRotation video_rotation
,
328 const Context3D
& context_3d
) {
334 dest
.set(dest_rect
.x(), dest_rect
.y(), dest_rect
.right(), dest_rect
.bottom());
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
);
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
)) {
371 DCHECK(video_frame
->visible_rect().width() ==
372 accelerated_last_frame_
.width() &&
373 video_frame
->visible_rect().height() ==
374 accelerated_last_frame_
.height());
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_
)) {
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();
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
) {
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
:
451 case VIDEO_ROTATION_90
:
452 angle
= SkFloatToScalar(90.0f
);
454 case VIDEO_ROTATION_180
:
455 angle
= SkFloatToScalar(180.0f
);
457 case VIDEO_ROTATION_270
:
458 angle
= SkFloatToScalar(270.0f
);
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
) {
467 gfx::SizeF(rotated_dest_size
.height(), rotated_dest_size
.width());
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
);
478 canvas
->drawBitmap(*target_frame
, 0, 0, &paint
);
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
,
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
);
497 void SkCanvasVideoRenderer::ConvertVideoFrameToRGBPixels(
498 const scoped_refptr
<VideoFrame
>& video_frame
,
501 if (!video_frame
->IsMappable()) {
502 NOTREACHED() << "Cannot extract pixels from non-CPU frame formats.";
505 if (!VideoFrame::IsYuvPlanar(video_frame
->format())) {
506 NOTREACHED() << "Non YUV formats are not supported";
510 DCHECK_EQ(video_frame
->stride(VideoFrame::kUPlane
),
511 video_frame
->stride(VideoFrame::kVPlane
));
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
)) {
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
),
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
,
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
),
562 video_frame
->visible_rect().width(),
563 video_frame
->visible_rect().height());
566 case VideoFrame::YV16
:
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
),
576 video_frame
->visible_rect().width(),
577 video_frame
->visible_rect().height());
580 case VideoFrame::YV12A
:
581 // Since libyuv doesn't support YUVA, fallback to media, which is not ARM
583 // TODO(fbarchard, mtomasz): Use libyuv, then copy the alpha channel.
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
),
599 case VideoFrame::YV24
:
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
),
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 && \
613 libyuv::ARGBToABGR(static_cast<uint8
*>(rgb_pixels
),
615 static_cast<uint8
*>(rgb_pixels
),
617 video_frame
->visible_rect().width(),
618 video_frame
->visible_rect().height());
621 #if defined(OS_MACOSX) || defined(OS_CHROMEOS)
622 case VideoFrame::NV12
:
624 case VideoFrame::ARGB
:
625 case VideoFrame::XRGB
:
626 case VideoFrame::UNKNOWN
:
632 void SkCanvasVideoRenderer::CopyVideoFrameSingleTextureToGLTexture(
633 gpu::gles2::GLES2Interface
* gl
,
634 VideoFrame
* video_frame
,
635 unsigned int texture
,
636 unsigned int internal_format
,
638 bool premultiply_alpha
,
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
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
);
666 SyncPointClientImpl
client(gl
);
667 video_frame
->UpdateReleaseSyncPoint(&client
);
670 void SkCanvasVideoRenderer::ResetLastFrame() {
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();