blob: 26dc6e9682fdff60bdb6aa35807b8d5d0a7c7acf [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 */
50 void removeUniqueKeyFromProxy(const GrUniqueKey&, GrTextureProxy*);
51
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 */
Brian Salomon2a4f9832018-03-03 22:43:43 -050079 sk_sp<GrTextureProxy> createMipMapProxy(const GrSurfaceDesc&, GrSurfaceOrigin, SkBudgeted);
Robert Phillips0bd24dc2018-01-16 08:06:32 -050080
81 /*
Greg Daniela4ead652018-02-07 10:21:48 -050082 * Creates a new mipmapped texture proxy for the bitmap with mip levels generated by the cpu.
83 */
Brian Osman2b23c4b2018-06-01 12:25:08 -040084 sk_sp<GrTextureProxy> createMipMapProxyFromBitmap(const SkBitmap& bitmap);
Greg Daniela4ead652018-02-07 10:21:48 -050085
86 /*
Robert Phillips0bd24dc2018-01-16 08:06:32 -050087 * Create a GrSurfaceProxy without any data.
88 */
Brian Salomon2a4f9832018-03-03 22:43:43 -050089 sk_sp<GrTextureProxy> createProxy(const GrSurfaceDesc&, GrSurfaceOrigin, GrMipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040090 SkBackingFit, SkBudgeted, GrInternalSurfaceFlags);
Greg Danielf6f7b672018-02-15 13:06:26 -050091
Robert Phillipsfe0253f2018-03-16 16:47:25 -040092 sk_sp<GrTextureProxy> createProxy(
93 const GrSurfaceDesc& desc, GrSurfaceOrigin origin,
94 SkBackingFit fit, SkBudgeted budgeted,
95 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone) {
96 return this->createProxy(desc, origin, GrMipMapped::kNo, fit, budgeted, surfaceFlags);
Greg Danielf6f7b672018-02-15 13:06:26 -050097 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -050098
Robert Phillipsadbe1322018-01-17 13:35:46 -050099 // These match the definitions in SkImage & GrTexture.h, for whence they came
100 typedef void* ReleaseContext;
101 typedef void (*ReleaseProc)(ReleaseContext);
102
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500103 /*
104 * Create a texture proxy that wraps a (non-renderable) backend texture.
105 */
Brian Salomon7578f3e2018-03-07 14:39:54 -0500106 sk_sp<GrTextureProxy> wrapBackendTexture(const GrBackendTexture&, GrSurfaceOrigin,
107 GrWrapOwnership = kBorrow_GrWrapOwnership,
108 ReleaseProc = nullptr, ReleaseContext = nullptr);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500109
110 /*
111 * Create a texture proxy that wraps a backend texture and is both texture-able and renderable
112 */
Brian Salomon7578f3e2018-03-07 14:39:54 -0500113 sk_sp<GrTextureProxy> wrapRenderableBackendTexture(const GrBackendTexture&,
114 GrSurfaceOrigin,
Brian Salomon02bd2952018-03-07 15:20:21 -0500115 int sampleCnt,
116 GrWrapOwnership = kBorrow_GrWrapOwnership);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500117
118 /*
119 * Create a render target proxy that wraps a backend rendertarget
120 */
Brian Salomon7578f3e2018-03-07 14:39:54 -0500121 sk_sp<GrSurfaceProxy> wrapBackendRenderTarget(const GrBackendRenderTarget&, GrSurfaceOrigin);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500122
123 /*
Brian Salomon7578f3e2018-03-07 14:39:54 -0500124 * Create a render target proxy that wraps a backend texture
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500125 */
Kevin Lubickb5502b22018-03-12 10:17:06 -0400126 sk_sp<GrSurfaceProxy> wrapBackendTextureAsRenderTarget(const GrBackendTexture& backendTex,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500127 GrSurfaceOrigin origin,
128 int sampleCnt);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500129
Robert Phillipsce5209a2018-02-13 11:13:51 -0500130 using LazyInstantiateCallback = std::function<sk_sp<GrSurface>(GrResourceProvider*)>;
Robert Phillipse8fabb22018-02-04 14:33:21 -0500131
Robert Phillips777707b2018-01-17 11:40:14 -0500132 enum class Renderable : bool {
133 kNo = false,
134 kYes = true
135 };
136
Brian Salomon7226c232018-07-30 13:13:17 -0400137 struct TextureInfo {
138 GrMipMapped fMipMapped;
139 GrTextureType fTextureType;
140 };
141
Greg Daniela8d92112018-03-09 12:05:04 -0500142 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
Robert Phillips777707b2018-01-17 11:40:14 -0500143 /**
144 * Creates a texture proxy that will be instantiated by a user-supplied callback during flush.
145 * (Stencil is not supported by this method.) The width and height must either both be greater
146 * than 0 or both less than or equal to zero. A non-positive value is a signal that the width
147 * and height are currently unknown.
148 *
149 * When called, the callback must be able to cleanup any resources that it captured at creation.
150 * It also must support being passed in a null GrResourceProvider. When this happens, the
151 * callback should cleanup any resources it captured and return an empty sk_sp<GrTextureProxy>.
152 */
153 sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrSurfaceDesc&,
Brian Salomon7226c232018-07-30 13:13:17 -0400154 GrSurfaceOrigin, GrMipMapped, GrTextureType,
155 GrInternalSurfaceFlags, SkBackingFit, SkBudgeted,
156 LazyInstantiationType);
Greg Daniela8d92112018-03-09 12:05:04 -0500157
158 sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrSurfaceDesc&,
Brian Salomon7226c232018-07-30 13:13:17 -0400159 GrSurfaceOrigin, GrMipMapped, GrTextureType,
160 GrInternalSurfaceFlags, SkBackingFit, SkBudgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500161
162 sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrSurfaceDesc&,
Brian Salomon7226c232018-07-30 13:13:17 -0400163 GrSurfaceOrigin, GrMipMapped, GrTextureType, SkBackingFit,
164 SkBudgeted);
165
166 /** A null TextureInfo indicates a non-textureable render target. */
Robert Phillipse8fabb22018-02-04 14:33:21 -0500167 sk_sp<GrRenderTargetProxy> createLazyRenderTargetProxy(LazyInstantiateCallback&&,
Greg Daniel2a303902018-02-20 10:25:54 -0500168 const GrSurfaceDesc&,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500169 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400170 GrInternalSurfaceFlags,
171 const TextureInfo*,
172 SkBackingFit,
173 SkBudgeted);
Robert Phillipse8fabb22018-02-04 14:33:21 -0500174
Chris Dalton4c458b12018-06-16 17:22:59 -0600175 /**
176 * Fully lazy proxies have unspecified width and height. Methods that rely on those values
177 * (e.g., width, height, getBoundsRect) should be avoided.
178 */
179 static sk_sp<GrTextureProxy> MakeFullyLazyProxy(LazyInstantiateCallback&&, Renderable,
180 GrSurfaceOrigin, GrPixelConfig, const GrCaps&);
181
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500182 // 'proxy' is about to be used as a texture src or drawn to. This query can be used to
183 // determine if it is going to need a texture domain or a full clear.
184 static bool IsFunctionallyExact(GrSurfaceProxy* proxy);
185
186 /**
187 * Either the proxy attached to the unique key is being deleted (in which case we
188 * don't want it cluttering up the hash table) or the client has indicated that
189 * it will never refer to the unique key again. In either case, remove the key
190 * from the hash table.
191 * Note: this does not, by itself, alter unique key attached to the underlying GrTexture.
192 */
193 void processInvalidProxyUniqueKey(const GrUniqueKey&);
194
195 /**
196 * Same as above, but you must pass in a GrTextureProxy to save having to search for it. The
197 * GrUniqueKey of the proxy must be valid and it must match the passed in key. This function
198 * also gives the option to invalidate the GrUniqueKey on the underlying GrTexture.
199 */
200 void processInvalidProxyUniqueKey(const GrUniqueKey&, GrTextureProxy*, bool invalidateSurface);
201
Brian Salomon238069b2018-07-11 15:58:57 -0400202 uint32_t contextUniqueID() const { return fContextUniqueID; }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500203 const GrCaps* caps() const { return fCaps.get(); }
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500204 sk_sp<const GrCaps> refCaps() const { return fCaps; }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500205
206 void abandon() {
207 fResourceCache = nullptr;
208 fResourceProvider = nullptr;
Robert Phillips4d120512018-01-19 13:22:07 -0500209 fAbandoned = true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500210 }
211
212 bool isAbandoned() const {
Robert Phillips4d120512018-01-19 13:22:07 -0500213#ifdef SK_DEBUG
214 if (fAbandoned) {
215 SkASSERT(!fResourceCache && !fResourceProvider);
216 }
217#endif
218 return fAbandoned;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500219 }
220
221 int numUniqueKeyProxies_TestOnly() const;
222
Robert Phillips0790f8a2018-09-18 13:11:03 -0400223 // This is called on a DDL's proxyprovider when the DDL is finished. The uniquely keyed
224 // proxies need to keep their unique key but cannot hold on to the proxy provider unique
225 // pointer.
226 void orphanAllUniqueKeys();
227 // This is only used by GrContext::releaseResourcesAndAbandonContext()
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500228 void removeAllUniqueKeys();
229
Greg Daniel7e1912a2018-02-08 09:15:33 -0500230 /**
Robert Phillips5c4b33b2018-03-20 16:23:08 -0400231 * Are we currently recording a DDL?
Greg Daniel7e1912a2018-02-08 09:15:33 -0500232 */
Robert Phillips5c4b33b2018-03-20 16:23:08 -0400233 bool recordingDDL() const { return !SkToBool(fResourceProvider); }
Greg Daniel7e1912a2018-02-08 09:15:33 -0500234
Chris Daltond004e0b2018-09-27 09:28:03 -0600235 /*
236 * Create a texture proxy that is backed by an instantiated GrSurface.
237 */
238 sk_sp<GrTextureProxy> testingOnly_createInstantiatedProxy(const GrSurfaceDesc&, GrSurfaceOrigin,
239 SkBackingFit, SkBudgeted);
240
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500241private:
Robert Phillipsadbe1322018-01-17 13:35:46 -0500242 friend class GrAHardwareBufferImageGenerator; // for createWrapped
Chris Daltond004e0b2018-09-27 09:28:03 -0600243 friend class GrResourceProvider; // for createWrapped
Robert Phillipsadbe1322018-01-17 13:35:46 -0500244
245 sk_sp<GrTextureProxy> createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin);
246
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500247 struct UniquelyKeyedProxyHashTraits {
248 static const GrUniqueKey& GetKey(const GrTextureProxy& p) { return p.getUniqueKey(); }
249
250 static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
251 };
252 typedef SkTDynamicHash<GrTextureProxy, GrUniqueKey, UniquelyKeyedProxyHashTraits> UniquelyKeyedProxyHash;
253
254 // This holds the texture proxies that have unique keys. The resourceCache does not get a ref
255 // on these proxies but they must send a message to the resourceCache when they are deleted.
256 UniquelyKeyedProxyHash fUniquelyKeyedProxies;
257
258 GrResourceProvider* fResourceProvider;
259 GrResourceCache* fResourceCache;
Robert Phillips4d120512018-01-19 13:22:07 -0500260 bool fAbandoned;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500261 sk_sp<const GrCaps> fCaps;
Brian Salomon238069b2018-07-11 15:58:57 -0400262 // If this provider is owned by a DDLContext then this is the DirectContext's ID.
263 uint32_t fContextUniqueID;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500264
265 // In debug builds we guard against improper thread handling
266 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
267};
268
269#endif