blob: 93596cf6bd6865587fd3ffc1dc95059abf6b2673 [file] [log] [blame]
Brian Osman13dddce2017-05-09 13:19:50 -04001/*
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"
Robert Phillips646e4292017-06-13 12:44:56 -040012#include "GrGpu.h"
Brian Osman13dddce2017-05-09 13:19:50 -040013#include "GrResourceCache.h"
14#include "GrResourceProvider.h"
15#include "GrSemaphore.h"
Robert Phillips646e4292017-06-13 12:44:56 -040016#include "GrTexture.h"
Brian Osman13dddce2017-05-09 13:19:50 -040017
18#include "SkGr.h"
19#include "SkMessageBus.h"
20
21GrBackendTextureImageGenerator::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?
32static GrBackendTexture make_backend_texture_from_handle(GrBackend backend,
33 int width, int height,
34 GrPixelConfig config,
35 GrBackendObject handle) {
Brian Salomon8fe24272017-07-07 12:56:11 -040036 switch (backend) {
37 case kOpenGL_GrBackend: {
38 const GrGLTextureInfo* glInfo = (const GrGLTextureInfo*)(handle);
39 return GrBackendTexture(width, height, config, *glInfo);
40 }
Mike Reedd20b5c42017-06-14 06:03:10 -040041#ifdef SK_VULKAN
Brian Salomon8fe24272017-07-07 12:56:11 -040042 case kVulkan_GrBackend: {
43 const GrVkImageInfo* vkInfo = (const GrVkImageInfo*)(handle);
44 return GrBackendTexture(width, height, *vkInfo);
45 }
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +000046#endif
Brian Salomon8fe24272017-07-07 12:56:11 -040047 case kMock_GrBackend: {
48 const GrMockTextureInfo* mockInfo = (const GrMockTextureInfo*)(handle);
49 return GrBackendTexture(width, height, config, *mockInfo);
50 }
51 default:
52 return GrBackendTexture();
53 }
Brian Osman13dddce2017-05-09 13:19:50 -040054}
55
56std::unique_ptr<SkImageGenerator>
Robert Phillipsb0e93a22017-08-29 08:26:54 -040057GrBackendTextureImageGenerator::Make(sk_sp<GrTexture> texture, GrSurfaceOrigin origin,
58 sk_sp<GrSemaphore> semaphore,
Brian Osman13dddce2017-05-09 13:19:50 -040059 SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace) {
60 if (colorSpace && (!colorSpace->gammaCloseToSRGB() && !colorSpace->gammaIsLinear())) {
61 return nullptr;
62 }
63
64 SkColorType colorType = kUnknown_SkColorType;
65 if (!GrPixelConfigToColorType(texture->config(), &colorType)) {
66 return nullptr;
67 }
68
69 GrContext* context = texture->getContext();
70
71 // Attach our texture to this context's resource cache. This ensures that deletion will happen
72 // in the correct thread/context. This adds the only ref to the texture that will persist from
73 // this point. That ref will be released when the generator's RefHelper is freed.
74 context->getResourceCache()->insertCrossContextGpuResource(texture.get());
75
76 GrBackend backend = context->contextPriv().getBackend();
77 GrBackendTexture backendTexture = make_backend_texture_from_handle(backend,
78 texture->width(),
79 texture->height(),
80 texture->config(),
81 texture->getTextureHandle());
82
83 SkImageInfo info = SkImageInfo::Make(texture->width(), texture->height(), colorType, alphaType,
84 std::move(colorSpace));
85 return std::unique_ptr<SkImageGenerator>(new GrBackendTextureImageGenerator(
Robert Phillipsb0e93a22017-08-29 08:26:54 -040086 info, texture.get(), origin, context->uniqueID(), std::move(semaphore), backendTexture));
Brian Osman13dddce2017-05-09 13:19:50 -040087}
88
89GrBackendTextureImageGenerator::GrBackendTextureImageGenerator(const SkImageInfo& info,
90 GrTexture* texture,
Robert Phillipsb0e93a22017-08-29 08:26:54 -040091 GrSurfaceOrigin origin,
Brian Osman13dddce2017-05-09 13:19:50 -040092 uint32_t owningContextID,
93 sk_sp<GrSemaphore> semaphore,
94 const GrBackendTexture& backendTex)
95 : INHERITED(info)
96 , fRefHelper(new RefHelper(texture, owningContextID))
97 , fSemaphore(std::move(semaphore))
98 , fLastBorrowingContextID(SK_InvalidGenID)
99 , fBackendTexture(backendTex)
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400100 , fSurfaceOrigin(origin) { }
Brian Osman13dddce2017-05-09 13:19:50 -0400101
102GrBackendTextureImageGenerator::~GrBackendTextureImageGenerator() {
103 fRefHelper->unref();
104}
105
Brian Osman13dddce2017-05-09 13:19:50 -0400106///////////////////////////////////////////////////////////////////////////////////////////////////
107
108#if SK_SUPPORT_GPU
109void GrBackendTextureImageGenerator::ReleaseRefHelper_TextureReleaseProc(void* ctx) {
110 RefHelper* refHelper = static_cast<RefHelper*>(ctx);
111 SkASSERT(refHelper);
112
113 // Release texture so another context can use it
114 refHelper->fBorrowedTexture = nullptr;
115 refHelper->fBorrowingContextID = SK_InvalidGenID;
116 refHelper->unref();
117}
118
Stan Ilievba81af22017-06-08 15:16:53 -0400119sk_sp<GrTextureProxy> GrBackendTextureImageGenerator::onGenerateTexture(
Christopher Cameron77e96662017-07-08 01:47:47 -0700120 GrContext* context, const SkImageInfo& info, const SkIPoint& origin,
Greg Danielf88c12e2017-10-09 09:57:35 -0400121 SkTransferFunctionBehavior, bool willNeedMipMaps) {
Brian Osman13dddce2017-05-09 13:19:50 -0400122 SkASSERT(context);
123
124 if (context->contextPriv().getBackend() != fBackendTexture.backend()) {
125 return nullptr;
126 }
127
128 sk_sp<GrTexture> tex;
129
130 if (fRefHelper->fBorrowingContextID == context->uniqueID()) {
131 // If a client re-draws the same image multiple times, the texture we return will be cached
132 // and re-used. If they draw a subset, though, we may be re-called. In that case, we want
133 // to re-use the borrowed texture we've previously created.
134 tex = sk_ref_sp(fRefHelper->fBorrowedTexture);
135 SkASSERT(tex);
136 } else {
137 // The texture is available or borrwed by another context. Try for exclusive access.
138 uint32_t expectedID = SK_InvalidGenID;
139 if (!fRefHelper->fBorrowingContextID.compare_exchange(&expectedID, context->uniqueID())) {
140 // Some other context is currently borrowing the texture. We aren't allowed to use it.
141 return nullptr;
142 } else {
143 // Wait on a semaphore when a new context has just started borrowing the texture. This
144 // is conservative, but shouldn't be too expensive.
145 if (fSemaphore && fLastBorrowingContextID != context->uniqueID()) {
146 context->getGpu()->waitSemaphore(fSemaphore);
147 fLastBorrowingContextID = context->uniqueID();
148 }
149 }
150
151 // We just gained access to the texture. If we're on the original context, we could use the
152 // original texture, but we'd have no way of detecting that it's no longer in-use. So we
153 // always make a wrapped copy, where the release proc informs us that the context is done
154 // with it. This is unfortunate - we'll have two texture objects referencing the same GPU
155 // object. However, no client can ever see the original texture, so this should be safe.
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400156 tex = context->resourceProvider()->wrapBackendTexture(fBackendTexture,
Brian Osman13dddce2017-05-09 13:19:50 -0400157 kBorrow_GrWrapOwnership);
158 if (!tex) {
159 fRefHelper->fBorrowingContextID = SK_InvalidGenID;
160 return nullptr;
161 }
162 fRefHelper->fBorrowedTexture = tex.get();
163
164 tex->setRelease(ReleaseRefHelper_TextureReleaseProc, fRefHelper);
165 fRefHelper->ref();
166 }
167
168 SkASSERT(fRefHelper->fBorrowingContextID == context->uniqueID());
169
Robert Phillips066f0202017-07-25 10:16:35 -0400170 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeWrapped(std::move(tex), fSurfaceOrigin);
Brian Osman13dddce2017-05-09 13:19:50 -0400171
172 if (0 == origin.fX && 0 == origin.fY &&
173 info.width() == fBackendTexture.width() && info.height() == fBackendTexture.height()) {
174 // If the caller wants the entire texture, we're done
175 return proxy;
176 } else {
177 // Otherwise, make a copy of the requested subset. Make sure our temporary is renderable,
178 // because Vulkan will want to do the copy as a draw.
Brian Salomon63e79732017-05-15 21:23:13 -0400179 GrSurfaceDesc desc;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400180 desc.fFlags = kRenderTarget_GrSurfaceFlag;
181 desc.fOrigin = proxy->origin();
Brian Osman13dddce2017-05-09 13:19:50 -0400182 desc.fWidth = info.width();
183 desc.fHeight = info.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -0400184 desc.fConfig = proxy->config();
Greg Daniel90f28ec2017-09-25 12:26:58 -0400185 // TODO: We should support the case where we can allocate the mips ahead of time then copy
186 // the subregion into the base layer and then let the GPU generate the rest of the mip
187 // levels.
188 SkASSERT(!proxy->isMipMapped());
Brian Salomon63e79732017-05-15 21:23:13 -0400189
Brian Osman13dddce2017-05-09 13:19:50 -0400190 sk_sp<GrSurfaceContext> sContext(context->contextPriv().makeDeferredSurfaceContext(
191 desc, SkBackingFit::kExact, SkBudgeted::kYes));
192 if (!sContext) {
193 return nullptr;
194 }
195
196 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height());
197 if (!sContext->copy(proxy.get(), subset, SkIPoint::Make(0, 0))) {
198 return nullptr;
199 }
200
201 return sContext->asTextureProxyRef();
202 }
203}
204#endif