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

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: Use scratch texture Created 6 years, 3 months 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 23 matching lines...) Expand all
34 #include "gpu/command_buffer/client/gles2_interface.h" 34 #include "gpu/command_buffer/client/gles2_interface.h"
35 #include "gpu/command_buffer/common/mailbox_holder.h" 35 #include "gpu/command_buffer/common/mailbox_holder.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 // TODO(xhwang): Remove when we remove prefixed EME implementation. 38 // TODO(xhwang): Remove when we remove prefixed EME implementation.
39 #include "media/base/media_keys.h" 39 #include "media/base/media_keys.h"
40 #include "media/base/media_log.h" 40 #include "media/base/media_log.h"
41 #include "media/base/media_switches.h" 41 #include "media/base/media_switches.h"
42 #include "media/base/video_frame.h" 42 #include "media/base/video_frame.h"
43 #include "net/base/mime_util.h" 43 #include "net/base/mime_util.h"
44 #include "third_party/WebKit/public/platform/Platform.h"
45 #include "third_party/WebKit/public/platform/WebContentDecryptionModuleResult.h" 44 #include "third_party/WebKit/public/platform/WebContentDecryptionModuleResult.h"
46 #include "third_party/WebKit/public/platform/WebGraphicsContext3DProvider.h" 45 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
47 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h" 46 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h"
48 #include "third_party/WebKit/public/platform/WebString.h" 47 #include "third_party/WebKit/public/platform/WebString.h"
49 #include "third_party/WebKit/public/platform/WebURL.h" 48 #include "third_party/WebKit/public/platform/WebURL.h"
50 #include "third_party/WebKit/public/web/WebDocument.h" 49 #include "third_party/WebKit/public/web/WebDocument.h"
51 #include "third_party/WebKit/public/web/WebFrame.h" 50 #include "third_party/WebKit/public/web/WebFrame.h"
52 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" 51 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
53 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" 52 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
54 #include "third_party/WebKit/public/web/WebView.h" 53 #include "third_party/WebKit/public/web/WebView.h"
54 #include "third_party/skia/include/core/SkBitmap.h"
55 #include "third_party/skia/include/core/SkCanvas.h" 55 #include "third_party/skia/include/core/SkCanvas.h"
56 #include "third_party/skia/include/core/SkPaint.h"
57 #include "third_party/skia/include/core/SkTypeface.h" 56 #include "third_party/skia/include/core/SkTypeface.h"
58 #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"
59 60
60 static const uint32 kGLTextureExternalOES = 0x8D65; 61 static const uint32 kGLTextureExternalOES = 0x8D65;
61 static const int kSDKVersionToSupportSecurityOriginCheck = 20; 62 static const int kSDKVersionToSupportSecurityOriginCheck = 20;
62 63
63 using blink::WebMediaPlayer; 64 using blink::WebMediaPlayer;
64 using blink::WebSize; 65 using blink::WebSize;
65 using blink::WebString; 66 using blink::WebString;
66 using blink::WebTimeRanges; 67 using blink::WebTimeRanges;
67 using blink::WebURL; 68 using blink::WebURL;
68 using gpu::gles2::GLES2Interface; 69 using gpu::gles2::GLES2Interface;
69 using media::MediaPlayerAndroid; 70 using media::MediaPlayerAndroid;
70 using media::VideoFrame; 71 using media::VideoFrame;
71 72
72 namespace { 73 namespace {
73 // Prefix for histograms related to Encrypted Media Extensions. 74 // Prefix for histograms related to Encrypted Media Extensions.
74 const char* kMediaEme = "Media.EME."; 75 const char* kMediaEme = "Media.EME.";
75 76
76 // File-static function is to allow it to run even after WMPA is deleted. 77 // File-static function is to allow it to run even after WMPA is deleted.
77 void OnReleaseTexture( 78 void OnReleaseTexture(
78 const scoped_refptr<content::StreamTextureFactory>& factories, 79 const scoped_refptr<content::StreamTextureFactory>& factories,
79 uint32 texture_id, 80 uint32 texture_id,
80 uint32 release_sync_point) { 81 uint32 release_sync_point) {
81 GLES2Interface* gl = factories->ContextGL(); 82 GLES2Interface* gl = factories->ContextGL();
82 gl->WaitSyncPointCHROMIUM(release_sync_point); 83 gl->WaitSyncPointCHROMIUM(release_sync_point);
83 gl->DeleteTextures(1, &texture_id); 84 gl->DeleteTextures(1, &texture_id);
84 } 85 }
85 86
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. 87 // Used for calls to decryptor_ready_cb_ where the result can be ignored.
104 void DoNothing(bool) { 88 void DoNothing(bool) {
105 } 89 }
106 90
107 } // namespace 91 } // namespace
108 92
109 namespace content { 93 namespace content {
110 94
111 WebMediaPlayerAndroid::WebMediaPlayerAndroid( 95 WebMediaPlayerAndroid::WebMediaPlayerAndroid(
112 blink::WebFrame* frame, 96 blink::WebFrame* frame,
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 461
478 return duration(); 462 return duration();
479 } 463 }
480 464
481 bool WebMediaPlayerAndroid::didLoadingProgress() { 465 bool WebMediaPlayerAndroid::didLoadingProgress() {
482 bool ret = did_loading_progress_; 466 bool ret = did_loading_progress_;
483 did_loading_progress_ = false; 467 did_loading_progress_ = false;
484 return ret; 468 return ret;
485 } 469 }
486 470
487 bool WebMediaPlayerAndroid::EnsureTextureBackedSkBitmap(GrContext* gr,
488 SkBitmap& bitmap,
489 const WebSize& size,
490 GrSurfaceOrigin origin,
491 GrPixelConfig config) {
492 DCHECK(main_thread_checker_.CalledOnValidThread());
493 if (!bitmap.getTexture() || bitmap.width() != size.width
494 || bitmap.height() != size.height) {
495 if (!gr)
496 return false;
497 GrTextureDesc desc;
498 desc.fConfig = config;
499 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
500 desc.fSampleCnt = 0;
501 desc.fOrigin = origin;
502 desc.fWidth = size.width;
503 desc.fHeight = size.height;
504 skia::RefPtr<GrTexture> texture;
505 texture = skia::AdoptRef(gr->createUncachedTexture(desc, 0, 0));
506 if (!texture.get())
507 return false;
508
509 SkImageInfo info = SkImageInfo::MakeN32Premul(desc.fWidth, desc.fHeight);
510 SkGrPixelRef* pixelRef = SkNEW_ARGS(SkGrPixelRef, (info, texture.get()));
511 if (!pixelRef)
512 return false;
513 bitmap.setInfo(info);
514 bitmap.setPixelRef(pixelRef)->unref();
515 }
516
517 return true;
518 }
519
520 void WebMediaPlayerAndroid::paint(blink::WebCanvas* canvas, 471 void WebMediaPlayerAndroid::paint(blink::WebCanvas* canvas,
521 const blink::WebRect& rect, 472 const blink::WebRect& rect,
522 unsigned char alpha) { 473 unsigned char alpha) {
523 paint(canvas, rect, alpha, SkXfermode::kSrcOver_Mode); 474 paint(canvas, rect, alpha, SkXfermode::kSrcOver_Mode);
524 } 475 }
525 476
526 void WebMediaPlayerAndroid::paint(blink::WebCanvas* canvas, 477 void WebMediaPlayerAndroid::paint(blink::WebCanvas* canvas,
527 const blink::WebRect& rect, 478 const blink::WebRect& rect,
528 unsigned char alpha, 479 unsigned char alpha,
529 SkXfermode::Mode mode) { 480 SkXfermode::Mode mode) {
530 DCHECK(main_thread_checker_.CalledOnValidThread()); 481 DCHECK(main_thread_checker_.CalledOnValidThread());
531 scoped_ptr<blink::WebGraphicsContext3DProvider> provider = 482 // Don't allow clients to copy an encrypted video frame.
532 scoped_ptr<blink::WebGraphicsContext3DProvider>(blink::Platform::current( 483 if (needs_external_surface_)
533 )->createSharedOffscreenGraphicsContext3DProvider());
534 if (!provider)
535 return;
536 blink::WebGraphicsContext3D* context3D = provider->context3d();
537 if (!context3D)
538 return; 484 return;
539 485
540 // Copy video texture into a RGBA texture based bitmap first as video texture 486 scoped_refptr<VideoFrame> video_frame;
541 // on Android is GL_TEXTURE_EXTERNAL_OES which is not supported by Skia yet. 487 {
542 // The bitmap's size needs to be the same as the video and use naturalSize() 488 base::AutoLock auto_lock(current_frame_lock_);
543 // here. Check if we could reuse existing texture based bitmap. 489 video_frame = current_frame_;
544 // Otherwise, release existing texture based bitmap and allocate
545 // a new one based on video size.
546 if (!EnsureTextureBackedSkBitmap(provider->grContext(), bitmap_,
547 naturalSize(), kTopLeft_GrSurfaceOrigin, kSkia8888_GrPixelConfig)) {
548 return;
549 } 490 }
550 491
551 unsigned textureId = static_cast<unsigned>( 492 gfx::Rect gfx_rect(rect);
552 (bitmap_.getTexture())->getTextureHandle()); 493 scoped_refptr<cc::ContextProvider> context_provider =
553 if (!copyVideoTextureToPlatformTexture(context3D, textureId, 0, 494 RenderThreadImpl::current()->SharedMainThreadContextProvider();
554 GL_RGBA, GL_UNSIGNED_BYTE, true, false)) { 495 media::SkCanvasVideoRenderer::Context3DProvider provider{
555 return; 496 context_provider->ContextGL(), context_provider->GrContext()};
556 } 497 skcanvas_video_renderer_.Paint(video_frame.get(),
557 498 canvas,
558 // Draw the texture based bitmap onto the Canvas. If the canvas is 499 gfx_rect,
559 // hardware based, this will do a GPU-GPU texture copy. 500 alpha,
560 // If the canvas is software based, the texture based bitmap will be 501 mode,
561 // readbacked to system memory then draw onto the canvas. 502 media::VIDEO_ROTATION_0,
562 SkRect dest; 503 &provider);
563 dest.set(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height);
564 SkPaint paint;
565 paint.setAlpha(alpha);
566 paint.setXfermodeMode(mode);
567 // It is not necessary to pass the dest into the drawBitmap call since all
568 // the context have been set up before calling paintCurrentFrameInContext.
569 canvas->drawBitmapRect(bitmap_, 0, dest, &paint);
570 } 504 }
571 505
572 bool WebMediaPlayerAndroid::copyVideoTextureToPlatformTexture( 506 bool WebMediaPlayerAndroid::copyVideoTextureToPlatformTexture(
573 blink::WebGraphicsContext3D* web_graphics_context, 507 blink::WebGraphicsContext3D* web_graphics_context,
574 unsigned int texture, 508 unsigned int texture,
575 unsigned int level, 509 unsigned int level,
576 unsigned int internal_format, 510 unsigned int internal_format,
577 unsigned int type, 511 unsigned int type,
578 bool premultiply_alpha, 512 bool premultiply_alpha,
579 bool flip_y) { 513 bool flip_y) {
580 DCHECK(main_thread_checker_.CalledOnValidThread()); 514 DCHECK(main_thread_checker_.CalledOnValidThread());
581 // Don't allow clients to copy an encrypted video frame. 515 // Don't allow clients to copy an encrypted video frame.
582 if (needs_external_surface_) 516 if (needs_external_surface_)
583 return false; 517 return false;
584 518
585 scoped_refptr<VideoFrame> video_frame; 519 scoped_refptr<VideoFrame> video_frame;
586 { 520 {
587 base::AutoLock auto_lock(current_frame_lock_); 521 base::AutoLock auto_lock(current_frame_lock_);
588 video_frame = current_frame_; 522 video_frame = current_frame_;
589 } 523 }
590 524
591 if (!video_frame || 525 if (!video_frame ||
592 video_frame->format() != media::VideoFrame::NATIVE_TEXTURE) 526 video_frame->format() != media::VideoFrame::NATIVE_TEXTURE)
593 return false; 527 return false;
528
529 #if DCHECK_IS_ON
594 const gpu::MailboxHolder* mailbox_holder = video_frame->mailbox_holder(); 530 const gpu::MailboxHolder* mailbox_holder = video_frame->mailbox_holder();
595 DCHECK((!is_remote_ && 531 DCHECK((!is_remote_ &&
596 mailbox_holder->texture_target == GL_TEXTURE_EXTERNAL_OES) || 532 mailbox_holder->texture_target == GL_TEXTURE_EXTERNAL_OES) ||
597 (is_remote_ && mailbox_holder->texture_target == GL_TEXTURE_2D)); 533 (is_remote_ && mailbox_holder->texture_target == GL_TEXTURE_2D));
534 #endif
598 535
599 web_graphics_context->waitSyncPoint(mailbox_holder->sync_point); 536 // For hidden video element (with style "display:none"), ensure the texture
537 // size is set.
538 if (!is_remote_ &&
539 (cached_stream_texture_size_.width != natural_size_.width ||
540 cached_stream_texture_size_.height != natural_size_.height)) {
541 stream_texture_factory_->SetStreamTextureSize(
542 stream_id_, gfx::Size(natural_size_.width, natural_size_.height));
543 cached_stream_texture_size_ = natural_size_;
544 }
600 545
601 // Ensure the target of texture is set before copyTextureCHROMIUM, otherwise 546 // TODO(dshwang): need more elegant way to convert WebGraphicsContext3D to
602 // an invalid texture target may be used for copy texture. 547 // GLES2Interface.
603 uint32 src_texture = web_graphics_context->createAndConsumeTextureCHROMIUM( 548 gpu::gles2::GLES2Interface* gl =
604 mailbox_holder->texture_target, mailbox_holder->mailbox.name); 549 static_cast<webkit::gpu::WebGraphicsContext3DImpl*>(web_graphics_context)
605 550 ->GetGLInterface();
606 // The video is stored in an unmultiplied format, so premultiply if 551 skcanvas_video_renderer_.CopyVideoFrameToTexture(gl,
607 // necessary. 552 video_frame.get(),
608 web_graphics_context->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM, 553 texture,
609 premultiply_alpha); 554 level,
610 555 internal_format,
611 // Application itself needs to take care of setting the right flip_y 556 type,
612 // value down to get the expected result. 557 premultiply_alpha,
613 // flip_y==true means to reverse the video orientation while 558 flip_y);
614 // flip_y==false means to keep the intrinsic orientation.
615 web_graphics_context->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, flip_y);
616 web_graphics_context->copyTextureCHROMIUM(GL_TEXTURE_2D, src_texture,
617 texture, level, internal_format,
618 type);
619 web_graphics_context->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, false);
620 web_graphics_context->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM,
621 false);
622
623 web_graphics_context->deleteTexture(src_texture);
624 web_graphics_context->flush();
625
626 SyncPointClientImpl client(web_graphics_context);
627 video_frame->UpdateReleaseSyncPoint(&client);
628 return true; 559 return true;
629 } 560 }
630 561
631 bool WebMediaPlayerAndroid::hasSingleSecurityOrigin() const { 562 bool WebMediaPlayerAndroid::hasSingleSecurityOrigin() const {
632 DCHECK(main_thread_checker_.CalledOnValidThread()); 563 DCHECK(main_thread_checker_.CalledOnValidThread());
633 if (player_type_ != MEDIA_PLAYER_TYPE_URL) 564 if (player_type_ != MEDIA_PLAYER_TYPE_URL)
634 return true; 565 return true;
635 566
636 if (!info_loader_ || !info_loader_->HasSingleOrigin()) 567 if (!info_loader_ || !info_loader_->HasSingleOrigin())
637 return false; 568 return false;
(...skipping 1109 matching lines...) Expand 10 before | Expand all | Expand 10 after
1747 1678
1748 bool WebMediaPlayerAndroid::IsHLSStream() const { 1679 bool WebMediaPlayerAndroid::IsHLSStream() const {
1749 std::string mime; 1680 std::string mime;
1750 GURL url = redirected_url_.is_empty() ? url_ : redirected_url_; 1681 GURL url = redirected_url_.is_empty() ? url_ : redirected_url_;
1751 if (!net::GetMimeTypeFromFile(base::FilePath(url.path()), &mime)) 1682 if (!net::GetMimeTypeFromFile(base::FilePath(url.path()), &mime))
1752 return false; 1683 return false;
1753 return !mime.compare("application/x-mpegurl"); 1684 return !mime.compare("application/x-mpegurl");
1754 } 1685 }
1755 1686
1756 } // namespace content 1687 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698