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

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: DCHECK context_provider and 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() &&
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, canvas, gfx_rect, alpha, mode,
608 // Draw the texture based bitmap onto the Canvas. If the canvas is 554 media::VIDEO_ROTATION_0, provider);
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 } 555 }
621 556
622 bool WebMediaPlayerAndroid::copyVideoTextureToPlatformTexture( 557 bool WebMediaPlayerAndroid::copyVideoTextureToPlatformTexture(
623 blink::WebGraphicsContext3D* web_graphics_context, 558 blink::WebGraphicsContext3D* web_graphics_context,
624 unsigned int texture, 559 unsigned int texture,
625 unsigned int level, 560 unsigned int level,
626 unsigned int internal_format, 561 unsigned int internal_format,
627 unsigned int type, 562 unsigned int type,
628 bool premultiply_alpha, 563 bool premultiply_alpha,
629 bool flip_y) { 564 bool flip_y) {
630 DCHECK(main_thread_checker_.CalledOnValidThread()); 565 DCHECK(main_thread_checker_.CalledOnValidThread());
631 // Don't allow clients to copy an encrypted video frame. 566 // Don't allow clients to copy an encrypted video frame.
632 if (needs_external_surface_) 567 if (needs_external_surface_)
633 return false; 568 return false;
634 569
635 scoped_refptr<VideoFrame> video_frame; 570 scoped_refptr<VideoFrame> video_frame;
636 { 571 {
637 base::AutoLock auto_lock(current_frame_lock_); 572 base::AutoLock auto_lock(current_frame_lock_);
638 video_frame = current_frame_; 573 video_frame = current_frame_;
639 } 574 }
640 575
641 if (!video_frame.get() || 576 if (!video_frame.get() ||
642 video_frame->format() != media::VideoFrame::NATIVE_TEXTURE) 577 video_frame->format() != media::VideoFrame::NATIVE_TEXTURE)
643 return false; 578 return false;
579
580 #if DCHECK_IS_ON
644 const gpu::MailboxHolder* mailbox_holder = video_frame->mailbox_holder(); 581 const gpu::MailboxHolder* mailbox_holder = video_frame->mailbox_holder();
645 DCHECK((!is_remote_ && 582 DCHECK((!is_remote_ &&
646 mailbox_holder->texture_target == GL_TEXTURE_EXTERNAL_OES) || 583 mailbox_holder->texture_target == GL_TEXTURE_EXTERNAL_OES) ||
647 (is_remote_ && mailbox_holder->texture_target == GL_TEXTURE_2D)); 584 (is_remote_ && mailbox_holder->texture_target == GL_TEXTURE_2D));
585 #endif
648 586
649 web_graphics_context->waitSyncPoint(mailbox_holder->sync_point); 587 // TODO(dshwang): need more elegant way to convert WebGraphicsContext3D to
jamesr 2014/11/13 23:24:22 this is really gross. why do you want a WebGraphi
650 588 // GLES2Interface.
651 // Ensure the target of texture is set before copyTextureCHROMIUM, otherwise 589 gpu::gles2::GLES2Interface* gl =
652 // an invalid texture target may be used for copy texture. 590 static_cast<webkit::gpu::WebGraphicsContext3DImpl*>(web_graphics_context)
653 uint32 src_texture = web_graphics_context->createAndConsumeTextureCHROMIUM( 591 ->GetGLInterface();
654 mailbox_holder->texture_target, mailbox_holder->mailbox.name); 592 media::SkCanvasVideoRenderer::CopyVideoFrameTextureToGLTexture(
655 593 gl, video_frame.get(), texture, level, internal_format, type,
656 // The video is stored in an unmultiplied format, so premultiply if 594 premultiply_alpha, flip_y);
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; 595 return true;
679 } 596 }
680 597
681 bool WebMediaPlayerAndroid::hasSingleSecurityOrigin() const { 598 bool WebMediaPlayerAndroid::hasSingleSecurityOrigin() const {
682 DCHECK(main_thread_checker_.CalledOnValidThread()); 599 DCHECK(main_thread_checker_.CalledOnValidThread());
683 if (player_type_ != MEDIA_PLAYER_TYPE_URL) 600 if (player_type_ != MEDIA_PLAYER_TYPE_URL)
684 return true; 601 return true;
685 602
686 if (!info_loader_ || !info_loader_->HasSingleOrigin()) 603 if (!info_loader_ || !info_loader_->HasSingleOrigin())
687 return false; 604 return false;
(...skipping 1137 matching lines...) Expand 10 before | Expand all | Expand 10 after
1825 1742
1826 bool WebMediaPlayerAndroid::IsHLSStream() const { 1743 bool WebMediaPlayerAndroid::IsHLSStream() const {
1827 std::string mime; 1744 std::string mime;
1828 GURL url = redirected_url_.is_empty() ? url_ : redirected_url_; 1745 GURL url = redirected_url_.is_empty() ? url_ : redirected_url_;
1829 if (!net::GetMimeTypeFromFile(base::FilePath(url.path()), &mime)) 1746 if (!net::GetMimeTypeFromFile(base::FilePath(url.path()), &mime))
1830 return false; 1747 return false;
1831 return !mime.compare("application/x-mpegurl"); 1748 return !mime.compare("application/x-mpegurl");
1832 } 1749 }
1833 1750
1834 } // namespace content 1751 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698