blob: ea3a28adc95f55de8d7a38239a115513e2162f04 [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;
bsalomon6663acf2016-05-10 09:14:17 -070021class GrStyle;
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 */
bsalomon6663acf2016-05-10 09:14:17 -070086 GrPath* createPath(const SkPath&, const GrStyle&);
87 GrPathRange* createPathRange(GrPathRange::PathGenerator*, const GrStyle&);
reeda9322c22016-04-12 06:47:05 -070088 GrPathRange* createGlyphs(const SkTypeface*, const SkScalerContextEffects&,
bsalomon6663acf2016-05-10 09:14:17 -070089 const SkDescriptor*, const GrStyle&);
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.
cdalton1bf3e712016-04-19 10:00:02 -0700113 * @param data optional data with which to initialize the buffer.
cdaltone2e71c22016-04-07 18:13:29 -0700114 *
115 * @return the buffer if successful, otherwise nullptr.
116 */
cdalton1bf3e712016-04-19 10:00:02 -0700117 GrBuffer* createBuffer(size_t size, GrBufferType intendedType, GrAccessPattern, uint32_t flags,
118 const void* data = nullptr);
bsalomoneae62002015-07-31 13:59:30 -0700119
120 GrTexture* createApproxTexture(const GrSurfaceDesc& desc, uint32_t flags) {
121 SkASSERT(0 == flags || kNoPendingIO_Flag == flags);
122 return this->internalCreateApproxTexture(desc, flags);
123 }
robertphillips1b8e1b52015-06-24 06:54:10 -0700124
joshualittb356cbc2015-08-05 06:36:39 -0700125 /** Returns a GrBatchAtlas. This function can be called anywhere, but the returned atlas should
126 * only be used inside of GrBatch::generateGeometry
127 * @param GrPixelConfig The pixel config which this atlas will store
128 * @param width width in pixels of the atlas
129 * @param height height in pixels of the atlas
130 * @param numPlotsX The number of plots the atlas should be broken up into in the X
131 * direction
132 * @param numPlotsY The number of plots the atlas should be broken up into in the Y
133 * direction
134 * @param func An eviction function which will be called whenever the atlas has to
135 * evict data
136 * @param data User supplied data which will be passed into func whenver an
137 * eviction occurs
138 *
halcanary96fcdcc2015-08-27 07:41:13 -0700139 * @return An initialized GrBatchAtlas, or nullptr if creation fails
joshualittb356cbc2015-08-05 06:36:39 -0700140 */
141 GrBatchAtlas* createAtlas(GrPixelConfig, int width, int height, int numPlotsX, int numPlotsY,
142 GrBatchAtlas::EvictionFunc func, void* data);
143
egdanielec00d942015-09-14 12:56:10 -0700144 /**
145 * If passed in render target already has a stencil buffer, return it. Otherwise attempt to
146 * attach one.
147 */
148 GrStencilAttachment* attachStencilAttachment(GrRenderTarget* rt);
149
bsalomon473addf2015-10-02 07:49:05 -0700150 const GrCaps* caps() { return this->gpu()->caps(); }
151
ericrkf7b8b8a2016-02-24 14:49:51 -0800152 /**
153 * Wraps an existing texture with a GrRenderTarget object. This is useful when the provided
154 * texture has a format that cannot be textured from by Skia, but we want to raster to it.
155 *
kkinnunen49c4c222016-04-01 04:50:37 -0700156 * The texture is wrapped as borrowed. The texture object will not be freed once the
157 * render target is destroyed.
158 *
ericrkf7b8b8a2016-02-24 14:49:51 -0800159 * @return GrRenderTarget object or NULL on failure.
160 */
kkinnunen49c4c222016-04-01 04:50:37 -0700161 GrRenderTarget* wrapBackendTextureAsRenderTarget(const GrBackendTextureDesc& desc);
ericrkf7b8b8a2016-02-24 14:49:51 -0800162
bsalomoned0bcad2015-05-04 10:36:42 -0700163private:
cdalton397536c2016-03-25 12:15:03 -0700164 const GrBuffer* createInstancedIndexBuffer(const uint16_t* pattern,
165 int patternSize,
166 int reps,
167 int vertCount,
168 const GrUniqueKey& key);
bsalomoned0bcad2015-05-04 10:36:42 -0700169
cdalton397536c2016-03-25 12:15:03 -0700170 const GrBuffer* createQuadIndexBuffer();
bsalomoned0bcad2015-05-04 10:36:42 -0700171
172 GrUniqueKey fQuadIndexBufferKey;
173
bsalomond309e7a2015-04-30 14:18:54 -0700174 typedef GrTextureProvider INHERITED;
175};
176
177#endif