Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(698)

Side by Side Diff: media/filters/skcanvas_video_renderer.cc

Issue 445013002: media: Optimize HW Video to 2D Canvas copy. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: SkCanvasVideoRenderer use WebGraphicsContext3D Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "gpu/GLES2/gl2extchromium.h"
8 #include "gpu/command_buffer/common/mailbox_holder.h"
8 #include "media/base/video_frame.h" 9 #include "media/base/video_frame.h"
9 #include "media/base/yuv_convert.h" 10 #include "media/base/yuv_convert.h"
11 #include "skia/ext/refptr.h"
12 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
10 #include "third_party/libyuv/include/libyuv.h" 13 #include "third_party/libyuv/include/libyuv.h"
11 #include "third_party/skia/include/core/SkCanvas.h" 14 #include "third_party/skia/include/core/SkCanvas.h"
12 #include "third_party/skia/include/core/SkImageGenerator.h" 15 #include "third_party/skia/include/core/SkImageGenerator.h"
13 #include "third_party/skia/include/gpu/GrContext.h" 16 #include "third_party/skia/include/gpu/GrContext.h"
17 #include "third_party/skia/include/gpu/SkGrPixelRef.h"
14 #include "ui/gfx/skbitmap_operations.h" 18 #include "ui/gfx/skbitmap_operations.h"
15 19
16 // Skia internal format depends on a platform. On Android it is ABGR, on others 20 // Skia internal format depends on a platform. On Android it is ABGR, on others
17 // it is ARGB. 21 // it is ARGB.
18 #if SK_B32_SHIFT == 0 && SK_G32_SHIFT == 8 && SK_R32_SHIFT == 16 && \ 22 #if SK_B32_SHIFT == 0 && SK_G32_SHIFT == 8 && SK_R32_SHIFT == 16 && \
19 SK_A32_SHIFT == 24 23 SK_A32_SHIFT == 24
20 #define LIBYUV_I420_TO_ARGB libyuv::I420ToARGB 24 #define LIBYUV_I420_TO_ARGB libyuv::I420ToARGB
21 #define LIBYUV_I422_TO_ARGB libyuv::I422ToARGB 25 #define LIBYUV_I422_TO_ARGB libyuv::I422ToARGB
22 #elif SK_R32_SHIFT == 0 && SK_G32_SHIFT == 8 && SK_B32_SHIFT == 16 && \ 26 #elif SK_R32_SHIFT == 0 && SK_G32_SHIFT == 8 && SK_B32_SHIFT == 16 && \
23 SK_A32_SHIFT == 24 27 SK_A32_SHIFT == 24
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 row_bytes); 211 row_bytes);
208 video_frame->ReadPixelsFromNativeTexture(tmp); 212 video_frame->ReadPixelsFromNativeTexture(tmp);
209 break; 213 break;
210 } 214 }
211 default: 215 default:
212 NOTREACHED(); 216 NOTREACHED();
213 break; 217 break;
214 } 218 }
215 } 219 }
216 220
221 bool IsSkBitmapProperlySizedTexture(const SkBitmap* bitmap,
222 const gfx::Size& size) {
223 return bitmap->getTexture() && bitmap->width() == size.width() &&
224 bitmap->height() == size.height();
225 }
226
227 bool AllocateSkBitmapTexture(GrContext* gr,
228 SkBitmap* bitmap,
229 const gfx::Size& size) {
230 DCHECK(gr);
231 GrTextureDesc desc;
232 // Use kRGBA_8888_GrPixelConfig, not kSkia8888_GrPixelConfig, because
233 // glCopyTextureChromium doesn't support GL_BGRA_EXT as internal format.
234 desc.fConfig = kRGBA_8888_GrPixelConfig;
235 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
236 desc.fSampleCnt = 0;
237 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
238 desc.fWidth = size.width();
239 desc.fHeight = size.height();
240 skia::RefPtr<GrTexture> texture = skia::AdoptRef(
241 gr->refScratchTexture(desc, GrContext::kExact_ScratchTexMatch));
242 if (!texture.get())
243 return false;
244
245 SkImageInfo info = SkImageInfo::MakeN32Premul(desc.fWidth, desc.fHeight);
246 SkGrPixelRef* pixel_ref = SkNEW_ARGS(SkGrPixelRef, (info, texture.get()));
247 if (!pixel_ref)
248 return false;
249 bitmap->setInfo(info);
250 bitmap->setPixelRef(pixel_ref)->unref();
251 return true;
252 }
253
254 bool CopyVideoFrameTextureToSkBitmapTexture(
255 VideoFrame* video_frame,
256 SkBitmap* bitmap,
257 blink::WebGraphicsContext3DProvider* context_provider) {
258 // Check if we could reuse existing texture based bitmap.
259 // Otherwise, release existing texture based bitmap and allocate
260 // a new one based on video size.
261 if (!IsSkBitmapProperlySizedTexture(bitmap,
262 video_frame->visible_rect().size())) {
263 if (!AllocateSkBitmapTexture(context_provider->grContext(), bitmap,
264 video_frame->visible_rect().size())) {
265 return false;
266 }
267 }
268
269 unsigned texture_id =
270 static_cast<unsigned>((bitmap->getTexture())->getTextureHandle());
271 // If CopyVideoFrameTextureToGLTexture() changes the state of the
272 // |texture_id|, it's needed to invalidate the state cached in skia,
273 // but currently the state isn't changed.
274 SkCanvasVideoRenderer::CopyVideoFrameTextureToGLTexture(
275 context_provider->context3d(), video_frame, texture_id, 0, GL_RGBA,
276 GL_UNSIGNED_BYTE, true, false);
277 return true;
278 }
279
280 class SyncPointClientImpl : public VideoFrame::SyncPointClient {
281 public:
282 explicit SyncPointClientImpl(blink::WebGraphicsContext3D* context3d)
283 : context3d_(context3d) {}
284 ~SyncPointClientImpl() override {}
285 uint32 InsertSyncPoint() override { return context3d_->insertSyncPoint(); }
286 void WaitSyncPoint(uint32 sync_point) override {
287 context3d_->waitSyncPoint(sync_point);
288 }
289
290 private:
291 blink::WebGraphicsContext3D* context3d_;
292
293 DISALLOW_IMPLICIT_CONSTRUCTORS(SyncPointClientImpl);
294 };
295
217 } // anonymous namespace 296 } // anonymous namespace
218 297
219 // Generates an RGB image from a VideoFrame. Convert YUV to RGB plain on GPU. 298 // Generates an RGB image from a VideoFrame. Convert YUV to RGB plain on GPU.
220 class VideoImageGenerator : public SkImageGenerator { 299 class VideoImageGenerator : public SkImageGenerator {
221 public: 300 public:
222 VideoImageGenerator(const scoped_refptr<VideoFrame>& frame) : frame_(frame) { 301 VideoImageGenerator(const scoped_refptr<VideoFrame>& frame) : frame_(frame) {
223 DCHECK(frame_.get()); 302 DCHECK(frame_.get());
224 } 303 }
225 ~VideoImageGenerator() override {} 304 ~VideoImageGenerator() override {}
226 305
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoImageGenerator); 378 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoImageGenerator);
300 }; 379 };
301 380
302 SkCanvasVideoRenderer::SkCanvasVideoRenderer() 381 SkCanvasVideoRenderer::SkCanvasVideoRenderer()
303 : last_frame_timestamp_(media::kNoTimestamp()), 382 : last_frame_timestamp_(media::kNoTimestamp()),
304 frame_deleting_timer_( 383 frame_deleting_timer_(
305 FROM_HERE, 384 FROM_HERE,
306 base::TimeDelta::FromSeconds(kTemporaryResourceDeletionDelay), 385 base::TimeDelta::FromSeconds(kTemporaryResourceDeletionDelay),
307 this, 386 this,
308 &SkCanvasVideoRenderer::ResetLastFrame), 387 &SkCanvasVideoRenderer::ResetLastFrame),
309 accelerated_generator_(NULL), 388 accelerated_generator_(nullptr),
310 accelerated_last_frame_timestamp_(media::kNoTimestamp()), 389 accelerated_last_frame_timestamp_(media::kNoTimestamp()),
311 accelerated_frame_deleting_timer_( 390 accelerated_frame_deleting_timer_(
312 FROM_HERE, 391 FROM_HERE,
313 base::TimeDelta::FromSeconds(kTemporaryResourceDeletionDelay), 392 base::TimeDelta::FromSeconds(kTemporaryResourceDeletionDelay),
314 this, 393 this,
315 &SkCanvasVideoRenderer::ResetAcceleratedLastFrame) { 394 &SkCanvasVideoRenderer::ResetAcceleratedLastFrame) {
316 last_frame_.setIsVolatile(true); 395 last_frame_.setIsVolatile(true);
317 } 396 }
318 397
319 SkCanvasVideoRenderer::~SkCanvasVideoRenderer() {} 398 SkCanvasVideoRenderer::~SkCanvasVideoRenderer() {}
320 399
321 void SkCanvasVideoRenderer::Paint(const scoped_refptr<VideoFrame>& video_frame, 400 void SkCanvasVideoRenderer::Paint(
322 SkCanvas* canvas, 401 const scoped_refptr<VideoFrame>& video_frame,
323 const gfx::RectF& dest_rect, 402 SkCanvas* canvas,
324 uint8 alpha, 403 const gfx::RectF& dest_rect,
325 SkXfermode::Mode mode, 404 uint8 alpha,
326 VideoRotation video_rotation) { 405 SkXfermode::Mode mode,
406 VideoRotation video_rotation,
407 scoped_ptr<blink::WebGraphicsContext3DProvider> context_provider) {
327 if (alpha == 0) { 408 if (alpha == 0) {
328 return; 409 return;
329 } 410 }
330 411
331 SkRect dest; 412 SkRect dest;
332 dest.set(dest_rect.x(), dest_rect.y(), dest_rect.right(), dest_rect.bottom()); 413 dest.set(dest_rect.x(), dest_rect.y(), dest_rect.right(), dest_rect.bottom());
333 414
334 SkPaint paint; 415 SkPaint paint;
335 paint.setAlpha(alpha); 416 paint.setAlpha(alpha);
336 417
337 // Paint black rectangle if there isn't a frame available or the 418 // Paint black rectangle if there isn't a frame available or the
338 // frame has an unexpected format. 419 // frame has an unexpected format.
339 if (!video_frame.get() || video_frame->natural_size().IsEmpty() || 420 if (!video_frame.get() || video_frame->natural_size().IsEmpty() ||
340 !IsYUVOrNative(video_frame->format())) { 421 !IsYUVOrNative(video_frame->format())) {
341 canvas->drawRect(dest, paint); 422 canvas->drawRect(dest, paint);
342 canvas->flush(); 423 canvas->flush();
343 return; 424 return;
344 } 425 }
345 426
346 SkBitmap* target_frame = NULL; 427 SkBitmap* target_frame = nullptr;
347 if (canvas->getGrContext()) { 428 if (canvas->getGrContext()) {
429 // TODO(dshwang): Android video decoder doesn't update the timestamp on a
430 // VideoFrame. To reduce redundant copy, Android should update the
431 // timestamp.
348 if (accelerated_last_frame_.isNull() || 432 if (accelerated_last_frame_.isNull() ||
433 #if defined(OS_ANDROID)
434 video_frame->timestamp() == base::TimeDelta() ||
435 #endif
349 video_frame->timestamp() != accelerated_last_frame_timestamp_) { 436 video_frame->timestamp() != accelerated_last_frame_timestamp_) {
350 accelerated_generator_ = new VideoImageGenerator(video_frame); 437 if (video_frame->format() == VideoFrame::NATIVE_TEXTURE) {
438 DCHECK(context_provider.get());
439 // Draw HW Video on HW Canvas.
440 DCHECK(!accelerated_generator_);
441 if (!CopyVideoFrameTextureToSkBitmapTexture(video_frame.get(),
442 &accelerated_last_frame_,
443 context_provider.get())) {
444 NOTREACHED();
445 return;
446 }
447 } else {
448 // Draw SW Video on HW Canvas.
449 accelerated_generator_ = new VideoImageGenerator(video_frame);
351 450
352 // Note: This takes ownership of |accelerated_generator_|. 451 // Note: This takes ownership of |accelerated_generator_|.
353 if (!SkInstallDiscardablePixelRef(accelerated_generator_, 452 if (!SkInstallDiscardablePixelRef(accelerated_generator_,
354 &accelerated_last_frame_)) { 453 &accelerated_last_frame_)) {
355 NOTREACHED(); 454 NOTREACHED();
455 return;
456 }
356 } 457 }
357 DCHECK(video_frame->visible_rect().width() == 458 DCHECK(video_frame->visible_rect().width() ==
358 accelerated_last_frame_.width() && 459 accelerated_last_frame_.width() &&
359 video_frame->visible_rect().height() == 460 video_frame->visible_rect().height() ==
360 accelerated_last_frame_.height()); 461 accelerated_last_frame_.height());
361 462
362 accelerated_last_frame_timestamp_ = video_frame->timestamp(); 463 accelerated_last_frame_timestamp_ = video_frame->timestamp();
363 } else { 464 } else if (accelerated_generator_) {
364 accelerated_generator_->set_frame(video_frame); 465 accelerated_generator_->set_frame(video_frame);
365 } 466 }
366 target_frame = &accelerated_last_frame_; 467 target_frame = &accelerated_last_frame_;
367 accelerated_frame_deleting_timer_.Reset(); 468 accelerated_frame_deleting_timer_.Reset();
368 } else { 469 } else {
369 // Check if we should convert and update |last_frame_|. 470 // Draw both SW and HW Video on SW Canvas.
370 if (last_frame_.isNull() || 471 if (last_frame_.isNull() ||
371 video_frame->timestamp() != last_frame_timestamp_) { 472 video_frame->timestamp() != last_frame_timestamp_) {
372 // Check if |bitmap| needs to be (re)allocated. 473 // Check if |bitmap| needs to be (re)allocated.
373 if (last_frame_.isNull() || 474 if (last_frame_.isNull() ||
374 last_frame_.width() != video_frame->visible_rect().width() || 475 last_frame_.width() != video_frame->visible_rect().width() ||
375 last_frame_.height() != video_frame->visible_rect().height()) { 476 last_frame_.height() != video_frame->visible_rect().height()) {
376 last_frame_.allocN32Pixels(video_frame->visible_rect().width(), 477 last_frame_.allocN32Pixels(video_frame->visible_rect().width(),
377 video_frame->visible_rect().height()); 478 video_frame->visible_rect().height());
378 last_frame_.setIsVolatile(true); 479 last_frame_.setIsVolatile(true);
379 } 480 }
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 SkFloatToScalar(rotated_dest_size.height() / target_frame->height())); 528 SkFloatToScalar(rotated_dest_size.height() / target_frame->height()));
428 canvas->translate(-SkFloatToScalar(target_frame->width() * 0.5f), 529 canvas->translate(-SkFloatToScalar(target_frame->width() * 0.5f),
429 -SkFloatToScalar(target_frame->height() * 0.5f)); 530 -SkFloatToScalar(target_frame->height() * 0.5f));
430 } 531 }
431 canvas->drawBitmap(*target_frame, 0, 0, &paint); 532 canvas->drawBitmap(*target_frame, 0, 0, &paint);
432 if (need_transform) 533 if (need_transform)
433 canvas->restore(); 534 canvas->restore();
434 canvas->flush(); 535 canvas->flush();
435 // SkCanvas::flush() causes the generator to generate SkImage, so delete 536 // SkCanvas::flush() causes the generator to generate SkImage, so delete
436 // |video_frame| not to be outlived. 537 // |video_frame| not to be outlived.
437 if (canvas->getGrContext()) 538 if (canvas->getGrContext() && accelerated_generator_)
438 accelerated_generator_->set_frame(NULL); 539 accelerated_generator_->set_frame(nullptr);
439 } 540 }
440 541
441 void SkCanvasVideoRenderer::Copy(const scoped_refptr<VideoFrame>& video_frame, 542 void SkCanvasVideoRenderer::Copy(const scoped_refptr<VideoFrame>& video_frame,
442 SkCanvas* canvas) { 543 SkCanvas* canvas) {
443 Paint(video_frame, 544 Paint(video_frame, canvas, video_frame->visible_rect(), 0xff,
444 canvas, 545 SkXfermode::kSrc_Mode, media::VIDEO_ROTATION_0,
445 video_frame->visible_rect(), 546 scoped_ptr<blink::WebGraphicsContext3DProvider>());
446 0xff, 547 }
447 SkXfermode::kSrc_Mode, 548
448 media::VIDEO_ROTATION_0); 549 // static
550 void SkCanvasVideoRenderer::CopyVideoFrameTextureToGLTexture(
551 blink::WebGraphicsContext3D* context3d,
552 VideoFrame* video_frame,
553 unsigned int texture,
554 unsigned int level,
555 unsigned int internal_format,
556 unsigned int type,
557 bool premultiply_alpha,
558 bool flip_y) {
559 DCHECK(video_frame && video_frame->format() == VideoFrame::NATIVE_TEXTURE);
560 const gpu::MailboxHolder* mailbox_holder = video_frame->mailbox_holder();
561 DCHECK(mailbox_holder->texture_target == GL_TEXTURE_2D ||
562 mailbox_holder->texture_target == GL_TEXTURE_EXTERNAL_OES);
563
564 context3d->waitSyncPoint(mailbox_holder->sync_point);
565 uint32 source_texture = context3d->createAndConsumeTextureCHROMIUM(
566 mailbox_holder->texture_target, mailbox_holder->mailbox.name);
567
568 // The video is stored in a unmultiplied format, so premultiply
569 // if necessary.
570 context3d->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM,
571 premultiply_alpha);
572 // Application itself needs to take care of setting the right |flip_y|
573 // value down to get the expected result.
574 // "flip_y == true" means to reverse the video orientation while
575 // "flip_y == false" means to keep the intrinsic orientation.
576 context3d->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, flip_y);
577 context3d->copyTextureCHROMIUM(GL_TEXTURE_2D, source_texture, texture, level,
578 internal_format, type);
579 context3d->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, false);
580 context3d->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM, false);
581
582 context3d->deleteTextures(1, &source_texture);
583 context3d->flush();
584
585 SyncPointClientImpl client(context3d);
586 video_frame->UpdateReleaseSyncPoint(&client);
449 } 587 }
450 588
451 void SkCanvasVideoRenderer::ResetLastFrame() { 589 void SkCanvasVideoRenderer::ResetLastFrame() {
452 last_frame_.reset(); 590 last_frame_.reset();
453 last_frame_timestamp_ = media::kNoTimestamp(); 591 last_frame_timestamp_ = media::kNoTimestamp();
454 } 592 }
455 593
456 void SkCanvasVideoRenderer::ResetAcceleratedLastFrame() { 594 void SkCanvasVideoRenderer::ResetAcceleratedLastFrame() {
457 accelerated_last_frame_.reset(); 595 accelerated_last_frame_.reset();
458 accelerated_generator_ = nullptr; 596 accelerated_generator_ = nullptr;
459 accelerated_last_frame_timestamp_ = media::kNoTimestamp(); 597 accelerated_last_frame_timestamp_ = media::kNoTimestamp();
460 } 598 }
461 599
462 } // namespace media 600 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698