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

Side by Side Diff: content/renderer/media/android/webmediaplayer_android.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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "content/renderer/media/android/webmediaplayer_android.h" 5 #include "content/renderer/media/android/webmediaplayer_android.h"
6 6
7 #include <limits> 7 #include <limits>
8 8
9 #include "base/android/build_info.h" 9 #include "base/android/build_info.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 26 matching lines...) Expand all
37 #include "media/base/key_systems.h" 37 #include "media/base/key_systems.h"
38 #include "media/base/media_keys.h" 38 #include "media/base/media_keys.h"
39 #include "media/base/media_log.h" 39 #include "media/base/media_log.h"
40 #include "media/base/media_switches.h" 40 #include "media/base/media_switches.h"
41 #include "media/base/video_frame.h" 41 #include "media/base/video_frame.h"
42 #include "media/blink/webmediaplayer_delegate.h" 42 #include "media/blink/webmediaplayer_delegate.h"
43 #include "media/blink/webmediaplayer_util.h" 43 #include "media/blink/webmediaplayer_util.h"
44 #include "net/base/mime_util.h" 44 #include "net/base/mime_util.h"
45 #include "third_party/WebKit/public/platform/Platform.h" 45 #include "third_party/WebKit/public/platform/Platform.h"
46 #include "third_party/WebKit/public/platform/WebContentDecryptionModuleResult.h" 46 #include "third_party/WebKit/public/platform/WebContentDecryptionModuleResult.h"
47 #include "third_party/WebKit/public/platform/WebGraphicsContext3DProvider.h" 47 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
48 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h" 48 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h"
49 #include "third_party/WebKit/public/platform/WebString.h" 49 #include "third_party/WebKit/public/platform/WebString.h"
50 #include "third_party/WebKit/public/platform/WebURL.h" 50 #include "third_party/WebKit/public/platform/WebURL.h"
51 #include "third_party/WebKit/public/web/WebDocument.h" 51 #include "third_party/WebKit/public/web/WebDocument.h"
52 #include "third_party/WebKit/public/web/WebFrame.h" 52 #include "third_party/WebKit/public/web/WebFrame.h"
53 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" 53 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
54 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" 54 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
55 #include "third_party/WebKit/public/web/WebView.h" 55 #include "third_party/WebKit/public/web/WebView.h"
56 #include "third_party/skia/include/core/SkCanvas.h" 56 #include "third_party/skia/include/core/SkCanvas.h"
57 #include "third_party/skia/include/core/SkPaint.h"
58 #include "third_party/skia/include/core/SkTypeface.h" 57 #include "third_party/skia/include/core/SkTypeface.h"
59 #include "ui/gfx/image/image.h" 58 #include "ui/gfx/image/image.h"
60 59
61 static const uint32 kGLTextureExternalOES = 0x8D65; 60 static const uint32 kGLTextureExternalOES = 0x8D65;
62 static const int kSDKVersionToSupportSecurityOriginCheck = 20; 61 static const int kSDKVersionToSupportSecurityOriginCheck = 20;
63 62
64 using blink::WebMediaPlayer; 63 using blink::WebMediaPlayer;
65 using blink::WebSize; 64 using blink::WebSize;
66 using blink::WebString; 65 using blink::WebString;
67 using blink::WebURL; 66 using blink::WebURL;
68 using gpu::gles2::GLES2Interface; 67 using gpu::gles2::GLES2Interface;
69 using media::MediaPlayerAndroid; 68 using media::MediaPlayerAndroid;
70 using media::VideoFrame; 69 using media::VideoFrame;
71 70
72 namespace { 71 namespace {
73 // Prefix for histograms related to Encrypted Media Extensions. 72 // Prefix for histograms related to Encrypted Media Extensions.
74 const char* kMediaEme = "Media.EME."; 73 const char* kMediaEme = "Media.EME.";
75 74
76 // File-static function is to allow it to run even after WMPA is deleted. 75 // File-static function is to allow it to run even after WMPA is deleted.
77 void OnReleaseTexture( 76 void OnReleaseTexture(
78 const scoped_refptr<content::StreamTextureFactory>& factories, 77 const scoped_refptr<content::StreamTextureFactory>& factories,
79 uint32 texture_id, 78 uint32 texture_id,
80 uint32 release_sync_point) { 79 uint32 release_sync_point) {
81 GLES2Interface* gl = factories->ContextGL(); 80 GLES2Interface* gl = factories->ContextGL();
82 gl->WaitSyncPointCHROMIUM(release_sync_point); 81 gl->WaitSyncPointCHROMIUM(release_sync_point);
83 gl->DeleteTextures(1, &texture_id); 82 gl->DeleteTextures(1, &texture_id);
84 } 83 }
85 84
86 class SyncPointClientImpl : public media::VideoFrame::SyncPointClient {
87 public:
88 explicit SyncPointClientImpl(
89 blink::WebGraphicsContext3D* web_graphics_context)
90 : web_graphics_context_(web_graphics_context) {}
91 virtual ~SyncPointClientImpl() {}
92 virtual uint32 InsertSyncPoint() override {
93 return web_graphics_context_->insertSyncPoint();
94 }
95 virtual void WaitSyncPoint(uint32 sync_point) override {
96 web_graphics_context_->waitSyncPoint(sync_point);
97 }
98
99 private:
100 blink::WebGraphicsContext3D* web_graphics_context_;
101 };
102
103 // Used for calls to decryptor_ready_cb_ where the result can be ignored. 85 // Used for calls to decryptor_ready_cb_ where the result can be ignored.
104 void DoNothing(bool) { 86 void DoNothing(bool) {
105 } 87 }
106 88
107 } // namespace 89 } // namespace
108 90
109 namespace content { 91 namespace content {
110 92
111 WebMediaPlayerAndroid::WebMediaPlayerAndroid( 93 WebMediaPlayerAndroid::WebMediaPlayerAndroid(
112 blink::WebFrame* frame, 94 blink::WebFrame* frame,
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after
533 const blink::WebTimeRange seekable_range(0.0, duration()); 515 const blink::WebTimeRange seekable_range(0.0, duration());
534 return blink::WebTimeRanges(&seekable_range, 1); 516 return blink::WebTimeRanges(&seekable_range, 1);
535 } 517 }
536 518
537 bool WebMediaPlayerAndroid::didLoadingProgress() { 519 bool WebMediaPlayerAndroid::didLoadingProgress() {
538 bool ret = did_loading_progress_; 520 bool ret = did_loading_progress_;
539 did_loading_progress_ = false; 521 did_loading_progress_ = false;
540 return ret; 522 return ret;
541 } 523 }
542 524
543 bool WebMediaPlayerAndroid::EnsureTextureBackedSkBitmap(GrContext* gr,
544 SkBitmap& bitmap,
545 const WebSize& size,
546 GrSurfaceOrigin origin,
547 GrPixelConfig config) {
548 DCHECK(main_thread_checker_.CalledOnValidThread());
549 if (!bitmap.getTexture() || bitmap.width() != size.width
550 || bitmap.height() != size.height) {
551 if (!gr)
552 return false;
553 GrTextureDesc desc;
554 desc.fConfig = config;
555 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
556 desc.fSampleCnt = 0;
557 desc.fOrigin = origin;
558 desc.fWidth = size.width;
559 desc.fHeight = size.height;
560 skia::RefPtr<GrTexture> texture;
561 texture = skia::AdoptRef(gr->createUncachedTexture(desc, 0, 0));
562 if (!texture.get())
563 return false;
564
565 SkImageInfo info = SkImageInfo::MakeN32Premul(desc.fWidth, desc.fHeight);
566 SkGrPixelRef* pixelRef = SkNEW_ARGS(SkGrPixelRef, (info, texture.get()));
567 if (!pixelRef)
568 return false;
569 bitmap.setInfo(info);
570 bitmap.setPixelRef(pixelRef)->unref();
571 }
572
573 return true;
574 }
575
576 void WebMediaPlayerAndroid::paint(blink::WebCanvas* canvas, 525 void WebMediaPlayerAndroid::paint(blink::WebCanvas* canvas,
577 const blink::WebRect& rect, 526 const blink::WebRect& rect,
578 unsigned char alpha, 527 unsigned char alpha,
579 SkXfermode::Mode mode) { 528 SkXfermode::Mode mode) {
580 DCHECK(main_thread_checker_.CalledOnValidThread()); 529 DCHECK(main_thread_checker_.CalledOnValidThread());
581 scoped_ptr<blink::WebGraphicsContext3DProvider> provider = 530 // Don't allow clients to copy an encrypted video frame.
582 scoped_ptr<blink::WebGraphicsContext3DProvider>(blink::Platform::current( 531 if (needs_external_surface_)
583 )->createSharedOffscreenGraphicsContext3DProvider());
584 if (!provider)
585 return;
586 blink::WebGraphicsContext3D* context3D = provider->context3d();
587 if (!context3D)
588 return; 532 return;
589 533
590 // Copy video texture into a RGBA texture based bitmap first as video texture 534 scoped_refptr<VideoFrame> video_frame;
591 // on Android is GL_TEXTURE_EXTERNAL_OES which is not supported by Skia yet. 535 {
592 // The bitmap's size needs to be the same as the video and use naturalSize() 536 base::AutoLock auto_lock(current_frame_lock_);
593 // here. Check if we could reuse existing texture based bitmap. 537 video_frame = current_frame_;
594 // Otherwise, release existing texture based bitmap and allocate
595 // a new one based on video size.
596 if (!EnsureTextureBackedSkBitmap(provider->grContext(), bitmap_,
597 naturalSize(), kTopLeft_GrSurfaceOrigin, kSkia8888_GrPixelConfig)) {
598 return;
599 } 538 }
600 539 gfx::Rect gfx_rect(rect);
601 unsigned textureId = static_cast<unsigned>( 540 scoped_ptr<blink::WebGraphicsContext3DProvider> provider;
602 (bitmap_.getTexture())->getTextureHandle()); 541 if (video_frame.get() &&
603 if (!copyVideoTextureToPlatformTexture(context3D, textureId, 0, 542 video_frame->format() == media::VideoFrame::NATIVE_TEXTURE) {
604 GL_RGBA, GL_UNSIGNED_BYTE, true, false)) { 543 provider =
605 return; 544 make_scoped_ptr(blink::Platform::current()
545 ->createSharedOffscreenGraphicsContext3DProvider());
546 // GPU Process crashed.
547 if (!provider.get())
548 return;
606 } 549 }
607 550 skcanvas_video_renderer_.Paint(video_frame, canvas, gfx_rect, alpha, mode,
608 // Draw the texture based bitmap onto the Canvas. If the canvas is 551 media::VIDEO_ROTATION_0, provider.Pass());
609 // hardware based, this will do a GPU-GPU texture copy.
610 // If the canvas is software based, the texture based bitmap will be
611 // readbacked to system memory then draw onto the canvas.
612 SkRect dest;
613 dest.set(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height);
614 SkPaint paint;
615 paint.setAlpha(alpha);
616 paint.setXfermodeMode(mode);
617 // It is not necessary to pass the dest into the drawBitmap call since all
618 // the context have been set up before calling paintCurrentFrameInContext.
619 canvas->drawBitmapRect(bitmap_, 0, dest, &paint);
620 } 552 }
621 553
622 bool WebMediaPlayerAndroid::copyVideoTextureToPlatformTexture( 554 bool WebMediaPlayerAndroid::copyVideoTextureToPlatformTexture(
623 blink::WebGraphicsContext3D* web_graphics_context, 555 blink::WebGraphicsContext3D* web_graphics_context,
624 unsigned int texture, 556 unsigned int texture,
625 unsigned int level, 557 unsigned int level,
626 unsigned int internal_format, 558 unsigned int internal_format,
627 unsigned int type, 559 unsigned int type,
628 bool premultiply_alpha, 560 bool premultiply_alpha,
629 bool flip_y) { 561 bool flip_y) {
630 DCHECK(main_thread_checker_.CalledOnValidThread()); 562 DCHECK(main_thread_checker_.CalledOnValidThread());
631 // Don't allow clients to copy an encrypted video frame. 563 // Don't allow clients to copy an encrypted video frame.
632 if (needs_external_surface_) 564 if (needs_external_surface_)
633 return false; 565 return false;
634 566
635 scoped_refptr<VideoFrame> video_frame; 567 scoped_refptr<VideoFrame> video_frame;
636 { 568 {
637 base::AutoLock auto_lock(current_frame_lock_); 569 base::AutoLock auto_lock(current_frame_lock_);
638 video_frame = current_frame_; 570 video_frame = current_frame_;
639 } 571 }
640 572
641 if (!video_frame.get() || 573 if (!video_frame.get() ||
642 video_frame->format() != media::VideoFrame::NATIVE_TEXTURE) 574 video_frame->format() != media::VideoFrame::NATIVE_TEXTURE)
643 return false; 575 return false;
576
577 #if DCHECK_IS_ON
644 const gpu::MailboxHolder* mailbox_holder = video_frame->mailbox_holder(); 578 const gpu::MailboxHolder* mailbox_holder = video_frame->mailbox_holder();
645 DCHECK((!is_remote_ && 579 DCHECK((!is_remote_ &&
646 mailbox_holder->texture_target == GL_TEXTURE_EXTERNAL_OES) || 580 mailbox_holder->texture_target == GL_TEXTURE_EXTERNAL_OES) ||
647 (is_remote_ && mailbox_holder->texture_target == GL_TEXTURE_2D)); 581 (is_remote_ && mailbox_holder->texture_target == GL_TEXTURE_2D));
582 #endif
648 583
649 web_graphics_context->waitSyncPoint(mailbox_holder->sync_point); 584 media::SkCanvasVideoRenderer::CopyVideoFrameTextureToGLTexture(
650 585 web_graphics_context, video_frame.get(), texture, level, internal_format,
651 // Ensure the target of texture is set before copyTextureCHROMIUM, otherwise 586 type, premultiply_alpha, flip_y);
652 // an invalid texture target may be used for copy texture.
653 uint32 src_texture = web_graphics_context->createAndConsumeTextureCHROMIUM(
654 mailbox_holder->texture_target, mailbox_holder->mailbox.name);
655
656 // The video is stored in an unmultiplied format, so premultiply if
657 // necessary.
658 web_graphics_context->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM,
659 premultiply_alpha);
660
661 // Application itself needs to take care of setting the right flip_y
662 // value down to get the expected result.
663 // flip_y==true means to reverse the video orientation while
664 // flip_y==false means to keep the intrinsic orientation.
665 web_graphics_context->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, flip_y);
666 web_graphics_context->copyTextureCHROMIUM(GL_TEXTURE_2D, src_texture,
667 texture, level, internal_format,
668 type);
669 web_graphics_context->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, false);
670 web_graphics_context->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM,
671 false);
672
673 web_graphics_context->deleteTexture(src_texture);
674 web_graphics_context->flush();
675
676 SyncPointClientImpl client(web_graphics_context);
677 video_frame->UpdateReleaseSyncPoint(&client);
678 return true; 587 return true;
679 } 588 }
680 589
681 bool WebMediaPlayerAndroid::hasSingleSecurityOrigin() const { 590 bool WebMediaPlayerAndroid::hasSingleSecurityOrigin() const {
682 DCHECK(main_thread_checker_.CalledOnValidThread()); 591 DCHECK(main_thread_checker_.CalledOnValidThread());
683 if (player_type_ != MEDIA_PLAYER_TYPE_URL) 592 if (player_type_ != MEDIA_PLAYER_TYPE_URL)
684 return true; 593 return true;
685 594
686 if (!info_loader_ || !info_loader_->HasSingleOrigin()) 595 if (!info_loader_ || !info_loader_->HasSingleOrigin())
687 return false; 596 return false;
(...skipping 1137 matching lines...) Expand 10 before | Expand all | Expand 10 after
1825 1734
1826 bool WebMediaPlayerAndroid::IsHLSStream() const { 1735 bool WebMediaPlayerAndroid::IsHLSStream() const {
1827 std::string mime; 1736 std::string mime;
1828 GURL url = redirected_url_.is_empty() ? url_ : redirected_url_; 1737 GURL url = redirected_url_.is_empty() ? url_ : redirected_url_;
1829 if (!net::GetMimeTypeFromFile(base::FilePath(url.path()), &mime)) 1738 if (!net::GetMimeTypeFromFile(base::FilePath(url.path()), &mime))
1830 return false; 1739 return false;
1831 return !mime.compare("application/x-mpegurl"); 1740 return !mime.compare("application/x-mpegurl");
1832 } 1741 }
1833 1742
1834 } // namespace content 1743 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698