blob: ac4fee37ac9f1758182e893c36e76b41be55554b [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 Phillips7294b852017-08-01 13:51:44 +000057GrBackendTextureImageGenerator::Make(sk_sp<GrTexture> texture, sk_sp<GrSemaphore> semaphore,
Brian Osman13dddce2017-05-09 13:19:50 -040058 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(
Robert Phillips7294b852017-08-01 13:51:44 +000085 info, texture.get(), context->uniqueID(), std::move(semaphore), backendTexture));
Brian Osman13dddce2017-05-09 13:19:50 -040086}
87
88GrBackendTextureImageGenerator::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)
Robert Phillips7294b852017-08-01 13:51:44 +000098 , fSurfaceOrigin(texture->origin()) { }
Brian Osman13dddce2017-05-09 13:19:50 -040099
100GrBackendTextureImageGenerator::~GrBackendTextureImageGenerator() {
101 fRefHelper->unref();
102}
103
Brian Osman13dddce2017-05-09 13:19:50 -0400104///////////////////////////////////////////////////////////////////////////////////////////////////
105
106#if SK_SUPPORT_GPU
107void 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
Stan Ilievba81af22017-06-08 15:16:53 -0400117sk_sp<GrTextureProxy> GrBackendTextureImageGenerator::onGenerateTexture(
Christopher Cameron77e96662017-07-08 01:47:47 -0700118 GrContext* context, const SkImageInfo& info, const SkIPoint& origin,
119 SkTransferFunctionBehavior) {
Brian Osman13dddce2017-05-09 13:19:50 -0400120 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.
Robert Phillips7294b852017-08-01 13:51:44 +0000154 tex = context->resourceProvider()->wrapBackendTexture(fBackendTexture, fSurfaceOrigin,
Brian Osman13dddce2017-05-09 13:19:50 -0400155 kBorrow_GrWrapOwnership);
156 if (!tex) {
157 fRefHelper->fBorrowingContextID = SK_InvalidGenID;
158 return nullptr;
159 }
160 fRefHelper->fBorrowedTexture = tex.get();
161
162 tex->setRelease(ReleaseRefHelper_TextureReleaseProc, fRefHelper);
163 fRefHelper->ref();
164 }
165
166 SkASSERT(fRefHelper->fBorrowingContextID == context->uniqueID());
167
Robert Phillips066f0202017-07-25 10:16:35 -0400168 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeWrapped(std::move(tex), fSurfaceOrigin);
Brian Osman13dddce2017-05-09 13:19:50 -0400169
170 if (0 == origin.fX && 0 == origin.fY &&
171 info.width() == fBackendTexture.width() && info.height() == fBackendTexture.height()) {
172 // If the caller wants the entire texture, we're done
173 return proxy;
174 } else {
175 // Otherwise, make a copy of the requested subset. Make sure our temporary is renderable,
176 // because Vulkan will want to do the copy as a draw.
Brian Salomon63e79732017-05-15 21:23:13 -0400177 GrSurfaceDesc desc;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400178 desc.fFlags = kRenderTarget_GrSurfaceFlag;
179 desc.fOrigin = proxy->origin();
Brian Osman13dddce2017-05-09 13:19:50 -0400180 desc.fWidth = info.width();
181 desc.fHeight = info.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -0400182 desc.fConfig = proxy->config();
Brian Salomon63e79732017-05-15 21:23:13 -0400183 desc.fIsMipMapped = proxy->isMipMapped();
Brian Salomon63e79732017-05-15 21:23:13 -0400184
Brian Osman13dddce2017-05-09 13:19:50 -0400185 sk_sp<GrSurfaceContext> sContext(context->contextPriv().makeDeferredSurfaceContext(
186 desc, SkBackingFit::kExact, SkBudgeted::kYes));
187 if (!sContext) {
188 return nullptr;
189 }
190
191 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height());
192 if (!sContext->copy(proxy.get(), subset, SkIPoint::Make(0, 0))) {
193 return nullptr;
194 }
195
196 return sContext->asTextureProxyRef();
197 }
198}
199#endif