Chromium Code Reviews| 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 textureId, GLuint destinationTexture, GLenum internalFormat, GLenum destTyp e, bool flipY) | |
| 106 { | |
| 107 gpu::gles2::GLES2Interface* gl = provider->contextGL(); | |
| 108 if (!gl) | |
| 109 return; | |
| 110 gl->CopyTextureCHROMIUM(textureId, destinationTexture, internalFormat, destT ype, flipY, false, false); | |
| 111 const GLuint64 fenceSync = gl->InsertFenceSyncCHROMIUM(); | |
| 112 gl->Flush(); | |
| 113 GLbyte syncToken[24]; | |
| 114 gl->GenSyncTokenCHROMIUM(fenceSync, syncToken); | |
| 115 } | |
| 116 | |
| 117 bool StaticBitmapImage::switchStorageToMailbox(WebGraphicsContext3DProvider* pro vider) | |
| 118 { | |
| 119 m_mailbox.textureSize = WebSize(m_image->width(), m_image->height()); | |
| 120 GrContext* grContext = provider->grContext(); | |
| 121 if (!grContext) | |
| 122 return false; | |
| 123 grContext->flush(); | |
| 124 m_mailbox.textureTarget = GL_TEXTURE_2D; | |
| 125 gpu::gles2::GLES2Interface* gl = provider->contextGL(); | |
| 126 if (!gl) | |
| 127 return false; | |
| 128 GLuint textureID = skia::GrBackendObjectToGrGLTextureInfo(m_image->getTextur eHandle(true))->fID; | |
| 129 gl->BindTexture(GL_TEXTURE_2D, textureID); | |
| 130 | |
| 131 gl->GenMailboxCHROMIUM(m_mailbox.name); | |
| 132 gl->ProduceTextureCHROMIUM(GL_TEXTURE_2D, m_mailbox.name); | |
| 133 const GLuint64 fenceSync = gl->InsertFenceSyncCHROMIUM(); | |
| 134 gl->Flush(); | |
| 135 gl->GenSyncTokenCHROMIUM(fenceSync, m_mailbox.syncToken); | |
| 136 m_mailbox.validSyncToken = true; | |
| 137 gl->BindTexture(GL_TEXTURE_2D, 0); | |
| 138 grContext->resetContext(kTextureBinding_GrGLBackendState); | |
| 139 return true; | |
| 140 } | |
| 141 | |
| 142 // This function is called only in the case that m_image is texture backed. | |
| 143 GLuint StaticBitmapImage::textureIdForWebGL(WebGraphicsContext3DProvider* contex tProvider) | |
| 144 { | |
| 145 DCHECK(!m_image || m_image->isTextureBacked()); | |
| 146 GLuint textureId = 0; | |
| 147 if (m_image) { | |
| 148 // SkImage is texture-backed on the shared context | |
| 149 if (!hasMailbox()) { | |
| 150 std::unique_ptr<WebGraphicsContext3DProvider> sharedProvider = wrapU nique(Platform::current()->createSharedOffscreenGraphicsContext3DProvider()); | |
| 151 if (!switchStorageToMailbox(sharedProvider.get())) | |
| 152 return 0; | |
| 153 textureId = switchStorageToSkImage(contextProvider); | |
| 154 // Get a new mailbox because we cannot retain a texture in the WebGL context. | |
| 155 if (!switchStorageToMailbox(contextProvider)) | |
| 156 return 0; | |
| 157 return textureId; | |
| 158 } | |
| 159 } | |
| 160 DCHECK(hasMailbox()); | |
| 161 textureId = switchStorageToSkImage(contextProvider); | |
| 162 if (!switchStorageToMailbox(contextProvider)) | |
| 163 return 0; | |
| 164 return textureId; | |
| 165 } | |
| 166 | |
| 167 PassRefPtr<SkImage> StaticBitmapImage::imageForCurrentFrame() | |
| 168 { | |
| 169 if (m_image) | |
| 170 return m_image; | |
| 171 // No mailbox, return null; | |
| 172 if (!hasMailbox()) | |
| 173 return nullptr; | |
| 174 // Has mailbox, consume mailbox, prepare a new mailbox if contextProvider is not null (3D). | |
| 175 DCHECK(isMainThread()); | |
| 176 // TODO(xidachen): make this work on a worker thread. | |
| 177 std::unique_ptr<WebGraphicsContext3DProvider> sharedProvider = wrapUnique(Pl atform::current()->createSharedOffscreenGraphicsContext3DProvider()); | |
| 178 switchStorageToSkImage(sharedProvider.get()); | |
|
Ken Russell (switch to Gerrit)
2016/06/24 01:25:10
What happens if switchStorageToSkImage returns 0?
xidachen
2016/06/24 14:36:07
Change it to return nullptr in that case.
| |
| 101 return m_image; | 179 return m_image; |
| 102 } | 180 } |
| 103 | 181 |
| 104 } // namespace blink | 182 } // namespace blink |
| OLD | NEW |