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