blob: 2f2eeb1113d16319735407e602c6c8123119b31d [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,
69 SkBackingFit, SkBudgeted, uint32_t flags = 0);
Robert Phillips0bd24dc2018-01-16 08:06:32 -050070
71 /*
72 * Create an un-mipmapped texture proxy with data.
73 * DDL TODO: need to refine ownership semantics of 'srcData' if we're in completely
74 * deferred mode
75 */
Brian Salomon58389b92018-03-07 13:01:25 -050076 sk_sp<GrTextureProxy> createTextureProxy(const GrSurfaceDesc&, SkBudgeted, const void* srcData,
77 size_t rowBytes);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050078
79 /*
Greg Daniel9d86f1d2018-01-29 09:33:59 -050080 * Create an un-mipmapped texture proxy with data. The SkImage must be a raster backend image.
81 * Since the SkImage is ref counted, we simply take a ref on it to keep the data alive until we
82 * actually upload the data to the gpu.
83 */
84 sk_sp<GrTextureProxy> createTextureProxy(sk_sp<SkImage> srcImage,
85 GrSurfaceFlags flags,
Greg Daniel9d86f1d2018-01-29 09:33:59 -050086 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -050087 SkBudgeted budgeted,
88 SkBackingFit fit);
Greg Daniel9d86f1d2018-01-29 09:33:59 -050089
90 /*
Robert Phillips0bd24dc2018-01-16 08:06:32 -050091 * Create a mipmapped texture proxy without any data.
92 *
93 * Like the call above but there are no texels to upload. A texture proxy is returned that
94 * simply has space allocated for the mips. We will allocated the full amount of mip levels
95 * based on the width and height in the GrSurfaceDesc.
96 */
Brian Salomon2a4f9832018-03-03 22:43:43 -050097 sk_sp<GrTextureProxy> createMipMapProxy(const GrSurfaceDesc&, GrSurfaceOrigin, SkBudgeted);
Robert Phillips0bd24dc2018-01-16 08:06:32 -050098
99 /*
Greg Daniela4ead652018-02-07 10:21:48 -0500100 * Creates a new mipmapped texture proxy for the bitmap with mip levels generated by the cpu.
101 */
102 sk_sp<GrTextureProxy> createMipMapProxyFromBitmap(const SkBitmap& bitmap,
103 SkColorSpace* dstColorSpace);
104
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,
109 SkBackingFit, SkBudgeted, uint32_t flags);
Greg Danielf6f7b672018-02-15 13:06:26 -0500110
Brian Salomon2a4f9832018-03-03 22:43:43 -0500111 sk_sp<GrTextureProxy> createProxy(const GrSurfaceDesc& desc, GrSurfaceOrigin origin,
112 SkBackingFit fit, SkBudgeted budgeted, uint32_t flags = 0) {
113 return this->createProxy(desc, origin, GrMipMapped::kNo, fit, budgeted, flags);
Greg Danielf6f7b672018-02-15 13:06:26 -0500114 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500115
Robert Phillipsadbe1322018-01-17 13:35:46 -0500116 // These match the definitions in SkImage & GrTexture.h, for whence they came
117 typedef void* ReleaseContext;
118 typedef void (*ReleaseProc)(ReleaseContext);
119
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500120 /*
121 * Create a texture proxy that wraps a (non-renderable) backend texture.
122 */
Brian Salomon7578f3e2018-03-07 14:39:54 -0500123 sk_sp<GrTextureProxy> wrapBackendTexture(const GrBackendTexture&, GrSurfaceOrigin,
124 GrWrapOwnership = kBorrow_GrWrapOwnership,
125 ReleaseProc = nullptr, ReleaseContext = nullptr);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500126
127 /*
128 * Create a texture proxy that wraps a backend texture and is both texture-able and renderable
129 */
Brian Salomon7578f3e2018-03-07 14:39:54 -0500130 sk_sp<GrTextureProxy> wrapRenderableBackendTexture(const GrBackendTexture&,
131 GrSurfaceOrigin,
Brian Salomon02bd2952018-03-07 15:20:21 -0500132 int sampleCnt,
133 GrWrapOwnership = kBorrow_GrWrapOwnership);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500134
135 /*
136 * Create a render target proxy that wraps a backend rendertarget
137 */
Brian Salomon7578f3e2018-03-07 14:39:54 -0500138 sk_sp<GrSurfaceProxy> wrapBackendRenderTarget(const GrBackendRenderTarget&, GrSurfaceOrigin);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500139
140 /*
Brian Salomon7578f3e2018-03-07 14:39:54 -0500141 * Create a render target proxy that wraps a backend texture
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500142 */
Brian Salomon7578f3e2018-03-07 14:39:54 -0500143 sk_sp<GrSurfaceProxy> wrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
144 GrSurfaceOrigin origin,
145 int sampleCnt);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500146
Robert Phillipsce5209a2018-02-13 11:13:51 -0500147 using LazyInstantiateCallback = std::function<sk_sp<GrSurface>(GrResourceProvider*)>;
Robert Phillipse8fabb22018-02-04 14:33:21 -0500148 enum class Textureable : bool {
149 kNo = false,
150 kYes = true
151 };
152
Robert Phillips777707b2018-01-17 11:40:14 -0500153 enum class Renderable : bool {
154 kNo = false,
155 kYes = true
156 };
157
158 /**
159 * Creates a texture proxy that will be instantiated by a user-supplied callback during flush.
160 * (Stencil is not supported by this method.) The width and height must either both be greater
161 * than 0 or both less than or equal to zero. A non-positive value is a signal that the width
162 * and height are currently unknown.
163 *
164 * When called, the callback must be able to cleanup any resources that it captured at creation.
165 * It also must support being passed in a null GrResourceProvider. When this happens, the
166 * callback should cleanup any resources it captured and return an empty sk_sp<GrTextureProxy>.
167 */
168 sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrSurfaceDesc&,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500169 GrSurfaceOrigin, GrMipMapped, GrRenderTargetFlags,
170 SkBackingFit, SkBudgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500171
172 sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrSurfaceDesc&,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500173 GrSurfaceOrigin, GrMipMapped, SkBackingFit, SkBudgeted);
Robert Phillips777707b2018-01-17 11:40:14 -0500174
Robert Phillipsce5209a2018-02-13 11:13:51 -0500175 /**
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 */
Robert Phillips777707b2018-01-17 11:40:14 -0500179 sk_sp<GrTextureProxy> createFullyLazyProxy(LazyInstantiateCallback&&,
Robert Phillipsce5209a2018-02-13 11:13:51 -0500180 Renderable, GrSurfaceOrigin, GrPixelConfig);
Robert Phillips777707b2018-01-17 11:40:14 -0500181
Robert Phillipse8fabb22018-02-04 14:33:21 -0500182 sk_sp<GrRenderTargetProxy> createLazyRenderTargetProxy(LazyInstantiateCallback&&,
Greg Daniel2a303902018-02-20 10:25:54 -0500183 const GrSurfaceDesc&,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500184 GrSurfaceOrigin origin,
Greg Daniel2a303902018-02-20 10:25:54 -0500185 GrRenderTargetFlags, Textureable,
Robert Phillipse8fabb22018-02-04 14:33:21 -0500186 GrMipMapped, SkBackingFit, SkBudgeted);
187
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500188 // 'proxy' is about to be used as a texture src or drawn to. This query can be used to
189 // determine if it is going to need a texture domain or a full clear.
190 static bool IsFunctionallyExact(GrSurfaceProxy* proxy);
191
192 /**
193 * Either the proxy attached to the unique key is being deleted (in which case we
194 * don't want it cluttering up the hash table) or the client has indicated that
195 * it will never refer to the unique key again. In either case, remove the key
196 * from the hash table.
197 * Note: this does not, by itself, alter unique key attached to the underlying GrTexture.
198 */
199 void processInvalidProxyUniqueKey(const GrUniqueKey&);
200
201 /**
202 * Same as above, but you must pass in a GrTextureProxy to save having to search for it. The
203 * GrUniqueKey of the proxy must be valid and it must match the passed in key. This function
204 * also gives the option to invalidate the GrUniqueKey on the underlying GrTexture.
205 */
206 void processInvalidProxyUniqueKey(const GrUniqueKey&, GrTextureProxy*, bool invalidateSurface);
207
208 const GrCaps* caps() const { return fCaps.get(); }
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500209 sk_sp<const GrCaps> refCaps() const { return fCaps; }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500210
211 void abandon() {
212 fResourceCache = nullptr;
213 fResourceProvider = nullptr;
Robert Phillips4d120512018-01-19 13:22:07 -0500214 fAbandoned = true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500215 }
216
217 bool isAbandoned() const {
Robert Phillips4d120512018-01-19 13:22:07 -0500218#ifdef SK_DEBUG
219 if (fAbandoned) {
220 SkASSERT(!fResourceCache && !fResourceProvider);
221 }
222#endif
223 return fAbandoned;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500224 }
225
226 int numUniqueKeyProxies_TestOnly() const;
227
228 void removeAllUniqueKeys();
229
Greg Daniel7e1912a2018-02-08 09:15:33 -0500230 /**
231 * Helper function for callers who are wrapping a bitmap into an SkImage so they know whether or
232 * not that bitmap should be copied or not.
233 */
234 bool mutableBitmapsNeedCopy() const { return !SkToBool(fResourceProvider); }
235
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500236private:
Robert Phillipsadbe1322018-01-17 13:35:46 -0500237 friend class GrAHardwareBufferImageGenerator; // for createWrapped
238
239 sk_sp<GrTextureProxy> createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin);
240
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500241 struct UniquelyKeyedProxyHashTraits {
242 static const GrUniqueKey& GetKey(const GrTextureProxy& p) { return p.getUniqueKey(); }
243
244 static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
245 };
246 typedef SkTDynamicHash<GrTextureProxy, GrUniqueKey, UniquelyKeyedProxyHashTraits> UniquelyKeyedProxyHash;
247
248 // This holds the texture proxies that have unique keys. The resourceCache does not get a ref
249 // on these proxies but they must send a message to the resourceCache when they are deleted.
250 UniquelyKeyedProxyHash fUniquelyKeyedProxies;
251
252 GrResourceProvider* fResourceProvider;
253 GrResourceCache* fResourceCache;
Robert Phillips4d120512018-01-19 13:22:07 -0500254 bool fAbandoned;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500255 sk_sp<const GrCaps> fCaps;
256
257 // In debug builds we guard against improper thread handling
258 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
259};
260
261#endif