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