blob: 5beb70a3d8773d9dc783f67070b7c7d777846900 [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"
cdalton397536c2016-03-25 12:15:03 -070012#include "GrBuffer.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;
bsalomon706f08f2015-05-22 07:35:58 -070017class GrPath;
egdanielec00d942015-09-14 12:56:10 -070018class GrRenderTarget;
joshualitt6d0872d2016-01-11 08:27:48 -080019class GrSingleOwner;
egdanielec00d942015-09-14 12:56:10 -070020class GrStencilAttachment;
bsalomon706f08f2015-05-22 07:35:58 -070021class GrStrokeInfo;
bsalomon706f08f2015-05-22 07:35:58 -070022class SkDescriptor;
23class SkPath;
24class SkTypeface;
bsalomoned0bcad2015-05-04 10:36:42 -070025
bsalomond309e7a2015-04-30 14:18:54 -070026/**
27 * An extension of the texture provider for arbitrary resource types. This class is intended for
28 * use within the Gr code base, not by clients or extensions (e.g. third party GrProcessor
29 * derivatives).
bsalomoneae62002015-07-31 13:59:30 -070030 *
31 * This currently inherits from GrTextureProvider non-publically to force callers to provider
32 * make a flags (pendingIO) decision and not use the GrTP methods that don't take flags. This
halcanary6950de62015-11-07 05:29:00 -080033 * can be relaxed once https://bug.skia.org/4156 is fixed.
bsalomond309e7a2015-04-30 14:18:54 -070034 */
bsalomoneae62002015-07-31 13:59:30 -070035class GrResourceProvider : protected GrTextureProvider {
bsalomond309e7a2015-04-30 14:18:54 -070036public:
joshualitt6d0872d2016-01-11 08:27:48 -080037 GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner);
bsalomoned0bcad2015-05-04 10:36:42 -070038
39 template <typename T> T* findAndRefTByUniqueKey(const GrUniqueKey& key) {
40 return static_cast<T*>(this->findAndRefResourceByUniqueKey(key));
41 }
42
43 /**
44 * Either finds and refs, or creates an index buffer for instanced drawing with a specific
45 * pattern if the index buffer is not found. If the return is non-null, the caller owns
cdalton397536c2016-03-25 12:15:03 -070046 * a ref on the returned GrBuffer.
bsalomoned0bcad2015-05-04 10:36:42 -070047 *
48 * @param pattern the pattern of indices to repeat
49 * @param patternSize size in bytes of the pattern
50 * @param reps number of times to repeat the pattern
51 * @param vertCount number of vertices the pattern references
52 * @param key Key to be assigned to the index buffer.
53 *
halcanary96fcdcc2015-08-27 07:41:13 -070054 * @return The index buffer if successful, otherwise nullptr.
bsalomoned0bcad2015-05-04 10:36:42 -070055 */
cdalton397536c2016-03-25 12:15:03 -070056 const GrBuffer* findOrCreateInstancedIndexBuffer(const uint16_t* pattern,
57 int patternSize,
58 int reps,
59 int vertCount,
60 const GrUniqueKey& key) {
61 if (GrBuffer* buffer = this->findAndRefTByUniqueKey<GrBuffer>(key)) {
bsalomoned0bcad2015-05-04 10:36:42 -070062 return buffer;
63 }
64 return this->createInstancedIndexBuffer(pattern, patternSize, reps, vertCount, key);
65 }
66
67 /**
68 * Returns an index buffer that can be used to render quads.
69 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
cdalton397536c2016-03-25 12:15:03 -070070 * The max number of quads is the buffer's index capacity divided by 6.
bsalomoned0bcad2015-05-04 10:36:42 -070071 * Draw with kTriangles_GrPrimitiveType
72 * @ return the quad index buffer
73 */
cdalton397536c2016-03-25 12:15:03 -070074 const GrBuffer* refQuadIndexBuffer() {
75 if (GrBuffer* buffer =
76 this->findAndRefTByUniqueKey<GrBuffer>(fQuadIndexBufferKey)) {
bsalomoned0bcad2015-05-04 10:36:42 -070077 return buffer;
78 }
79 return this->createQuadIndexBuffer();
80 }
81
bsalomon706f08f2015-05-22 07:35:58 -070082 /**
83 * Factories for GrPath and GrPathRange objects. It's an error to call these if path rendering
84 * is not supported.
85 */
86 GrPath* createPath(const SkPath&, const GrStrokeInfo&);
87 GrPathRange* createPathRange(GrPathRange::PathGenerator*, const GrStrokeInfo&);
reeda9322c22016-04-12 06:47:05 -070088 GrPathRange* createGlyphs(const SkTypeface*, const SkScalerContextEffects&,
89 const SkDescriptor*, const GrStrokeInfo&);
bsalomon706f08f2015-05-22 07:35:58 -070090
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.
halcanary6950de62015-11-07 05:29:00 -0800101 * Make this automatic: https://bug.skia.org/4156
bsalomoneae62002015-07-31 13:59:30 -0700102 */
103 kNoPendingIO_Flag = kNoPendingIO_ScratchTextureFlag,
104 };
105
cdaltone2e71c22016-04-07 18:13:29 -0700106 /**
107 * Returns a buffer.
108 *
109 * @param size minimum size of buffer to return.
110 * @param intendedType hint to the graphics subsystem about what the buffer will be used for.
111 * @param GrAccessPattern hint to the graphics subsystem about how the data will be accessed.
112 * @param flags see Flags enum.
113 *
114 * @return the buffer if successful, otherwise nullptr.
115 */
116 GrBuffer* createBuffer(size_t size, GrBufferType intendedType, GrAccessPattern, 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 *
kkinnunen49c4c222016-04-01 04:50:37 -0700154 * The texture is wrapped as borrowed. The texture object will not be freed once the
155 * render target is destroyed.
156 *
ericrkf7b8b8a2016-02-24 14:49:51 -0800157 * @return GrRenderTarget object or NULL on failure.
158 */
kkinnunen49c4c222016-04-01 04:50:37 -0700159 GrRenderTarget* wrapBackendTextureAsRenderTarget(const GrBackendTextureDesc& desc);
ericrkf7b8b8a2016-02-24 14:49:51 -0800160
bsalomoned0bcad2015-05-04 10:36:42 -0700161private:
cdalton397536c2016-03-25 12:15:03 -0700162 const GrBuffer* createInstancedIndexBuffer(const uint16_t* pattern,
163 int patternSize,
164 int reps,
165 int vertCount,
166 const GrUniqueKey& key);
bsalomoned0bcad2015-05-04 10:36:42 -0700167
cdalton397536c2016-03-25 12:15:03 -0700168 const GrBuffer* createQuadIndexBuffer();
bsalomoned0bcad2015-05-04 10:36:42 -0700169
170 GrUniqueKey fQuadIndexBufferKey;
171
bsalomond309e7a2015-04-30 14:18:54 -0700172 typedef GrTextureProvider INHERITED;
173};
174
175#endif