blob: 44c8cbc6578424aaed6a0e3422d9e4915597ee6e [file] [log] [blame]
bsalomond309e7a2015-04-30 14:18:54 -07001/*
2 * Copyright 2015 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 GrTextureProvider_DEFINED
9#define GrTextureProvider_DEFINED
10
11#include "GrTexture.h"
12
13class SK_API GrTextureProvider {
14public:
15 ///////////////////////////////////////////////////////////////////////////
16 // Textures
17
18 /**
19 * Creates a new texture in the resource cache and returns it. The caller owns a
20 * ref on the returned texture which must be balanced by a call to unref.
21 *
22 * @param desc Description of the texture properties.
23 * @param budgeted Does the texture count against the resource cache budget?
24 * @param srcData Pointer to the pixel values (optional).
25 * @param rowBytes The number of bytes between rows of the texture. Zero
26 * implies tightly packed rows. For compressed pixel configs, this
27 * field is ignored.
28 */
29 GrTexture* createTexture(const GrSurfaceDesc& desc, bool budgeted, const void* srcData,
30 size_t rowBytes);
31
32 /** Shortcut for creating a texture with no initial data to upload. */
33 GrTexture* createTexture(const GrSurfaceDesc& desc, bool budgeted) {
34 return this->createTexture(desc, budgeted, NULL, 0);
35 }
36
37 /** Assigns a unique key to the texture. The texture will be findable via this key using
38 findTextureByUniqueKey(). If an existing texture has this key, it's key will be removed. */
39 void assignUniqueKeyToTexture(const GrUniqueKey& key, GrTexture* texture) {
40 this->assignUniqueKeyToResource(key, texture);
41 }
42
43 /** Finds a texture by unique key. If the texture is found it is ref'ed and returned. */
44 GrTexture* findAndRefTextureByUniqueKey(const GrUniqueKey& key) {
45 GrGpuResource* resource = this->findAndRefResourceByUniqueKey(key);
46 if (resource) {
47 GrTexture* texture = static_cast<GrSurface*>(resource)->asTexture();
48 SkASSERT(texture);
49 return texture;
50 }
51 return NULL;
52 }
53
54 /**
55 * Determines whether a texture is associated with the unique key. If the texture is found it
56 * will not be locked or returned. This call does not affect the priority of the resource for
57 * deletion.
58 */
59 bool existsTextureWithUniqueKey(const GrUniqueKey& key) const {
60 return this->existsResourceWithUniqueKey(key);
61 }
62
63 /**
bsalomoneae62002015-07-31 13:59:30 -070064 * Finds a texture that approximately matches the descriptor. Will be at least as large in width
65 * and height as desc specifies. If desc specifies that the texture should be a render target
66 * then result will be a render target. Format and sample count will always match the request.
67 * The contents of the texture are undefined. The caller owns a ref on the returned texture and
68 * must balance with a call to unref.
bsalomond309e7a2015-04-30 14:18:54 -070069 */
bsalomoneae62002015-07-31 13:59:30 -070070 GrTexture* createApproxTexture(const GrSurfaceDesc&);
71
72 /** Legacy function that no longer should be used. */
bsalomond309e7a2015-04-30 14:18:54 -070073 enum ScratchTexMatch {
bsalomond309e7a2015-04-30 14:18:54 -070074 kExact_ScratchTexMatch,
bsalomond309e7a2015-04-30 14:18:54 -070075 kApprox_ScratchTexMatch
76 };
bsalomoneae62002015-07-31 13:59:30 -070077 GrTexture* refScratchTexture(const GrSurfaceDesc& desc, ScratchTexMatch match) {
78 if (kApprox_ScratchTexMatch == match) {
79 return this->createApproxTexture(desc);
80 } else {
81 return this->createTexture(desc, true);
82 }
83 }
bsalomond309e7a2015-04-30 14:18:54 -070084
85 ///////////////////////////////////////////////////////////////////////////
86 // Wrapped Backend Surfaces
87
88 /**
89 * Wraps an existing texture with a GrTexture object.
90 *
91 * OpenGL: if the object is a texture Gr may change its GL texture params
92 * when it is drawn.
93 *
bsalomond309e7a2015-04-30 14:18:54 -070094 * @return GrTexture object or NULL on failure.
95 */
bsalomon6dc6f5f2015-06-18 09:12:16 -070096 GrTexture* wrapBackendTexture(const GrBackendTextureDesc& desc,
97 GrWrapOwnership = kBorrow_GrWrapOwnership);
bsalomond309e7a2015-04-30 14:18:54 -070098
99 /**
100 * Wraps an existing render target with a GrRenderTarget object. It is
101 * similar to wrapBackendTexture but can be used to draw into surfaces
102 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
bsalomon6dc6f5f2015-06-18 09:12:16 -0700103 * the client will resolve to a texture). Currently wrapped render targets
104 * always use the kBorrow_GrWrapOwnership semantics.
bsalomond309e7a2015-04-30 14:18:54 -0700105 *
106 * @return GrTexture object or NULL on failure.
107 */
108 GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc);
109
110protected:
111 GrTextureProvider(GrGpu* gpu, GrResourceCache* cache) : fCache(cache), fGpu(gpu) {}
112
113 /**
114 * Assigns a unique key to a resource. If the key is associated with another resource that
115 * association is removed and replaced by this resource.
116 */
117 void assignUniqueKeyToResource(const GrUniqueKey&, GrGpuResource*);
118
119 /**
120 * Finds a resource in the cache, based on the specified key. This is intended for use in
121 * conjunction with addResourceToCache(). The return value will be NULL if not found. The
122 * caller must balance with a call to unref().
123 */
124 GrGpuResource* findAndRefResourceByUniqueKey(const GrUniqueKey&);
125
126 /**
127 * Determines whether a resource is in the cache. If the resource is found it
128 * will not be locked or returned. This call does not affect the priority of
129 * the resource for deletion.
130 */
131 bool existsResourceWithUniqueKey(const GrUniqueKey& key) const;
132
bsalomoneae62002015-07-31 13:59:30 -0700133 enum ScratchTextureFlags {
134 kExact_ScratchTextureFlag = 0x1,
135 kNoPendingIO_ScratchTextureFlag = 0x2, // (http://skbug.com/4156)
136 kNoCreate_ScratchTextureFlag = 0x4,
137 };
138
139 /** A common impl for GrTextureProvider and GrResourceProvider variants. */
140 GrTexture* internalCreateApproxTexture(const GrSurfaceDesc& desc, uint32_t scratchTextureFlags);
141
142 GrTexture* refScratchTexture(const GrSurfaceDesc&, uint32_t scratchTextureFlags);
bsalomond309e7a2015-04-30 14:18:54 -0700143
144 void abandon() {
145 fCache = NULL;
146 fGpu = NULL;
147 }
148
bsalomoned0bcad2015-05-04 10:36:42 -0700149 GrResourceCache* cache() { return fCache; }
150 const GrResourceCache* cache() const { return fCache; }
151
152 GrGpu* gpu() { return fGpu; }
153 const GrGpu* gpu() const { return fGpu; }
154
bsalomond309e7a2015-04-30 14:18:54 -0700155 bool isAbandoned() const {
156 SkASSERT(SkToBool(fGpu) == SkToBool(fCache));
157 return !SkToBool(fCache);
158 }
159
robertphillips1b8e1b52015-06-24 06:54:10 -0700160private:
bsalomond309e7a2015-04-30 14:18:54 -0700161 GrResourceCache* fCache;
162 GrGpu* fGpu;
163};
164
165#endif