blob: 64b9ac43556152368c76cdbd5fd9fc07c9e87f89 [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*);
30
31 ~GrProxyProvider();
32
33 /*
34 * Assigns a unique key to a proxy. The proxy will be findable via this key using
35 * findProxyByUniqueKey(). It is an error if an existing proxy already has a key.
36 */
Robert Phillipsadbe1322018-01-17 13:35:46 -050037 bool assignUniqueKeyToProxy(const GrUniqueKey&, GrTextureProxy*);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050038
39 /*
40 * Sets the unique key of the provided proxy to the unique key of the surface. The surface must
41 * have a valid unique key.
42 */
43 void adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface*);
44
45 /*
46 * Removes a unique key from a proxy. If the proxy has already been instantiated, it will
47 * also remove the unique key from the target GrSurface.
48 */
49 void removeUniqueKeyFromProxy(const GrUniqueKey&, GrTextureProxy*);
50
51 /*
52 * Finds a proxy by unique key.
53 */
54 sk_sp<GrTextureProxy> findProxyByUniqueKey(const GrUniqueKey&, GrSurfaceOrigin);
55
56 /*
57 * Finds a proxy by unique key or creates a new one that wraps a resource matching the unique
58 * key.
59 */
60 sk_sp<GrTextureProxy> findOrCreateProxyByUniqueKey(const GrUniqueKey&, GrSurfaceOrigin);
61
62 /*
Robert Phillips0bd24dc2018-01-16 08:06:32 -050063 * Create a texture proxy that is backed by an instantiated GrSurface. This is almost entirely
64 * used by Skia's testing code.
65 * DDL TODO: remove the remaining Skia-internal use of this method and make it truly
66 * testing-only.
Robert Phillips1afd4cd2018-01-08 13:40:32 -050067 */
Brian Salomon2a4f9832018-03-03 22:43:43 -050068 sk_sp<GrTextureProxy> createInstantiatedProxy(const GrSurfaceDesc&, GrSurfaceOrigin,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040069 SkBackingFit, SkBudgeted,
70 GrSurfaceDescFlags = kNone_GrSurfaceFlags);
Robert Phillips0bd24dc2018-01-16 08:06:32 -050071
72 /*
73 * Create an un-mipmapped texture proxy with data.
74 * DDL TODO: need to refine ownership semantics of 'srcData' if we're in completely
75 * deferred mode
76 */
Brian Salomon58389b92018-03-07 13:01:25 -050077 sk_sp<GrTextureProxy> createTextureProxy(const GrSurfaceDesc&, SkBudgeted, const void* srcData,
78 size_t rowBytes);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050079
80 /*
Greg Daniel9d86f1d2018-01-29 09:33:59 -050081 * Create an un-mipmapped texture proxy with data. The SkImage must be a raster backend image.
82 * Since the SkImage is ref counted, we simply take a ref on it to keep the data alive until we
83 * actually upload the data to the gpu.
84 */
85 sk_sp<GrTextureProxy> createTextureProxy(sk_sp<SkImage> srcImage,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040086 GrSurfaceDescFlags descFlags,
Greg Daniel9d86f1d2018-01-29 09:33:59 -050087 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -050088 SkBudgeted budgeted,
89 SkBackingFit fit);
Greg Daniel9d86f1d2018-01-29 09:33:59 -050090
91 /*
Robert Phillips0bd24dc2018-01-16 08:06:32 -050092 * Create a mipmapped texture proxy without any data.
93 *
94 * Like the call above but there are no texels to upload. A texture proxy is returned that
95 * simply has space allocated for the mips. We will allocated the full amount of mip levels
96 * based on the width and height in the GrSurfaceDesc.
97 */
Brian Salomon2a4f9832018-03-03 22:43:43 -050098 sk_sp<GrTextureProxy> createMipMapProxy(const GrSurfaceDesc&, GrSurfaceOrigin, SkBudgeted);
Robert Phillips0bd24dc2018-01-16 08:06:32 -050099
100 /*
Greg Daniela4ead652018-02-07 10:21:48 -0500101 * Creates a new mipmapped texture proxy for the bitmap with mip levels generated by the cpu.
102 */
Brian Osman2b23c4b2018-06-01 12:25:08 -0400103 sk_sp<GrTextureProxy> createMipMapProxyFromBitmap(const SkBitmap& bitmap);
Greg Daniela4ead652018-02-07 10:21:48 -0500104
105 /*
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500106 * Create a GrSurfaceProxy without any data.
107 */
Brian Salomon2a4f9832018-03-03 22:43:43 -0500108 sk_sp<GrTextureProxy> createProxy(const GrSurfaceDesc&, GrSurfaceOrigin, GrMipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400109 SkBackingFit, SkBudgeted, GrInternalSurfaceFlags);
Greg Danielf6f7b672018-02-15 13:06:26 -0500110
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400111 sk_sp<GrTextureProxy> createProxy(
112 const GrSurfaceDesc& desc, GrSurfaceOrigin origin,
113 SkBackingFit fit, SkBudgeted budgeted,
114 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone) {
115 return this->createProxy(desc, origin, GrMipMapped::kNo, fit, budgeted, surfaceFlags);
Greg Danielf6f7b672018-02-15 13:06:26 -0500116 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500117
Robert Phillipsadbe1322018-01-17 13:35:46 -0500118 // These match the definitions in SkImage & GrTexture.h, for whence they came
119 typedef void* ReleaseContext;
120 typedef void (*ReleaseProc)(ReleaseContext);
121
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500122 /*
123 * Create a texture proxy that wraps a (non-renderable) backend texture.
124 */
Brian Salomon7578f3e2018-03-07 14:39:54 -0500125 sk_sp<GrTextureProxy> wrapBackendTexture(const GrBackendTexture&, GrSurfaceOrigin,
126 GrWrapOwnership = kBorrow_GrWrapOwnership,
127 ReleaseProc = nullptr, ReleaseContext = nullptr);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500128
129 /*
130 * Create a texture proxy that wraps a backend texture and is both texture-able and renderable
131 */
Brian Salomon7578f3e2018-03-07 14:39:54 -0500132 sk_sp<GrTextureProxy> wrapRenderableBackendTexture(const GrBackendTexture&,
133 GrSurfaceOrigin,
Brian Salomon02bd2952018-03-07 15:20:21 -0500134 int sampleCnt,
135 GrWrapOwnership = kBorrow_GrWrapOwnership);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500136
137 /*
138 * Create a render target proxy that wraps a backend rendertarget
139 */
Brian Salomon7578f3e2018-03-07 14:39:54 -0500140 sk_sp<GrSurfaceProxy> wrapBackendRenderTarget(const GrBackendRenderTarget&, GrSurfaceOrigin);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500141
142 /*
Brian Salomon7578f3e2018-03-07 14:39:54 -0500143 * Create a render target proxy that wraps a backend texture
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500144 */
Kevin Lubickb5502b22018-03-12 10:17:06 -0400145 sk_sp<GrSurfaceProxy> wrapBackendTextureAsRenderTarget(const GrBackendTexture& backendTex,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500146 GrSurfaceOrigin origin,
147 int sampleCnt);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500148
Robert Phillipsce5209a2018-02-13 11:13:51 -0500149 using LazyInstantiateCallback = std::function<sk_sp<GrSurface>(GrResourceProvider*)>;
Robert Phillipse8fabb22018-02-04 14:33:21 -0500150 enum class Textureable : bool {
151 kNo = false,
152 kYes = true
153 };
154
Robert Phillips777707b2018-01-17 11:40:14 -0500155 enum class Renderable : bool {
156 kNo = false,
157 kYes = true
158 };
159
Greg Daniela8d92112018-03-09 12:05:04 -0500160 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
Robert Phillips777707b2018-01-17 11:40:14 -0500161 /**
162 * Creates a texture proxy that will be instantiated by a user-supplied callback during flush.
163 * (Stencil is not supported by this method.) The width and height must either both be greater
164 * than 0 or both less than or equal to zero. A non-positive value is a signal that the width
165 * and height are currently unknown.
166 *
167 * When called, the callback must be able to cleanup any resources that it captured at creation.
168 * It also must support being passed in a null GrResourceProvider. When this happens, the
169 * callback should cleanup any resources it captured and return an empty sk_sp<GrTextureProxy>.
170 */
171 sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrSurfaceDesc&,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400172 GrSurfaceOrigin, GrMipMapped, GrInternalSurfaceFlags,
Greg Daniela8d92112018-03-09 12:05:04 -0500173 SkBackingFit, SkBudgeted, LazyInstantiationType);
174
175 sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrSurfaceDesc&,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400176 GrSurfaceOrigin, GrMipMapped, GrInternalSurfaceFlags,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500177 SkBackingFit, SkBudgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500178
179 sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrSurfaceDesc&,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500180 GrSurfaceOrigin, GrMipMapped, SkBackingFit, SkBudgeted);
Robert Phillipse8fabb22018-02-04 14:33:21 -0500181 sk_sp<GrRenderTargetProxy> createLazyRenderTargetProxy(LazyInstantiateCallback&&,
Greg Daniel2a303902018-02-20 10:25:54 -0500182 const GrSurfaceDesc&,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500183 GrSurfaceOrigin origin,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400184 GrInternalSurfaceFlags, Textureable,
Robert Phillipse8fabb22018-02-04 14:33:21 -0500185 GrMipMapped, SkBackingFit, SkBudgeted);
186
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 */
191 static sk_sp<GrTextureProxy> MakeFullyLazyProxy(LazyInstantiateCallback&&, Renderable,
192 GrSurfaceOrigin, GrPixelConfig, const GrCaps&);
193
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500194 // 'proxy' is about to be used as a texture src or drawn to. This query can be used to
195 // determine if it is going to need a texture domain or a full clear.
196 static bool IsFunctionallyExact(GrSurfaceProxy* proxy);
197
198 /**
199 * Either the proxy attached to the unique key is being deleted (in which case we
200 * don't want it cluttering up the hash table) or the client has indicated that
201 * it will never refer to the unique key again. In either case, remove the key
202 * from the hash table.
203 * Note: this does not, by itself, alter unique key attached to the underlying GrTexture.
204 */
205 void processInvalidProxyUniqueKey(const GrUniqueKey&);
206
207 /**
208 * Same as above, but you must pass in a GrTextureProxy to save having to search for it. The
209 * GrUniqueKey of the proxy must be valid and it must match the passed in key. This function
210 * also gives the option to invalidate the GrUniqueKey on the underlying GrTexture.
211 */
212 void processInvalidProxyUniqueKey(const GrUniqueKey&, GrTextureProxy*, bool invalidateSurface);
213
214 const GrCaps* caps() const { return fCaps.get(); }
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500215 sk_sp<const GrCaps> refCaps() const { return fCaps; }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500216
217 void abandon() {
218 fResourceCache = nullptr;
219 fResourceProvider = nullptr;
Robert Phillips4d120512018-01-19 13:22:07 -0500220 fAbandoned = true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500221 }
222
223 bool isAbandoned() const {
Robert Phillips4d120512018-01-19 13:22:07 -0500224#ifdef SK_DEBUG
225 if (fAbandoned) {
226 SkASSERT(!fResourceCache && !fResourceProvider);
227 }
228#endif
229 return fAbandoned;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500230 }
231
232 int numUniqueKeyProxies_TestOnly() const;
233
234 void removeAllUniqueKeys();
235
Greg Daniel7e1912a2018-02-08 09:15:33 -0500236 /**
Robert Phillips5c4b33b2018-03-20 16:23:08 -0400237 * Are we currently recording a DDL?
Greg Daniel7e1912a2018-02-08 09:15:33 -0500238 */
Robert Phillips5c4b33b2018-03-20 16:23:08 -0400239 bool recordingDDL() const { return !SkToBool(fResourceProvider); }
Greg Daniel7e1912a2018-02-08 09:15:33 -0500240
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500241private:
Robert Phillipsadbe1322018-01-17 13:35:46 -0500242 friend class GrAHardwareBufferImageGenerator; // for createWrapped
243
244 sk_sp<GrTextureProxy> createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin);
245
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500246 struct UniquelyKeyedProxyHashTraits {
247 static const GrUniqueKey& GetKey(const GrTextureProxy& p) { return p.getUniqueKey(); }
248
249 static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
250 };
251 typedef SkTDynamicHash<GrTextureProxy, GrUniqueKey, UniquelyKeyedProxyHashTraits> UniquelyKeyedProxyHash;
252
253 // This holds the texture proxies that have unique keys. The resourceCache does not get a ref
254 // on these proxies but they must send a message to the resourceCache when they are deleted.
255 UniquelyKeyedProxyHash fUniquelyKeyedProxies;
256
257 GrResourceProvider* fResourceProvider;
258 GrResourceCache* fResourceCache;
Robert Phillips4d120512018-01-19 13:22:07 -0500259 bool fAbandoned;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500260 sk_sp<const GrCaps> fCaps;
261
262 // In debug builds we guard against improper thread handling
263 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
264};
265
266#endif