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