blob: f84cdfc40ba8d7f8821ee60fee69b9887b3ce53c [file] [log] [blame]
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001/*
2 * Copyright 2018 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#ifndef GrProxyProvider_DEFINED
9#define GrProxyProvider_DEFINED
10
Robert Phillipsc4039ea2018-03-01 11:36:45 -050011#include "GrCaps.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050012#include "GrResourceKey.h"
13#include "GrTextureProxy.h"
14#include "GrTypes.h"
15#include "SkRefCnt.h"
16#include "SkTDynamicHash.h"
17
Robert Phillips1afd4cd2018-01-08 13:40:32 -050018class GrResourceProvider;
19class GrSingleOwner;
Robert Phillips0bd24dc2018-01-16 08:06:32 -050020class GrBackendRenderTarget;
Greg Daniela4ead652018-02-07 10:21:48 -050021class SkBitmap;
Greg Daniel9d86f1d2018-01-29 09:33:59 -050022class SkImage;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050023
24/*
25 * A factory for creating GrSurfaceProxy-derived objects.
26 */
27class GrProxyProvider {
28public:
29 GrProxyProvider(GrResourceProvider*, GrResourceCache*, sk_sp<const GrCaps>, GrSingleOwner*);
Brian Salomon238069b2018-07-11 15:58:57 -040030 GrProxyProvider(uint32_t contextUniqueID, sk_sp<const GrCaps>, GrSingleOwner*);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050031
32 ~GrProxyProvider();
33
34 /*
35 * Assigns a unique key to a proxy. The proxy will be findable via this key using
36 * findProxyByUniqueKey(). It is an error if an existing proxy already has a key.
37 */
Robert Phillipsadbe1322018-01-17 13:35:46 -050038 bool assignUniqueKeyToProxy(const GrUniqueKey&, GrTextureProxy*);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050039
40 /*
41 * Sets the unique key of the provided proxy to the unique key of the surface. The surface must
42 * have a valid unique key.
43 */
44 void adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface*);
45
46 /*
47 * Removes a unique key from a proxy. If the proxy has already been instantiated, it will
48 * also remove the unique key from the target GrSurface.
49 */
Chris Dalton2de13dd2019-01-03 15:11:59 -070050 void removeUniqueKeyFromProxy(GrTextureProxy*);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050051
52 /*
53 * Finds a proxy by unique key.
54 */
55 sk_sp<GrTextureProxy> findProxyByUniqueKey(const GrUniqueKey&, GrSurfaceOrigin);
56
57 /*
58 * Finds a proxy by unique key or creates a new one that wraps a resource matching the unique
59 * key.
60 */
61 sk_sp<GrTextureProxy> findOrCreateProxyByUniqueKey(const GrUniqueKey&, GrSurfaceOrigin);
62
63 /*
Greg Daniel9d86f1d2018-01-29 09:33:59 -050064 * Create an un-mipmapped texture proxy with data. The SkImage must be a raster backend image.
65 * Since the SkImage is ref counted, we simply take a ref on it to keep the data alive until we
66 * actually upload the data to the gpu.
67 */
Chris Daltond004e0b2018-09-27 09:28:03 -060068 sk_sp<GrTextureProxy> createTextureProxy(
69 sk_sp<SkImage> srcImage, GrSurfaceDescFlags, int sampleCnt, SkBudgeted, SkBackingFit,
70 GrInternalSurfaceFlags = GrInternalSurfaceFlags::kNone);
Greg Daniel9d86f1d2018-01-29 09:33:59 -050071
72 /*
Robert Phillips0bd24dc2018-01-16 08:06:32 -050073 * Create a mipmapped texture proxy without any data.
74 *
75 * Like the call above but there are no texels to upload. A texture proxy is returned that
76 * simply has space allocated for the mips. We will allocated the full amount of mip levels
77 * based on the width and height in the GrSurfaceDesc.
78 */
Greg Daniel4065d452018-11-16 15:43:41 -050079 sk_sp<GrTextureProxy> createMipMapProxy(const GrBackendFormat&, const GrSurfaceDesc&,
80 GrSurfaceOrigin, SkBudgeted);
Robert Phillips0bd24dc2018-01-16 08:06:32 -050081
82 /*
Greg Daniela4ead652018-02-07 10:21:48 -050083 * Creates a new mipmapped texture proxy for the bitmap with mip levels generated by the cpu.
84 */
Brian Osman2b23c4b2018-06-01 12:25:08 -040085 sk_sp<GrTextureProxy> createMipMapProxyFromBitmap(const SkBitmap& bitmap);
Greg Daniela4ead652018-02-07 10:21:48 -050086
87 /*
Robert Phillips0bd24dc2018-01-16 08:06:32 -050088 * Create a GrSurfaceProxy without any data.
89 */
Greg Daniel4065d452018-11-16 15:43:41 -050090 sk_sp<GrTextureProxy> createProxy(const GrBackendFormat&, const GrSurfaceDesc&, GrSurfaceOrigin,
91 GrMipMapped, SkBackingFit, SkBudgeted,
92 GrInternalSurfaceFlags);
Greg Danielf6f7b672018-02-15 13:06:26 -050093
Robert Phillipsfe0253f2018-03-16 16:47:25 -040094 sk_sp<GrTextureProxy> createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -050095 const GrBackendFormat& format, const GrSurfaceDesc& desc,
96 GrSurfaceOrigin origin, SkBackingFit fit, SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040097 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone) {
Greg Daniel4065d452018-11-16 15:43:41 -050098 return this->createProxy(format, desc, origin, GrMipMapped::kNo, fit, budgeted,
99 surfaceFlags);
Greg Danielf6f7b672018-02-15 13:06:26 -0500100 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500101
Jim Van Verthee06b332019-01-18 10:36:32 -0500102 /*
103 * Create a texture proxy with data. It's assumed that the data is packed tightly.
104 */
105 sk_sp<GrTextureProxy> createProxy(sk_sp<SkData>, const GrSurfaceDesc& desc);
106
Robert Phillipsadbe1322018-01-17 13:35:46 -0500107 // These match the definitions in SkImage & GrTexture.h, for whence they came
108 typedef void* ReleaseContext;
109 typedef void (*ReleaseProc)(ReleaseContext);
110
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500111 /*
Brian Salomonc67c31c2018-12-06 10:00:03 -0500112 * Create a texture proxy that wraps a (non-renderable) backend texture. GrIOType must be
113 * kRead or kRW.
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500114 */
Brian Salomon7578f3e2018-03-07 14:39:54 -0500115 sk_sp<GrTextureProxy> wrapBackendTexture(const GrBackendTexture&, GrSurfaceOrigin,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500116 GrWrapOwnership, GrWrapCacheable, GrIOType,
117 ReleaseProc = nullptr, ReleaseContext = nullptr);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500118
119 /*
120 * Create a texture proxy that wraps a backend texture and is both texture-able and renderable
121 */
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500122 sk_sp<GrTextureProxy> wrapRenderableBackendTexture(const GrBackendTexture&, GrSurfaceOrigin,
123 int sampleCnt, GrWrapOwnership,
124 GrWrapCacheable);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500125
126 /*
Brian Salomonc67c31c2018-12-06 10:00:03 -0500127 * Create a render target proxy that wraps a backend render target
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500128 */
Brian Salomon7578f3e2018-03-07 14:39:54 -0500129 sk_sp<GrSurfaceProxy> wrapBackendRenderTarget(const GrBackendRenderTarget&, GrSurfaceOrigin);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500130
131 /*
Brian Salomon7578f3e2018-03-07 14:39:54 -0500132 * Create a render target proxy that wraps a backend texture
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500133 */
Kevin Lubickb5502b22018-03-12 10:17:06 -0400134 sk_sp<GrSurfaceProxy> wrapBackendTextureAsRenderTarget(const GrBackendTexture& backendTex,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500135 GrSurfaceOrigin origin,
136 int sampleCnt);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500137
Greg Danielb46add82019-01-02 14:51:29 -0500138 sk_sp<GrRenderTargetProxy> wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo&,
139 const GrVkDrawableInfo&);
140
Robert Phillipsce5209a2018-02-13 11:13:51 -0500141 using LazyInstantiateCallback = std::function<sk_sp<GrSurface>(GrResourceProvider*)>;
Robert Phillipse8fabb22018-02-04 14:33:21 -0500142
Robert Phillips777707b2018-01-17 11:40:14 -0500143 enum class Renderable : bool {
144 kNo = false,
145 kYes = true
146 };
147
Brian Salomon7226c232018-07-30 13:13:17 -0400148 struct TextureInfo {
149 GrMipMapped fMipMapped;
150 GrTextureType fTextureType;
151 };
152
Greg Daniela8d92112018-03-09 12:05:04 -0500153 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
Robert Phillips777707b2018-01-17 11:40:14 -0500154 /**
155 * Creates a texture proxy that will be instantiated by a user-supplied callback during flush.
156 * (Stencil is not supported by this method.) The width and height must either both be greater
157 * than 0 or both less than or equal to zero. A non-positive value is a signal that the width
158 * and height are currently unknown.
159 *
160 * When called, the callback must be able to cleanup any resources that it captured at creation.
161 * It also must support being passed in a null GrResourceProvider. When this happens, the
162 * callback should cleanup any resources it captured and return an empty sk_sp<GrTextureProxy>.
163 */
Greg Daniel4065d452018-11-16 15:43:41 -0500164 sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrBackendFormat&,
165 const GrSurfaceDesc&, GrSurfaceOrigin, GrMipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400166 GrInternalSurfaceFlags, SkBackingFit, SkBudgeted,
167 LazyInstantiationType);
Greg Daniela8d92112018-03-09 12:05:04 -0500168
Greg Daniel4065d452018-11-16 15:43:41 -0500169 sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrBackendFormat&,
170 const GrSurfaceDesc&, GrSurfaceOrigin, GrMipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400171 GrInternalSurfaceFlags, SkBackingFit, SkBudgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500172
Greg Daniel4065d452018-11-16 15:43:41 -0500173 sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrBackendFormat&,
174 const GrSurfaceDesc&, GrSurfaceOrigin, GrMipMapped,
175 SkBackingFit, SkBudgeted);
Brian Salomon7226c232018-07-30 13:13:17 -0400176
177 /** A null TextureInfo indicates a non-textureable render target. */
Robert Phillipse8fabb22018-02-04 14:33:21 -0500178 sk_sp<GrRenderTargetProxy> createLazyRenderTargetProxy(LazyInstantiateCallback&&,
Greg Daniel4065d452018-11-16 15:43:41 -0500179 const GrBackendFormat&,
Greg Daniel2a303902018-02-20 10:25:54 -0500180 const GrSurfaceDesc&,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500181 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400182 GrInternalSurfaceFlags,
183 const TextureInfo*,
184 SkBackingFit,
185 SkBudgeted);
Robert Phillipse8fabb22018-02-04 14:33:21 -0500186
Chris Dalton4c458b12018-06-16 17:22:59 -0600187 /**
188 * Fully lazy proxies have unspecified width and height. Methods that rely on those values
189 * (e.g., width, height, getBoundsRect) should be avoided.
190 */
Greg Daniel4065d452018-11-16 15:43:41 -0500191 static sk_sp<GrTextureProxy> MakeFullyLazyProxy(LazyInstantiateCallback&&,
192 const GrBackendFormat&, Renderable,
Chris Dalton4c458b12018-06-16 17:22:59 -0600193 GrSurfaceOrigin, GrPixelConfig, const GrCaps&);
194
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500195 // 'proxy' is about to be used as a texture src or drawn to. This query can be used to
196 // determine if it is going to need a texture domain or a full clear.
197 static bool IsFunctionallyExact(GrSurfaceProxy* proxy);
198
Robert Phillips427966a2018-12-20 17:20:43 -0500199 enum class InvalidateGPUResource : bool { kNo = false, kYes = true };
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500200
Robert Phillips427966a2018-12-20 17:20:43 -0500201 /*
202 * This method ensures that, if a proxy w/ the supplied unique key exists, it is removed from
203 * the proxy provider's map and its unique key is removed. If 'invalidateSurface' is true, it
204 * will independently ensure that the unique key is removed from any GrGpuResources that may
205 * have it.
206 *
207 * If 'proxy' is provided (as an optimization to stop re-looking it up), its unique key must be
208 * valid and match the provided unique key.
209 *
210 * This method is called if either the proxy attached to the unique key is being deleted
211 * (in which case we don't want it cluttering up the hash table) or the client has indicated
212 * that it will never refer to the unique key again.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500213 */
Chris Dalton2de13dd2019-01-03 15:11:59 -0700214 void processInvalidUniqueKey(const GrUniqueKey&, GrTextureProxy*, InvalidateGPUResource);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500215
Brian Salomon238069b2018-07-11 15:58:57 -0400216 uint32_t contextUniqueID() const { return fContextUniqueID; }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500217 const GrCaps* caps() const { return fCaps.get(); }
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500218 sk_sp<const GrCaps> refCaps() const { return fCaps; }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500219
220 void abandon() {
221 fResourceCache = nullptr;
222 fResourceProvider = nullptr;
Robert Phillips4d120512018-01-19 13:22:07 -0500223 fAbandoned = true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500224 }
225
226 bool isAbandoned() const {
Robert Phillips4d120512018-01-19 13:22:07 -0500227#ifdef SK_DEBUG
228 if (fAbandoned) {
229 SkASSERT(!fResourceCache && !fResourceProvider);
230 }
231#endif
232 return fAbandoned;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500233 }
234
235 int numUniqueKeyProxies_TestOnly() const;
236
Robert Phillips0790f8a2018-09-18 13:11:03 -0400237 // This is called on a DDL's proxyprovider when the DDL is finished. The uniquely keyed
238 // proxies need to keep their unique key but cannot hold on to the proxy provider unique
239 // pointer.
240 void orphanAllUniqueKeys();
241 // This is only used by GrContext::releaseResourcesAndAbandonContext()
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500242 void removeAllUniqueKeys();
243
Greg Daniel7e1912a2018-02-08 09:15:33 -0500244 /**
Robert Phillips5c4b33b2018-03-20 16:23:08 -0400245 * Are we currently recording a DDL?
Greg Daniel7e1912a2018-02-08 09:15:33 -0500246 */
Robert Phillips5c4b33b2018-03-20 16:23:08 -0400247 bool recordingDDL() const { return !SkToBool(fResourceProvider); }
Greg Daniel7e1912a2018-02-08 09:15:33 -0500248
Chris Daltond004e0b2018-09-27 09:28:03 -0600249 /*
250 * Create a texture proxy that is backed by an instantiated GrSurface.
251 */
252 sk_sp<GrTextureProxy> testingOnly_createInstantiatedProxy(const GrSurfaceDesc&, GrSurfaceOrigin,
253 SkBackingFit, SkBudgeted);
254
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500255private:
Robert Phillipsadbe1322018-01-17 13:35:46 -0500256 friend class GrAHardwareBufferImageGenerator; // for createWrapped
Chris Daltond004e0b2018-09-27 09:28:03 -0600257 friend class GrResourceProvider; // for createWrapped
Robert Phillipsadbe1322018-01-17 13:35:46 -0500258
259 sk_sp<GrTextureProxy> createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin);
260
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500261 struct UniquelyKeyedProxyHashTraits {
262 static const GrUniqueKey& GetKey(const GrTextureProxy& p) { return p.getUniqueKey(); }
263
264 static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
265 };
266 typedef SkTDynamicHash<GrTextureProxy, GrUniqueKey, UniquelyKeyedProxyHashTraits> UniquelyKeyedProxyHash;
267
268 // This holds the texture proxies that have unique keys. The resourceCache does not get a ref
269 // on these proxies but they must send a message to the resourceCache when they are deleted.
270 UniquelyKeyedProxyHash fUniquelyKeyedProxies;
271
272 GrResourceProvider* fResourceProvider;
273 GrResourceCache* fResourceCache;
Robert Phillips4d120512018-01-19 13:22:07 -0500274 bool fAbandoned;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500275 sk_sp<const GrCaps> fCaps;
Brian Salomon238069b2018-07-11 15:58:57 -0400276 // If this provider is owned by a DDLContext then this is the DirectContext's ID.
277 uint32_t fContextUniqueID;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500278
279 // In debug builds we guard against improper thread handling
280 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
281};
282
283#endif