blob: 83efb5bf7b0622c3568a4f3d3e0b69e972606c5f [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"
cblume55f2d2d2016-02-26 13:20:48 -080012#include "GrTypes.h"
reedc9b5f8b2015-10-22 13:20:20 -070013#include "SkImageFilter.h"
bsalomond309e7a2015-04-30 14:18:54 -070014
joshualitt6d0872d2016-01-11 08:27:48 -080015class GrSingleOwner;
16
bsalomond309e7a2015-04-30 14:18:54 -070017class SK_API GrTextureProvider {
18public:
19 ///////////////////////////////////////////////////////////////////////////
20 // Textures
21
22 /**
23 * Creates a new texture in the resource cache and returns it. The caller owns a
24 * ref on the returned texture which must be balanced by a call to unref.
25 *
cblume55f2d2d2016-02-26 13:20:48 -080026 * @param desc Description of the texture properties.
27 * @param budgeted Does the texture count against the resource cache budget?
28 * @param texels A contiguous array of mipmap levels
29 * @param mipLevelCount The amount of elements in the texels array
30 */
31 GrTexture* createMipMappedTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
32 const GrMipLevel* texels, int mipLevelCount);
33
34 /**
35 * This function is a shim which creates a SkTArray<GrMipLevel> of size 1.
36 * It then calls createTexture with that SkTArray.
37 *
bsalomond309e7a2015-04-30 14:18:54 -070038 * @param srcData Pointer to the pixel values (optional).
39 * @param rowBytes The number of bytes between rows of the texture. Zero
40 * implies tightly packed rows. For compressed pixel configs, this
41 * field is ignored.
42 */
bsalomon5ec26ae2016-02-25 08:33:02 -080043 GrTexture* createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, const void* srcData,
bsalomond309e7a2015-04-30 14:18:54 -070044 size_t rowBytes);
45
46 /** Shortcut for creating a texture with no initial data to upload. */
bsalomon5ec26ae2016-02-25 08:33:02 -080047 GrTexture* createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted) {
bsalomond3312592016-03-04 07:06:43 -080048 return this->createTexture(desc, budgeted, NULL, 0);
bsalomond309e7a2015-04-30 14:18:54 -070049 }
50
51 /** Assigns a unique key to the texture. The texture will be findable via this key using
52 findTextureByUniqueKey(). If an existing texture has this key, it's key will be removed. */
53 void assignUniqueKeyToTexture(const GrUniqueKey& key, GrTexture* texture) {
54 this->assignUniqueKeyToResource(key, texture);
55 }
56
57 /** Finds a texture by unique key. If the texture is found it is ref'ed and returned. */
joshualitt6d0872d2016-01-11 08:27:48 -080058 GrTexture* findAndRefTextureByUniqueKey(const GrUniqueKey& key);
bsalomond309e7a2015-04-30 14:18:54 -070059
60 /**
61 * Determines whether a texture is associated with the unique key. If the texture is found it
62 * will not be locked or returned. This call does not affect the priority of the resource for
63 * deletion.
64 */
65 bool existsTextureWithUniqueKey(const GrUniqueKey& key) const {
66 return this->existsResourceWithUniqueKey(key);
67 }
68
69 /**
bsalomoneae62002015-07-31 13:59:30 -070070 * Finds a texture that approximately matches the descriptor. Will be at least as large in width
71 * and height as desc specifies. If desc specifies that the texture should be a render target
72 * then result will be a render target. Format and sample count will always match the request.
73 * The contents of the texture are undefined. The caller owns a ref on the returned texture and
74 * must balance with a call to unref.
bsalomond309e7a2015-04-30 14:18:54 -070075 */
bsalomoneae62002015-07-31 13:59:30 -070076 GrTexture* createApproxTexture(const GrSurfaceDesc&);
77
78 /** Legacy function that no longer should be used. */
bsalomond309e7a2015-04-30 14:18:54 -070079 enum ScratchTexMatch {
bsalomond309e7a2015-04-30 14:18:54 -070080 kExact_ScratchTexMatch,
bsalomond309e7a2015-04-30 14:18:54 -070081 kApprox_ScratchTexMatch
82 };
bsalomoneae62002015-07-31 13:59:30 -070083 GrTexture* refScratchTexture(const GrSurfaceDesc& desc, ScratchTexMatch match) {
84 if (kApprox_ScratchTexMatch == match) {
85 return this->createApproxTexture(desc);
86 } else {
bsalomon5ec26ae2016-02-25 08:33:02 -080087 return this->createTexture(desc, SkBudgeted::kYes);
bsalomoneae62002015-07-31 13:59:30 -070088 }
89 }
bsalomond309e7a2015-04-30 14:18:54 -070090
91 ///////////////////////////////////////////////////////////////////////////
92 // Wrapped Backend Surfaces
93
94 /**
95 * Wraps an existing texture with a GrTexture object.
96 *
97 * OpenGL: if the object is a texture Gr may change its GL texture params
98 * when it is drawn.
99 *
bsalomond309e7a2015-04-30 14:18:54 -0700100 * @return GrTexture object or NULL on failure.
101 */
bsalomon6dc6f5f2015-06-18 09:12:16 -0700102 GrTexture* wrapBackendTexture(const GrBackendTextureDesc& desc,
103 GrWrapOwnership = kBorrow_GrWrapOwnership);
bsalomond309e7a2015-04-30 14:18:54 -0700104
105 /**
106 * Wraps an existing render target with a GrRenderTarget object. It is
107 * similar to wrapBackendTexture but can be used to draw into surfaces
108 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
bsalomon6dc6f5f2015-06-18 09:12:16 -0700109 * the client will resolve to a texture). Currently wrapped render targets
110 * always use the kBorrow_GrWrapOwnership semantics.
bsalomond309e7a2015-04-30 14:18:54 -0700111 *
ericrkf7b8b8a2016-02-24 14:49:51 -0800112 * @return GrRenderTarget object or NULL on failure.
bsalomond309e7a2015-04-30 14:18:54 -0700113 */
114 GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc);
115
116protected:
joshualitt6d0872d2016-01-11 08:27:48 -0800117 GrTextureProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* singleOwner);
bsalomond309e7a2015-04-30 14:18:54 -0700118
119 /**
120 * Assigns a unique key to a resource. If the key is associated with another resource that
121 * association is removed and replaced by this resource.
122 */
123 void assignUniqueKeyToResource(const GrUniqueKey&, GrGpuResource*);
124
125 /**
126 * Finds a resource in the cache, based on the specified key. This is intended for use in
127 * conjunction with addResourceToCache(). The return value will be NULL if not found. The
128 * caller must balance with a call to unref().
129 */
130 GrGpuResource* findAndRefResourceByUniqueKey(const GrUniqueKey&);
131
132 /**
133 * Determines whether a resource is in the cache. If the resource is found it
134 * will not be locked or returned. This call does not affect the priority of
135 * the resource for deletion.
136 */
137 bool existsResourceWithUniqueKey(const GrUniqueKey& key) const;
138
bsalomoneae62002015-07-31 13:59:30 -0700139 enum ScratchTextureFlags {
140 kExact_ScratchTextureFlag = 0x1,
141 kNoPendingIO_ScratchTextureFlag = 0x2, // (http://skbug.com/4156)
142 kNoCreate_ScratchTextureFlag = 0x4,
143 };
144
145 /** A common impl for GrTextureProvider and GrResourceProvider variants. */
146 GrTexture* internalCreateApproxTexture(const GrSurfaceDesc& desc, uint32_t scratchTextureFlags);
147
148 GrTexture* refScratchTexture(const GrSurfaceDesc&, uint32_t scratchTextureFlags);
bsalomond309e7a2015-04-30 14:18:54 -0700149
150 void abandon() {
151 fCache = NULL;
152 fGpu = NULL;
153 }
154
bsalomoned0bcad2015-05-04 10:36:42 -0700155 GrResourceCache* cache() { return fCache; }
156 const GrResourceCache* cache() const { return fCache; }
157
158 GrGpu* gpu() { return fGpu; }
159 const GrGpu* gpu() const { return fGpu; }
160
bsalomond309e7a2015-04-30 14:18:54 -0700161 bool isAbandoned() const {
162 SkASSERT(SkToBool(fGpu) == SkToBool(fCache));
163 return !SkToBool(fCache);
164 }
165
robertphillips1b8e1b52015-06-24 06:54:10 -0700166private:
bsalomond309e7a2015-04-30 14:18:54 -0700167 GrResourceCache* fCache;
168 GrGpu* fGpu;
joshualitt6d0872d2016-01-11 08:27:48 -0800169
170 // In debug builds we guard against improper thread handling
171 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
bsalomond309e7a2015-04-30 14:18:54 -0700172};
173
174#endif