blob: 0b6d64e862284245bec3a2db05356e4ffb781342 [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) {
Mike Reedd20b5c42017-06-14 06:03:10 -040036#ifdef SK_VULKAN
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +000037 if (kVulkan_GrBackend == backend) {
Brian Osman13dddce2017-05-09 13:19:50 -040038 GrVkImageInfo* vkInfo = (GrVkImageInfo*)(handle);
39 return GrBackendTexture(width, height, *vkInfo);
40 }
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +000041#endif
42 SkASSERT(kOpenGL_GrBackend == backend);
43 GrGLTextureInfo* glInfo = (GrGLTextureInfo*)(handle);
44 return GrBackendTexture(width, height, config, *glInfo);
Brian Osman13dddce2017-05-09 13:19:50 -040045}
46
47std::unique_ptr<SkImageGenerator>
48GrBackendTextureImageGenerator::Make(sk_sp<GrTexture> texture, sk_sp<GrSemaphore> semaphore,
49 SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace) {
50 if (colorSpace && (!colorSpace->gammaCloseToSRGB() && !colorSpace->gammaIsLinear())) {
51 return nullptr;
52 }
53
54 SkColorType colorType = kUnknown_SkColorType;
55 if (!GrPixelConfigToColorType(texture->config(), &colorType)) {
56 return nullptr;
57 }
58
59 GrContext* context = texture->getContext();
60
61 // Attach our texture to this context's resource cache. This ensures that deletion will happen
62 // in the correct thread/context. This adds the only ref to the texture that will persist from
63 // this point. That ref will be released when the generator's RefHelper is freed.
64 context->getResourceCache()->insertCrossContextGpuResource(texture.get());
65
66 GrBackend backend = context->contextPriv().getBackend();
67 GrBackendTexture backendTexture = make_backend_texture_from_handle(backend,
68 texture->width(),
69 texture->height(),
70 texture->config(),
71 texture->getTextureHandle());
72
73 SkImageInfo info = SkImageInfo::Make(texture->width(), texture->height(), colorType, alphaType,
74 std::move(colorSpace));
75 return std::unique_ptr<SkImageGenerator>(new GrBackendTextureImageGenerator(
76 info, texture.get(), context->uniqueID(), std::move(semaphore), backendTexture));
77}
78
79GrBackendTextureImageGenerator::GrBackendTextureImageGenerator(const SkImageInfo& info,
80 GrTexture* texture,
81 uint32_t owningContextID,
82 sk_sp<GrSemaphore> semaphore,
83 const GrBackendTexture& backendTex)
84 : INHERITED(info)
85 , fRefHelper(new RefHelper(texture, owningContextID))
86 , fSemaphore(std::move(semaphore))
87 , fLastBorrowingContextID(SK_InvalidGenID)
88 , fBackendTexture(backendTex)
89 , fSurfaceOrigin(texture->origin()) { }
90
91GrBackendTextureImageGenerator::~GrBackendTextureImageGenerator() {
92 fRefHelper->unref();
93}
94
Brian Osman13dddce2017-05-09 13:19:50 -040095///////////////////////////////////////////////////////////////////////////////////////////////////
96
97#if SK_SUPPORT_GPU
98void GrBackendTextureImageGenerator::ReleaseRefHelper_TextureReleaseProc(void* ctx) {
99 RefHelper* refHelper = static_cast<RefHelper*>(ctx);
100 SkASSERT(refHelper);
101
102 // Release texture so another context can use it
103 refHelper->fBorrowedTexture = nullptr;
104 refHelper->fBorrowingContextID = SK_InvalidGenID;
105 refHelper->unref();
106}
107
Stan Ilievba81af22017-06-08 15:16:53 -0400108sk_sp<GrTextureProxy> GrBackendTextureImageGenerator::onGenerateTexture(
109 GrContext* context, const SkImageInfo& info, const SkIPoint& origin) {
Brian Osman13dddce2017-05-09 13:19:50 -0400110 SkASSERT(context);
111
112 if (context->contextPriv().getBackend() != fBackendTexture.backend()) {
113 return nullptr;
114 }
115
116 sk_sp<GrTexture> tex;
117
118 if (fRefHelper->fBorrowingContextID == context->uniqueID()) {
119 // If a client re-draws the same image multiple times, the texture we return will be cached
120 // and re-used. If they draw a subset, though, we may be re-called. In that case, we want
121 // to re-use the borrowed texture we've previously created.
122 tex = sk_ref_sp(fRefHelper->fBorrowedTexture);
123 SkASSERT(tex);
124 } else {
125 // The texture is available or borrwed by another context. Try for exclusive access.
126 uint32_t expectedID = SK_InvalidGenID;
127 if (!fRefHelper->fBorrowingContextID.compare_exchange(&expectedID, context->uniqueID())) {
128 // Some other context is currently borrowing the texture. We aren't allowed to use it.
129 return nullptr;
130 } else {
131 // Wait on a semaphore when a new context has just started borrowing the texture. This
132 // is conservative, but shouldn't be too expensive.
133 if (fSemaphore && fLastBorrowingContextID != context->uniqueID()) {
134 context->getGpu()->waitSemaphore(fSemaphore);
135 fLastBorrowingContextID = context->uniqueID();
136 }
137 }
138
139 // We just gained access to the texture. If we're on the original context, we could use the
140 // original texture, but we'd have no way of detecting that it's no longer in-use. So we
141 // always make a wrapped copy, where the release proc informs us that the context is done
142 // with it. This is unfortunate - we'll have two texture objects referencing the same GPU
143 // object. However, no client can ever see the original texture, so this should be safe.
144 tex = context->resourceProvider()->wrapBackendTexture(fBackendTexture, fSurfaceOrigin,
145 kNone_GrBackendTextureFlag, 0,
146 kBorrow_GrWrapOwnership);
147 if (!tex) {
148 fRefHelper->fBorrowingContextID = SK_InvalidGenID;
149 return nullptr;
150 }
151 fRefHelper->fBorrowedTexture = tex.get();
152
153 tex->setRelease(ReleaseRefHelper_TextureReleaseProc, fRefHelper);
154 fRefHelper->ref();
155 }
156
157 SkASSERT(fRefHelper->fBorrowingContextID == context->uniqueID());
158
159 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeWrapped(std::move(tex));
160
161 if (0 == origin.fX && 0 == origin.fY &&
162 info.width() == fBackendTexture.width() && info.height() == fBackendTexture.height()) {
163 // If the caller wants the entire texture, we're done
164 return proxy;
165 } else {
166 // Otherwise, make a copy of the requested subset. Make sure our temporary is renderable,
167 // because Vulkan will want to do the copy as a draw.
Brian Salomon63e79732017-05-15 21:23:13 -0400168 GrSurfaceDesc desc;
169 desc.fConfig = proxy->config();
Brian Osman13dddce2017-05-09 13:19:50 -0400170 desc.fWidth = info.width();
171 desc.fHeight = info.height();
Brian Salomon63e79732017-05-15 21:23:13 -0400172 desc.fOrigin = proxy->origin();
173 desc.fIsMipMapped = proxy->isMipMapped();
Brian Osman13dddce2017-05-09 13:19:50 -0400174 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Brian Salomon63e79732017-05-15 21:23:13 -0400175
Brian Osman13dddce2017-05-09 13:19:50 -0400176 sk_sp<GrSurfaceContext> sContext(context->contextPriv().makeDeferredSurfaceContext(
177 desc, SkBackingFit::kExact, SkBudgeted::kYes));
178 if (!sContext) {
179 return nullptr;
180 }
181
182 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height());
183 if (!sContext->copy(proxy.get(), subset, SkIPoint::Make(0, 0))) {
184 return nullptr;
185 }
186
187 return sContext->asTextureProxyRef();
188 }
189}
190#endif