OLD | NEW |
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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 #include "third_party/WebKit/public/platform/WebString.h" | 50 #include "third_party/WebKit/public/platform/WebString.h" |
51 #include "third_party/WebKit/public/platform/WebURL.h" | 51 #include "third_party/WebKit/public/platform/WebURL.h" |
52 #include "third_party/WebKit/public/web/WebDocument.h" | 52 #include "third_party/WebKit/public/web/WebDocument.h" |
53 #include "third_party/WebKit/public/web/WebFrame.h" | 53 #include "third_party/WebKit/public/web/WebFrame.h" |
54 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" | 54 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" |
55 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" | 55 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" |
56 #include "third_party/WebKit/public/web/WebView.h" | 56 #include "third_party/WebKit/public/web/WebView.h" |
57 #include "third_party/skia/include/core/SkCanvas.h" | 57 #include "third_party/skia/include/core/SkCanvas.h" |
58 #include "third_party/skia/include/core/SkPaint.h" | 58 #include "third_party/skia/include/core/SkPaint.h" |
59 #include "third_party/skia/include/core/SkTypeface.h" | 59 #include "third_party/skia/include/core/SkTypeface.h" |
| 60 #include "third_party/skia/include/gpu/GrContext.h" |
| 61 #include "third_party/skia/include/gpu/SkGrPixelRef.h" |
60 #include "ui/gfx/image/image.h" | 62 #include "ui/gfx/image/image.h" |
61 | 63 |
62 static const uint32 kGLTextureExternalOES = 0x8D65; | 64 static const uint32 kGLTextureExternalOES = 0x8D65; |
63 static const int kSDKVersionToSupportSecurityOriginCheck = 20; | 65 static const int kSDKVersionToSupportSecurityOriginCheck = 20; |
64 | 66 |
65 using blink::WebMediaPlayer; | 67 using blink::WebMediaPlayer; |
66 using blink::WebSize; | 68 using blink::WebSize; |
67 using blink::WebString; | 69 using blink::WebString; |
68 using blink::WebURL; | 70 using blink::WebURL; |
69 using gpu::gles2::GLES2Interface; | 71 using gpu::gles2::GLES2Interface; |
70 using media::MediaPlayerAndroid; | 72 using media::MediaPlayerAndroid; |
71 using media::VideoFrame; | 73 using media::VideoFrame; |
72 | 74 |
73 namespace { | 75 namespace { |
74 // Prefix for histograms related to Encrypted Media Extensions. | 76 // Prefix for histograms related to Encrypted Media Extensions. |
75 const char* kMediaEme = "Media.EME."; | 77 const char* kMediaEme = "Media.EME."; |
76 | 78 |
77 // File-static function is to allow it to run even after WMPA is deleted. | 79 // File-static function is to allow it to run even after WMPA is deleted. |
78 void OnReleaseTexture( | 80 void OnReleaseTexture( |
79 const scoped_refptr<content::StreamTextureFactory>& factories, | 81 const scoped_refptr<content::StreamTextureFactory>& factories, |
80 uint32 texture_id, | 82 uint32 texture_id, |
81 uint32 release_sync_point) { | 83 uint32 release_sync_point) { |
82 GLES2Interface* gl = factories->ContextGL(); | 84 GLES2Interface* gl = factories->ContextGL(); |
83 gl->WaitSyncPointCHROMIUM(release_sync_point); | 85 gl->WaitSyncPointCHROMIUM(release_sync_point); |
84 gl->DeleteTextures(1, &texture_id); | 86 gl->DeleteTextures(1, &texture_id); |
85 } | 87 } |
86 | 88 |
| 89 bool IsSkBitmapProperlySizedTexture(const SkBitmap* bitmap, |
| 90 const gfx::Size& size) { |
| 91 return bitmap->getTexture() && bitmap->width() == size.width() && |
| 92 bitmap->height() == size.height(); |
| 93 } |
| 94 |
| 95 bool AllocateSkBitmapTexture(GrContext* gr, |
| 96 SkBitmap* bitmap, |
| 97 const gfx::Size& size) { |
| 98 DCHECK(gr); |
| 99 GrTextureDesc desc; |
| 100 // Use kRGBA_8888_GrPixelConfig, not kSkia8888_GrPixelConfig, to avoid |
| 101 // RGBA to BGRA conversion. |
| 102 desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 103 // kRenderTarget_GrTextureFlagBit avoids a copy before readback in skia. |
| 104 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; |
| 105 desc.fSampleCnt = 0; |
| 106 desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
| 107 desc.fWidth = size.width(); |
| 108 desc.fHeight = size.height(); |
| 109 skia::RefPtr<GrTexture> texture = skia::AdoptRef( |
| 110 gr->refScratchTexture(desc, GrContext::kExact_ScratchTexMatch)); |
| 111 if (!texture.get()) |
| 112 return false; |
| 113 |
| 114 SkImageInfo info = SkImageInfo::MakeN32Premul(desc.fWidth, desc.fHeight); |
| 115 SkGrPixelRef* pixel_ref = SkNEW_ARGS(SkGrPixelRef, (info, texture.get())); |
| 116 if (!pixel_ref) |
| 117 return false; |
| 118 bitmap->setInfo(info); |
| 119 bitmap->setPixelRef(pixel_ref)->unref(); |
| 120 return true; |
| 121 } |
| 122 |
87 class SyncPointClientImpl : public media::VideoFrame::SyncPointClient { | 123 class SyncPointClientImpl : public media::VideoFrame::SyncPointClient { |
88 public: | 124 public: |
89 explicit SyncPointClientImpl( | 125 explicit SyncPointClientImpl( |
90 blink::WebGraphicsContext3D* web_graphics_context) | 126 blink::WebGraphicsContext3D* web_graphics_context) |
91 : web_graphics_context_(web_graphics_context) {} | 127 : web_graphics_context_(web_graphics_context) {} |
92 virtual ~SyncPointClientImpl() {} | 128 virtual ~SyncPointClientImpl() {} |
93 virtual uint32 InsertSyncPoint() override { | 129 virtual uint32 InsertSyncPoint() override { |
94 return web_graphics_context_->insertSyncPoint(); | 130 return web_graphics_context_->insertSyncPoint(); |
95 } | 131 } |
96 virtual void WaitSyncPoint(uint32 sync_point) override { | 132 virtual void WaitSyncPoint(uint32 sync_point) override { |
(...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
533 const blink::WebTimeRange seekable_range(0.0, duration()); | 569 const blink::WebTimeRange seekable_range(0.0, duration()); |
534 return blink::WebTimeRanges(&seekable_range, 1); | 570 return blink::WebTimeRanges(&seekable_range, 1); |
535 } | 571 } |
536 | 572 |
537 bool WebMediaPlayerAndroid::didLoadingProgress() { | 573 bool WebMediaPlayerAndroid::didLoadingProgress() { |
538 bool ret = did_loading_progress_; | 574 bool ret = did_loading_progress_; |
539 did_loading_progress_ = false; | 575 did_loading_progress_ = false; |
540 return ret; | 576 return ret; |
541 } | 577 } |
542 | 578 |
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, | 579 void WebMediaPlayerAndroid::paint(blink::WebCanvas* canvas, |
577 const blink::WebRect& rect, | 580 const blink::WebRect& rect, |
578 unsigned char alpha, | 581 unsigned char alpha, |
579 SkXfermode::Mode mode) { | 582 SkXfermode::Mode mode) { |
580 DCHECK(main_thread_checker_.CalledOnValidThread()); | 583 DCHECK(main_thread_checker_.CalledOnValidThread()); |
581 scoped_ptr<blink::WebGraphicsContext3DProvider> provider = | 584 scoped_ptr<blink::WebGraphicsContext3DProvider> provider = |
582 scoped_ptr<blink::WebGraphicsContext3DProvider>(blink::Platform::current( | 585 scoped_ptr<blink::WebGraphicsContext3DProvider>(blink::Platform::current( |
583 )->createSharedOffscreenGraphicsContext3DProvider()); | 586 )->createSharedOffscreenGraphicsContext3DProvider()); |
584 if (!provider) | 587 if (!provider) |
585 return; | 588 return; |
586 blink::WebGraphicsContext3D* context3D = provider->context3d(); | 589 blink::WebGraphicsContext3D* context3D = provider->context3d(); |
587 if (!context3D) | 590 if (!context3D) |
588 return; | 591 return; |
589 | 592 |
590 // Copy video texture into a RGBA texture based bitmap first as video texture | 593 // Copy video texture into a RGBA texture based bitmap first as video texture |
591 // on Android is GL_TEXTURE_EXTERNAL_OES which is not supported by Skia yet. | 594 // on Android is GL_TEXTURE_EXTERNAL_OES which is not supported by Skia yet. |
592 // The bitmap's size needs to be the same as the video and use naturalSize() | 595 // The bitmap's size needs to be the same as the video and use naturalSize() |
593 // here. Check if we could reuse existing texture based bitmap. | 596 // here. Check if we could reuse existing texture based bitmap. |
594 // Otherwise, release existing texture based bitmap and allocate | 597 // Otherwise, release existing texture based bitmap and allocate |
595 // a new one based on video size. | 598 // a new one based on video size. |
596 if (!EnsureTextureBackedSkBitmap(provider->grContext(), bitmap_, | 599 if (!IsSkBitmapProperlySizedTexture(&bitmap_, naturalSize())) { |
597 naturalSize(), kTopLeft_GrSurfaceOrigin, kSkia8888_GrPixelConfig)) { | 600 if (!AllocateSkBitmapTexture(provider->grContext(), &bitmap_, |
598 return; | 601 naturalSize())) { |
| 602 return; |
| 603 } |
599 } | 604 } |
600 | 605 |
601 unsigned textureId = static_cast<unsigned>( | 606 unsigned textureId = static_cast<unsigned>( |
602 (bitmap_.getTexture())->getTextureHandle()); | 607 (bitmap_.getTexture())->getTextureHandle()); |
603 if (!copyVideoTextureToPlatformTexture(context3D, textureId, 0, | 608 if (!copyVideoTextureToPlatformTexture(context3D, textureId, 0, |
604 GL_RGBA, GL_UNSIGNED_BYTE, true, false)) { | 609 GL_RGBA, GL_UNSIGNED_BYTE, true, false)) { |
605 return; | 610 return; |
606 } | 611 } |
607 | 612 |
608 // Draw the texture based bitmap onto the Canvas. If the canvas is | 613 // Draw the texture based bitmap onto the Canvas. If the canvas is |
(...skipping 1185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1794 | 1799 |
1795 bool WebMediaPlayerAndroid::IsHLSStream() const { | 1800 bool WebMediaPlayerAndroid::IsHLSStream() const { |
1796 std::string mime; | 1801 std::string mime; |
1797 GURL url = redirected_url_.is_empty() ? url_ : redirected_url_; | 1802 GURL url = redirected_url_.is_empty() ? url_ : redirected_url_; |
1798 if (!net::GetMimeTypeFromFile(base::FilePath(url.path()), &mime)) | 1803 if (!net::GetMimeTypeFromFile(base::FilePath(url.path()), &mime)) |
1799 return false; | 1804 return false; |
1800 return !mime.compare("application/x-mpegurl"); | 1805 return !mime.compare("application/x-mpegurl"); |
1801 } | 1806 } |
1802 | 1807 |
1803 } // namespace content | 1808 } // namespace content |
OLD | NEW |