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

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

Powered by Google App Engine
This is Rietveld 408576698