blob: fe0b5defa53f245ca27bde3d2efce0d214acdec8 [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&);
88 GrPathRange* createGlyphs(const SkTypeface*, const SkDescriptor*, const GrStrokeInfo&);
89
bsalomond309e7a2015-04-30 14:18:54 -070090 using GrTextureProvider::assignUniqueKeyToResource;
91 using GrTextureProvider::findAndRefResourceByUniqueKey;
bsalomon473addf2015-10-02 07:49:05 -070092 using GrTextureProvider::findAndRefTextureByUniqueKey;
bsalomond309e7a2015-04-30 14:18:54 -070093 using GrTextureProvider::abandon;
94
bsalomoneae62002015-07-31 13:59:30 -070095 enum Flags {
96 /** If the caller intends to do direct reads/writes to/from the CPU then this flag must be
97 * set when accessing resources during a GrDrawTarget flush. This includes the execution of
98 * GrBatch objects. The reason is that these memory operations are done immediately and
99 * will occur out of order WRT the operations being flushed.
halcanary6950de62015-11-07 05:29:00 -0800100 * Make this automatic: https://bug.skia.org/4156
bsalomoneae62002015-07-31 13:59:30 -0700101 */
102 kNoPendingIO_Flag = kNoPendingIO_ScratchTextureFlag,
103 };
104
cdalton397536c2016-03-25 12:15:03 -0700105 GrBuffer* createBuffer(GrBufferType, size_t size, GrAccessPattern, uint32_t flags);
bsalomoneae62002015-07-31 13:59:30 -0700106
107 GrTexture* createApproxTexture(const GrSurfaceDesc& desc, uint32_t flags) {
108 SkASSERT(0 == flags || kNoPendingIO_Flag == flags);
109 return this->internalCreateApproxTexture(desc, flags);
110 }
robertphillips1b8e1b52015-06-24 06:54:10 -0700111
joshualittb356cbc2015-08-05 06:36:39 -0700112 /** Returns a GrBatchAtlas. This function can be called anywhere, but the returned atlas should
113 * only be used inside of GrBatch::generateGeometry
114 * @param GrPixelConfig The pixel config which this atlas will store
115 * @param width width in pixels of the atlas
116 * @param height height in pixels of the atlas
117 * @param numPlotsX The number of plots the atlas should be broken up into in the X
118 * direction
119 * @param numPlotsY The number of plots the atlas should be broken up into in the Y
120 * direction
121 * @param func An eviction function which will be called whenever the atlas has to
122 * evict data
123 * @param data User supplied data which will be passed into func whenver an
124 * eviction occurs
125 *
halcanary96fcdcc2015-08-27 07:41:13 -0700126 * @return An initialized GrBatchAtlas, or nullptr if creation fails
joshualittb356cbc2015-08-05 06:36:39 -0700127 */
128 GrBatchAtlas* createAtlas(GrPixelConfig, int width, int height, int numPlotsX, int numPlotsY,
129 GrBatchAtlas::EvictionFunc func, void* data);
130
egdanielec00d942015-09-14 12:56:10 -0700131 /**
132 * If passed in render target already has a stencil buffer, return it. Otherwise attempt to
133 * attach one.
134 */
135 GrStencilAttachment* attachStencilAttachment(GrRenderTarget* rt);
136
bsalomon473addf2015-10-02 07:49:05 -0700137 const GrCaps* caps() { return this->gpu()->caps(); }
138
ericrkf7b8b8a2016-02-24 14:49:51 -0800139 /**
140 * Wraps an existing texture with a GrRenderTarget object. This is useful when the provided
141 * texture has a format that cannot be textured from by Skia, but we want to raster to it.
142 *
kkinnunen49c4c222016-04-01 04:50:37 -0700143 * The texture is wrapped as borrowed. The texture object will not be freed once the
144 * render target is destroyed.
145 *
ericrkf7b8b8a2016-02-24 14:49:51 -0800146 * @return GrRenderTarget object or NULL on failure.
147 */
kkinnunen49c4c222016-04-01 04:50:37 -0700148 GrRenderTarget* wrapBackendTextureAsRenderTarget(const GrBackendTextureDesc& desc);
ericrkf7b8b8a2016-02-24 14:49:51 -0800149
bsalomoned0bcad2015-05-04 10:36:42 -0700150private:
cdalton397536c2016-03-25 12:15:03 -0700151 const GrBuffer* createInstancedIndexBuffer(const uint16_t* pattern,
152 int patternSize,
153 int reps,
154 int vertCount,
155 const GrUniqueKey& key);
bsalomoned0bcad2015-05-04 10:36:42 -0700156
cdalton397536c2016-03-25 12:15:03 -0700157 const GrBuffer* createQuadIndexBuffer();
bsalomoned0bcad2015-05-04 10:36:42 -0700158
159 GrUniqueKey fQuadIndexBufferKey;
160
bsalomond309e7a2015-04-30 14:18:54 -0700161 typedef GrTextureProvider INHERITED;
162};
163
164#endif