blob: 3dfc9ba863334299c5ea1c7142a6ea8d27be9e9e [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;
joshualitt6d0872d2016-01-11 08:27:48 -080020class GrSingleOwner;
egdanielec00d942015-09-14 12:56:10 -070021class GrStencilAttachment;
bsalomon706f08f2015-05-22 07:35:58 -070022class GrStrokeInfo;
bsalomoned0bcad2015-05-04 10:36:42 -070023class GrVertexBuffer;
bsalomon706f08f2015-05-22 07:35:58 -070024class SkDescriptor;
25class SkPath;
26class SkTypeface;
bsalomoned0bcad2015-05-04 10:36:42 -070027
bsalomond309e7a2015-04-30 14:18:54 -070028/**
29 * An extension of the texture provider for arbitrary resource types. This class is intended for
30 * use within the Gr code base, not by clients or extensions (e.g. third party GrProcessor
31 * derivatives).
bsalomoneae62002015-07-31 13:59:30 -070032 *
33 * This currently inherits from GrTextureProvider non-publically to force callers to provider
34 * make a flags (pendingIO) decision and not use the GrTP methods that don't take flags. This
halcanary6950de62015-11-07 05:29:00 -080035 * can be relaxed once https://bug.skia.org/4156 is fixed.
bsalomond309e7a2015-04-30 14:18:54 -070036 */
bsalomoneae62002015-07-31 13:59:30 -070037class GrResourceProvider : protected GrTextureProvider {
bsalomond309e7a2015-04-30 14:18:54 -070038public:
joshualitt6d0872d2016-01-11 08:27:48 -080039 GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner);
bsalomoned0bcad2015-05-04 10:36:42 -070040
41 template <typename T> T* findAndRefTByUniqueKey(const GrUniqueKey& key) {
42 return static_cast<T*>(this->findAndRefResourceByUniqueKey(key));
43 }
44
45 /**
46 * Either finds and refs, or creates an index buffer for instanced drawing with a specific
47 * pattern if the index buffer is not found. If the return is non-null, the caller owns
48 * a ref on the returned GrIndexBuffer.
49 *
50 * @param pattern the pattern of indices to repeat
51 * @param patternSize size in bytes of the pattern
52 * @param reps number of times to repeat the pattern
53 * @param vertCount number of vertices the pattern references
54 * @param key Key to be assigned to the index buffer.
55 *
halcanary96fcdcc2015-08-27 07:41:13 -070056 * @return The index buffer if successful, otherwise nullptr.
bsalomoned0bcad2015-05-04 10:36:42 -070057 */
bsalomoneae62002015-07-31 13:59:30 -070058 const GrIndexBuffer* findOrCreateInstancedIndexBuffer(const uint16_t* pattern,
59 int patternSize,
60 int reps,
61 int vertCount,
62 const GrUniqueKey& key) {
bsalomoned0bcad2015-05-04 10:36:42 -070063 if (GrIndexBuffer* buffer = this->findAndRefTByUniqueKey<GrIndexBuffer>(key)) {
64 return buffer;
65 }
66 return this->createInstancedIndexBuffer(pattern, patternSize, reps, vertCount, key);
67 }
68
69 /**
70 * Returns an index buffer that can be used to render quads.
71 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
72 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
73 * Draw with kTriangles_GrPrimitiveType
74 * @ return the quad index buffer
75 */
76 const GrIndexBuffer* refQuadIndexBuffer() {
77 if (GrIndexBuffer* buffer =
78 this->findAndRefTByUniqueKey<GrIndexBuffer>(fQuadIndexBufferKey)) {
79 return buffer;
80 }
81 return this->createQuadIndexBuffer();
82 }
83
bsalomon706f08f2015-05-22 07:35:58 -070084 /**
85 * Factories for GrPath and GrPathRange objects. It's an error to call these if path rendering
86 * is not supported.
87 */
88 GrPath* createPath(const SkPath&, const GrStrokeInfo&);
89 GrPathRange* createPathRange(GrPathRange::PathGenerator*, const GrStrokeInfo&);
90 GrPathRange* createGlyphs(const SkTypeface*, const SkDescriptor*, const GrStrokeInfo&);
91
bsalomond309e7a2015-04-30 14:18:54 -070092 using GrTextureProvider::assignUniqueKeyToResource;
93 using GrTextureProvider::findAndRefResourceByUniqueKey;
bsalomon473addf2015-10-02 07:49:05 -070094 using GrTextureProvider::findAndRefTextureByUniqueKey;
bsalomond309e7a2015-04-30 14:18:54 -070095 using GrTextureProvider::abandon;
96
bsalomoneae62002015-07-31 13:59:30 -070097 enum Flags {
98 /** If the caller intends to do direct reads/writes to/from the CPU then this flag must be
99 * set when accessing resources during a GrDrawTarget flush. This includes the execution of
100 * GrBatch objects. The reason is that these memory operations are done immediately and
101 * will occur out of order WRT the operations being flushed.
halcanary6950de62015-11-07 05:29:00 -0800102 * Make this automatic: https://bug.skia.org/4156
bsalomoneae62002015-07-31 13:59:30 -0700103 */
104 kNoPendingIO_Flag = kNoPendingIO_ScratchTextureFlag,
105 };
106
107 enum BufferUsage {
108 /** Caller intends to specify the buffer data rarely with respect to the number of draws
109 that read the data. */
110 kStatic_BufferUsage,
111 /** Caller intends to respecify the buffer data frequently between draws. */
112 kDynamic_BufferUsage,
113 };
114 GrIndexBuffer* createIndexBuffer(size_t size, BufferUsage, uint32_t flags);
115 GrVertexBuffer* createVertexBuffer(size_t size, BufferUsage, uint32_t flags);
jvanverth17aa0472016-01-05 10:41:27 -0800116 GrTransferBuffer* createTransferBuffer(size_t size, TransferType, uint32_t flags);
bsalomoneae62002015-07-31 13:59:30 -0700117
118 GrTexture* createApproxTexture(const GrSurfaceDesc& desc, uint32_t flags) {
119 SkASSERT(0 == flags || kNoPendingIO_Flag == flags);
120 return this->internalCreateApproxTexture(desc, flags);
121 }
robertphillips1b8e1b52015-06-24 06:54:10 -0700122
joshualittb356cbc2015-08-05 06:36:39 -0700123 /** Returns a GrBatchAtlas. This function can be called anywhere, but the returned atlas should
124 * only be used inside of GrBatch::generateGeometry
125 * @param GrPixelConfig The pixel config which this atlas will store
126 * @param width width in pixels of the atlas
127 * @param height height in pixels of the atlas
128 * @param numPlotsX The number of plots the atlas should be broken up into in the X
129 * direction
130 * @param numPlotsY The number of plots the atlas should be broken up into in the Y
131 * direction
132 * @param func An eviction function which will be called whenever the atlas has to
133 * evict data
134 * @param data User supplied data which will be passed into func whenver an
135 * eviction occurs
136 *
halcanary96fcdcc2015-08-27 07:41:13 -0700137 * @return An initialized GrBatchAtlas, or nullptr if creation fails
joshualittb356cbc2015-08-05 06:36:39 -0700138 */
139 GrBatchAtlas* createAtlas(GrPixelConfig, int width, int height, int numPlotsX, int numPlotsY,
140 GrBatchAtlas::EvictionFunc func, void* data);
141
egdanielec00d942015-09-14 12:56:10 -0700142 /**
143 * If passed in render target already has a stencil buffer, return it. Otherwise attempt to
144 * attach one.
145 */
146 GrStencilAttachment* attachStencilAttachment(GrRenderTarget* rt);
147
bsalomon473addf2015-10-02 07:49:05 -0700148 const GrCaps* caps() { return this->gpu()->caps(); }
149
ericrkf7b8b8a2016-02-24 14:49:51 -0800150 /**
151 * Wraps an existing texture with a GrRenderTarget object. This is useful when the provided
152 * texture has a format that cannot be textured from by Skia, but we want to raster to it.
153 *
154 * @return GrRenderTarget object or NULL on failure.
155 */
156 GrRenderTarget* wrapBackendTextureAsRenderTarget(const GrBackendTextureDesc& desc,
157 GrWrapOwnership = kBorrow_GrWrapOwnership);
158
bsalomoned0bcad2015-05-04 10:36:42 -0700159private:
160 const GrIndexBuffer* createInstancedIndexBuffer(const uint16_t* pattern,
161 int patternSize,
162 int reps,
163 int vertCount,
164 const GrUniqueKey& key);
165
166 const GrIndexBuffer* createQuadIndexBuffer();
167
168 GrUniqueKey fQuadIndexBufferKey;
169
bsalomond309e7a2015-04-30 14:18:54 -0700170 typedef GrTextureProvider INHERITED;
171};
172
173#endif