OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "platform/graphics/SkiaTextureHolder.h" |
| 6 |
| 7 #include "gpu/command_buffer/client/gles2_interface.h" |
| 8 #include "platform/CrossThreadFunctional.h" |
| 9 #include "platform/graphics/MailboxTextureHolder.h" |
| 10 #include "platform/graphics/gpu/SharedGpuContext.h" |
| 11 #include "public/platform/Platform.h" |
| 12 #include "skia/ext/texture_handle.h" |
| 13 #include "third_party/skia/include/gpu/GrContext.h" |
| 14 |
| 15 namespace blink { |
| 16 |
| 17 SkiaTextureHolder::SkiaTextureHolder(sk_sp<SkImage> image) |
| 18 : m_image(std::move(image)), |
| 19 m_sharedContextId(SharedGpuContext::contextId()) {} |
| 20 |
| 21 SkiaTextureHolder::SkiaTextureHolder( |
| 22 std::unique_ptr<TextureHolder> textureHolder) { |
| 23 DCHECK(textureHolder->isMailboxTextureHolder()); |
| 24 const gpu::Mailbox mailbox = textureHolder->mailbox(); |
| 25 const gpu::SyncToken syncToken = textureHolder->syncToken(); |
| 26 const IntSize mailboxSize = textureHolder->size(); |
| 27 |
| 28 gpu::gles2::GLES2Interface* sharedGL = SharedGpuContext::gl(); |
| 29 GrContext* sharedGrContext = SharedGpuContext::gr(); |
| 30 DCHECK(sharedGL && |
| 31 sharedGrContext); // context isValid already checked in callers |
| 32 |
| 33 sharedGL->WaitSyncTokenCHROMIUM(syncToken.GetConstData()); |
| 34 GLuint sharedContextTextureId = |
| 35 sharedGL->CreateAndConsumeTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name); |
| 36 GrGLTextureInfo textureInfo; |
| 37 textureInfo.fTarget = GL_TEXTURE_2D; |
| 38 textureInfo.fID = sharedContextTextureId; |
| 39 GrBackendTextureDesc backendTexture; |
| 40 backendTexture.fOrigin = kBottomLeft_GrSurfaceOrigin; |
| 41 backendTexture.fWidth = mailboxSize.width(); |
| 42 backendTexture.fHeight = mailboxSize.height(); |
| 43 backendTexture.fConfig = kSkia8888_GrPixelConfig; |
| 44 backendTexture.fTextureHandle = |
| 45 skia::GrGLTextureInfoToGrBackendObject(textureInfo); |
| 46 |
| 47 sk_sp<SkImage> newImage = |
| 48 SkImage::MakeFromAdoptedTexture(sharedGrContext, backendTexture); |
| 49 releaseImageThreadSafe(); |
| 50 m_image = newImage; |
| 51 m_sharedContextId = SharedGpuContext::contextId(); |
| 52 } |
| 53 |
| 54 SkiaTextureHolder::~SkiaTextureHolder() { |
| 55 releaseImageThreadSafe(); |
| 56 } |
| 57 |
| 58 unsigned SkiaTextureHolder::sharedContextId() { |
| 59 return m_sharedContextId; |
| 60 } |
| 61 |
| 62 static void releaseImage(sk_sp<SkImage>&& image, |
| 63 std::unique_ptr<gpu::SyncToken>&& syncToken) { |
| 64 if (SharedGpuContext::isValid() && syncToken->HasData()) |
| 65 SharedGpuContext::gl()->WaitSyncTokenCHROMIUM(syncToken->GetData()); |
| 66 image.reset(); |
| 67 } |
| 68 |
| 69 void SkiaTextureHolder::releaseImageThreadSafe() { |
| 70 // If m_image belongs to a GrContext that is on another thread, it |
| 71 // must be released on that thread. |
| 72 if (m_textureThreadTaskRunner && m_image && |
| 73 m_textureThreadDoNotCall != Platform::current()->currentThread() && |
| 74 SharedGpuContext::isValid()) { |
| 75 gpu::gles2::GLES2Interface* sharedGL = SharedGpuContext::gl(); |
| 76 std::unique_ptr<gpu::SyncToken> releaseSyncToken(new gpu::SyncToken); |
| 77 const GLuint64 fenceSync = sharedGL->InsertFenceSyncCHROMIUM(); |
| 78 sharedGL->Flush(); |
| 79 sharedGL->GenSyncTokenCHROMIUM(fenceSync, releaseSyncToken->GetData()); |
| 80 m_textureThreadTaskRunner->postTask( |
| 81 BLINK_FROM_HERE, |
| 82 crossThreadBind(&releaseImage, passed(std::move(m_image)), |
| 83 passed(std::move(releaseSyncToken)))); |
| 84 } |
| 85 m_image = nullptr; |
| 86 m_textureThreadDoNotCall = nullptr; |
| 87 m_textureThreadTaskRunner = nullptr; |
| 88 } |
| 89 |
| 90 } // namespace blink |
OLD | NEW |