blob: 58c884e3a7fbc545de6f1d8fc5d7788c4303ba07 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrTypes.h"
12#include "include/private/GrResourceKey.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/core/SkTDynamicHash.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040014#include "src/gpu/GrTextureProxy.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050015
Robert Phillipsa41c6852019-02-07 10:44:10 -050016class GrImageContext;
Robert Phillips0bd24dc2018-01-16 08:06:32 -050017class GrBackendRenderTarget;
Greg Daniela4ead652018-02-07 10:21:48 -050018class SkBitmap;
Greg Daniel9d86f1d2018-01-29 09:33:59 -050019class SkImage;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050020
21/*
22 * A factory for creating GrSurfaceProxy-derived objects.
23 */
24class GrProxyProvider {
25public:
Robert Phillipsa41c6852019-02-07 10:44:10 -050026 GrProxyProvider(GrImageContext*);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050027
28 ~GrProxyProvider();
29
30 /*
31 * Assigns a unique key to a proxy. The proxy will be findable via this key using
32 * findProxyByUniqueKey(). It is an error if an existing proxy already has a key.
33 */
Robert Phillipsadbe1322018-01-17 13:35:46 -050034 bool assignUniqueKeyToProxy(const GrUniqueKey&, GrTextureProxy*);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050035
36 /*
37 * Sets the unique key of the provided proxy to the unique key of the surface. The surface must
38 * have a valid unique key.
39 */
40 void adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface*);
41
42 /*
43 * Removes a unique key from a proxy. If the proxy has already been instantiated, it will
44 * also remove the unique key from the target GrSurface.
45 */
Chris Dalton2de13dd2019-01-03 15:11:59 -070046 void removeUniqueKeyFromProxy(GrTextureProxy*);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050047
48 /*
49 * Finds a proxy by unique key.
50 */
51 sk_sp<GrTextureProxy> findProxyByUniqueKey(const GrUniqueKey&, GrSurfaceOrigin);
52
53 /*
54 * Finds a proxy by unique key or creates a new one that wraps a resource matching the unique
Brian Salomon2af3e702019-08-11 19:10:31 -040055 * key. GrColorType is required to set the proxy's texture swizzle on creation. For any key,
56 * each call that might result in a cache hit must provide the same colorType as the call that
57 * caused a cache miss and created the proxy.
Robert Phillips1afd4cd2018-01-08 13:40:32 -050058 */
Brian Salomon2af3e702019-08-11 19:10:31 -040059 sk_sp<GrTextureProxy> findOrCreateProxyByUniqueKey(const GrUniqueKey&, GrColorType colorType,
60 GrSurfaceOrigin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050061
62 /*
Greg Daniel9d86f1d2018-01-29 09:33:59 -050063 * Create an un-mipmapped texture proxy with data. The SkImage must be a raster backend image.
64 * Since the SkImage is ref counted, we simply take a ref on it to keep the data alive until we
65 * actually upload the data to the gpu.
66 */
Chris Daltond004e0b2018-09-27 09:28:03 -060067 sk_sp<GrTextureProxy> createTextureProxy(
Brian Salomonf2c2ba92019-07-17 09:59:59 -040068 sk_sp<SkImage> srcImage, GrRenderable, int sampleCnt, SkBudgeted, SkBackingFit,
Chris Daltond004e0b2018-09-27 09:28:03 -060069 GrInternalSurfaceFlags = GrInternalSurfaceFlags::kNone);
Greg Daniel9d86f1d2018-01-29 09:33:59 -050070
71 /*
Robert Phillips0bd24dc2018-01-16 08:06:32 -050072 * Create a mipmapped texture proxy without any data.
73 *
74 * Like the call above but there are no texels to upload. A texture proxy is returned that
75 * simply has space allocated for the mips. We will allocated the full amount of mip levels
76 * based on the width and height in the GrSurfaceDesc.
77 */
Greg Daniel4065d452018-11-16 15:43:41 -050078 sk_sp<GrTextureProxy> createMipMapProxy(const GrBackendFormat&, const GrSurfaceDesc&,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040079 GrRenderable, int renderTargetSampleCnt,
80 GrSurfaceOrigin, SkBudgeted, GrProtected);
Robert Phillips0bd24dc2018-01-16 08:06:32 -050081
82 /*
Brian Osmande496652019-03-22 13:42:33 -040083 * Creates a new texture proxy for the bitmap, optionally with mip levels generated by the cpu.
Greg Daniela4ead652018-02-07 10:21:48 -050084 */
Brian Osmande496652019-03-22 13:42:33 -040085 sk_sp<GrTextureProxy> createProxyFromBitmap(const SkBitmap& bitmap, GrMipMapped);
Greg Daniela4ead652018-02-07 10:21:48 -050086
87 /*
Robert Phillips0bd24dc2018-01-16 08:06:32 -050088 * Create a GrSurfaceProxy without any data.
89 */
Brian Salomonf2c2ba92019-07-17 09:59:59 -040090 sk_sp<GrTextureProxy> createProxy(const GrBackendFormat&, const GrSurfaceDesc&, GrRenderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040091 int renderTargetSampleCnt, GrSurfaceOrigin, GrMipMapped,
92 SkBackingFit, SkBudgeted, GrProtected,
93 GrInternalSurfaceFlags);
Greg Danielf6f7b672018-02-15 13:06:26 -050094
Robert Phillipsfe0253f2018-03-16 16:47:25 -040095 sk_sp<GrTextureProxy> createProxy(
Brian Salomonf2c2ba92019-07-17 09:59:59 -040096 const GrBackendFormat& format, const GrSurfaceDesc& desc, GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040097 int renderTargetSampleCnt, GrSurfaceOrigin origin, SkBackingFit fit,
98 SkBudgeted budgeted, GrProtected isProtected,
Brian Salomonf2c2ba92019-07-17 09:59:59 -040099 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400100 return this->createProxy(format, desc, renderable, renderTargetSampleCnt, origin,
101 GrMipMapped::kNo, fit, budgeted, isProtected, surfaceFlags);
Greg Danielf6f7b672018-02-15 13:06:26 -0500102 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500103
Jim Van Verthee06b332019-01-18 10:36:32 -0500104 /*
Brian Salomonbb8dde82019-06-27 10:52:13 -0400105 * Create a texture proxy from compressed texture data.
Jim Van Verthee06b332019-01-18 10:36:32 -0500106 */
Brian Salomonbb8dde82019-06-27 10:52:13 -0400107 sk_sp<GrTextureProxy> createCompressedTextureProxy(int width, int height, SkBudgeted budgeted,
108 SkImage::CompressionType compressionType,
109 sk_sp<SkData> data);
Jim Van Verthee06b332019-01-18 10:36:32 -0500110
Robert Phillipsadbe1322018-01-17 13:35:46 -0500111 // These match the definitions in SkImage & GrTexture.h, for whence they came
112 typedef void* ReleaseContext;
113 typedef void (*ReleaseProc)(ReleaseContext);
114
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500115 /*
Brian Salomonc67c31c2018-12-06 10:00:03 -0500116 * Create a texture proxy that wraps a (non-renderable) backend texture. GrIOType must be
117 * kRead or kRW.
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500118 */
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400119 sk_sp<GrTextureProxy> wrapBackendTexture(const GrBackendTexture&, GrColorType, GrSurfaceOrigin,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500120 GrWrapOwnership, GrWrapCacheable, GrIOType,
121 ReleaseProc = nullptr, ReleaseContext = nullptr);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500122
123 /*
124 * Create a texture proxy that wraps a backend texture and is both texture-able and renderable
125 */
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500126 sk_sp<GrTextureProxy> wrapRenderableBackendTexture(const GrBackendTexture&, GrSurfaceOrigin,
Robert Phillips0902c982019-07-16 07:47:56 -0400127 int sampleCnt, GrColorType,
128 GrWrapOwnership, GrWrapCacheable,
129 ReleaseProc = nullptr,
Brian Salomon6c1205a2019-06-14 11:49:03 -0400130 ReleaseContext = nullptr);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500131
132 /*
Brian Salomonc67c31c2018-12-06 10:00:03 -0500133 * Create a render target proxy that wraps a backend render target
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500134 */
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400135 sk_sp<GrSurfaceProxy> wrapBackendRenderTarget(const GrBackendRenderTarget&, GrColorType,
136 GrSurfaceOrigin,
Brian Salomon6c1205a2019-06-14 11:49:03 -0400137 ReleaseProc = nullptr, ReleaseContext = nullptr);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500138
139 /*
Brian Salomon7578f3e2018-03-07 14:39:54 -0500140 * Create a render target proxy that wraps a backend texture
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500141 */
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400142 sk_sp<GrSurfaceProxy> wrapBackendTextureAsRenderTarget(const GrBackendTexture&,
143 GrColorType,
144 GrSurfaceOrigin,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500145 int sampleCnt);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500146
Greg Danielb46add82019-01-02 14:51:29 -0500147 sk_sp<GrRenderTargetProxy> wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo&,
148 const GrVkDrawableInfo&);
149
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400150 using LazyInstantiationKeyMode = GrSurfaceProxy::LazyInstantiationKeyMode;
151 using LazyInstantiationResult = GrSurfaceProxy::LazyInstantiationResult;
152 using LazyInstantiateCallback = GrSurfaceProxy::LazyInstantiateCallback;
Robert Phillipse8fabb22018-02-04 14:33:21 -0500153
Brian Salomon7226c232018-07-30 13:13:17 -0400154 struct TextureInfo {
155 GrMipMapped fMipMapped;
156 GrTextureType fTextureType;
157 };
158
Greg Daniela8d92112018-03-09 12:05:04 -0500159 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
Robert Phillips777707b2018-01-17 11:40:14 -0500160 /**
161 * Creates a texture proxy that will be instantiated by a user-supplied callback during flush.
162 * (Stencil is not supported by this method.) The width and height must either both be greater
163 * than 0 or both less than or equal to zero. A non-positive value is a signal that the width
164 * and height are currently unknown.
165 *
166 * When called, the callback must be able to cleanup any resources that it captured at creation.
167 * It also must support being passed in a null GrResourceProvider. When this happens, the
168 * callback should cleanup any resources it captured and return an empty sk_sp<GrTextureProxy>.
169 */
Greg Daniel4065d452018-11-16 15:43:41 -0500170 sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrBackendFormat&,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400171 const GrSurfaceDesc&, GrRenderable,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600172 int renderTargetSampleCnt, GrSurfaceOrigin,
173 GrMipMapped, GrMipMapsStatus, GrInternalSurfaceFlags,
174 SkBackingFit, SkBudgeted, GrProtected,
175 LazyInstantiationType);
Greg Daniela8d92112018-03-09 12:05:04 -0500176
Greg Daniel4065d452018-11-16 15:43:41 -0500177 sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrBackendFormat&,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400178 const GrSurfaceDesc&, GrRenderable,
179 int renderTargetSampleCnt, GrSurfaceOrigin, GrMipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600180 GrMipMapsStatus, GrInternalSurfaceFlags, SkBackingFit,
181 SkBudgeted, GrProtected);
Greg Daniel2a303902018-02-20 10:25:54 -0500182
Greg Daniel4065d452018-11-16 15:43:41 -0500183 sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrBackendFormat&,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400184 const GrSurfaceDesc&, GrRenderable,
185 int renderTargetSampleCnt, GrSurfaceOrigin, GrMipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600186 GrMipMapsStatus, SkBackingFit, SkBudgeted, GrProtected);
Brian Salomon7226c232018-07-30 13:13:17 -0400187
188 /** A null TextureInfo indicates a non-textureable render target. */
Robert Phillipse8fabb22018-02-04 14:33:21 -0500189 sk_sp<GrRenderTargetProxy> createLazyRenderTargetProxy(LazyInstantiateCallback&&,
Greg Daniel4065d452018-11-16 15:43:41 -0500190 const GrBackendFormat&,
Greg Daniel2a303902018-02-20 10:25:54 -0500191 const GrSurfaceDesc&,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400192 int renderTargetSampleCnt,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500193 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400194 GrInternalSurfaceFlags,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600195 const TextureInfo*, GrMipMapsStatus,
196 SkBackingFit, SkBudgeted, GrProtected,
Greg Danielb085fa92019-03-05 16:55:12 -0500197 bool wrapsVkSecondaryCB);
Robert Phillipse8fabb22018-02-04 14:33:21 -0500198
Chris Dalton4c458b12018-06-16 17:22:59 -0600199 /**
200 * Fully lazy proxies have unspecified width and height. Methods that rely on those values
201 * (e.g., width, height, getBoundsRect) should be avoided.
202 */
Brian Salomone8a766b2019-07-19 14:24:36 -0400203 static sk_sp<GrTextureProxy> MakeFullyLazyProxy(LazyInstantiateCallback&&,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400204 const GrBackendFormat&, GrRenderable,
205 int renderTargetSampleCnt, GrProtected,
206 GrSurfaceOrigin, GrPixelConfig, const GrCaps&);
Chris Dalton4c458b12018-06-16 17:22:59 -0600207
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500208 // 'proxy' is about to be used as a texture src or drawn to. This query can be used to
209 // determine if it is going to need a texture domain or a full clear.
210 static bool IsFunctionallyExact(GrSurfaceProxy* proxy);
211
Robert Phillips427966a2018-12-20 17:20:43 -0500212 enum class InvalidateGPUResource : bool { kNo = false, kYes = true };
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500213
Robert Phillips427966a2018-12-20 17:20:43 -0500214 /*
215 * This method ensures that, if a proxy w/ the supplied unique key exists, it is removed from
216 * the proxy provider's map and its unique key is removed. If 'invalidateSurface' is true, it
217 * will independently ensure that the unique key is removed from any GrGpuResources that may
218 * have it.
219 *
220 * If 'proxy' is provided (as an optimization to stop re-looking it up), its unique key must be
221 * valid and match the provided unique key.
222 *
223 * This method is called if either the proxy attached to the unique key is being deleted
224 * (in which case we don't want it cluttering up the hash table) or the client has indicated
225 * that it will never refer to the unique key again.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500226 */
Chris Dalton2de13dd2019-01-03 15:11:59 -0700227 void processInvalidUniqueKey(const GrUniqueKey&, GrTextureProxy*, InvalidateGPUResource);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500228
Robert Phillipsa41c6852019-02-07 10:44:10 -0500229 // TODO: remove these entry points - it is a bit sloppy to be getting context info from here
230 uint32_t contextID() const;
231 const GrCaps* caps() const;
232 sk_sp<const GrCaps> refCaps() const;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500233
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500234 int numUniqueKeyProxies_TestOnly() const;
235
Robert Phillips0790f8a2018-09-18 13:11:03 -0400236 // This is called on a DDL's proxyprovider when the DDL is finished. The uniquely keyed
237 // proxies need to keep their unique key but cannot hold on to the proxy provider unique
238 // pointer.
239 void orphanAllUniqueKeys();
240 // This is only used by GrContext::releaseResourcesAndAbandonContext()
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500241 void removeAllUniqueKeys();
242
Greg Daniel7e1912a2018-02-08 09:15:33 -0500243 /**
Robert Phillipsa41c6852019-02-07 10:44:10 -0500244 * Does the proxy provider have access to a GrDirectContext? If so, proxies will be
245 * instantiated immediately.
Greg Daniel7e1912a2018-02-08 09:15:33 -0500246 */
Robert Phillipsa41c6852019-02-07 10:44:10 -0500247 bool renderingDirectly() const;
Greg Daniel7e1912a2018-02-08 09:15:33 -0500248
Robert Phillipsa41c6852019-02-07 10:44:10 -0500249#if GR_TEST_UTILS
Chris Daltond004e0b2018-09-27 09:28:03 -0600250 /*
251 * Create a texture proxy that is backed by an instantiated GrSurface.
Brian Salomon4eb38b72019-08-05 12:58:39 -0400252 * TODO: Remove GrColorType. Currently used to infer a GrPixelConfig.
Chris Daltond004e0b2018-09-27 09:28:03 -0600253 */
Brian Salomon4eb38b72019-08-05 12:58:39 -0400254 sk_sp<GrTextureProxy> testingOnly_createInstantiatedProxy(const SkISize& size,
255 GrColorType colorType,
256 const GrBackendFormat& format,
257 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400258 int renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400259 GrSurfaceOrigin origin,
260 SkBackingFit fit,
261 SkBudgeted budgeted,
262 GrProtected isProtected);
263
264 /** Version of above that picks the default format for the color type. */
265 sk_sp<GrTextureProxy> testingOnly_createInstantiatedProxy(const SkISize& size,
266 GrColorType colorType,
267 GrRenderable renderable,
268 int renderTargetSampleCnt,
269 GrSurfaceOrigin origin,
270 SkBackingFit fit,
271 SkBudgeted budgeted,
272 GrProtected isProtected);
273
Brian Salomon2af3e702019-08-11 19:10:31 -0400274 sk_sp<GrTextureProxy> testingOnly_createWrapped(sk_sp<GrTexture>, GrColorType, GrSurfaceOrigin);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500275#endif
Chris Daltond004e0b2018-09-27 09:28:03 -0600276
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500277private:
Robert Phillipsadbe1322018-01-17 13:35:46 -0500278 friend class GrAHardwareBufferImageGenerator; // for createWrapped
Chris Daltond004e0b2018-09-27 09:28:03 -0600279 friend class GrResourceProvider; // for createWrapped
Robert Phillipsadbe1322018-01-17 13:35:46 -0500280
Robert Phillipsa9162df2019-02-11 14:12:03 -0500281 bool isAbandoned() const;
282
Brian Salomon2af3e702019-08-11 19:10:31 -0400283 // GrColorType is used to determine the proxy's texture swizzle.
284 sk_sp<GrTextureProxy> createWrapped(sk_sp<GrTexture> tex, GrColorType, GrSurfaceOrigin origin);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500285
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500286 struct UniquelyKeyedProxyHashTraits {
287 static const GrUniqueKey& GetKey(const GrTextureProxy& p) { return p.getUniqueKey(); }
288
289 static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
290 };
291 typedef SkTDynamicHash<GrTextureProxy, GrUniqueKey, UniquelyKeyedProxyHashTraits> UniquelyKeyedProxyHash;
292
293 // This holds the texture proxies that have unique keys. The resourceCache does not get a ref
294 // on these proxies but they must send a message to the resourceCache when they are deleted.
295 UniquelyKeyedProxyHash fUniquelyKeyedProxies;
296
Robert Phillipsa41c6852019-02-07 10:44:10 -0500297 GrImageContext* fImageContext;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500298};
299
300#endif