blob: 58dacf65496c3f551ae80c80b8e03929efbc585f [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"
Greg Daniel261b8aa2017-10-23 09:37:36 -040013#include "GrRenderTargetContext.h"
Brian Osman13dddce2017-05-09 13:19:50 -040014#include "GrResourceCache.h"
15#include "GrResourceProvider.h"
16#include "GrSemaphore.h"
Robert Phillips646e4292017-06-13 12:44:56 -040017#include "GrTexture.h"
Greg Daniel261b8aa2017-10-23 09:37:36 -040018#include "GrTexturePriv.h"
Brian Osman13dddce2017-05-09 13:19:50 -040019
20#include "SkGr.h"
21#include "SkMessageBus.h"
22
23GrBackendTextureImageGenerator::RefHelper::~RefHelper() {
24 SkASSERT(nullptr == fBorrowedTexture);
25 SkASSERT(SK_InvalidGenID == fBorrowingContextID);
26
27 // Generator has been freed, and no one is borrowing the texture. Notify the original cache
28 // that it can free the last ref, so it happens on the correct thread.
29 GrGpuResourceFreedMessage msg { fOriginalTexture, fOwningContextID };
30 SkMessageBus<GrGpuResourceFreedMessage>::Post(msg);
31}
32
33// TODO: I copied this from SkImage_Gpu, perhaps we put a version of this somewhere else?
34static GrBackendTexture make_backend_texture_from_handle(GrBackend backend,
35 int width, int height,
36 GrPixelConfig config,
Greg Daniel261b8aa2017-10-23 09:37:36 -040037 GrMipMapped mipMapped,
Brian Osman13dddce2017-05-09 13:19:50 -040038 GrBackendObject handle) {
Brian Salomon8fe24272017-07-07 12:56:11 -040039 switch (backend) {
40 case kOpenGL_GrBackend: {
41 const GrGLTextureInfo* glInfo = (const GrGLTextureInfo*)(handle);
Greg Daniel261b8aa2017-10-23 09:37:36 -040042 return GrBackendTexture(width, height, config, mipMapped, *glInfo);
Brian Salomon8fe24272017-07-07 12:56:11 -040043 }
Mike Reedd20b5c42017-06-14 06:03:10 -040044#ifdef SK_VULKAN
Brian Salomon8fe24272017-07-07 12:56:11 -040045 case kVulkan_GrBackend: {
46 const GrVkImageInfo* vkInfo = (const GrVkImageInfo*)(handle);
47 return GrBackendTexture(width, height, *vkInfo);
48 }
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +000049#endif
Brian Salomon8fe24272017-07-07 12:56:11 -040050 case kMock_GrBackend: {
51 const GrMockTextureInfo* mockInfo = (const GrMockTextureInfo*)(handle);
Greg Daniel261b8aa2017-10-23 09:37:36 -040052 return GrBackendTexture(width, height, config, mipMapped, *mockInfo);
Brian Salomon8fe24272017-07-07 12:56:11 -040053 }
54 default:
55 return GrBackendTexture();
56 }
Brian Osman13dddce2017-05-09 13:19:50 -040057}
58
59std::unique_ptr<SkImageGenerator>
Robert Phillipsb0e93a22017-08-29 08:26:54 -040060GrBackendTextureImageGenerator::Make(sk_sp<GrTexture> texture, GrSurfaceOrigin origin,
61 sk_sp<GrSemaphore> semaphore,
Brian Osman13dddce2017-05-09 13:19:50 -040062 SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace) {
63 if (colorSpace && (!colorSpace->gammaCloseToSRGB() && !colorSpace->gammaIsLinear())) {
64 return nullptr;
65 }
66
67 SkColorType colorType = kUnknown_SkColorType;
68 if (!GrPixelConfigToColorType(texture->config(), &colorType)) {
69 return nullptr;
70 }
71
72 GrContext* context = texture->getContext();
73
74 // Attach our texture to this context's resource cache. This ensures that deletion will happen
75 // in the correct thread/context. This adds the only ref to the texture that will persist from
76 // this point. That ref will be released when the generator's RefHelper is freed.
77 context->getResourceCache()->insertCrossContextGpuResource(texture.get());
78
79 GrBackend backend = context->contextPriv().getBackend();
Greg Daniele252f082017-10-23 16:05:23 -040080 GrMipMapped mipMapped = texture->texturePriv().mipMapped();
Brian Osman13dddce2017-05-09 13:19:50 -040081 GrBackendTexture backendTexture = make_backend_texture_from_handle(backend,
82 texture->width(),
83 texture->height(),
84 texture->config(),
Greg Daniel261b8aa2017-10-23 09:37:36 -040085 mipMapped,
Brian Osman13dddce2017-05-09 13:19:50 -040086 texture->getTextureHandle());
87
88 SkImageInfo info = SkImageInfo::Make(texture->width(), texture->height(), colorType, alphaType,
89 std::move(colorSpace));
90 return std::unique_ptr<SkImageGenerator>(new GrBackendTextureImageGenerator(
Robert Phillipsb0e93a22017-08-29 08:26:54 -040091 info, texture.get(), origin, context->uniqueID(), std::move(semaphore), backendTexture));
Brian Osman13dddce2017-05-09 13:19:50 -040092}
93
94GrBackendTextureImageGenerator::GrBackendTextureImageGenerator(const SkImageInfo& info,
95 GrTexture* texture,
Robert Phillipsb0e93a22017-08-29 08:26:54 -040096 GrSurfaceOrigin origin,
Brian Osman13dddce2017-05-09 13:19:50 -040097 uint32_t owningContextID,
98 sk_sp<GrSemaphore> semaphore,
99 const GrBackendTexture& backendTex)
100 : INHERITED(info)
101 , fRefHelper(new RefHelper(texture, owningContextID))
102 , fSemaphore(std::move(semaphore))
103 , fLastBorrowingContextID(SK_InvalidGenID)
104 , fBackendTexture(backendTex)
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400105 , fSurfaceOrigin(origin) { }
Brian Osman13dddce2017-05-09 13:19:50 -0400106
107GrBackendTextureImageGenerator::~GrBackendTextureImageGenerator() {
108 fRefHelper->unref();
109}
110
Brian Osman13dddce2017-05-09 13:19:50 -0400111///////////////////////////////////////////////////////////////////////////////////////////////////
112
113#if SK_SUPPORT_GPU
114void GrBackendTextureImageGenerator::ReleaseRefHelper_TextureReleaseProc(void* ctx) {
115 RefHelper* refHelper = static_cast<RefHelper*>(ctx);
116 SkASSERT(refHelper);
117
118 // Release texture so another context can use it
119 refHelper->fBorrowedTexture = nullptr;
120 refHelper->fBorrowingContextID = SK_InvalidGenID;
121 refHelper->unref();
122}
123
Stan Ilievba81af22017-06-08 15:16:53 -0400124sk_sp<GrTextureProxy> GrBackendTextureImageGenerator::onGenerateTexture(
Christopher Cameron77e96662017-07-08 01:47:47 -0700125 GrContext* context, const SkImageInfo& info, const SkIPoint& origin,
Greg Danielf88c12e2017-10-09 09:57:35 -0400126 SkTransferFunctionBehavior, bool willNeedMipMaps) {
Brian Osman13dddce2017-05-09 13:19:50 -0400127 SkASSERT(context);
128
129 if (context->contextPriv().getBackend() != fBackendTexture.backend()) {
130 return nullptr;
131 }
132
133 sk_sp<GrTexture> tex;
134
135 if (fRefHelper->fBorrowingContextID == context->uniqueID()) {
136 // If a client re-draws the same image multiple times, the texture we return will be cached
137 // and re-used. If they draw a subset, though, we may be re-called. In that case, we want
138 // to re-use the borrowed texture we've previously created.
139 tex = sk_ref_sp(fRefHelper->fBorrowedTexture);
140 SkASSERT(tex);
141 } else {
142 // The texture is available or borrwed by another context. Try for exclusive access.
143 uint32_t expectedID = SK_InvalidGenID;
144 if (!fRefHelper->fBorrowingContextID.compare_exchange(&expectedID, context->uniqueID())) {
145 // Some other context is currently borrowing the texture. We aren't allowed to use it.
146 return nullptr;
147 } else {
148 // Wait on a semaphore when a new context has just started borrowing the texture. This
149 // is conservative, but shouldn't be too expensive.
150 if (fSemaphore && fLastBorrowingContextID != context->uniqueID()) {
151 context->getGpu()->waitSemaphore(fSemaphore);
152 fLastBorrowingContextID = context->uniqueID();
153 }
154 }
155
156 // We just gained access to the texture. If we're on the original context, we could use the
157 // original texture, but we'd have no way of detecting that it's no longer in-use. So we
158 // always make a wrapped copy, where the release proc informs us that the context is done
159 // with it. This is unfortunate - we'll have two texture objects referencing the same GPU
160 // object. However, no client can ever see the original texture, so this should be safe.
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400161 tex = context->resourceProvider()->wrapBackendTexture(fBackendTexture,
Brian Osman13dddce2017-05-09 13:19:50 -0400162 kBorrow_GrWrapOwnership);
163 if (!tex) {
164 fRefHelper->fBorrowingContextID = SK_InvalidGenID;
165 return nullptr;
166 }
167 fRefHelper->fBorrowedTexture = tex.get();
168
169 tex->setRelease(ReleaseRefHelper_TextureReleaseProc, fRefHelper);
170 fRefHelper->ref();
171 }
172
173 SkASSERT(fRefHelper->fBorrowingContextID == context->uniqueID());
174
Robert Phillips066f0202017-07-25 10:16:35 -0400175 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeWrapped(std::move(tex), fSurfaceOrigin);
Brian Osman13dddce2017-05-09 13:19:50 -0400176
177 if (0 == origin.fX && 0 == origin.fY &&
Greg Daniel261b8aa2017-10-23 09:37:36 -0400178 info.width() == fBackendTexture.width() && info.height() == fBackendTexture.height() &&
Greg Daniele252f082017-10-23 16:05:23 -0400179 (!willNeedMipMaps || GrMipMapped::kYes == proxy->mipMapped())) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400180 // If the caller wants the entire texture and we have the correct mip support, we're done
Brian Osman13dddce2017-05-09 13:19:50 -0400181 return proxy;
182 } else {
183 // Otherwise, make a copy of the requested subset. Make sure our temporary is renderable,
Greg Daniel261b8aa2017-10-23 09:37:36 -0400184 // because Vulkan will want to do the copy as a draw. All other copies would require a
185 // layout change in Vulkan and we do not change the layout of borrowed images.
Greg Daniel45d63032017-10-30 13:41:26 -0400186 GrMipMapped mipMapped = willNeedMipMaps ? GrMipMapped::kYes : GrMipMapped::kNo;
187
Greg Daniel261b8aa2017-10-23 09:37:36 -0400188 sk_sp<GrRenderTargetContext> rtContext(context->makeDeferredRenderTargetContext(
189 SkBackingFit::kExact, info.width(), info.height(), proxy->config(), nullptr,
Greg Daniel45d63032017-10-30 13:41:26 -0400190 0, mipMapped, proxy->origin(), nullptr, SkBudgeted::kYes));
Brian Salomon63e79732017-05-15 21:23:13 -0400191
Greg Daniel261b8aa2017-10-23 09:37:36 -0400192 if (!rtContext) {
Brian Osman13dddce2017-05-09 13:19:50 -0400193 return nullptr;
194 }
195
196 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height());
Greg Daniel261b8aa2017-10-23 09:37:36 -0400197 if (!rtContext->copy(proxy.get(), subset, SkIPoint::Make(0, 0))) {
Brian Osman13dddce2017-05-09 13:19:50 -0400198 return nullptr;
199 }
200
Greg Daniel261b8aa2017-10-23 09:37:36 -0400201 return rtContext->asTextureProxyRef();
Brian Osman13dddce2017-05-09 13:19:50 -0400202 }
203}
204#endif