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 "platform/graphics/AcceleratedStaticBitmapImage.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" | |
| 11 #include "public/platform/WebGraphicsContext3DProvider.h" | |
| 12 #include "skia/ext/texture_handle.h" | |
| 13 #include "third_party/skia/include/core/SkCanvas.h" | 10 #include "third_party/skia/include/core/SkCanvas.h" |
| 14 #include "third_party/skia/include/core/SkImage.h" | 11 #include "third_party/skia/include/core/SkImage.h" |
| 15 #include "third_party/skia/include/core/SkPaint.h" | 12 #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" | |
| 18 #include "wtf/PtrUtil.h" | 13 #include "wtf/PtrUtil.h" |
| 19 #include <memory> | 14 #include <memory> |
| 20 | 15 |
| 21 namespace blink { | 16 namespace blink { |
| 22 | 17 |
| 23 PassRefPtr<StaticBitmapImage> StaticBitmapImage::create(PassRefPtr<SkImage> imag e) | 18 PassRefPtr<StaticBitmapImage> StaticBitmapImage::create(PassRefPtr<SkImage> imag e) |
| 24 { | 19 { |
| 25 if (!image) | 20 if (!image) |
| 26 return nullptr; | 21 return nullptr; |
| 22 if (image->isTextureBacked()) | |
| 23 return AcceleratedStaticBitmapImage::create(image); | |
|
Justin Novosad
2016/08/02 15:26:00
Excellent!
| |
| 27 return adoptRef(new StaticBitmapImage(image)); | 24 return adoptRef(new StaticBitmapImage(image)); |
| 28 } | 25 } |
| 29 | 26 |
| 30 PassRefPtr<StaticBitmapImage> StaticBitmapImage::create(WebExternalTextureMailbo x& mailbox) | |
| 31 { | |
| 32 return adoptRef(new StaticBitmapImage(mailbox)); | |
| 33 } | |
| 34 | |
| 35 StaticBitmapImage::StaticBitmapImage(PassRefPtr<SkImage> image) : m_image(image) | 27 StaticBitmapImage::StaticBitmapImage(PassRefPtr<SkImage> image) : m_image(image) |
| 36 { | 28 { |
| 37 ASSERT(m_image); | 29 ASSERT(m_image); |
| 38 } | 30 } |
| 39 | 31 |
| 40 StaticBitmapImage::StaticBitmapImage(WebExternalTextureMailbox& mailbox) : m_mai lbox(mailbox) | |
| 41 { | |
| 42 } | |
| 43 | |
| 44 StaticBitmapImage::~StaticBitmapImage() { } | |
| 45 | |
| 46 IntSize StaticBitmapImage::size() const | 32 IntSize StaticBitmapImage::size() const |
| 47 { | 33 { |
| 48 if (!m_image) | |
| 49 return IntSize(m_mailbox.textureSize.width, m_mailbox.textureSize.height ); | |
| 50 return IntSize(m_image->width(), m_image->height()); | 34 return IntSize(m_image->width(), m_image->height()); |
| 51 } | 35 } |
| 52 | 36 |
| 53 bool StaticBitmapImage::isTextureBacked() | 37 bool StaticBitmapImage::isTextureBacked() |
| 54 { | 38 { |
| 55 return m_image && m_image->isTextureBacked(); | 39 return m_image && m_image->isTextureBacked(); |
| 56 } | 40 } |
| 57 | 41 |
| 58 bool StaticBitmapImage::currentFrameKnownToBeOpaque(MetadataMode) | 42 bool StaticBitmapImage::currentFrameKnownToBeOpaque(MetadataMode) |
| 59 { | 43 { |
| 60 return m_image->isOpaque(); | 44 return m_image->isOpaque(); |
| 61 } | 45 } |
| 62 | 46 |
| 63 void StaticBitmapImage::draw(SkCanvas* canvas, const SkPaint& paint, const Float Rect& dstRect, | 47 void StaticBitmapImage::draw(SkCanvas* canvas, const SkPaint& paint, const Float Rect& dstRect, |
| 64 const FloatRect& srcRect, RespectImageOrientationEnum, ImageClampingMode cla mpMode) | 48 const FloatRect& srcRect, RespectImageOrientationEnum, ImageClampingMode cla mpMode) |
| 65 { | 49 { |
| 66 FloatRect adjustedSrcRect = srcRect; | 50 FloatRect adjustedSrcRect = srcRect; |
| 67 adjustedSrcRect.intersect(SkRect::Make(m_image->bounds())); | 51 adjustedSrcRect.intersect(SkRect::Make(m_image->bounds())); |
| 68 | 52 |
| 69 if (dstRect.isEmpty() || adjustedSrcRect.isEmpty()) | 53 if (dstRect.isEmpty() || adjustedSrcRect.isEmpty()) |
| 70 return; // Nothing to draw. | 54 return; // Nothing to draw. |
| 71 | 55 |
| 72 canvas->drawImageRect(m_image.get(), adjustedSrcRect, dstRect, &paint, | 56 canvas->drawImageRect(m_image.get(), adjustedSrcRect, dstRect, &paint, |
| 73 WebCoreClampingModeToSkiaRectConstraint(clampMode)); | 57 WebCoreClampingModeToSkiaRectConstraint(clampMode)); |
| 74 | 58 |
| 75 if (ImageObserver* observer = getImageObserver()) | 59 if (ImageObserver* observer = getImageObserver()) |
| 76 observer->didDraw(this); | 60 observer->didDraw(this); |
| 77 } | 61 } |
| 78 | 62 |
| 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 { | |
| 107 GLuint textureId = switchStorageToSkImageForWebGL(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 // Get a new mailbox because we cannot retain a texture in the WebGL context . | |
| 117 switchStorageToMailbox(provider); | |
| 118 } | |
| 119 | |
| 120 bool StaticBitmapImage::switchStorageToMailbox(WebGraphicsContext3DProvider* pro vider) | |
| 121 { | |
| 122 m_mailbox.textureSize = WebSize(m_image->width(), m_image->height()); | |
| 123 GrContext* grContext = provider->grContext(); | |
| 124 if (!grContext) | |
| 125 return false; | |
| 126 grContext->flush(); | |
| 127 m_mailbox.textureTarget = GL_TEXTURE_2D; | |
| 128 gpu::gles2::GLES2Interface* gl = provider->contextGL(); | |
| 129 if (!gl) | |
| 130 return false; | |
| 131 GLuint textureID = skia::GrBackendObjectToGrGLTextureInfo(m_image->getTextur eHandle(true))->fID; | |
| 132 gl->BindTexture(GL_TEXTURE_2D, textureID); | |
| 133 | |
| 134 gl->GenMailboxCHROMIUM(m_mailbox.name); | |
| 135 gl->ProduceTextureCHROMIUM(GL_TEXTURE_2D, m_mailbox.name); | |
| 136 const GLuint64 fenceSync = gl->InsertFenceSyncCHROMIUM(); | |
| 137 gl->Flush(); | |
| 138 gl->GenSyncTokenCHROMIUM(fenceSync, m_mailbox.syncToken); | |
| 139 m_mailbox.validSyncToken = true; | |
| 140 gl->BindTexture(GL_TEXTURE_2D, 0); | |
| 141 grContext->resetContext(kTextureBinding_GrGLBackendState); | |
| 142 m_image = nullptr; | |
| 143 return true; | |
| 144 } | |
| 145 | |
| 146 // This function is called only in the case that m_image is texture backed. | |
| 147 GLuint StaticBitmapImage::switchStorageToSkImageForWebGL(WebGraphicsContext3DPro vider* contextProvider) | |
| 148 { | |
| 149 DCHECK(!m_image || m_image->isTextureBacked()); | |
| 150 GLuint textureId = 0; | |
| 151 if (m_image) { | |
| 152 // SkImage is texture-backed on the shared context | |
| 153 if (!hasMailbox()) { | |
| 154 std::unique_ptr<WebGraphicsContext3DProvider> sharedProvider = wrapU nique(Platform::current()->createSharedOffscreenGraphicsContext3DProvider()); | |
| 155 if (!switchStorageToMailbox(sharedProvider.get())) | |
| 156 return 0; | |
| 157 textureId = switchStorageToSkImage(contextProvider); | |
| 158 return textureId; | |
| 159 } | |
| 160 } | |
| 161 DCHECK(hasMailbox()); | |
| 162 textureId = switchStorageToSkImage(contextProvider); | |
| 163 return textureId; | |
| 164 } | |
| 165 | |
| 166 PassRefPtr<SkImage> StaticBitmapImage::imageForCurrentFrame() | 63 PassRefPtr<SkImage> StaticBitmapImage::imageForCurrentFrame() |
| 167 { | 64 { |
| 168 if (m_image) | |
| 169 return m_image; | |
| 170 // No mailbox, return null; | |
| 171 if (!hasMailbox()) | |
| 172 return nullptr; | |
| 173 // Has mailbox, consume mailbox, prepare a new mailbox if contextProvider is not null (3D). | |
| 174 DCHECK(isMainThread()); | |
| 175 // TODO(xidachen): make this work on a worker thread. | |
| 176 std::unique_ptr<WebGraphicsContext3DProvider> sharedProvider = wrapUnique(Pl atform::current()->createSharedOffscreenGraphicsContext3DProvider()); | |
| 177 if (!switchStorageToSkImage(sharedProvider.get())) | |
| 178 return nullptr; | |
| 179 return m_image; | 65 return m_image; |
| 180 } | 66 } |
| 181 | 67 |
| 182 } // namespace blink | 68 } // namespace blink |
| OLD | NEW |