| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "platform/graphics/StaticBitmapImage.h" | 5 #include "platform/graphics/AcceleratedStaticBitmapImage.h" |
| 6 | 6 |
| 7 #include "gpu/command_buffer/client/gles2_interface.h" | 7 #include "gpu/command_buffer/client/gles2_interface.h" |
| 8 #include "platform/graphics/GraphicsContext.h" | 8 #include "platform/graphics/StaticBitmapImage.h" |
| 9 #include "platform/graphics/ImageObserver.h" | 9 #include "platform/graphics/skia/SkiaUtils.h" |
| 10 #include "public/platform/Platform.h" | 10 #include "public/platform/Platform.h" |
| 11 #include "public/platform/WebGraphicsContext3DProvider.h" | 11 #include "public/platform/WebGraphicsContext3DProvider.h" |
| 12 #include "skia/ext/texture_handle.h" | 12 #include "skia/ext/texture_handle.h" |
| 13 #include "third_party/skia/include/core/SkCanvas.h" | |
| 14 #include "third_party/skia/include/core/SkImage.h" | 13 #include "third_party/skia/include/core/SkImage.h" |
| 15 #include "third_party/skia/include/core/SkPaint.h" | |
| 16 #include "third_party/skia/include/core/SkShader.h" | |
| 17 #include "third_party/skia/include/gpu/GrContext.h" | 14 #include "third_party/skia/include/gpu/GrContext.h" |
| 18 #include "wtf/PtrUtil.h" | 15 #include "wtf/PtrUtil.h" |
| 19 #include <memory> | 16 #include <memory> |
| 20 | 17 |
| 21 namespace blink { | 18 namespace blink { |
| 22 | 19 |
| 23 PassRefPtr<StaticBitmapImage> StaticBitmapImage::create(PassRefPtr<SkImage> imag
e) | 20 PassRefPtr<AcceleratedStaticBitmapImage> AcceleratedStaticBitmapImage::create(We
bExternalTextureMailbox& mailbox) |
| 24 { | 21 { |
| 25 if (!image) | 22 return adoptRef(new AcceleratedStaticBitmapImage(mailbox)); |
| 26 return nullptr; | |
| 27 return adoptRef(new StaticBitmapImage(image)); | |
| 28 } | 23 } |
| 29 | 24 |
| 30 PassRefPtr<StaticBitmapImage> StaticBitmapImage::create(WebExternalTextureMailbo
x& mailbox) | 25 PassRefPtr<AcceleratedStaticBitmapImage> AcceleratedStaticBitmapImage::create(Pa
ssRefPtr<SkImage> image) |
| 31 { | 26 { |
| 32 return adoptRef(new StaticBitmapImage(mailbox)); | 27 return adoptRef(new AcceleratedStaticBitmapImage(image)); |
| 33 } | 28 } |
| 34 | 29 |
| 35 StaticBitmapImage::StaticBitmapImage(PassRefPtr<SkImage> image) : m_image(image) | 30 AcceleratedStaticBitmapImage::AcceleratedStaticBitmapImage(WebExternalTextureMai
lbox& mailbox) |
| 36 { | 31 : StaticBitmapImage() |
| 37 ASSERT(m_image); | 32 , m_mailbox(mailbox) |
| 38 } | |
| 39 | |
| 40 StaticBitmapImage::StaticBitmapImage(WebExternalTextureMailbox& mailbox) : m_mai
lbox(mailbox) | |
| 41 { | 33 { |
| 42 } | 34 } |
| 43 | 35 |
| 44 StaticBitmapImage::~StaticBitmapImage() { } | 36 AcceleratedStaticBitmapImage::AcceleratedStaticBitmapImage(PassRefPtr<SkImage> i
mage) |
| 45 | 37 : StaticBitmapImage(image) |
| 46 IntSize StaticBitmapImage::size() const | |
| 47 { | 38 { |
| 48 if (!m_image) | |
| 49 return IntSize(m_mailbox.textureSize.width, m_mailbox.textureSize.height
); | |
| 50 return IntSize(m_image->width(), m_image->height()); | |
| 51 } | 39 } |
| 52 | 40 |
| 53 bool StaticBitmapImage::isTextureBacked() | 41 IntSize AcceleratedStaticBitmapImage::size() const |
| 54 { | 42 { |
| 55 return m_image && m_image->isTextureBacked(); | 43 if (m_image) |
| 44 return IntSize(m_image->width(), m_image->height()); |
| 45 return IntSize(m_mailbox.textureSize.width, m_mailbox.textureSize.height); |
| 56 } | 46 } |
| 57 | 47 |
| 58 bool StaticBitmapImage::currentFrameKnownToBeOpaque(MetadataMode) | 48 void AcceleratedStaticBitmapImage::copyToTexture(WebGraphicsContext3DProvider* p
rovider, GLuint destinationTexture, GLenum internalFormat, GLenum destType, bool
flipY) |
| 59 { | |
| 60 return m_image->isOpaque(); | |
| 61 } | |
| 62 | |
| 63 void StaticBitmapImage::draw(SkCanvas* canvas, const SkPaint& paint, const Float
Rect& dstRect, | |
| 64 const FloatRect& srcRect, RespectImageOrientationEnum, ImageClampingMode cla
mpMode) | |
| 65 { | |
| 66 FloatRect adjustedSrcRect = srcRect; | |
| 67 adjustedSrcRect.intersect(SkRect::Make(m_image->bounds())); | |
| 68 | |
| 69 if (dstRect.isEmpty() || adjustedSrcRect.isEmpty()) | |
| 70 return; // Nothing to draw. | |
| 71 | |
| 72 canvas->drawImageRect(m_image.get(), adjustedSrcRect, dstRect, &paint, | |
| 73 WebCoreClampingModeToSkiaRectConstraint(clampMode)); | |
| 74 | |
| 75 if (ImageObserver* observer = getImageObserver()) | |
| 76 observer->didDraw(this); | |
| 77 } | |
| 78 | |
| 79 GLuint StaticBitmapImage::switchStorageToSkImage(WebGraphicsContext3DProvider* p
rovider) | |
| 80 { | |
| 81 if (!provider) | |
| 82 return 0; | |
| 83 GrContext* grContext = provider->grContext(); | |
| 84 if (!grContext) | |
| 85 return 0; | |
| 86 gpu::gles2::GLES2Interface* gl = provider->contextGL(); | |
| 87 if (!gl) | |
| 88 return 0; | |
| 89 gl->WaitSyncTokenCHROMIUM(m_mailbox.syncToken); | |
| 90 GLuint textureId = gl->CreateAndConsumeTextureCHROMIUM(GL_TEXTURE_2D, m_mail
box.name); | |
| 91 GrGLTextureInfo textureInfo; | |
| 92 textureInfo.fTarget = GL_TEXTURE_2D; | |
| 93 textureInfo.fID = textureId; | |
| 94 GrBackendTextureDesc backendTexture; | |
| 95 backendTexture.fOrigin = kBottomLeft_GrSurfaceOrigin; | |
| 96 backendTexture.fWidth = m_mailbox.textureSize.width; | |
| 97 backendTexture.fHeight = m_mailbox.textureSize.height; | |
| 98 backendTexture.fConfig = kSkia8888_GrPixelConfig; | |
| 99 backendTexture.fTextureHandle = skia::GrGLTextureInfoToGrBackendObject(textu
reInfo); | |
| 100 sk_sp<SkImage> skImage = SkImage::MakeFromAdoptedTexture(grContext, backendT
exture); | |
| 101 m_image = fromSkSp(skImage); | |
| 102 return textureId; | |
| 103 } | |
| 104 | |
| 105 void StaticBitmapImage::copyToTexture(WebGraphicsContext3DProvider* provider, GL
uint destinationTexture, GLenum internalFormat, GLenum destType, bool flipY) | |
| 106 { | 49 { |
| 107 GLuint textureId = switchStorageToSkImageForWebGL(provider); | 50 GLuint textureId = switchStorageToSkImageForWebGL(provider); |
| 108 gpu::gles2::GLES2Interface* gl = provider->contextGL(); | 51 gpu::gles2::GLES2Interface* gl = provider->contextGL(); |
| 109 if (!gl) | 52 if (!gl) |
| 110 return; | 53 return; |
| 111 gl->CopyTextureCHROMIUM(textureId, destinationTexture, internalFormat, destT
ype, flipY, false, false); | 54 gl->CopyTextureCHROMIUM(textureId, destinationTexture, internalFormat, destT
ype, flipY, false, false); |
| 112 const GLuint64 fenceSync = gl->InsertFenceSyncCHROMIUM(); | 55 const GLuint64 fenceSync = gl->InsertFenceSyncCHROMIUM(); |
| 113 gl->Flush(); | 56 gl->Flush(); |
| 114 GLbyte syncToken[24]; | 57 GLbyte syncToken[24]; |
| 115 gl->GenSyncTokenCHROMIUM(fenceSync, syncToken); | 58 gl->GenSyncTokenCHROMIUM(fenceSync, syncToken); |
| 116 // Get a new mailbox because we cannot retain a texture in the WebGL context
. | 59 // Get a new mailbox because we cannot retain a texture in the WebGL context
. |
| 117 switchStorageToMailbox(provider); | 60 switchStorageToMailbox(provider); |
| 118 } | 61 } |
| 119 | 62 |
| 120 bool StaticBitmapImage::switchStorageToMailbox(WebGraphicsContext3DProvider* pro
vider) | 63 bool AcceleratedStaticBitmapImage::switchStorageToMailbox(WebGraphicsContext3DPr
ovider* provider) |
| 121 { | 64 { |
| 122 m_mailbox.textureSize = WebSize(m_image->width(), m_image->height()); | 65 m_mailbox.textureSize = WebSize(m_image->width(), m_image->height()); |
| 123 GrContext* grContext = provider->grContext(); | 66 GrContext* grContext = provider->grContext(); |
| 124 if (!grContext) | 67 if (!grContext) |
| 125 return false; | 68 return false; |
| 126 grContext->flush(); | 69 grContext->flush(); |
| 127 m_mailbox.textureTarget = GL_TEXTURE_2D; | 70 m_mailbox.textureTarget = GL_TEXTURE_2D; |
| 128 gpu::gles2::GLES2Interface* gl = provider->contextGL(); | 71 gpu::gles2::GLES2Interface* gl = provider->contextGL(); |
| 129 if (!gl) | 72 if (!gl) |
| 130 return false; | 73 return false; |
| 131 GLuint textureID = skia::GrBackendObjectToGrGLTextureInfo(m_image->getTextur
eHandle(true))->fID; | 74 GLuint textureID = skia::GrBackendObjectToGrGLTextureInfo(m_image->getTextur
eHandle(true))->fID; |
| 132 gl->BindTexture(GL_TEXTURE_2D, textureID); | 75 gl->BindTexture(GL_TEXTURE_2D, textureID); |
| 133 | 76 |
| 134 gl->GenMailboxCHROMIUM(m_mailbox.name); | 77 gl->GenMailboxCHROMIUM(m_mailbox.name); |
| 135 gl->ProduceTextureCHROMIUM(GL_TEXTURE_2D, m_mailbox.name); | 78 gl->ProduceTextureCHROMIUM(GL_TEXTURE_2D, m_mailbox.name); |
| 136 const GLuint64 fenceSync = gl->InsertFenceSyncCHROMIUM(); | 79 const GLuint64 fenceSync = gl->InsertFenceSyncCHROMIUM(); |
| 137 gl->Flush(); | 80 gl->Flush(); |
| 138 gl->GenSyncTokenCHROMIUM(fenceSync, m_mailbox.syncToken); | 81 gl->GenSyncTokenCHROMIUM(fenceSync, m_mailbox.syncToken); |
| 139 m_mailbox.validSyncToken = true; | 82 m_mailbox.validSyncToken = true; |
| 140 gl->BindTexture(GL_TEXTURE_2D, 0); | 83 gl->BindTexture(GL_TEXTURE_2D, 0); |
| 141 grContext->resetContext(kTextureBinding_GrGLBackendState); | 84 grContext->resetContext(kTextureBinding_GrGLBackendState); |
| 142 m_image = nullptr; | 85 m_image = nullptr; |
| 143 return true; | 86 return true; |
| 144 } | 87 } |
| 145 | 88 |
| 146 // This function is called only in the case that m_image is texture backed. | 89 // This function is called only in the case that m_image is texture backed. |
| 147 GLuint StaticBitmapImage::switchStorageToSkImageForWebGL(WebGraphicsContext3DPro
vider* contextProvider) | 90 GLuint AcceleratedStaticBitmapImage::switchStorageToSkImageForWebGL(WebGraphicsC
ontext3DProvider* contextProvider) |
| 148 { | 91 { |
| 149 DCHECK(!m_image || m_image->isTextureBacked()); | 92 DCHECK(!m_image || m_image->isTextureBacked()); |
| 150 GLuint textureId = 0; | 93 GLuint textureId = 0; |
| 151 if (m_image) { | 94 if (m_image) { |
| 152 // SkImage is texture-backed on the shared context | 95 // SkImage is texture-backed on the shared context |
| 153 if (!hasMailbox()) { | 96 if (!hasMailbox()) { |
| 154 std::unique_ptr<WebGraphicsContext3DProvider> sharedProvider = wrapU
nique(Platform::current()->createSharedOffscreenGraphicsContext3DProvider()); | 97 std::unique_ptr<WebGraphicsContext3DProvider> sharedProvider = wrapU
nique(Platform::current()->createSharedOffscreenGraphicsContext3DProvider()); |
| 155 if (!switchStorageToMailbox(sharedProvider.get())) | 98 if (!switchStorageToMailbox(sharedProvider.get())) |
| 156 return 0; | 99 return 0; |
| 157 textureId = switchStorageToSkImage(contextProvider); | 100 textureId = switchStorageToSkImage(contextProvider); |
| 158 return textureId; | 101 return textureId; |
| 159 } | 102 } |
| 160 } | 103 } |
| 161 DCHECK(hasMailbox()); | 104 DCHECK(hasMailbox()); |
| 162 textureId = switchStorageToSkImage(contextProvider); | 105 textureId = switchStorageToSkImage(contextProvider); |
| 163 return textureId; | 106 return textureId; |
| 164 } | 107 } |
| 165 | 108 |
| 166 PassRefPtr<SkImage> StaticBitmapImage::imageForCurrentFrame() | 109 GLuint AcceleratedStaticBitmapImage::switchStorageToSkImage(WebGraphicsContext3D
Provider* provider) |
| 110 { |
| 111 if (!provider) |
| 112 return 0; |
| 113 GrContext* grContext = provider->grContext(); |
| 114 if (!grContext) |
| 115 return 0; |
| 116 gpu::gles2::GLES2Interface* gl = provider->contextGL(); |
| 117 if (!gl) |
| 118 return 0; |
| 119 gl->WaitSyncTokenCHROMIUM(m_mailbox.syncToken); |
| 120 GLuint textureId = gl->CreateAndConsumeTextureCHROMIUM(GL_TEXTURE_2D, m_mail
box.name); |
| 121 GrGLTextureInfo textureInfo; |
| 122 textureInfo.fTarget = GL_TEXTURE_2D; |
| 123 textureInfo.fID = textureId; |
| 124 GrBackendTextureDesc backendTexture; |
| 125 backendTexture.fOrigin = kBottomLeft_GrSurfaceOrigin; |
| 126 backendTexture.fWidth = m_mailbox.textureSize.width; |
| 127 backendTexture.fHeight = m_mailbox.textureSize.height; |
| 128 backendTexture.fConfig = kSkia8888_GrPixelConfig; |
| 129 backendTexture.fTextureHandle = skia::GrGLTextureInfoToGrBackendObject(textu
reInfo); |
| 130 sk_sp<SkImage> skImage = SkImage::MakeFromAdoptedTexture(grContext, backendT
exture); |
| 131 m_image = fromSkSp(skImage); |
| 132 return textureId; |
| 133 } |
| 134 |
| 135 PassRefPtr<SkImage> AcceleratedStaticBitmapImage::imageForCurrentFrame() |
| 167 { | 136 { |
| 168 if (m_image) | 137 if (m_image) |
| 169 return m_image; | 138 return m_image; |
| 170 // No mailbox, return null; | |
| 171 if (!hasMailbox()) | 139 if (!hasMailbox()) |
| 172 return nullptr; | 140 return nullptr; |
| 173 // Has mailbox, consume mailbox, prepare a new mailbox if contextProvider is
not null (3D). | 141 // Has mailbox, consume mailbox, prepare a new mailbox if contextProvider is
not null (3D). |
| 174 DCHECK(isMainThread()); | 142 DCHECK(isMainThread()); |
| 175 // TODO(xidachen): make this work on a worker thread. | 143 // TODO(xidachen): make this work on a worker thread. |
| 176 std::unique_ptr<WebGraphicsContext3DProvider> sharedProvider = wrapUnique(Pl
atform::current()->createSharedOffscreenGraphicsContext3DProvider()); | 144 std::unique_ptr<WebGraphicsContext3DProvider> sharedProvider = wrapUnique(Pl
atform::current()->createSharedOffscreenGraphicsContext3DProvider()); |
| 177 if (!switchStorageToSkImage(sharedProvider.get())) | 145 if (!switchStorageToSkImage(sharedProvider.get())) |
| 178 return nullptr; | 146 return nullptr; |
| 179 return m_image; | 147 return m_image; |
| 180 } | 148 } |
| 181 | 149 |
| 182 } // namespace blink | 150 } // namespace blink |
| OLD | NEW |