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