blob: 56e91a297f56811a6f195ed91f09ee3b6d3d029a [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/gpu/GrContext.h"
9#include "include/gpu/GrTexture.h"
10#include "include/private/GrRecordingContext.h"
Ben Wagner21bca282019-05-15 10:15:52 -040011#include "src/core/SkMessageBus.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrBackendTextureImageGenerator.h"
13#include "src/gpu/GrContextPriv.h"
14#include "src/gpu/GrGpu.h"
15#include "src/gpu/GrProxyProvider.h"
16#include "src/gpu/GrRecordingContextPriv.h"
17#include "src/gpu/GrRenderTargetContext.h"
18#include "src/gpu/GrResourceCache.h"
19#include "src/gpu/GrResourceProvider.h"
20#include "src/gpu/GrResourceProviderPriv.h"
21#include "src/gpu/GrSemaphore.h"
22#include "src/gpu/GrTexturePriv.h"
23#include "src/gpu/GrTextureProxyPriv.h"
24#include "src/gpu/SkGr.h"
25#include "src/gpu/gl/GrGLTexture.h"
Brian Osman13dddce2017-05-09 13:19:50 -040026
Brian Salomonb916b7b2019-04-01 13:34:34 -040027GrBackendTextureImageGenerator::RefHelper::RefHelper(GrTexture* texture, uint32_t owningContextID)
28 : fOriginalTexture(texture)
29 , fOwningContextID(owningContextID)
30 , fBorrowingContextReleaseProc(nullptr)
31 , fBorrowingContextID(SK_InvalidGenID) {}
32
Brian Osman13dddce2017-05-09 13:19:50 -040033GrBackendTextureImageGenerator::RefHelper::~RefHelper() {
Brian Salomonb916b7b2019-04-01 13:34:34 -040034 SkASSERT(fBorrowingContextID == SK_InvalidUniqueID);
Brian Osman13dddce2017-05-09 13:19:50 -040035
36 // Generator has been freed, and no one is borrowing the texture. Notify the original cache
37 // that it can free the last ref, so it happens on the correct thread.
38 GrGpuResourceFreedMessage msg { fOriginalTexture, fOwningContextID };
39 SkMessageBus<GrGpuResourceFreedMessage>::Post(msg);
40}
41
Brian Osman13dddce2017-05-09 13:19:50 -040042std::unique_ptr<SkImageGenerator>
Robert Phillipsb0e93a22017-08-29 08:26:54 -040043GrBackendTextureImageGenerator::Make(sk_sp<GrTexture> texture, GrSurfaceOrigin origin,
Brian Osman052ef692018-03-27 09:56:31 -040044 sk_sp<GrSemaphore> semaphore, SkColorType colorType,
Brian Osman13dddce2017-05-09 13:19:50 -040045 SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace) {
Brian Osman13dddce2017-05-09 13:19:50 -040046 GrContext* context = texture->getContext();
47
48 // Attach our texture to this context's resource cache. This ensures that deletion will happen
49 // in the correct thread/context. This adds the only ref to the texture that will persist from
50 // this point. That ref will be released when the generator's RefHelper is freed.
Brian Salomon876a0172019-03-08 11:12:14 -050051 context->priv().getResourceCache()->insertDelayedResourceUnref(texture.get());
Brian Osman13dddce2017-05-09 13:19:50 -040052
Robert Phillipsb67821d2017-12-13 15:00:45 -050053 GrBackendTexture backendTexture = texture->getBackendTexture();
Robert Phillips97256382019-07-17 15:26:36 -040054
55 // TODO: delete this block
56 {
57 GrBackendFormat backendFormat = backendTexture.getBackendFormat();
58 if (!backendFormat.isValid()) {
59 return nullptr;
60 }
61
62 backendTexture.fConfig = context->priv().caps()->getConfigFromBackendFormat(
63 backendFormat,
64 SkColorTypeToGrColorType(colorType));
65 if (backendTexture.fConfig == kUnknown_GrPixelConfig) {
66 return nullptr;
67 }
Brian Osman052ef692018-03-27 09:56:31 -040068 }
Brian Osman13dddce2017-05-09 13:19:50 -040069
70 SkImageInfo info = SkImageInfo::Make(texture->width(), texture->height(), colorType, alphaType,
71 std::move(colorSpace));
72 return std::unique_ptr<SkImageGenerator>(new GrBackendTextureImageGenerator(
Robert Phillips9da87e02019-02-04 13:26:26 -050073 info, texture.get(), origin, context->priv().contextID(),
Robert Phillipsfd0d9702019-02-01 10:19:42 -050074 std::move(semaphore), backendTexture));
Brian Osman13dddce2017-05-09 13:19:50 -040075}
76
77GrBackendTextureImageGenerator::GrBackendTextureImageGenerator(const SkImageInfo& info,
78 GrTexture* texture,
Robert Phillipsb0e93a22017-08-29 08:26:54 -040079 GrSurfaceOrigin origin,
Brian Osman13dddce2017-05-09 13:19:50 -040080 uint32_t owningContextID,
81 sk_sp<GrSemaphore> semaphore,
82 const GrBackendTexture& backendTex)
Brian Salomonb916b7b2019-04-01 13:34:34 -040083 : INHERITED(info)
84 , fRefHelper(new RefHelper(texture, owningContextID))
85 , fSemaphore(std::move(semaphore))
86 , fBackendTexture(backendTex)
Brian Salomonb916b7b2019-04-01 13:34:34 -040087 , fSurfaceOrigin(origin) {}
Brian Osman13dddce2017-05-09 13:19:50 -040088
89GrBackendTextureImageGenerator::~GrBackendTextureImageGenerator() {
90 fRefHelper->unref();
91}
92
Brian Osman13dddce2017-05-09 13:19:50 -040093///////////////////////////////////////////////////////////////////////////////////////////////////
94
Brian Osman13dddce2017-05-09 13:19:50 -040095void GrBackendTextureImageGenerator::ReleaseRefHelper_TextureReleaseProc(void* ctx) {
96 RefHelper* refHelper = static_cast<RefHelper*>(ctx);
97 SkASSERT(refHelper);
98
Greg Danielabba9982018-02-01 10:07:57 -050099 refHelper->fBorrowingContextReleaseProc = nullptr;
Greg Daniel48661b82018-01-22 16:11:35 -0500100 refHelper->fBorrowingContextID = SK_InvalidGenID;
Brian Osman13dddce2017-05-09 13:19:50 -0400101 refHelper->unref();
102}
103
Stan Ilievba81af22017-06-08 15:16:53 -0400104sk_sp<GrTextureProxy> GrBackendTextureImageGenerator::onGenerateTexture(
Robert Phillips9338c602019-02-19 12:52:29 -0500105 GrRecordingContext* context, const SkImageInfo& info,
106 const SkIPoint& origin, bool willNeedMipMaps) {
Brian Osman13dddce2017-05-09 13:19:50 -0400107 SkASSERT(context);
108
Robert Phillips4217ea72019-01-30 13:08:28 -0500109 if (context->backend() != fBackendTexture.backend()) {
Brian Osman13dddce2017-05-09 13:19:50 -0400110 return nullptr;
111 }
Brian Osman052ef692018-03-27 09:56:31 -0400112 if (info.colorType() != this->getInfo().colorType()) {
113 return nullptr;
114 }
Brian Osman13dddce2017-05-09 13:19:50 -0400115
Robert Phillips9da87e02019-02-04 13:26:26 -0500116 auto proxyProvider = context->priv().proxyProvider();
Robert Phillips97256382019-07-17 15:26:36 -0400117 const GrCaps* caps = context->priv().caps();
Robert Phillips777707b2018-01-17 11:40:14 -0500118
Greg Danielabba9982018-02-01 10:07:57 -0500119 fBorrowingMutex.acquire();
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500120 sk_sp<GrRefCntedCallback> releaseProcHelper;
Greg Danielabba9982018-02-01 10:07:57 -0500121 if (SK_InvalidGenID != fRefHelper->fBorrowingContextID) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500122 if (fRefHelper->fBorrowingContextID != context->priv().contextID()) {
Greg Danielabba9982018-02-01 10:07:57 -0500123 fBorrowingMutex.release();
Greg Daniel966db9e2018-01-12 13:15:06 -0500124 return nullptr;
Greg Danielabba9982018-02-01 10:07:57 -0500125 } else {
126 SkASSERT(fRefHelper->fBorrowingContextReleaseProc);
127 // Ref the release proc to be held by the proxy we make below
128 releaseProcHelper = sk_ref_sp(fRefHelper->fBorrowingContextReleaseProc);
Greg Daniel966db9e2018-01-12 13:15:06 -0500129 }
Greg Danielabba9982018-02-01 10:07:57 -0500130 } else {
131 SkASSERT(!fRefHelper->fBorrowingContextReleaseProc);
132 // The ref we add to fRefHelper here will be passed into and owned by the
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500133 // GrRefCntedCallback.
Greg Danielabba9982018-02-01 10:07:57 -0500134 fRefHelper->ref();
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500135 releaseProcHelper.reset(
136 new GrRefCntedCallback(ReleaseRefHelper_TextureReleaseProc, fRefHelper));
Greg Danielabba9982018-02-01 10:07:57 -0500137 fRefHelper->fBorrowingContextReleaseProc = releaseProcHelper.get();
Brian Osman13dddce2017-05-09 13:19:50 -0400138 }
Robert Phillips9da87e02019-02-04 13:26:26 -0500139 fRefHelper->fBorrowingContextID = context->priv().contextID();
Brian Salomonb916b7b2019-04-01 13:34:34 -0400140 if (!fRefHelper->fBorrowedTextureKey.isValid()) {
141 static const auto kDomain = GrUniqueKey::GenerateDomain();
142 GrUniqueKey::Builder builder(&fRefHelper->fBorrowedTextureKey, kDomain, 1);
143 builder[0] = this->uniqueID();
144 }
Greg Danielabba9982018-02-01 10:07:57 -0500145 fBorrowingMutex.release();
Brian Osman13dddce2017-05-09 13:19:50 -0400146
Robert Phillips9da87e02019-02-04 13:26:26 -0500147 SkASSERT(fRefHelper->fBorrowingContextID == context->priv().contextID());
Brian Osman13dddce2017-05-09 13:19:50 -0400148
Robert Phillips97256382019-07-17 15:26:36 -0400149
150 GrBackendFormat backendFormat = fBackendTexture.getBackendFormat();
151 SkASSERT(backendFormat.isValid());
152
153 GrColorType grColorType = SkColorTypeToGrColorType(info.colorType());
154
155 GrPixelConfig config = caps->getConfigFromBackendFormat(backendFormat, grColorType);
156 if (kUnknown_GrPixelConfig == config) {
157 return nullptr;
158 }
159
160 SkASSERT(GrCaps::AreConfigsCompatible(fBackendTexture.config(),
161 caps->getConfigFromBackendFormat(backendFormat,
162 grColorType)));
163
Greg Daniele728f672018-01-17 10:52:04 -0500164 GrSurfaceDesc desc;
Greg Daniele728f672018-01-17 10:52:04 -0500165 desc.fWidth = fBackendTexture.width();
166 desc.fHeight = fBackendTexture.height();
Robert Phillips97256382019-07-17 15:26:36 -0400167 desc.fConfig = config;
Greg Daniele728f672018-01-17 10:52:04 -0500168 GrMipMapped mipMapped = fBackendTexture.hasMipMaps() ? GrMipMapped::kYes : GrMipMapped::kNo;
169
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400170 // Must make copies of member variables to capture in the lambda since this image generator may
171 // be deleted before we actually execute the lambda.
Robert Phillips777707b2018-01-17 11:40:14 -0500172 sk_sp<GrTextureProxy> proxy = proxyProvider->createLazyProxy(
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400173 [refHelper = fRefHelper, releaseProcHelper, semaphore = fSemaphore,
Robert Phillips97256382019-07-17 15:26:36 -0400174 backendTexture = fBackendTexture, grColorType](GrResourceProvider* resourceProvider)
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400175 -> GrSurfaceProxy::LazyInstantiationResult {
Greg Daniel48661b82018-01-22 16:11:35 -0500176 if (semaphore) {
Greg Daniele728f672018-01-17 10:52:04 -0500177 resourceProvider->priv().gpu()->waitSemaphore(semaphore);
178 }
179
Brian Salomonb916b7b2019-04-01 13:34:34 -0400180 // If a client re-draws the same image multiple times, the texture we return
181 // will be cached and re-used. If they draw a subset, though, we may be
182 // re-called. In that case, we want to re-use the borrowed texture we've
183 // previously created.
Greg Daniele728f672018-01-17 10:52:04 -0500184 sk_sp<GrTexture> tex;
Brian Salomonb916b7b2019-04-01 13:34:34 -0400185 SkASSERT(refHelper->fBorrowedTextureKey.isValid());
186 auto surf = resourceProvider->findByUniqueKey<GrSurface>(
187 refHelper->fBorrowedTextureKey);
188 if (surf) {
189 SkASSERT(surf->asTexture());
190 tex = sk_ref_sp(surf->asTexture());
Greg Daniele728f672018-01-17 10:52:04 -0500191 } else {
192 // We just gained access to the texture. If we're on the original context, we
193 // could use the original texture, but we'd have no way of detecting that it's
194 // no longer in-use. So we always make a wrapped copy, where the release proc
195 // informs us that the context is done with it. This is unfortunate - we'll have
196 // two texture objects referencing the same GPU object. However, no client can
197 // ever see the original texture, so this should be safe.
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500198 // We make the texture uncacheable so that the release proc is called ASAP.
Brian Salomonc67c31c2018-12-06 10:00:03 -0500199 tex = resourceProvider->wrapBackendTexture(
Robert Phillips97256382019-07-17 15:26:36 -0400200 backendTexture, grColorType, kBorrow_GrWrapOwnership,
201 GrWrapCacheable::kNo, kRead_GrIOType);
Greg Daniele728f672018-01-17 10:52:04 -0500202 if (!tex) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400203 return {};
Greg Daniele728f672018-01-17 10:52:04 -0500204 }
Greg Danielabba9982018-02-01 10:07:57 -0500205 tex->setRelease(releaseProcHelper);
Brian Salomonb916b7b2019-04-01 13:34:34 -0400206 tex->resourcePriv().setUniqueKey(refHelper->fBorrowedTextureKey);
Greg Daniele728f672018-01-17 10:52:04 -0500207 }
Brian Salomonb916b7b2019-04-01 13:34:34 -0400208 // We use keys to avoid re-wrapping the GrBackendTexture in a GrTexture. This is
209 // unrelated to the whatever SkImage key may be assigned to the proxy.
210 return {std::move(tex), GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced};
Brian Salomon2a4f9832018-03-03 22:43:43 -0500211 },
Robert Phillips97256382019-07-17 15:26:36 -0400212 backendFormat, desc, GrRenderable::kNo, fSurfaceOrigin, mipMapped,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400213 GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, SkBudgeted::kNo);
Greg Daniele3204862018-04-16 11:24:10 -0400214 if (!proxy) {
215 return nullptr;
216 }
217
Brian Osman13dddce2017-05-09 13:19:50 -0400218 if (0 == origin.fX && 0 == origin.fY &&
Greg Daniel261b8aa2017-10-23 09:37:36 -0400219 info.width() == fBackendTexture.width() && info.height() == fBackendTexture.height() &&
Greg Daniele252f082017-10-23 16:05:23 -0400220 (!willNeedMipMaps || GrMipMapped::kYes == proxy->mipMapped())) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400221 // If the caller wants the entire texture and we have the correct mip support, we're done
Brian Osman13dddce2017-05-09 13:19:50 -0400222 return proxy;
223 } else {
224 // Otherwise, make a copy of the requested subset. Make sure our temporary is renderable,
Greg Daniel261b8aa2017-10-23 09:37:36 -0400225 // because Vulkan will want to do the copy as a draw. All other copies would require a
226 // layout change in Vulkan and we do not change the layout of borrowed images.
Greg Daniel45d63032017-10-30 13:41:26 -0400227 GrMipMapped mipMapped = willNeedMipMaps ? GrMipMapped::kYes : GrMipMapped::kNo;
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400228 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height());
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400229
Greg Daniel46cfbc62019-06-07 11:43:30 -0400230 return GrSurfaceProxy::Copy(context, proxy.get(), mipMapped, subset, SkBackingFit::kExact,
231 SkBudgeted::kYes);
Brian Osman13dddce2017-05-09 13:19:50 -0400232 }
233}
Greg Daniel9cc28232018-04-06 10:00:09 -0400234