1 /*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "GrBackendTextureImageGenerator.h"
9
10 #include "GrContext.h"
11 #include "GrContextPriv.h"
12 #include "GrGpu.h"
13 #include "GrResourceCache.h"
14 #include "GrResourceProvider.h"
15 #include "GrSemaphore.h"
16 #include "GrTexture.h"
17
18 #include "SkGr.h"
19 #include "SkMessageBus.h"
20
~RefHelper()21 GrBackendTextureImageGenerator::RefHelper::~RefHelper() {
22 SkASSERT(nullptr == fBorrowedTexture);
23 SkASSERT(SK_InvalidGenID == fBorrowingContextID);
24
25 // Generator has been freed, and no one is borrowing the texture. Notify the original cache
26 // that it can free the last ref, so it happens on the correct thread.
27 GrGpuResourceFreedMessage msg { fOriginalTexture, fOwningContextID };
28 SkMessageBus<GrGpuResourceFreedMessage>::Post(msg);
29 }
30
31 // TODO: I copied this from SkImage_Gpu, perhaps we put a version of this somewhere else?
make_backend_texture_from_handle(GrBackend backend,int width,int height,GrPixelConfig config,GrBackendObject handle)32 static GrBackendTexture make_backend_texture_from_handle(GrBackend backend,
33 int width, int height,
34 GrPixelConfig config,
35 GrBackendObject handle) {
36 switch (backend) {
37 case kOpenGL_GrBackend: {
38 const GrGLTextureInfo* glInfo = (const GrGLTextureInfo*)(handle);
39 return GrBackendTexture(width, height, config, *glInfo);
40 }
41 #ifdef SK_VULKAN
42 case kVulkan_GrBackend: {
43 const GrVkImageInfo* vkInfo = (const GrVkImageInfo*)(handle);
44 return GrBackendTexture(width, height, *vkInfo);
45 }
46 #endif
47 case kMock_GrBackend: {
48 const GrMockTextureInfo* mockInfo = (const GrMockTextureInfo*)(handle);
49 return GrBackendTexture(width, height, config, *mockInfo);
50 }
51 default:
52 return GrBackendTexture();
53 }
54 }
55
56 std::unique_ptr<SkImageGenerator>
Make(sk_sp<GrTexture> texture,sk_sp<GrSemaphore> semaphore,SkAlphaType alphaType,sk_sp<SkColorSpace> colorSpace)57 GrBackendTextureImageGenerator::Make(sk_sp<GrTexture> texture, sk_sp<GrSemaphore> semaphore,
58 SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace) {
59 if (colorSpace && (!colorSpace->gammaCloseToSRGB() && !colorSpace->gammaIsLinear())) {
60 return nullptr;
61 }
62
63 SkColorType colorType = kUnknown_SkColorType;
64 if (!GrPixelConfigToColorType(texture->config(), &colorType)) {
65 return nullptr;
66 }
67
68 GrContext* context = texture->getContext();
69
70 // Attach our texture to this context's resource cache. This ensures that deletion will happen
71 // in the correct thread/context. This adds the only ref to the texture that will persist from
72 // this point. That ref will be released when the generator's RefHelper is freed.
73 context->getResourceCache()->insertCrossContextGpuResource(texture.get());
74
75 GrBackend backend = context->contextPriv().getBackend();
76 GrBackendTexture backendTexture = make_backend_texture_from_handle(backend,
77 texture->width(),
78 texture->height(),
79 texture->config(),
80 texture->getTextureHandle());
81
82 SkImageInfo info = SkImageInfo::Make(texture->width(), texture->height(), colorType, alphaType,
83 std::move(colorSpace));
84 return std::unique_ptr<SkImageGenerator>(new GrBackendTextureImageGenerator(
85 info, texture.get(), context->uniqueID(), std::move(semaphore), backendTexture));
86 }
87
GrBackendTextureImageGenerator(const SkImageInfo & info,GrTexture * texture,uint32_t owningContextID,sk_sp<GrSemaphore> semaphore,const GrBackendTexture & backendTex)88 GrBackendTextureImageGenerator::GrBackendTextureImageGenerator(const SkImageInfo& info,
89 GrTexture* texture,
90 uint32_t owningContextID,
91 sk_sp<GrSemaphore> semaphore,
92 const GrBackendTexture& backendTex)
93 : INHERITED(info)
94 , fRefHelper(new RefHelper(texture, owningContextID))
95 , fSemaphore(std::move(semaphore))
96 , fLastBorrowingContextID(SK_InvalidGenID)
97 , fBackendTexture(backendTex)
98 , fSurfaceOrigin(texture->origin()) { }
99
~GrBackendTextureImageGenerator()100 GrBackendTextureImageGenerator::~GrBackendTextureImageGenerator() {
101 fRefHelper->unref();
102 }
103
104 ///////////////////////////////////////////////////////////////////////////////////////////////////
105
106 #if SK_SUPPORT_GPU
ReleaseRefHelper_TextureReleaseProc(void * ctx)107 void GrBackendTextureImageGenerator::ReleaseRefHelper_TextureReleaseProc(void* ctx) {
108 RefHelper* refHelper = static_cast<RefHelper*>(ctx);
109 SkASSERT(refHelper);
110
111 // Release texture so another context can use it
112 refHelper->fBorrowedTexture = nullptr;
113 refHelper->fBorrowingContextID = SK_InvalidGenID;
114 refHelper->unref();
115 }
116
onGenerateTexture(GrContext * context,const SkImageInfo & info,const SkIPoint & origin,SkTransferFunctionBehavior)117 sk_sp<GrTextureProxy> GrBackendTextureImageGenerator::onGenerateTexture(
118 GrContext* context, const SkImageInfo& info, const SkIPoint& origin,
119 SkTransferFunctionBehavior) {
120 SkASSERT(context);
121
122 if (context->contextPriv().getBackend() != fBackendTexture.backend()) {
123 return nullptr;
124 }
125
126 sk_sp<GrTexture> tex;
127
128 if (fRefHelper->fBorrowingContextID == context->uniqueID()) {
129 // If a client re-draws the same image multiple times, the texture we return will be cached
130 // and re-used. If they draw a subset, though, we may be re-called. In that case, we want
131 // to re-use the borrowed texture we've previously created.
132 tex = sk_ref_sp(fRefHelper->fBorrowedTexture);
133 SkASSERT(tex);
134 } else {
135 // The texture is available or borrwed by another context. Try for exclusive access.
136 uint32_t expectedID = SK_InvalidGenID;
137 if (!fRefHelper->fBorrowingContextID.compare_exchange(&expectedID, context->uniqueID())) {
138 // Some other context is currently borrowing the texture. We aren't allowed to use it.
139 return nullptr;
140 } else {
141 // Wait on a semaphore when a new context has just started borrowing the texture. This
142 // is conservative, but shouldn't be too expensive.
143 if (fSemaphore && fLastBorrowingContextID != context->uniqueID()) {
144 context->getGpu()->waitSemaphore(fSemaphore);
145 fLastBorrowingContextID = context->uniqueID();
146 }
147 }
148
149 // We just gained access to the texture. If we're on the original context, we could use the
150 // original texture, but we'd have no way of detecting that it's no longer in-use. So we
151 // always make a wrapped copy, where the release proc informs us that the context is done
152 // with it. This is unfortunate - we'll have two texture objects referencing the same GPU
153 // object. However, no client can ever see the original texture, so this should be safe.
154 tex = context->resourceProvider()->wrapBackendTexture(fBackendTexture, fSurfaceOrigin,
155 kNone_GrBackendTextureFlag, 0,
156 kBorrow_GrWrapOwnership);
157 if (!tex) {
158 fRefHelper->fBorrowingContextID = SK_InvalidGenID;
159 return nullptr;
160 }
161 fRefHelper->fBorrowedTexture = tex.get();
162
163 tex->setRelease(ReleaseRefHelper_TextureReleaseProc, fRefHelper);
164 fRefHelper->ref();
165 }
166
167 SkASSERT(fRefHelper->fBorrowingContextID == context->uniqueID());
168
169 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeWrapped(std::move(tex));
170
171 if (0 == origin.fX && 0 == origin.fY &&
172 info.width() == fBackendTexture.width() && info.height() == fBackendTexture.height()) {
173 // If the caller wants the entire texture, we're done
174 return proxy;
175 } else {
176 // Otherwise, make a copy of the requested subset. Make sure our temporary is renderable,
177 // because Vulkan will want to do the copy as a draw.
178 GrSurfaceDesc desc;
179 desc.fConfig = proxy->config();
180 desc.fWidth = info.width();
181 desc.fHeight = info.height();
182 desc.fOrigin = proxy->origin();
183 desc.fIsMipMapped = proxy->isMipMapped();
184 desc.fFlags = kRenderTarget_GrSurfaceFlag;
185
186 sk_sp<GrSurfaceContext> sContext(context->contextPriv().makeDeferredSurfaceContext(
187 desc, SkBackingFit::kExact, SkBudgeted::kYes));
188 if (!sContext) {
189 return nullptr;
190 }
191
192 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height());
193 if (!sContext->copy(proxy.get(), subset, SkIPoint::Make(0, 0))) {
194 return nullptr;
195 }
196
197 return sContext->asTextureProxyRef();
198 }
199 }
200 #endif
201