blob: a709c7056481e1c4e673c7604e9b8ab893a19f2b [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 GrResourceProvider_DEFINED
9#define GrResourceProvider_DEFINED
10
joshualittb356cbc2015-08-05 06:36:39 -070011#include "GrBatchAtlas.h"
bsalomonedd77a12015-05-29 09:45:57 -070012#include "GrIndexBuffer.h"
bsalomond309e7a2015-04-30 14:18:54 -070013#include "GrTextureProvider.h"
bsalomon706f08f2015-05-22 07:35:58 -070014#include "GrPathRange.h"
bsalomond309e7a2015-04-30 14:18:54 -070015
joshualittb356cbc2015-08-05 06:36:39 -070016class GrBatchAtlas;
bsalomoned0bcad2015-05-04 10:36:42 -070017class GrIndexBuffer;
bsalomon706f08f2015-05-22 07:35:58 -070018class GrPath;
egdanielec00d942015-09-14 12:56:10 -070019class GrRenderTarget;
20class GrStencilAttachment;
bsalomon706f08f2015-05-22 07:35:58 -070021class GrStrokeInfo;
bsalomoned0bcad2015-05-04 10:36:42 -070022class GrVertexBuffer;
bsalomon706f08f2015-05-22 07:35:58 -070023class SkDescriptor;
24class SkPath;
25class SkTypeface;
bsalomoned0bcad2015-05-04 10:36:42 -070026
bsalomond309e7a2015-04-30 14:18:54 -070027/**
28 * An extension of the texture provider for arbitrary resource types. This class is intended for
29 * use within the Gr code base, not by clients or extensions (e.g. third party GrProcessor
30 * derivatives).
bsalomoneae62002015-07-31 13:59:30 -070031 *
32 * This currently inherits from GrTextureProvider non-publically to force callers to provider
33 * make a flags (pendingIO) decision and not use the GrTP methods that don't take flags. This
34 * can be relaxed once http://skbug.com/4156 is fixed.
bsalomond309e7a2015-04-30 14:18:54 -070035 */
bsalomoneae62002015-07-31 13:59:30 -070036class GrResourceProvider : protected GrTextureProvider {
bsalomond309e7a2015-04-30 14:18:54 -070037public:
bsalomoned0bcad2015-05-04 10:36:42 -070038 GrResourceProvider(GrGpu* gpu, GrResourceCache* cache);
39
40 template <typename T> T* findAndRefTByUniqueKey(const GrUniqueKey& key) {
41 return static_cast<T*>(this->findAndRefResourceByUniqueKey(key));
42 }
43
44 /**
45 * Either finds and refs, or creates an index buffer for instanced drawing with a specific
46 * pattern if the index buffer is not found. If the return is non-null, the caller owns
47 * a ref on the returned GrIndexBuffer.
48 *
49 * @param pattern the pattern of indices to repeat
50 * @param patternSize size in bytes of the pattern
51 * @param reps number of times to repeat the pattern
52 * @param vertCount number of vertices the pattern references
53 * @param key Key to be assigned to the index buffer.
54 *
halcanary96fcdcc2015-08-27 07:41:13 -070055 * @return The index buffer if successful, otherwise nullptr.
bsalomoned0bcad2015-05-04 10:36:42 -070056 */
bsalomoneae62002015-07-31 13:59:30 -070057 const GrIndexBuffer* findOrCreateInstancedIndexBuffer(const uint16_t* pattern,
58 int patternSize,
59 int reps,
60 int vertCount,
61 const GrUniqueKey& key) {
bsalomoned0bcad2015-05-04 10:36:42 -070062 if (GrIndexBuffer* buffer = this->findAndRefTByUniqueKey<GrIndexBuffer>(key)) {
63 return buffer;
64 }
65 return this->createInstancedIndexBuffer(pattern, patternSize, reps, vertCount, key);
66 }
67
68 /**
69 * Returns an index buffer that can be used to render quads.
70 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
71 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
72 * Draw with kTriangles_GrPrimitiveType
73 * @ return the quad index buffer
74 */
75 const GrIndexBuffer* refQuadIndexBuffer() {
76 if (GrIndexBuffer* buffer =
77 this->findAndRefTByUniqueKey<GrIndexBuffer>(fQuadIndexBufferKey)) {
78 return buffer;
79 }
80 return this->createQuadIndexBuffer();
81 }
82
bsalomon706f08f2015-05-22 07:35:58 -070083 /**
84 * Factories for GrPath and GrPathRange objects. It's an error to call these if path rendering
85 * is not supported.
86 */
87 GrPath* createPath(const SkPath&, const GrStrokeInfo&);
88 GrPathRange* createPathRange(GrPathRange::PathGenerator*, const GrStrokeInfo&);
89 GrPathRange* createGlyphs(const SkTypeface*, const SkDescriptor*, const GrStrokeInfo&);
90
bsalomond309e7a2015-04-30 14:18:54 -070091 using GrTextureProvider::assignUniqueKeyToResource;
92 using GrTextureProvider::findAndRefResourceByUniqueKey;
bsalomon473addf2015-10-02 07:49:05 -070093 using GrTextureProvider::findAndRefTextureByUniqueKey;
bsalomond309e7a2015-04-30 14:18:54 -070094 using GrTextureProvider::abandon;
95
bsalomoneae62002015-07-31 13:59:30 -070096 enum Flags {
97 /** If the caller intends to do direct reads/writes to/from the CPU then this flag must be
98 * set when accessing resources during a GrDrawTarget flush. This includes the execution of
99 * GrBatch objects. The reason is that these memory operations are done immediately and
100 * will occur out of order WRT the operations being flushed.
101 * Make this automatic: http://skbug.com/4156
102 */
103 kNoPendingIO_Flag = kNoPendingIO_ScratchTextureFlag,
104 };
105
106 enum BufferUsage {
107 /** Caller intends to specify the buffer data rarely with respect to the number of draws
108 that read the data. */
109 kStatic_BufferUsage,
110 /** Caller intends to respecify the buffer data frequently between draws. */
111 kDynamic_BufferUsage,
112 };
113 GrIndexBuffer* createIndexBuffer(size_t size, BufferUsage, uint32_t flags);
114 GrVertexBuffer* createVertexBuffer(size_t size, BufferUsage, uint32_t flags);
115
116 GrTexture* createApproxTexture(const GrSurfaceDesc& desc, uint32_t flags) {
117 SkASSERT(0 == flags || kNoPendingIO_Flag == flags);
118 return this->internalCreateApproxTexture(desc, flags);
119 }
robertphillips1b8e1b52015-06-24 06:54:10 -0700120
joshualittb356cbc2015-08-05 06:36:39 -0700121 /** Returns a GrBatchAtlas. This function can be called anywhere, but the returned atlas should
122 * only be used inside of GrBatch::generateGeometry
123 * @param GrPixelConfig The pixel config which this atlas will store
124 * @param width width in pixels of the atlas
125 * @param height height in pixels of the atlas
126 * @param numPlotsX The number of plots the atlas should be broken up into in the X
127 * direction
128 * @param numPlotsY The number of plots the atlas should be broken up into in the Y
129 * direction
130 * @param func An eviction function which will be called whenever the atlas has to
131 * evict data
132 * @param data User supplied data which will be passed into func whenver an
133 * eviction occurs
134 *
halcanary96fcdcc2015-08-27 07:41:13 -0700135 * @return An initialized GrBatchAtlas, or nullptr if creation fails
joshualittb356cbc2015-08-05 06:36:39 -0700136 */
137 GrBatchAtlas* createAtlas(GrPixelConfig, int width, int height, int numPlotsX, int numPlotsY,
138 GrBatchAtlas::EvictionFunc func, void* data);
139
egdanielec00d942015-09-14 12:56:10 -0700140 /**
141 * If passed in render target already has a stencil buffer, return it. Otherwise attempt to
142 * attach one.
143 */
144 GrStencilAttachment* attachStencilAttachment(GrRenderTarget* rt);
145
bsalomon473addf2015-10-02 07:49:05 -0700146 const GrCaps* caps() { return this->gpu()->caps(); }
147
bsalomoned0bcad2015-05-04 10:36:42 -0700148private:
149 const GrIndexBuffer* createInstancedIndexBuffer(const uint16_t* pattern,
150 int patternSize,
151 int reps,
152 int vertCount,
153 const GrUniqueKey& key);
154
155 const GrIndexBuffer* createQuadIndexBuffer();
156
157 GrUniqueKey fQuadIndexBufferKey;
158
bsalomond309e7a2015-04-30 14:18:54 -0700159 typedef GrTextureProvider INHERITED;
160};
161
162#endif