1/*
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
11#include "GrCaps.h"
12#include "GrResourceKey.h"
13#include "GrTextureProxy.h"
14#include "GrTypes.h"
15#include "SkRefCnt.h"
16#include "SkTDynamicHash.h"
17
18class GrResourceProvider;
19class GrSingleOwner;
20class GrBackendRenderTarget;
21class SkBitmap;
22class SkImage;
23
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     */
37    bool assignUniqueKeyToProxy(const GrUniqueKey&, GrTextureProxy*);
38
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    /*
63     * 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.
67     */
68    sk_sp<GrTextureProxy> createInstantiatedProxy(const GrSurfaceDesc&, SkBackingFit, SkBudgeted,
69                                                  uint32_t flags = 0);
70
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     */
76    sk_sp<GrTextureProxy> createTextureProxy(const GrSurfaceDesc&, SkBudgeted,
77                                             const void* srcData, size_t rowBytes);
78
79    /*
80     * 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,
86                                             GrSurfaceOrigin origin,
87                                             int sampleCnt,
88                                             SkBudgeted budgeted,
89                                             SkBackingFit fit);
90
91    /*
92     * 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     */
98    sk_sp<GrTextureProxy> createMipMapProxy(const GrSurfaceDesc&, SkBudgeted);
99
100    /*
101     * Creates a new mipmapped texture proxy for the bitmap with mip levels generated by the cpu.
102     */
103    sk_sp<GrTextureProxy> createMipMapProxyFromBitmap(const SkBitmap& bitmap,
104                                                      SkColorSpace* dstColorSpace);
105
106    /*
107     * Create a GrSurfaceProxy without any data.
108     */
109    sk_sp<GrTextureProxy> createProxy(const GrSurfaceDesc&, GrMipMapped, SkBackingFit, SkBudgeted,
110                                      uint32_t flags);
111
112    sk_sp<GrTextureProxy> createProxy(const GrSurfaceDesc& desc, SkBackingFit fit,
113                                      SkBudgeted budgeted, uint32_t flags = 0) {
114        return this->createProxy(desc, GrMipMapped::kNo, fit, budgeted, flags);
115    }
116
117    // These match the definitions in SkImage & GrTexture.h, for whence they came
118    typedef void* ReleaseContext;
119    typedef void (*ReleaseProc)(ReleaseContext);
120
121    /*
122     * Create a texture proxy that wraps a (non-renderable) backend texture.
123     */
124    sk_sp<GrTextureProxy> createWrappedTextureProxy(const GrBackendTexture&, GrSurfaceOrigin,
125                                                    GrWrapOwnership = kBorrow_GrWrapOwnership,
126                                                    ReleaseProc = nullptr,
127                                                    ReleaseContext = nullptr);
128
129    /*
130     * Create a texture proxy that wraps a backend texture and is both texture-able and renderable
131     */
132    sk_sp<GrTextureProxy> createWrappedTextureProxy(const GrBackendTexture&,
133                                                    GrSurfaceOrigin,
134                                                    int sampleCnt);
135
136    /*
137     * Create a render target proxy that wraps a backend rendertarget
138     */
139    sk_sp<GrSurfaceProxy> createWrappedRenderTargetProxy(const GrBackendRenderTarget&,
140                                                         GrSurfaceOrigin);
141
142    /*
143     * Create a render target proxy that wraps a backend texture?
144     */
145    sk_sp<GrSurfaceProxy> createWrappedRenderTargetProxy(const GrBackendTexture& tex,
146                                                         GrSurfaceOrigin origin,
147                                                         int sampleCnt);
148
149    using LazyInstantiateCallback = std::function<sk_sp<GrSurface>(GrResourceProvider*)>;
150    enum class Textureable : bool {
151        kNo = false,
152        kYes = true
153    };
154
155    enum class Renderable : bool {
156        kNo = false,
157        kYes = true
158    };
159
160    /**
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     */
170    sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrSurfaceDesc&,
171                                          GrMipMapped, GrRenderTargetFlags, SkBackingFit,
172                                          SkBudgeted);
173
174    sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrSurfaceDesc&,
175                                          GrMipMapped, SkBackingFit, SkBudgeted);
176
177    /**
178     * Fully lazy proxies have unspecified width and height. Methods that rely on those values
179     * (e.g., width, height, getBoundsRect) should be avoided.
180     */
181    sk_sp<GrTextureProxy> createFullyLazyProxy(LazyInstantiateCallback&&,
182                                               Renderable, GrSurfaceOrigin, GrPixelConfig);
183
184    sk_sp<GrRenderTargetProxy> createLazyRenderTargetProxy(LazyInstantiateCallback&&,
185                                                           const GrSurfaceDesc&,
186                                                           GrRenderTargetFlags, Textureable,
187                                                           GrMipMapped, SkBackingFit, SkBudgeted);
188
189    // 'proxy' is about to be used as a texture src or drawn to. This query can be used to
190    // determine if it is going to need a texture domain or a full clear.
191    static bool IsFunctionallyExact(GrSurfaceProxy* proxy);
192
193    /**
194     * Either the proxy attached to the unique key is being deleted (in which case we
195     * don't want it cluttering up the hash table) or the client has indicated that
196     * it will never refer to the unique key again. In either case, remove the key
197     * from the hash table.
198     * Note: this does not, by itself, alter unique key attached to the underlying GrTexture.
199     */
200    void processInvalidProxyUniqueKey(const GrUniqueKey&);
201
202    /**
203     * Same as above, but you must pass in a GrTextureProxy to save having to search for it. The
204     * GrUniqueKey of the proxy must be valid and it must match the passed in key. This function
205     * also gives the option to invalidate the GrUniqueKey on the underlying GrTexture.
206     */
207    void processInvalidProxyUniqueKey(const GrUniqueKey&, GrTextureProxy*, bool invalidateSurface);
208
209    const GrCaps* caps() const { return fCaps.get(); }
210    sk_sp<const GrCaps> refCaps() const { return fCaps; }
211
212    void abandon() {
213        fResourceCache = nullptr;
214        fResourceProvider = nullptr;
215        fAbandoned = true;
216    }
217
218    bool isAbandoned() const {
219#ifdef SK_DEBUG
220        if (fAbandoned) {
221            SkASSERT(!fResourceCache && !fResourceProvider);
222        }
223#endif
224        return fAbandoned;
225    }
226
227    int numUniqueKeyProxies_TestOnly() const;
228
229    void removeAllUniqueKeys();
230
231    /**
232     * Helper function for callers who are wrapping a bitmap into an SkImage so they know whether or
233     * not that bitmap should be copied or not.
234     */
235    bool mutableBitmapsNeedCopy() const { return !SkToBool(fResourceProvider); }
236
237private:
238    friend class GrAHardwareBufferImageGenerator; // for createWrapped
239
240    sk_sp<GrTextureProxy> createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin);
241
242    struct UniquelyKeyedProxyHashTraits {
243        static const GrUniqueKey& GetKey(const GrTextureProxy& p) { return p.getUniqueKey(); }
244
245        static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
246    };
247    typedef SkTDynamicHash<GrTextureProxy, GrUniqueKey, UniquelyKeyedProxyHashTraits> UniquelyKeyedProxyHash;
248
249    // This holds the texture proxies that have unique keys. The resourceCache does not get a ref
250    // on these proxies but they must send a message to the resourceCache when they are deleted.
251    UniquelyKeyedProxyHash fUniquelyKeyedProxies;
252
253    GrResourceProvider*    fResourceProvider;
254    GrResourceCache*       fResourceCache;
255    bool                   fAbandoned;
256    sk_sp<const GrCaps>    fCaps;
257
258    // In debug builds we guard against improper thread handling
259    SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
260};
261
262#endif
263