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

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: Resolve comments, rebase to ToT 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 24 matching lines...) Expand all
35 #include "media/base/android/media_common_android.h" 35 #include "media/base/android/media_common_android.h"
36 #include "media/base/android/media_player_android.h" 36 #include "media/base/android/media_player_android.h"
37 #include "media/base/bind_to_current_loop.h" 37 #include "media/base/bind_to_current_loop.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"
46 #include "third_party/WebKit/public/platform/WebContentDecryptionModuleResult.h" 45 #include "third_party/WebKit/public/platform/WebContentDecryptionModuleResult.h"
47 #include "third_party/WebKit/public/platform/WebGraphicsContext3DProvider.h" 46 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
48 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h" 47 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h"
49 #include "third_party/WebKit/public/platform/WebString.h" 48 #include "third_party/WebKit/public/platform/WebString.h"
50 #include "third_party/WebKit/public/platform/WebURL.h" 49 #include "third_party/WebKit/public/platform/WebURL.h"
51 #include "third_party/WebKit/public/web/WebDocument.h" 50 #include "third_party/WebKit/public/web/WebDocument.h"
52 #include "third_party/WebKit/public/web/WebFrame.h" 51 #include "third_party/WebKit/public/web/WebFrame.h"
53 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" 52 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
54 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" 53 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
55 #include "third_party/WebKit/public/web/WebView.h" 54 #include "third_party/WebKit/public/web/WebView.h"
56 #include "third_party/skia/include/core/SkCanvas.h" 55 #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" 56 #include "third_party/skia/include/core/SkTypeface.h"
59 #include "ui/gfx/image/image.h" 57 #include "ui/gfx/image/image.h"
58 #include "webkit/common/gpu/context_provider_web_context.h"
59 #include "webkit/common/gpu/webgraphicscontext3d_impl.h"
60 60
61 static const uint32 kGLTextureExternalOES = 0x8D65; 61 static const uint32 kGLTextureExternalOES = 0x8D65;
62 static const int kSDKVersionToSupportSecurityOriginCheck = 20; 62 static const int kSDKVersionToSupportSecurityOriginCheck = 20;
63 63
64 using blink::WebMediaPlayer; 64 using blink::WebMediaPlayer;
65 using blink::WebSize; 65 using blink::WebSize;
66 using blink::WebString; 66 using blink::WebString;
67 using blink::WebURL; 67 using blink::WebURL;
68 using gpu::gles2::GLES2Interface; 68 using gpu::gles2::GLES2Interface;
69 using media::MediaPlayerAndroid; 69 using media::MediaPlayerAndroid;
70 using media::VideoFrame; 70 using media::VideoFrame;
71 71
72 namespace { 72 namespace {
73 // Prefix for histograms related to Encrypted Media Extensions. 73 // Prefix for histograms related to Encrypted Media Extensions.
74 const char* kMediaEme = "Media.EME."; 74 const char* kMediaEme = "Media.EME.";
75 75
76 // File-static function is to allow it to run even after WMPA is deleted. 76 // File-static function is to allow it to run even after WMPA is deleted.
77 void OnReleaseTexture( 77 void OnReleaseTexture(
78 const scoped_refptr<content::StreamTextureFactory>& factories, 78 const scoped_refptr<content::StreamTextureFactory>& factories,
79 uint32 texture_id, 79 uint32 texture_id,
80 uint32 release_sync_point) { 80 uint32 release_sync_point) {
81 GLES2Interface* gl = factories->ContextGL(); 81 GLES2Interface* gl = factories->ContextGL();
82 gl->WaitSyncPointCHROMIUM(release_sync_point); 82 gl->WaitSyncPointCHROMIUM(release_sync_point);
83 gl->DeleteTextures(1, &texture_id); 83 gl->DeleteTextures(1, &texture_id);
84 } 84 }
85 85
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. 86 // Used for calls to decryptor_ready_cb_ where the result can be ignored.
104 void DoNothing(bool) { 87 void DoNothing(bool) {
105 } 88 }
106 89
107 } // namespace 90 } // namespace
108 91
109 namespace content { 92 namespace content {
110 93
111 WebMediaPlayerAndroid::WebMediaPlayerAndroid( 94 WebMediaPlayerAndroid::WebMediaPlayerAndroid(
112 blink::WebFrame* frame, 95 blink::WebFrame* frame,
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after
533 const blink::WebTimeRange seekable_range(0.0, duration()); 516 const blink::WebTimeRange seekable_range(0.0, duration());
534 return blink::WebTimeRanges(&seekable_range, 1); 517 return blink::WebTimeRanges(&seekable_range, 1);
535 } 518 }
536 519
537 bool WebMediaPlayerAndroid::didLoadingProgress() { 520 bool WebMediaPlayerAndroid::didLoadingProgress() {
538 bool ret = did_loading_progress_; 521 bool ret = did_loading_progress_;
539 did_loading_progress_ = false; 522 did_loading_progress_ = false;
540 return ret; 523 return ret;
541 } 524 }
542 525
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, 526 void WebMediaPlayerAndroid::paint(blink::WebCanvas* canvas,
577 const blink::WebRect& rect, 527 const blink::WebRect& rect,
578 unsigned char alpha, 528 unsigned char alpha,
579 SkXfermode::Mode mode) { 529 SkXfermode::Mode mode) {
580 DCHECK(main_thread_checker_.CalledOnValidThread()); 530 DCHECK(main_thread_checker_.CalledOnValidThread());
581 scoped_ptr<blink::WebGraphicsContext3DProvider> provider = 531 // Don't allow clients to copy an encrypted video frame.
582 scoped_ptr<blink::WebGraphicsContext3DProvider>(blink::Platform::current( 532 if (needs_external_surface_)
583 )->createSharedOffscreenGraphicsContext3DProvider());
584 if (!provider)
585 return;
586 blink::WebGraphicsContext3D* context3D = provider->context3d();
587 if (!context3D)
588 return; 533 return;
589 534
590 // Copy video texture into a RGBA texture based bitmap first as video texture 535 scoped_refptr<VideoFrame> video_frame;
591 // on Android is GL_TEXTURE_EXTERNAL_OES which is not supported by Skia yet. 536 {
592 // The bitmap's size needs to be the same as the video and use naturalSize() 537 base::AutoLock auto_lock(current_frame_lock_);
593 // here. Check if we could reuse existing texture based bitmap. 538 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 } 539 }
600 540
601 unsigned textureId = static_cast<unsigned>( 541 gfx::Rect gfx_rect(rect);
602 (bitmap_.getTexture())->getTextureHandle()); 542 media::Context3DProvider provider{nullptr, nullptr};
603 if (!copyVideoTextureToPlatformTexture(context3D, textureId, 0, 543 if (canvas->getGrContext() ||
604 GL_RGBA, GL_UNSIGNED_BYTE, true, false)) { 544 (video_frame.get() &&
scherkus (not reviewing) 2014/10/30 20:40:11 nit: is the .get() needed?
danakj 2014/10/30 21:02:52 It is for scoped_refptr for now, which this is. Bu
dshwang 2014/10/31 09:29:16 danakj, thank you for answering. scoped_refptr::op
605 return; 545 video_frame->format() == media::VideoFrame::NATIVE_TEXTURE)) {
546 scoped_refptr<cc::ContextProvider> context_provider =
547 RenderThreadImpl::current()->SharedMainThreadContextProvider();
548 // GPU Process crashed.
549 if (!context_provider.get())
550 return;
551 provider = {context_provider->ContextGL(), context_provider->GrContext()};
606 } 552 }
607 553 skcanvas_video_renderer_.Paint(video_frame.get(),
608 // Draw the texture based bitmap onto the Canvas. If the canvas is 554 canvas,
609 // hardware based, this will do a GPU-GPU texture copy. 555 gfx_rect,
610 // If the canvas is software based, the texture based bitmap will be 556 alpha,
611 // readbacked to system memory then draw onto the canvas. 557 mode,
612 SkRect dest; 558 media::VIDEO_ROTATION_0,
613 dest.set(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height); 559 provider);
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 } 560 }
621 561
622 bool WebMediaPlayerAndroid::copyVideoTextureToPlatformTexture( 562 bool WebMediaPlayerAndroid::copyVideoTextureToPlatformTexture(
623 blink::WebGraphicsContext3D* web_graphics_context, 563 blink::WebGraphicsContext3D* web_graphics_context,
624 unsigned int texture, 564 unsigned int texture,
625 unsigned int level, 565 unsigned int level,
626 unsigned int internal_format, 566 unsigned int internal_format,
627 unsigned int type, 567 unsigned int type,
628 bool premultiply_alpha, 568 bool premultiply_alpha,
629 bool flip_y) { 569 bool flip_y) {
630 DCHECK(main_thread_checker_.CalledOnValidThread()); 570 DCHECK(main_thread_checker_.CalledOnValidThread());
631 // Don't allow clients to copy an encrypted video frame. 571 // Don't allow clients to copy an encrypted video frame.
632 if (needs_external_surface_) 572 if (needs_external_surface_)
633 return false; 573 return false;
634 574
635 scoped_refptr<VideoFrame> video_frame; 575 scoped_refptr<VideoFrame> video_frame;
636 { 576 {
637 base::AutoLock auto_lock(current_frame_lock_); 577 base::AutoLock auto_lock(current_frame_lock_);
638 video_frame = current_frame_; 578 video_frame = current_frame_;
639 } 579 }
640 580
641 if (!video_frame.get() || 581 if (!video_frame.get() ||
642 video_frame->format() != media::VideoFrame::NATIVE_TEXTURE) 582 video_frame->format() != media::VideoFrame::NATIVE_TEXTURE)
643 return false; 583 return false;
584
585 #if DCHECK_IS_ON
644 const gpu::MailboxHolder* mailbox_holder = video_frame->mailbox_holder(); 586 const gpu::MailboxHolder* mailbox_holder = video_frame->mailbox_holder();
645 DCHECK((!is_remote_ && 587 DCHECK((!is_remote_ &&
646 mailbox_holder->texture_target == GL_TEXTURE_EXTERNAL_OES) || 588 mailbox_holder->texture_target == GL_TEXTURE_EXTERNAL_OES) ||
647 (is_remote_ && mailbox_holder->texture_target == GL_TEXTURE_2D)); 589 (is_remote_ && mailbox_holder->texture_target == GL_TEXTURE_2D));
590 #endif
648 591
649 web_graphics_context->waitSyncPoint(mailbox_holder->sync_point); 592 // TODO(dshwang): need more elegant way to convert WebGraphicsContext3D to
650 593 // GLES2Interface.
651 // Ensure the target of texture is set before copyTextureCHROMIUM, otherwise 594 gpu::gles2::GLES2Interface* gl =
652 // an invalid texture target may be used for copy texture. 595 static_cast<webkit::gpu::WebGraphicsContext3DImpl*>(web_graphics_context)
653 uint32 src_texture = web_graphics_context->createAndConsumeTextureCHROMIUM( 596 ->GetGLInterface();
654 mailbox_holder->texture_target, mailbox_holder->mailbox.name); 597 media::SkCanvasVideoRenderer::CopyVideoFrameTextureToGLTexture(
655 598 gl,
656 // The video is stored in an unmultiplied format, so premultiply if 599 video_frame.get(),
scherkus (not reviewing) 2014/10/30 20:40:11 nit: is .get() needed?
dshwang 2014/10/31 09:29:16 not needed. done.
657 // necessary. 600 texture,
658 web_graphics_context->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM, 601 level,
659 premultiply_alpha); 602 internal_format,
660 603 type,
661 // Application itself needs to take care of setting the right flip_y 604 premultiply_alpha,
662 // value down to get the expected result. 605 flip_y);
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; 606 return true;
679 } 607 }
680 608
681 bool WebMediaPlayerAndroid::hasSingleSecurityOrigin() const { 609 bool WebMediaPlayerAndroid::hasSingleSecurityOrigin() const {
682 DCHECK(main_thread_checker_.CalledOnValidThread()); 610 DCHECK(main_thread_checker_.CalledOnValidThread());
683 if (player_type_ != MEDIA_PLAYER_TYPE_URL) 611 if (player_type_ != MEDIA_PLAYER_TYPE_URL)
684 return true; 612 return true;
685 613
686 if (!info_loader_ || !info_loader_->HasSingleOrigin()) 614 if (!info_loader_ || !info_loader_->HasSingleOrigin())
687 return false; 615 return false;
(...skipping 1137 matching lines...) Expand 10 before | Expand all | Expand 10 after
1825 1753
1826 bool WebMediaPlayerAndroid::IsHLSStream() const { 1754 bool WebMediaPlayerAndroid::IsHLSStream() const {
1827 std::string mime; 1755 std::string mime;
1828 GURL url = redirected_url_.is_empty() ? url_ : redirected_url_; 1756 GURL url = redirected_url_.is_empty() ? url_ : redirected_url_;
1829 if (!net::GetMimeTypeFromFile(base::FilePath(url.path()), &mime)) 1757 if (!net::GetMimeTypeFromFile(base::FilePath(url.path()), &mime))
1830 return false; 1758 return false;
1831 return !mime.compare("application/x-mpegurl"); 1759 return !mime.compare("application/x-mpegurl");
1832 } 1760 }
1833 1761
1834 } // namespace content 1762 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698