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 | 18 |
| 18 namespace blink { | 19 namespace blink { |
| 19 | 20 |
| 20 PassRefPtr<StaticBitmapImage> StaticBitmapImage::create(PassRefPtr<SkImage> imag e) | 21 PassRefPtr<StaticBitmapImage> StaticBitmapImage::create(PassRefPtr<SkImage> imag e) |
| 21 { | 22 { |
| 22 if (!image) | 23 if (!image) |
| 23 return nullptr; | 24 return nullptr; |
| 24 return adoptRef(new StaticBitmapImage(image)); | 25 return adoptRef(new StaticBitmapImage(image)); |
| 25 } | 26 } |
| 26 | 27 |
| 27 PassRefPtr<StaticBitmapImage> StaticBitmapImage::create(WebExternalTextureMailbo x& mailbox) | 28 PassRefPtr<StaticBitmapImage> StaticBitmapImage::create(WebExternalTextureMailbo x& mailbox) |
| 28 { | 29 { |
| 29 return adoptRef(new StaticBitmapImage(mailbox)); | 30 return adoptRef(new StaticBitmapImage(mailbox)); |
| 30 } | 31 } |
| 31 | 32 |
| 32 StaticBitmapImage::StaticBitmapImage(PassRefPtr<SkImage> image) : m_image(image) | 33 StaticBitmapImage::StaticBitmapImage(PassRefPtr<SkImage> image) : m_image(image) |
| 33 { | 34 { |
| 34 ASSERT(m_image); | 35 ASSERT(m_image); |
| 35 } | 36 } |
| 36 | 37 |
| 37 StaticBitmapImage::StaticBitmapImage(WebExternalTextureMailbox& mailbox) : m_mai lbox(mailbox) | 38 StaticBitmapImage::StaticBitmapImage(WebExternalTextureMailbox& mailbox) : m_mai lbox(mailbox) |
| 38 { | 39 { |
| 39 } | 40 } |
| 40 | 41 |
| 41 StaticBitmapImage::~StaticBitmapImage() { } | 42 StaticBitmapImage::~StaticBitmapImage() { } |
| 42 | 43 |
| 43 IntSize StaticBitmapImage::size() const | 44 IntSize StaticBitmapImage::size() const |
| 44 { | 45 { |
| 46 if (!m_image) | |
| 47 return IntSize(m_mailbox.textureSize.width, m_mailbox.textureSize.height ); | |
| 45 return IntSize(m_image->width(), m_image->height()); | 48 return IntSize(m_image->width(), m_image->height()); |
| 46 } | 49 } |
| 47 | 50 |
| 48 bool StaticBitmapImage::currentFrameKnownToBeOpaque(MetadataMode) | 51 bool StaticBitmapImage::currentFrameKnownToBeOpaque(MetadataMode) |
| 49 { | 52 { |
| 50 return m_image->isOpaque(); | 53 return m_image->isOpaque(); |
| 51 } | 54 } |
| 52 | 55 |
| 53 void StaticBitmapImage::draw(SkCanvas* canvas, const SkPaint& paint, const Float Rect& dstRect, | 56 void StaticBitmapImage::draw(SkCanvas* canvas, const SkPaint& paint, const Float Rect& dstRect, |
| 54 const FloatRect& srcRect, RespectImageOrientationEnum, ImageClampingMode cla mpMode) | 57 const FloatRect& srcRect, RespectImageOrientationEnum, ImageClampingMode cla mpMode) |
| 55 { | 58 { |
| 56 FloatRect adjustedSrcRect = srcRect; | 59 FloatRect adjustedSrcRect = srcRect; |
| 57 adjustedSrcRect.intersect(SkRect::Make(m_image->bounds())); | 60 adjustedSrcRect.intersect(SkRect::Make(m_image->bounds())); |
| 58 | 61 |
| 59 if (dstRect.isEmpty() || adjustedSrcRect.isEmpty()) | 62 if (dstRect.isEmpty() || adjustedSrcRect.isEmpty()) |
| 60 return; // Nothing to draw. | 63 return; // Nothing to draw. |
| 61 | 64 |
| 62 canvas->drawImageRect(m_image.get(), adjustedSrcRect, dstRect, &paint, | 65 canvas->drawImageRect(m_image.get(), adjustedSrcRect, dstRect, &paint, |
| 63 WebCoreClampingModeToSkiaRectConstraint(clampMode)); | 66 WebCoreClampingModeToSkiaRectConstraint(clampMode)); |
| 64 | 67 |
| 65 if (ImageObserver* observer = getImageObserver()) | 68 if (ImageObserver* observer = getImageObserver()) |
| 66 observer->didDraw(this); | 69 observer->didDraw(this); |
| 67 } | 70 } |
| 68 | 71 |
| 69 PassRefPtr<SkImage> StaticBitmapImage::imageForCurrentFrame() | 72 void StaticBitmapImage::consumeTextureMailbox(WebGraphicsContext3DProvider* prov ider) |
| 70 { | 73 { |
| 71 if (m_image) | |
| 72 return m_image; | |
| 73 DCHECK(isMainThread()); | |
| 74 // In the place when we consume an ImageBitmap that is gpu texture backed, | |
| 75 // create a new SkImage from that texture. | |
| 76 // TODO(xidachen): make this work on a worker thread. | |
| 77 OwnPtr<WebGraphicsContext3DProvider> provider = adoptPtr(Platform::current() ->createSharedOffscreenGraphicsContext3DProvider()); | |
| 78 if (!provider) | 74 if (!provider) |
| 79 return nullptr; | 75 return; |
| 80 GrContext* grContext = provider->grContext(); | 76 GrContext* grContext = provider->grContext(); |
| 81 if (!grContext) | 77 if (!grContext) |
| 82 return nullptr; | 78 return; |
| 83 gpu::gles2::GLES2Interface* gl = provider->contextGL(); | 79 gpu::gles2::GLES2Interface* gl = provider->contextGL(); |
| 84 if (!gl) | 80 if (!gl) |
| 85 return nullptr; | 81 return; |
| 86 gl->WaitSyncTokenCHROMIUM(m_mailbox.syncToken); | 82 gl->WaitSyncTokenCHROMIUM(m_mailbox.syncToken); |
| 87 GLuint textureId = gl->CreateAndConsumeTextureCHROMIUM(GL_TEXTURE_2D, m_mail box.name); | 83 m_textureId = gl->CreateAndConsumeTextureCHROMIUM(GL_TEXTURE_2D, m_mailbox.n ame); |
|
Ken Russell (switch to Gerrit)
2016/06/01 22:55:09
Is it legal to cache this value in StaticBitmapIma
xidachen
2016/06/02 13:52:52
I re-organized the structure of the imageForCurren
| |
| 88 GrGLTextureInfo textureInfo; | 84 GrGLTextureInfo textureInfo; |
| 89 textureInfo.fTarget = GL_TEXTURE_2D; | 85 textureInfo.fTarget = GL_TEXTURE_2D; |
| 90 textureInfo.fID = textureId; | 86 textureInfo.fID = m_textureId; |
| 91 GrBackendTextureDesc backendTexture; | 87 GrBackendTextureDesc backendTexture; |
| 92 backendTexture.fOrigin = kBottomLeft_GrSurfaceOrigin; | 88 backendTexture.fOrigin = kBottomLeft_GrSurfaceOrigin; |
| 93 backendTexture.fWidth = m_mailbox.textureSize.width; | 89 backendTexture.fWidth = m_mailbox.textureSize.width; |
| 94 backendTexture.fHeight = m_mailbox.textureSize.height; | 90 backendTexture.fHeight = m_mailbox.textureSize.height; |
| 95 backendTexture.fConfig = kSkia8888_GrPixelConfig; | 91 backendTexture.fConfig = kSkia8888_GrPixelConfig; |
| 96 backendTexture.fTextureHandle = skia::GrGLTextureInfoToGrBackendObject(textu reInfo); | 92 backendTexture.fTextureHandle = skia::GrGLTextureInfoToGrBackendObject(textu reInfo); |
| 97 sk_sp<SkImage> skImage = SkImage::MakeFromAdoptedTexture(grContext, backendT exture); | 93 sk_sp<SkImage> skImage = SkImage::MakeFromAdoptedTexture(grContext, backendT exture); |
| 98 m_image = fromSkSp(skImage); | 94 m_image = fromSkSp(skImage); |
| 95 } | |
| 96 | |
| 97 void StaticBitmapImage::copyTexture(WebGraphicsContext3DProvider* provider, GLui nt texture, GLenum internalFormat, GLenum destType) | |
| 98 { | |
| 99 gpu::gles2::GLES2Interface* gl = provider->contextGL(); | |
| 100 if (!gl) | |
| 101 return; | |
| 102 gl->CopyTextureCHROMIUM(m_textureId, texture, internalFormat, destType, fals e, false, false); | |
| 103 const GLuint64 fenceSync = gl->InsertFenceSyncCHROMIUM(); | |
| 104 gl->Flush(); | |
| 105 GLbyte syncToken[24]; | |
| 106 gl->GenSyncTokenCHROMIUM(fenceSync, syncToken); | |
| 107 } | |
| 108 | |
| 109 bool StaticBitmapImage::prepareMailboxForSkImage(WebGraphicsContext3DProvider* p rovider) | |
| 110 { | |
| 111 m_mailbox.textureSize = WebSize(m_image->width(), m_image->height()); | |
| 112 GrContext* grContext = m_provider->grContext(); | |
| 113 if (!grContext) | |
| 114 return false; | |
| 115 grContext->flush(); | |
| 116 m_image->getTexture()->textureParamsModified(); | |
| 117 m_mailbox.textureTarget = GL_TEXTURE_2D; | |
| 118 gpu::gles2::GLES2Interface* gl = m_provider->contextGL(); | |
| 119 if (!gl) | |
| 120 return false; | |
| 121 GLuint textureID = skia::GrBackendObjectToGrGLTextureInfo(m_image->getTextur eHandle(true))->fID; | |
| 122 gl->BindTexture(GL_TEXTURE_2D, textureID); | |
| 123 | |
| 124 // Re-use the texture's existing mailbox, if there is one. | |
| 125 if (m_image->getTexture()->getCustomData()) { | |
| 126 DCHECK(m_image->getTexture()->getCustomData()->size() == sizeof(m_mailbo x.name)); | |
| 127 memcpy(&m_mailbox.name[0], m_image->getTexture()->getCustomData()->data( ), sizeof(m_mailbox.name)); | |
| 128 } else { | |
| 129 gl->GenMailboxCHROMIUM(m_mailbox.name); | |
| 130 RefPtr<SkData> mailboxNameData = adoptRef(SkData::NewWithCopy(&m_mailbox .name[0], sizeof(m_mailbox.name))); | |
| 131 m_image->getTexture()->setCustomData(mailboxNameData.get()); | |
| 132 gl->ProduceTextureCHROMIUM(GL_TEXTURE_2D, m_mailbox.name); | |
| 133 } | |
| 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 // There are four cases depending on whether m_image == nullptr && contextProvid er == nullptr. | |
| 144 PassRefPtr<SkImage> StaticBitmapImage::imageForCurrentFrame(WebGraphicsContext3D Provider* contextProvider) | |
| 145 { | |
| 146 if (m_image && (!m_image->isTextureBacked() || !contextProvider || !m_provid er)) | |
| 147 return m_image; | |
| 148 if (!m_image) { | |
| 149 if (!contextProvider) { | |
| 150 DCHECK(isMainThread()); | |
| 151 // TODO(xidachen): make this work on a worker thread. | |
| 152 OwnPtr<WebGraphicsContext3DProvider> provider = adoptPtr(Platform::c urrent()->createSharedOffscreenGraphicsContext3DProvider()); | |
| 153 consumeTextureMailbox(provider.get()); | |
| 154 m_provider = std::move(provider); | |
| 155 } else { | |
| 156 consumeTextureMailbox(contextProvider); | |
| 157 } | |
| 158 return m_image; | |
| 159 } | |
| 160 if (!prepareMailboxForSkImage(contextProvider)) | |
| 161 return m_image; | |
| 162 consumeTextureMailbox(contextProvider); | |
| 99 return m_image; | 163 return m_image; |
| 100 } | 164 } |
| 101 | 165 |
| 102 } // namespace blink | 166 } // namespace blink |
| OLD | NEW |