blob: 838eac8d7a20d4475c25a703ef3ab7aa4c8f510e [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
55 * key.
56 */
57 sk_sp<GrTextureProxy> findOrCreateProxyByUniqueKey(const GrUniqueKey&, GrSurfaceOrigin);
58
59 /*
Greg Daniel9d86f1d2018-01-29 09:33:59 -050060 * Create an un-mipmapped texture proxy with data. The SkImage must be a raster backend image.
61 * Since the SkImage is ref counted, we simply take a ref on it to keep the data alive until we
62 * actually upload the data to the gpu.
63 */
Chris Daltond004e0b2018-09-27 09:28:03 -060064 sk_sp<GrTextureProxy> createTextureProxy(
Brian Salomonf2c2ba92019-07-17 09:59:59 -040065 sk_sp<SkImage> srcImage, GrRenderable, int sampleCnt, SkBudgeted, SkBackingFit,
Chris Daltond004e0b2018-09-27 09:28:03 -060066 GrInternalSurfaceFlags = GrInternalSurfaceFlags::kNone);
Greg Daniel9d86f1d2018-01-29 09:33:59 -050067
68 /*
Robert Phillips0bd24dc2018-01-16 08:06:32 -050069 * Create a mipmapped texture proxy without any data.
70 *
71 * Like the call above but there are no texels to upload. A texture proxy is returned that
72 * simply has space allocated for the mips. We will allocated the full amount of mip levels
73 * based on the width and height in the GrSurfaceDesc.
74 */
Greg Daniel4065d452018-11-16 15:43:41 -050075 sk_sp<GrTextureProxy> createMipMapProxy(const GrBackendFormat&, const GrSurfaceDesc&,
Brian Salomone8a766b2019-07-19 14:24:36 -040076 GrRenderable, GrSurfaceOrigin, SkBudgeted, GrProtected);
Robert Phillips0bd24dc2018-01-16 08:06:32 -050077
78 /*
Brian Osmande496652019-03-22 13:42:33 -040079 * Creates a new texture proxy for the bitmap, optionally with mip levels generated by the cpu.
Greg Daniela4ead652018-02-07 10:21:48 -050080 */
Brian Osmande496652019-03-22 13:42:33 -040081 sk_sp<GrTextureProxy> createProxyFromBitmap(const SkBitmap& bitmap, GrMipMapped);
Greg Daniela4ead652018-02-07 10:21:48 -050082
83 /*
Robert Phillips0bd24dc2018-01-16 08:06:32 -050084 * Create a GrSurfaceProxy without any data.
85 */
Brian Salomonf2c2ba92019-07-17 09:59:59 -040086 sk_sp<GrTextureProxy> createProxy(const GrBackendFormat&, const GrSurfaceDesc&, GrRenderable,
87 GrSurfaceOrigin, GrMipMapped, SkBackingFit, SkBudgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -040088 GrProtected, GrInternalSurfaceFlags);
Greg Danielf6f7b672018-02-15 13:06:26 -050089
Robert Phillipsfe0253f2018-03-16 16:47:25 -040090 sk_sp<GrTextureProxy> createProxy(
Brian Salomonf2c2ba92019-07-17 09:59:59 -040091 const GrBackendFormat& format, const GrSurfaceDesc& desc, GrRenderable renderable,
Brian Salomone8a766b2019-07-19 14:24:36 -040092 GrSurfaceOrigin origin, SkBackingFit fit, SkBudgeted budgeted, GrProtected isProtected,
Brian Salomonf2c2ba92019-07-17 09:59:59 -040093 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone) {
94 return this->createProxy(format, desc, renderable, origin, GrMipMapped::kNo, fit, budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -040095 isProtected, surfaceFlags);
Greg Danielf6f7b672018-02-15 13:06:26 -050096 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -050097
Jim Van Verthee06b332019-01-18 10:36:32 -050098 /*
Brian Salomonbb8dde82019-06-27 10:52:13 -040099 * Create a texture proxy from compressed texture data.
Jim Van Verthee06b332019-01-18 10:36:32 -0500100 */
Brian Salomonbb8dde82019-06-27 10:52:13 -0400101 sk_sp<GrTextureProxy> createCompressedTextureProxy(int width, int height, SkBudgeted budgeted,
102 SkImage::CompressionType compressionType,
103 sk_sp<SkData> data);
Jim Van Verthee06b332019-01-18 10:36:32 -0500104
Robert Phillipsadbe1322018-01-17 13:35:46 -0500105 // These match the definitions in SkImage & GrTexture.h, for whence they came
106 typedef void* ReleaseContext;
107 typedef void (*ReleaseProc)(ReleaseContext);
108
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500109 /*
Brian Salomonc67c31c2018-12-06 10:00:03 -0500110 * Create a texture proxy that wraps a (non-renderable) backend texture. GrIOType must be
111 * kRead or kRW.
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500112 */
Robert Phillipsdd399802019-07-18 12:28:00 +0000113 sk_sp<GrTextureProxy> wrapBackendTexture(const GrBackendTexture&, GrSurfaceOrigin,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500114 GrWrapOwnership, GrWrapCacheable, GrIOType,
115 ReleaseProc = nullptr, ReleaseContext = nullptr);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500116
117 /*
118 * Create a texture proxy that wraps a backend texture and is both texture-able and renderable
119 */
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500120 sk_sp<GrTextureProxy> wrapRenderableBackendTexture(const GrBackendTexture&, GrSurfaceOrigin,
Robert Phillips0902c982019-07-16 07:47:56 -0400121 int sampleCnt, GrColorType,
122 GrWrapOwnership, GrWrapCacheable,
123 ReleaseProc = nullptr,
Brian Salomon6c1205a2019-06-14 11:49:03 -0400124 ReleaseContext = nullptr);
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 */
Robert Phillipsdd399802019-07-18 12:28:00 +0000129 sk_sp<GrSurfaceProxy> wrapBackendRenderTarget(const GrBackendRenderTarget&, GrSurfaceOrigin,
Brian Salomon6c1205a2019-06-14 11:49:03 -0400130 ReleaseProc = nullptr, ReleaseContext = nullptr);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500131
132 /*
Brian Salomon7578f3e2018-03-07 14:39:54 -0500133 * Create a render target proxy that wraps a backend texture
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500134 */
Robert Phillipsdd399802019-07-18 12:28:00 +0000135 sk_sp<GrSurfaceProxy> wrapBackendTextureAsRenderTarget(const GrBackendTexture& backendTex,
136 GrSurfaceOrigin origin,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500137 int sampleCnt);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500138
Greg Danielb46add82019-01-02 14:51:29 -0500139 sk_sp<GrRenderTargetProxy> wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo&,
140 const GrVkDrawableInfo&);
141
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400142 using LazyInstantiationKeyMode = GrSurfaceProxy::LazyInstantiationKeyMode;
143 using LazyInstantiationResult = GrSurfaceProxy::LazyInstantiationResult;
144 using LazyInstantiateCallback = GrSurfaceProxy::LazyInstantiateCallback;
Robert Phillipse8fabb22018-02-04 14:33:21 -0500145
Robert Phillips777707b2018-01-17 11:40:14 -0500146 enum class Renderable : bool {
147 kNo = false,
148 kYes = true
149 };
150
Brian Salomon7226c232018-07-30 13:13:17 -0400151 struct TextureInfo {
152 GrMipMapped fMipMapped;
153 GrTextureType fTextureType;
154 };
155
Greg Daniela8d92112018-03-09 12:05:04 -0500156 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
Robert Phillips777707b2018-01-17 11:40:14 -0500157 /**
158 * Creates a texture proxy that will be instantiated by a user-supplied callback during flush.
159 * (Stencil is not supported by this method.) The width and height must either both be greater
160 * than 0 or both less than or equal to zero. A non-positive value is a signal that the width
161 * and height are currently unknown.
162 *
163 * When called, the callback must be able to cleanup any resources that it captured at creation.
164 * It also must support being passed in a null GrResourceProvider. When this happens, the
165 * callback should cleanup any resources it captured and return an empty sk_sp<GrTextureProxy>.
166 */
Greg Daniel4065d452018-11-16 15:43:41 -0500167 sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrBackendFormat&,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400168 const GrSurfaceDesc&, GrRenderable, GrSurfaceOrigin,
169 GrMipMapped, GrInternalSurfaceFlags, SkBackingFit,
Brian Salomone8a766b2019-07-19 14:24:36 -0400170 SkBudgeted, GrProtected, LazyInstantiationType);
Greg Daniela8d92112018-03-09 12:05:04 -0500171
Greg Daniel4065d452018-11-16 15:43:41 -0500172 sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrBackendFormat&,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400173 const GrSurfaceDesc&, GrRenderable, GrSurfaceOrigin,
174 GrMipMapped, GrInternalSurfaceFlags, SkBackingFit,
Brian Salomone8a766b2019-07-19 14:24:36 -0400175 SkBudgeted, GrProtected);
Greg Daniel2a303902018-02-20 10:25:54 -0500176
Greg Daniel4065d452018-11-16 15:43:41 -0500177 sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrBackendFormat&,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400178 const GrSurfaceDesc&, GrRenderable, GrSurfaceOrigin,
Brian Salomone8a766b2019-07-19 14:24:36 -0400179 GrMipMapped, SkBackingFit, SkBudgeted, GrProtected);
Brian Salomon7226c232018-07-30 13:13:17 -0400180
181 /** A null TextureInfo indicates a non-textureable render target. */
Robert Phillipse8fabb22018-02-04 14:33:21 -0500182 sk_sp<GrRenderTargetProxy> createLazyRenderTargetProxy(LazyInstantiateCallback&&,
Greg Daniel4065d452018-11-16 15:43:41 -0500183 const GrBackendFormat&,
Greg Daniel2a303902018-02-20 10:25:54 -0500184 const GrSurfaceDesc&,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500185 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400186 GrInternalSurfaceFlags,
187 const TextureInfo*,
188 SkBackingFit,
Greg Danielb085fa92019-03-05 16:55:12 -0500189 SkBudgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400190 GrProtected,
Greg Danielb085fa92019-03-05 16:55:12 -0500191 bool wrapsVkSecondaryCB);
Robert Phillipse8fabb22018-02-04 14:33:21 -0500192
Chris Dalton4c458b12018-06-16 17:22:59 -0600193 /**
194 * Fully lazy proxies have unspecified width and height. Methods that rely on those values
195 * (e.g., width, height, getBoundsRect) should be avoided.
196 */
Brian Salomone8a766b2019-07-19 14:24:36 -0400197 static sk_sp<GrTextureProxy> MakeFullyLazyProxy(LazyInstantiateCallback&&,
198 const GrBackendFormat&, Renderable, GrProtected,
199 GrSurfaceOrigin, GrPixelConfig, const GrCaps&,
200 int sampleCnt = 1);
Chris Dalton4c458b12018-06-16 17:22:59 -0600201
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500202 // 'proxy' is about to be used as a texture src or drawn to. This query can be used to
203 // determine if it is going to need a texture domain or a full clear.
204 static bool IsFunctionallyExact(GrSurfaceProxy* proxy);
205
Robert Phillips427966a2018-12-20 17:20:43 -0500206 enum class InvalidateGPUResource : bool { kNo = false, kYes = true };
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500207
Robert Phillips427966a2018-12-20 17:20:43 -0500208 /*
209 * This method ensures that, if a proxy w/ the supplied unique key exists, it is removed from
210 * the proxy provider's map and its unique key is removed. If 'invalidateSurface' is true, it
211 * will independently ensure that the unique key is removed from any GrGpuResources that may
212 * have it.
213 *
214 * If 'proxy' is provided (as an optimization to stop re-looking it up), its unique key must be
215 * valid and match the provided unique key.
216 *
217 * This method is called if either the proxy attached to the unique key is being deleted
218 * (in which case we don't want it cluttering up the hash table) or the client has indicated
219 * that it will never refer to the unique key again.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500220 */
Chris Dalton2de13dd2019-01-03 15:11:59 -0700221 void processInvalidUniqueKey(const GrUniqueKey&, GrTextureProxy*, InvalidateGPUResource);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500222
Robert Phillipsa41c6852019-02-07 10:44:10 -0500223 // TODO: remove these entry points - it is a bit sloppy to be getting context info from here
224 uint32_t contextID() const;
225 const GrCaps* caps() const;
226 sk_sp<const GrCaps> refCaps() const;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500227
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500228 int numUniqueKeyProxies_TestOnly() const;
229
Robert Phillips0790f8a2018-09-18 13:11:03 -0400230 // This is called on a DDL's proxyprovider when the DDL is finished. The uniquely keyed
231 // proxies need to keep their unique key but cannot hold on to the proxy provider unique
232 // pointer.
233 void orphanAllUniqueKeys();
234 // This is only used by GrContext::releaseResourcesAndAbandonContext()
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500235 void removeAllUniqueKeys();
236
Greg Daniel7e1912a2018-02-08 09:15:33 -0500237 /**
Robert Phillipsa41c6852019-02-07 10:44:10 -0500238 * Does the proxy provider have access to a GrDirectContext? If so, proxies will be
239 * instantiated immediately.
Greg Daniel7e1912a2018-02-08 09:15:33 -0500240 */
Robert Phillipsa41c6852019-02-07 10:44:10 -0500241 bool renderingDirectly() const;
Greg Daniel7e1912a2018-02-08 09:15:33 -0500242
Robert Phillipsa41c6852019-02-07 10:44:10 -0500243#if GR_TEST_UTILS
Chris Daltond004e0b2018-09-27 09:28:03 -0600244 /*
245 * Create a texture proxy that is backed by an instantiated GrSurface.
246 */
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400247 sk_sp<GrTextureProxy> testingOnly_createInstantiatedProxy(const GrSurfaceDesc&, GrRenderable,
248 GrSurfaceOrigin, SkBackingFit,
Brian Salomone8a766b2019-07-19 14:24:36 -0400249 SkBudgeted, GrProtected);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500250 sk_sp<GrTextureProxy> testingOnly_createWrapped(sk_sp<GrTexture>, GrSurfaceOrigin);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500251#endif
Chris Daltond004e0b2018-09-27 09:28:03 -0600252
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500253private:
Robert Phillipsadbe1322018-01-17 13:35:46 -0500254 friend class GrAHardwareBufferImageGenerator; // for createWrapped
Chris Daltond004e0b2018-09-27 09:28:03 -0600255 friend class GrResourceProvider; // for createWrapped
Robert Phillipsadbe1322018-01-17 13:35:46 -0500256
Robert Phillipsa9162df2019-02-11 14:12:03 -0500257 bool isAbandoned() const;
258
Robert Phillipsadbe1322018-01-17 13:35:46 -0500259 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
Robert Phillipsa41c6852019-02-07 10:44:10 -0500272 GrImageContext* fImageContext;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500273};
274
275#endif