blob: f0f28ff2cc7a6785aad93eb7e53e5661bb8b90a6 [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;
19class GrStrokeInfo;
bsalomoned0bcad2015-05-04 10:36:42 -070020class GrVertexBuffer;
bsalomon706f08f2015-05-22 07:35:58 -070021class SkDescriptor;
22class SkPath;
23class SkTypeface;
bsalomoned0bcad2015-05-04 10:36:42 -070024
bsalomond309e7a2015-04-30 14:18:54 -070025/**
26 * An extension of the texture provider for arbitrary resource types. This class is intended for
27 * use within the Gr code base, not by clients or extensions (e.g. third party GrProcessor
28 * derivatives).
bsalomoneae62002015-07-31 13:59:30 -070029 *
30 * This currently inherits from GrTextureProvider non-publically to force callers to provider
31 * make a flags (pendingIO) decision and not use the GrTP methods that don't take flags. This
32 * can be relaxed once http://skbug.com/4156 is fixed.
bsalomond309e7a2015-04-30 14:18:54 -070033 */
bsalomoneae62002015-07-31 13:59:30 -070034class GrResourceProvider : protected GrTextureProvider {
bsalomond309e7a2015-04-30 14:18:54 -070035public:
bsalomoned0bcad2015-05-04 10:36:42 -070036 GrResourceProvider(GrGpu* gpu, GrResourceCache* cache);
37
38 template <typename T> T* findAndRefTByUniqueKey(const GrUniqueKey& key) {
39 return static_cast<T*>(this->findAndRefResourceByUniqueKey(key));
40 }
41
42 /**
43 * Either finds and refs, or creates an index buffer for instanced drawing with a specific
44 * pattern if the index buffer is not found. If the return is non-null, the caller owns
45 * a ref on the returned GrIndexBuffer.
46 *
47 * @param pattern the pattern of indices to repeat
48 * @param patternSize size in bytes of the pattern
49 * @param reps number of times to repeat the pattern
50 * @param vertCount number of vertices the pattern references
51 * @param key Key to be assigned to the index buffer.
52 *
53 * @return The index buffer if successful, otherwise NULL.
54 */
bsalomoneae62002015-07-31 13:59:30 -070055 const GrIndexBuffer* findOrCreateInstancedIndexBuffer(const uint16_t* pattern,
56 int patternSize,
57 int reps,
58 int vertCount,
59 const GrUniqueKey& key) {
bsalomoned0bcad2015-05-04 10:36:42 -070060 if (GrIndexBuffer* buffer = this->findAndRefTByUniqueKey<GrIndexBuffer>(key)) {
61 return buffer;
62 }
63 return this->createInstancedIndexBuffer(pattern, patternSize, reps, vertCount, key);
64 }
65
66 /**
67 * Returns an index buffer that can be used to render quads.
68 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
69 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
70 * Draw with kTriangles_GrPrimitiveType
71 * @ return the quad index buffer
72 */
73 const GrIndexBuffer* refQuadIndexBuffer() {
74 if (GrIndexBuffer* buffer =
75 this->findAndRefTByUniqueKey<GrIndexBuffer>(fQuadIndexBufferKey)) {
76 return buffer;
77 }
78 return this->createQuadIndexBuffer();
79 }
80
bsalomon706f08f2015-05-22 07:35:58 -070081 /**
82 * Factories for GrPath and GrPathRange objects. It's an error to call these if path rendering
83 * is not supported.
84 */
85 GrPath* createPath(const SkPath&, const GrStrokeInfo&);
86 GrPathRange* createPathRange(GrPathRange::PathGenerator*, const GrStrokeInfo&);
87 GrPathRange* createGlyphs(const SkTypeface*, const SkDescriptor*, const GrStrokeInfo&);
88
bsalomond309e7a2015-04-30 14:18:54 -070089 using GrTextureProvider::assignUniqueKeyToResource;
90 using GrTextureProvider::findAndRefResourceByUniqueKey;
91 using GrTextureProvider::abandon;
92
bsalomoneae62002015-07-31 13:59:30 -070093 enum Flags {
94 /** If the caller intends to do direct reads/writes to/from the CPU then this flag must be
95 * set when accessing resources during a GrDrawTarget flush. This includes the execution of
96 * GrBatch objects. The reason is that these memory operations are done immediately and
97 * will occur out of order WRT the operations being flushed.
98 * Make this automatic: http://skbug.com/4156
99 */
100 kNoPendingIO_Flag = kNoPendingIO_ScratchTextureFlag,
101 };
102
103 enum BufferUsage {
104 /** Caller intends to specify the buffer data rarely with respect to the number of draws
105 that read the data. */
106 kStatic_BufferUsage,
107 /** Caller intends to respecify the buffer data frequently between draws. */
108 kDynamic_BufferUsage,
109 };
110 GrIndexBuffer* createIndexBuffer(size_t size, BufferUsage, uint32_t flags);
111 GrVertexBuffer* createVertexBuffer(size_t size, BufferUsage, uint32_t flags);
112
113 GrTexture* createApproxTexture(const GrSurfaceDesc& desc, uint32_t flags) {
114 SkASSERT(0 == flags || kNoPendingIO_Flag == flags);
115 return this->internalCreateApproxTexture(desc, flags);
116 }
robertphillips1b8e1b52015-06-24 06:54:10 -0700117
joshualittb356cbc2015-08-05 06:36:39 -0700118 /** Returns a GrBatchAtlas. This function can be called anywhere, but the returned atlas should
119 * only be used inside of GrBatch::generateGeometry
120 * @param GrPixelConfig The pixel config which this atlas will store
121 * @param width width in pixels of the atlas
122 * @param height height in pixels of the atlas
123 * @param numPlotsX The number of plots the atlas should be broken up into in the X
124 * direction
125 * @param numPlotsY The number of plots the atlas should be broken up into in the Y
126 * direction
127 * @param func An eviction function which will be called whenever the atlas has to
128 * evict data
129 * @param data User supplied data which will be passed into func whenver an
130 * eviction occurs
131 *
132 * @return An initialized GrBatchAtlas, or NULL if creation fails
133 */
134 GrBatchAtlas* createAtlas(GrPixelConfig, int width, int height, int numPlotsX, int numPlotsY,
135 GrBatchAtlas::EvictionFunc func, void* data);
136
bsalomoned0bcad2015-05-04 10:36:42 -0700137private:
138 const GrIndexBuffer* createInstancedIndexBuffer(const uint16_t* pattern,
139 int patternSize,
140 int reps,
141 int vertCount,
142 const GrUniqueKey& key);
143
144 const GrIndexBuffer* createQuadIndexBuffer();
145
146 GrUniqueKey fQuadIndexBufferKey;
147
bsalomond309e7a2015-04-30 14:18:54 -0700148 typedef GrTextureProvider INHERITED;
149};
150
151#endif