blob: 76953655037b82ee033e5238dcc9f005d2627c64 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 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.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#ifndef GrDrawTarget_DEFINED
9#define GrDrawTarget_DEFINED
10
bsalomon@google.com8d67c072012-12-13 20:38:14 +000011#include "GrClipData.h"
joshualitt6db519c2014-10-29 08:48:18 -070012#include "GrClipMaskManager.h"
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +000013#include "GrContext.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000014#include "GrDrawState.h"
bsalomon@google.com934c5702012-03-20 21:17:58 +000015#include "GrIndexBuffer.h"
kkinnunenccdaa042014-08-20 01:36:23 -070016#include "GrPathRendering.h"
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +000017#include "GrTraceMarker.h"
joshualitt7eb8c7b2014-11-18 14:24:27 -080018#include "GrVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000019
bsalomon@google.com8d67c072012-12-13 20:38:14 +000020#include "SkClipStack.h"
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000021#include "SkMatrix.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000022#include "SkPath.h"
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +000023#include "SkStrokeRec.h"
robertphillips@google.coma2d71482012-08-01 20:08:47 +000024#include "SkTArray.h"
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000025#include "SkTLazy.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000026#include "SkTypes.h"
bsalomon@google.com8d67c072012-12-13 20:38:14 +000027#include "SkXfermode.h"
Scroggo97c88c22011-05-11 14:05:25 +000028
robertphillips@google.coma2d71482012-08-01 20:08:47 +000029class GrClipData;
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000030class GrDrawTargetCaps;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000031class GrPath;
cdaltonb85a0aa2014-07-21 15:32:44 -070032class GrPathRange;
sugoi@google.com12b4e272012-12-06 20:13:11 +000033
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000034class GrDrawTarget : public SkRefCnt {
bsalomon@google.comf6601872012-08-28 21:11:35 +000035public:
36 SK_DECLARE_INST_COUNT(GrDrawTarget)
37
cdalton55b24af2014-11-25 11:00:56 -080038 typedef GrPathRange::PathIndexType PathIndexType;
39 typedef GrPathRendering::PathTransformType PathTransformType;
kkinnunenccdaa042014-08-20 01:36:23 -070040
reed@google.comac10a2d2010-12-22 21:39:39 +000041 ///////////////////////////////////////////////////////////////////////////
42
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000043 // The context may not be fully constructed and should not be used during GrDrawTarget
44 // construction.
45 GrDrawTarget(GrContext* context);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000046 virtual ~GrDrawTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +000047
48 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000049 * Gets the capabilities of the draw target.
50 */
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000051 const GrDrawTargetCaps* caps() const { return fCaps.get(); }
bsalomon@google.com18c9c192011-09-22 21:01:31 +000052
53 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000054 * Sets the current clip to the region specified by clip. All draws will be
55 * clipped against this clip if kClip_StateBit is enabled.
56 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000057 * Setting the clip may (or may not) zero out the client's stencil bits.
58 *
reed@google.comac10a2d2010-12-22 21:39:39 +000059 * @param description of the clipping region
60 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +000061 void setClip(const GrClipData* clip);
reed@google.comac10a2d2010-12-22 21:39:39 +000062
63 /**
64 * Gets the current clip.
65 *
66 * @return the clip.
67 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +000068 const GrClipData* getClip() const;
reed@google.comac10a2d2010-12-22 21:39:39 +000069
bsalomon@google.coma5d056a2012-03-27 15:59:58 +000070 /**
bsalomonf90a02b2014-11-26 12:28:00 -080071 * There are two types of "sources" of geometry (vertices and indices) for
bsalomon@google.come3d70952012-03-13 12:40:53 +000072 * draw calls made on the target. When performing an indexed draw, the
73 * indices and vertices can use different source types. Once a source is
bsalomon@google.com934c5702012-03-20 21:17:58 +000074 * specified it can be used for multiple draws. However, the time at which
75 * the geometry data is no longer editable depends on the source type.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000076 *
77 * Sometimes it is necessary to perform a draw while upstack code has
bsalomon@google.come3d70952012-03-13 12:40:53 +000078 * already specified geometry that it isn't finished with. So there are push
79 * and pop methods. This allows the client to push the sources, draw
80 * something using alternate sources, and then pop to restore the original
81 * sources.
bsalomon@google.com1c13c962011-02-14 16:51:21 +000082 *
bsalomon@google.come3d70952012-03-13 12:40:53 +000083 * Aside from pushes and pops, a source remains valid until another source
84 * is set or resetVertexSource / resetIndexSource is called. Drawing from
85 * a reset source is an error.
86 *
bsalomonf90a02b2014-11-26 12:28:00 -080087 * The two types of sources are:
bsalomon@google.come3d70952012-03-13 12:40:53 +000088 *
bsalomonf90a02b2014-11-26 12:28:00 -080089 * 1. Reserve. This is most useful when the caller has data it must
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000090 * transform before drawing and is not long-lived. The caller requests
91 * that the draw target make room for some amount of vertex and/or index
92 * data. The target provides ptrs to hold the vertex and/or index data.
93 *
rmistry@google.comd6176b02012-08-23 18:14:13 +000094 * The data is writable up until the next drawIndexed, drawNonIndexed,
bsalomon@google.come4617bf2013-04-03 14:56:40 +000095 * drawIndexedInstances, drawRect, copySurface, or pushGeometrySource. At
96 * this point the data is frozen and the ptrs are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +000097 *
bsalomon@google.come3d70952012-03-13 12:40:53 +000098 * Where the space is allocated and how it is uploaded to the GPU is
99 * subclass-dependent.
100 *
bsalomonf90a02b2014-11-26 12:28:00 -0800101 * 2. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.come3d70952012-03-13 12:40:53 +0000102 * is long-lived. When the data in the buffer is consumed depends on the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000103 * GrDrawTarget subclass. For deferred subclasses the caller has to
bsalomon@google.come3d70952012-03-13 12:40:53 +0000104 * guarantee that the data is still available in the buffers at playback.
105 * (TODO: Make this more automatic as we have done for read/write pixels)
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000106 */
107
108 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000109 * Reserves space for vertices and/or indices. Zero can be specifed as
110 * either the vertex or index count if the caller desires to only reserve
rmistry@google.comd6176b02012-08-23 18:14:13 +0000111 * space for only indices or only vertices. If zero is specifed for
bsalomon@google.come3d70952012-03-13 12:40:53 +0000112 * vertexCount then the vertex source will be unmodified and likewise for
113 * indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000114 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000115 * If the function returns true then the reserve suceeded and the vertices
116 * and indices pointers will point to the space created.
reed@google.comac10a2d2010-12-22 21:39:39 +0000117 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000118 * If the target cannot make space for the request then this function will
119 * return false. If vertexCount was non-zero then upon failure the vertex
120 * source is reset and likewise for indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000121 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000122 * The pointers to the space allocated for vertices and indices remain valid
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000123 * until a drawIndexed, drawNonIndexed, drawIndexedInstances, drawRect,
124 * copySurface, or push/popGeomtrySource is called. At that point logically a
125 * snapshot of the data is made and the pointers are invalid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000126 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000127 * @param vertexCount the number of vertices to reserve space for. Can be
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000128 * 0. Vertex size is queried from the current GrDrawState.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000129 * @param indexCount the number of indices to reserve space for. Can be 0.
bsalomon@google.come3d70952012-03-13 12:40:53 +0000130 * @param vertices will point to reserved vertex space if vertexCount is
rmistry@google.comd6176b02012-08-23 18:14:13 +0000131 * non-zero. Illegal to pass NULL if vertexCount > 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000132 * @param indices will point to reserved index space if indexCount is
133 * non-zero. Illegal to pass NULL if indexCount > 0.
134 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000135 bool reserveVertexAndIndexSpace(int vertexCount,
joshualitt9853cce2014-11-17 14:22:48 -0800136 size_t vertexStride,
bsalomon@google.com97805382012-03-13 14:32:07 +0000137 int indexCount,
138 void** vertices,
139 void** indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000140
reed@google.comac10a2d2010-12-22 21:39:39 +0000141 /**
142 * Provides hints to caller about the number of vertices and indices
143 * that can be allocated cheaply. This can be useful if caller is reserving
144 * space but doesn't know exactly how much geometry is needed.
145 *
146 * Also may hint whether the draw target should be flushed first. This is
147 * useful for deferred targets.
148 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000149 * @param vertexCount in: hint about how many vertices the caller would
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000150 * like to allocate. Vertex size is queried from the
151 * current GrDrawState.
reed@google.comac10a2d2010-12-22 21:39:39 +0000152 * out: a hint about the number of vertices that can be
153 * allocated cheaply. Negative means no hint.
154 * Ignored if NULL.
155 * @param indexCount in: hint about how many indices the caller would
156 * like to allocate.
157 * out: a hint about the number of indices that can be
158 * allocated cheaply. Negative means no hint.
159 * Ignored if NULL.
160 *
161 * @return true if target should be flushed based on the input values.
162 */
joshualitt9853cce2014-11-17 14:22:48 -0800163 virtual bool geometryHints(size_t vertexStride, int* vertexCount, int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000164
165 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000166 * Sets source of vertex data for the next draw. Data does not have to be
bsalomon@google.com934c5702012-03-20 21:17:58 +0000167 * in the buffer until drawIndexed, drawNonIndexed, or drawIndexedInstances.
reed@google.comac10a2d2010-12-22 21:39:39 +0000168 *
169 * @param buffer vertex buffer containing vertex data. Must be
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000170 * unlocked before draw call. Vertex size is queried
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000171 * from current GrDrawState.
reed@google.comac10a2d2010-12-22 21:39:39 +0000172 */
joshualitt9853cce2014-11-17 14:22:48 -0800173 void setVertexSourceToBuffer(const GrVertexBuffer* buffer, size_t vertexStride);
reed@google.comac10a2d2010-12-22 21:39:39 +0000174
175 /**
176 * Sets source of index data for the next indexed draw. Data does not have
bsalomon@google.com934c5702012-03-20 21:17:58 +0000177 * to be in the buffer until drawIndexed.
reed@google.comac10a2d2010-12-22 21:39:39 +0000178 *
179 * @param buffer index buffer containing indices. Must be unlocked
180 * before indexed draw call.
181 */
182 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000183
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000184 /**
185 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
186 * source to reserved, array, or buffer before next draw. May be able to free
187 * up temporary storage allocated by setVertexSourceToArray or
188 * reserveVertexSpace.
189 */
190 void resetVertexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000191
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000192 /**
193 * Resets index source. Indexed Drawing from reset indices is illegal. Set
194 * index source to reserved, array, or buffer before next indexed draw. May
195 * be able to free up temporary storage allocated by setIndexSourceToArray
196 * or reserveIndexSpace.
197 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000198 void resetIndexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000199
bsalomon@google.com97805382012-03-13 14:32:07 +0000200 /**
201 * Query to find out if the vertex or index source is reserved.
202 */
203 bool hasReservedVerticesOrIndices() const {
bsalomon@google.com73d98aa2012-03-13 14:41:19 +0000204 return kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc ||
bsalomon@google.com97805382012-03-13 14:32:07 +0000205 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
206 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000207
208 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000209 * Pushes and resets the vertex/index sources. Any reserved vertex / index
210 * data is finalized (i.e. cannot be updated after the matching pop but can
211 * be drawn from). Must be balanced by a pop.
212 */
213 void pushGeometrySource();
214
215 /**
216 * Pops the vertex / index sources from the matching push.
217 */
218 void popGeometrySource();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000219
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000220 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000221 * Draws indexed geometry using the current state and current vertex / index
222 * sources.
223 *
224 * @param type The type of primitives to draw.
225 * @param startVertex the vertex in the vertex array/buffer corresponding
226 * to index 0
227 * @param startIndex first index to read from index src.
228 * @param vertexCount one greater than the max index.
229 * @param indexCount the number of index elements to read. The index count
230 * is effectively trimmed to the last completely
231 * specified primitive.
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000232 * @param devBounds optional bounds hint. This is a promise from the caller,
233 * not a request for clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000234 */
joshualitt9853cce2014-11-17 14:22:48 -0800235 void drawIndexed(GrDrawState*,
236 GrPrimitiveType type,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000237 int startVertex,
238 int startIndex,
239 int vertexCount,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000240 int indexCount,
241 const SkRect* devBounds = NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +0000242
243 /**
244 * Draws non-indexed geometry using the current state and current vertex
245 * sources.
246 *
247 * @param type The type of primitives to draw.
248 * @param startVertex the vertex in the vertex array/buffer corresponding
249 * to index 0
250 * @param vertexCount one greater than the max index.
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000251 * @param devBounds optional bounds hint. This is a promise from the caller,
252 * not a request for clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000253 */
joshualitt9853cce2014-11-17 14:22:48 -0800254 void drawNonIndexed(GrDrawState*,
255 GrPrimitiveType type,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000256 int startVertex,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000257 int vertexCount,
258 const SkRect* devBounds = NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +0000259
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000260 /**
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000261 * Draws path into the stencil buffer. The fill must be either even/odd or
262 * winding (not inverse or hairline). It will respect the HW antialias flag
joshualitt92e496f2014-10-31 13:56:50 -0700263 * on the draw state (if possible in the 3D API). Note, we will never have an inverse fill
264 * with stencil path
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000265 */
joshualitt9853cce2014-11-17 14:22:48 -0800266 void stencilPath(GrDrawState*, const GrPath*, GrPathRendering::FillType fill);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000267
268 /**
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000269 * Draws a path. Fill must not be a hairline. It will respect the HW
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000270 * antialias flag on the draw state (if possible in the 3D API).
271 */
joshualitt9853cce2014-11-17 14:22:48 -0800272 void drawPath(GrDrawState*, const GrPath*, GrPathRendering::FillType fill);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000273
274 /**
cdalton55b24af2014-11-25 11:00:56 -0800275 * Draws the aggregate path from combining multiple. Note that this will not
276 * always be equivalent to back-to-back calls to drawPath(). It will respect
277 * the HW antialias flag on the draw state (if possible in the 3D API).
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000278 *
cdalton55b24af2014-11-25 11:00:56 -0800279 * @param pathRange Source paths to draw from
280 * @param indices Array of path indices to draw
281 * @param indexType Data type of the array elements in indexBuffer
282 * @param transformValues Array of transforms for the individual paths
283 * @param transformType Type of transforms in transformBuffer
284 * @param count Number of paths to draw
cdaltonb85a0aa2014-07-21 15:32:44 -0700285 * @param fill Fill type for drawing all the paths
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000286 */
joshualitt9853cce2014-11-17 14:22:48 -0800287 void drawPaths(GrDrawState*, const GrPathRange* pathRange,
cdalton55b24af2014-11-25 11:00:56 -0800288 const void* indices,
289 PathIndexType indexType,
290 const float transformValues[],
291 PathTransformType transformType,
joshualitt9853cce2014-11-17 14:22:48 -0800292 int count,
joshualitt92e496f2014-10-31 13:56:50 -0700293 GrPathRendering::FillType fill);
cdaltonb85a0aa2014-07-21 15:32:44 -0700294
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000295 /**
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000296 * Helper function for drawing rects. It performs a geometry src push and pop
297 * and thus will finalize any reserved geometry.
skia.committer@gmail.com044679e2013-02-15 07:16:57 +0000298 *
bsalomon@google.comc7818882013-03-20 19:19:53 +0000299 * @param rect the rect to draw
bsalomon@google.comc7818882013-03-20 19:19:53 +0000300 * @param localRect optional rect that specifies local coords to map onto
301 * rect. If NULL then rect serves as the local coords.
302 * @param localMatrix optional matrix applied to localRect. If
skia.committer@gmail.com044679e2013-02-15 07:16:57 +0000303 * srcRect is non-NULL and srcMatrix is non-NULL
304 * then srcRect will be transformed by srcMatrix.
jvanverth@google.com39768252013-02-14 15:25:44 +0000305 * srcMatrix can be NULL when no srcMatrix is desired.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000306 */
joshualitt9853cce2014-11-17 14:22:48 -0800307 void drawRect(GrDrawState* ds,
308 const SkRect& rect,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000309 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000310 const SkMatrix* localMatrix) {
311 AutoGeometryPush agp(this);
joshualitt9853cce2014-11-17 14:22:48 -0800312 this->onDrawRect(ds, rect, localRect, localMatrix);
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000313 }
jvanverth@google.com39768252013-02-14 15:25:44 +0000314
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000315 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000316 * Helper for drawRect when the caller doesn't need separate local rects or matrices.
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000317 */
joshualitt9853cce2014-11-17 14:22:48 -0800318 void drawSimpleRect(GrDrawState* ds, const SkRect& rect) {
319 this->drawRect(ds, rect, NULL, NULL);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000320 }
joshualitt9853cce2014-11-17 14:22:48 -0800321 void drawSimpleRect(GrDrawState* ds, const SkIRect& irect) {
reed@google.com44699382013-10-31 17:28:30 +0000322 SkRect rect = SkRect::Make(irect);
joshualitt9853cce2014-11-17 14:22:48 -0800323 this->drawRect(ds, rect, NULL, NULL);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000324 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000325
326 /**
bsalomon@google.com934c5702012-03-20 21:17:58 +0000327 * This call is used to draw multiple instances of some geometry with a
328 * given number of vertices (V) and indices (I) per-instance. The indices in
329 * the index source must have the form i[k+I] == i[k] + V. Also, all indices
330 * i[kI] ... i[(k+1)I-1] must be elements of the range kV ... (k+1)V-1. As a
331 * concrete example, the following index buffer for drawing a series of
332 * quads each as two triangles each satisfies these conditions with V=4 and
333 * I=6:
334 * (0,1,2,0,2,3, 4,5,6,4,6,7, 8,9,10,8,10,11, ...)
335 *
336 * The call assumes that the pattern of indices fills the entire index
337 * source. The size of the index buffer limits the number of instances that
338 * can be drawn by the GPU in a single draw. However, the caller may specify
339 * any (positive) number for instanceCount and if necessary multiple GPU
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000340 * draws will be issued. Moreover, when drawIndexedInstances is called
bsalomon@google.com934c5702012-03-20 21:17:58 +0000341 * multiple times it may be possible for GrDrawTarget to group them into a
342 * single GPU draw.
343 *
344 * @param type the type of primitives to draw
345 * @param instanceCount the number of instances to draw. Each instance
346 * consists of verticesPerInstance vertices indexed by
347 * indicesPerInstance indices drawn as the primitive
348 * type specified by type.
349 * @param verticesPerInstance The number of vertices in each instance (V
350 * in the above description).
351 * @param indicesPerInstance The number of indices in each instance (I
352 * in the above description).
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000353 * @param devBounds optional bounds hint. This is a promise from the caller,
354 * not a request for clipping.
bsalomon@google.com934c5702012-03-20 21:17:58 +0000355 */
joshualitt9853cce2014-11-17 14:22:48 -0800356 void drawIndexedInstances(GrDrawState*,
357 GrPrimitiveType type,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000358 int instanceCount,
359 int verticesPerInstance,
360 int indicesPerInstance,
361 const SkRect* devBounds = NULL);
bsalomon@google.com934c5702012-03-20 21:17:58 +0000362
363 /**
bsalomon89c62982014-11-03 12:08:42 -0800364 * Clear the passed in render target. Ignores the draw state and clip. Clears the whole thing if
365 * rect is NULL, otherwise just the rect. If canIgnoreRect is set then the entire render target
366 * can be optionally cleared.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000367 */
joshualitt9853cce2014-11-17 14:22:48 -0800368 void clear(const SkIRect* rect,
369 GrColor color,
370 bool canIgnoreRect,
bsalomon63b21962014-11-05 07:05:34 -0800371 GrRenderTarget* renderTarget);
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000372
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000373 /**
bsalomon89c62982014-11-03 12:08:42 -0800374 * Discards the contents render target.
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000375 **/
bsalomon89c62982014-11-03 12:08:42 -0800376 virtual void discard(GrRenderTarget*) = 0;
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000377
378 /**
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000379 * Called at start and end of gpu trace marking
380 * GR_CREATE_GPU_TRACE_MARKER(marker_str, target) will automatically call these at the start
381 * and end of a code block respectively
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000382 */
egdaniel3eee3832014-06-18 13:09:11 -0700383 void addGpuTraceMarker(const GrGpuTraceMarker* marker);
384 void removeGpuTraceMarker(const GrGpuTraceMarker* marker);
385
386 /**
387 * Takes the current active set of markers and stores them for later use. Any current marker
388 * in the active set is removed from the active set and the targets remove function is called.
389 * These functions do not work as a stack so you cannot call save a second time before calling
390 * restore. Also, it is assumed that when restore is called the current active set of markers
391 * is empty. When the stored markers are added back into the active set, the targets add marker
392 * is called.
393 */
394 void saveActiveTraceMarkers();
395 void restoreActiveTraceMarkers();
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000396
397 /**
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000398 * Copies a pixel rectangle from one surface to another. This call may finalize
399 * reserved vertex/index data (as though a draw call was made). The src pixels
400 * copied are specified by srcRect. They are copied to a rect of the same
401 * size in dst with top left at dstPoint. If the src rect is clipped by the
402 * src bounds then pixel values in the dst rect corresponding to area clipped
403 * by the src rect are not overwritten. This method can fail and return false
404 * depending on the type of surface, configs, etc, and the backend-specific
405 * limitations. If rect is clipped out entirely by the src or dst bounds then
406 * true is returned since there is no actual copy necessary to succeed.
407 */
bsalomonf90a02b2014-11-26 12:28:00 -0800408 bool copySurface(GrSurface* dst,
409 GrSurface* src,
410 const SkIRect& srcRect,
411 const SkIPoint& dstPoint);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000412 /**
bsalomonf90a02b2014-11-26 12:28:00 -0800413 * Function that determines whether a copySurface call would succeed without actually
bsalomon@google.com116ad842013-04-09 15:38:19 +0000414 * performing the copy.
415 */
bsalomonf90a02b2014-11-26 12:28:00 -0800416 bool canCopySurface(const GrSurface* dst,
417 const GrSurface* src,
418 const SkIRect& srcRect,
419 const SkIPoint& dstPoint);
bsalomon@google.comeb851172013-04-15 13:51:00 +0000420
421 /**
robertphillips@google.comff175842012-05-14 19:31:39 +0000422 * Release any resources that are cached but not currently in use. This
423 * is intended to give an application some recourse when resources are low.
424 */
425 virtual void purgeResources() {};
426
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000427 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000428
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000429 class AutoReleaseGeometry : public ::SkNoncopyable {
reed@google.comac10a2d2010-12-22 21:39:39 +0000430 public:
431 AutoReleaseGeometry(GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000432 int vertexCount,
joshualitt9853cce2014-11-17 14:22:48 -0800433 size_t vertexStride,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000434 int indexCount);
435 AutoReleaseGeometry();
436 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000437 bool set(GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000438 int vertexCount,
joshualitt9853cce2014-11-17 14:22:48 -0800439 size_t vertexStride,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000440 int indexCount);
bsalomon49f085d2014-09-05 13:34:00 -0700441 bool succeeded() const { return SkToBool(fTarget); }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000442 void* vertices() const { SkASSERT(this->succeeded()); return fVertices; }
443 void* indices() const { SkASSERT(this->succeeded()); return fIndices; }
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000444 SkPoint* positions() const {
445 return static_cast<SkPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +0000446 }
447
448 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000449 void reset();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000450
reed@google.comac10a2d2010-12-22 21:39:39 +0000451 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000452 void* fVertices;
453 void* fIndices;
454 };
455
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000456 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000457
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000458 class AutoClipRestore : public ::SkNoncopyable {
reed@google.comac10a2d2010-12-22 21:39:39 +0000459 public:
460 AutoClipRestore(GrDrawTarget* target) {
461 fTarget = target;
462 fClip = fTarget->getClip();
463 }
464
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000465 AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip);
466
reed@google.comac10a2d2010-12-22 21:39:39 +0000467 ~AutoClipRestore() {
468 fTarget->setClip(fClip);
469 }
470 private:
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000471 GrDrawTarget* fTarget;
472 const GrClipData* fClip;
473 SkTLazy<SkClipStack> fStack;
474 GrClipData fReplacementClip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000475 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000476
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000477 ////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000478
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000479 /**
480 * Saves the geometry src state at construction and restores in the destructor. It also saves
481 * and then restores the vertex attrib state.
482 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000483 class AutoGeometryPush : public ::SkNoncopyable {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000484 public:
joshualitt9853cce2014-11-17 14:22:48 -0800485 AutoGeometryPush(GrDrawTarget* target) {
bsalomon49f085d2014-09-05 13:34:00 -0700486 SkASSERT(target);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000487 fTarget = target;
488 target->pushGeometrySource();
489 }
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000490
491 ~AutoGeometryPush() { fTarget->popGeometrySource(); }
492
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000493 private:
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000494 GrDrawTarget* fTarget;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000495 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000496
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000497 ///////////////////////////////////////////////////////////////////////////
498 // Draw execution tracking (for font atlases and other resources)
499 class DrawToken {
500 public:
501 DrawToken(GrDrawTarget* drawTarget, uint32_t drawID) :
502 fDrawTarget(drawTarget), fDrawID(drawID) {}
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000503
bsalomon49f085d2014-09-05 13:34:00 -0700504 bool isIssued() { return fDrawTarget && fDrawTarget->isIssued(fDrawID); }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000505
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000506 private:
507 GrDrawTarget* fDrawTarget;
508 uint32_t fDrawID; // this may wrap, but we're doing direct comparison
509 // so that should be okay
510 };
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000511
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000512 virtual DrawToken getCurrentDrawToken() { return DrawToken(this, 0); }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000513
joshualitt3322fa42014-11-07 08:48:51 -0800514 /**
515 * Used to communicate draws to GPUs / subclasses
516 */
517 class DrawInfo {
518 public:
519 DrawInfo(const DrawInfo& di) { (*this) = di; }
520 DrawInfo& operator =(const DrawInfo& di);
521
522 GrPrimitiveType primitiveType() const { return fPrimitiveType; }
523 int startVertex() const { return fStartVertex; }
524 int startIndex() const { return fStartIndex; }
525 int vertexCount() const { return fVertexCount; }
526 int indexCount() const { return fIndexCount; }
527 int verticesPerInstance() const { return fVerticesPerInstance; }
528 int indicesPerInstance() const { return fIndicesPerInstance; }
529 int instanceCount() const { return fInstanceCount; }
530
531 bool isIndexed() const { return fIndexCount > 0; }
532#ifdef SK_DEBUG
533 bool isInstanced() const; // this version is longer because of asserts
534#else
535 bool isInstanced() const { return fInstanceCount > 0; }
536#endif
537
538 // adds or remove instances
539 void adjustInstanceCount(int instanceOffset);
540 // shifts the start vertex
541 void adjustStartVertex(int vertexOffset);
542 // shifts the start index
543 void adjustStartIndex(int indexOffset);
544
545 void setDevBounds(const SkRect& bounds) {
546 fDevBoundsStorage = bounds;
547 fDevBounds = &fDevBoundsStorage;
548 }
joshualitt7eb8c7b2014-11-18 14:24:27 -0800549 const GrVertexBuffer* vertexBuffer() const { return fVertexBuffer.get(); }
550 const GrIndexBuffer* indexBuffer() const { return fIndexBuffer.get(); }
551 void setVertexBuffer(const GrVertexBuffer* vb) {
552 fVertexBuffer.reset(vb);
553 }
554 void setIndexBuffer(const GrIndexBuffer* ib) {
555 fIndexBuffer.reset(ib);
556 }
joshualitt3322fa42014-11-07 08:48:51 -0800557 const SkRect* getDevBounds() const { return fDevBounds; }
558
joshualitt3322fa42014-11-07 08:48:51 -0800559 private:
560 DrawInfo() { fDevBounds = NULL; }
561
562 friend class GrDrawTarget;
563
564 GrPrimitiveType fPrimitiveType;
565
566 int fStartVertex;
567 int fStartIndex;
568 int fVertexCount;
569 int fIndexCount;
570
571 int fInstanceCount;
572 int fVerticesPerInstance;
573 int fIndicesPerInstance;
574
575 SkRect fDevBoundsStorage;
576 SkRect* fDevBounds;
577
joshualitt7eb8c7b2014-11-18 14:24:27 -0800578 GrPendingIOResource<const GrVertexBuffer, kRead_GrIOType> fVertexBuffer;
579 GrPendingIOResource<const GrIndexBuffer, kRead_GrIOType> fIndexBuffer;
joshualitt3322fa42014-11-07 08:48:51 -0800580 };
joshualitt2c93efe2014-11-06 12:57:13 -0800581
joshualitt7eb8c7b2014-11-18 14:24:27 -0800582 virtual void setDrawBuffers(DrawInfo*) = 0;;
joshualitt2c93efe2014-11-06 12:57:13 -0800583 bool programUnitTest(int maxStages);
584
reed@google.comac10a2d2010-12-22 21:39:39 +0000585protected:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000586 enum GeometrySrcType {
587 kNone_GeometrySrcType, //<! src has not been specified
588 kReserved_GeometrySrcType, //<! src was set using reserve*Space
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000589 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
590 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000591
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000592 struct GeometrySrcState {
593 GeometrySrcType fVertexSrc;
594 union {
595 // valid if src type is buffer
596 const GrVertexBuffer* fVertexBuffer;
597 // valid if src type is reserved or array
598 int fVertexCount;
599 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000600
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000601 GeometrySrcType fIndexSrc;
602 union {
603 // valid if src type is buffer
604 const GrIndexBuffer* fIndexBuffer;
605 // valid if src type is reserved or array
606 int fIndexCount;
607 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000608
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000609 size_t fVertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000610 };
bsalomon@google.com934c5702012-03-20 21:17:58 +0000611
612 int indexCountInCurrentSource() const {
613 const GeometrySrcState& src = this->getGeomSrc();
614 switch (src.fIndexSrc) {
615 case kNone_GeometrySrcType:
616 return 0;
617 case kReserved_GeometrySrcType:
bsalomon@google.com934c5702012-03-20 21:17:58 +0000618 return src.fIndexCount;
619 case kBuffer_GeometrySrcType:
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000620 return static_cast<int>(src.fIndexBuffer->gpuMemorySize() / sizeof(uint16_t));
bsalomon@google.com934c5702012-03-20 21:17:58 +0000621 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000622 SkFAIL("Unexpected Index Source.");
bsalomon@google.com934c5702012-03-20 21:17:58 +0000623 return 0;
624 }
625 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000626
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000627 GrContext* getContext() { return fContext; }
628 const GrContext* getContext() const { return fContext; }
629
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000630 // subclasses must call this in their destructors to ensure all vertex
631 // and index sources have been released (including those held by
632 // pushGeometrySource())
633 void releaseGeometry();
634
635 // accessors for derived classes
636 const GeometrySrcState& getGeomSrc() const { return fGeoSrcStateStack.back(); }
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000637 // it is preferable to call this rather than getGeomSrc()->fVertexSize because of the assert.
638 size_t getVertexSize() const {
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000639 // the vertex layout is only valid if a vertex source has been specified.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000640 SkASSERT(this->getGeomSrc().fVertexSrc != kNone_GeometrySrcType);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000641 return this->getGeomSrc().fVertexSize;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000642 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000643
bsalomon@google.combcce8922013-03-25 15:38:39 +0000644 // Subclass must initialize this in its constructor.
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000645 SkAutoTUnref<const GrDrawTargetCaps> fCaps;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000646
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000647 const GrTraceMarkerSet& getActiveTraceMarkers() { return fActiveTraceMarkers; }
648
joshualitt65171342014-10-09 07:25:36 -0700649 // Makes a copy of the dst if it is necessary for the draw. Returns false if a copy is required
650 // but couldn't be made. Otherwise, returns true. This method needs to be protected because it
651 // needs to be accessed by GLPrograms to setup a correct drawstate
joshualitt9853cce2014-11-17 14:22:48 -0800652 bool setupDstReadIfNecessary(GrDrawState*,
653 GrDeviceCoordTexture* dstCopy,
654 const SkRect* drawBounds);
joshualitt65171342014-10-09 07:25:36 -0700655
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000656private:
bsalomonf90a02b2014-11-26 12:28:00 -0800657 /**
658 * This will be called before allocating a texture as a dst for copySurface. This function
659 * populates the dstDesc's config, flags, and origin so as to maximize efficiency and guarantee
660 * success of the copySurface call.
661 */
662 void initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* dstDesc) {
663 if (!this->onInitCopySurfaceDstDesc(src, dstDesc)) {
664 dstDesc->fOrigin = kDefault_GrSurfaceOrigin;
665 dstDesc->fFlags = kRenderTarget_GrSurfaceFlag | kNoStencil_GrSurfaceFlag;
666 dstDesc->fConfig = src->config();
667 }
668 }
669
670 /** Internal implementation of canCopySurface. */
671 bool internalCanCopySurface(const GrSurface* dst,
672 const GrSurface* src,
673 const SkIRect& clippedSrcRect,
674 const SkIPoint& clippedDstRect);
675
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000676 // A subclass can optionally overload this function to be notified before
677 // vertex and index space is reserved.
joshualitt9853cce2014-11-17 14:22:48 -0800678 virtual void willReserveVertexAndIndexSpace(int vertexCount,
679 size_t vertexStride,
680 int indexCount) {}
bsalomon@google.com97805382012-03-13 14:32:07 +0000681
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000682 // implemented by subclass to allocate space for reserved geom
jvanverth@google.coma6338982013-01-31 21:34:25 +0000683 virtual bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices) = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000684 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
685 // implemented by subclass to handle release of reserved geom space
686 virtual void releaseReservedVertexSpace() = 0;
687 virtual void releaseReservedIndexSpace() = 0;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000688 // subclass overrides to be notified just before geo src state is pushed/popped.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000689 virtual void geometrySourceWillPush() = 0;
690 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
691 // subclass called to perform drawing
joshualitt9853cce2014-11-17 14:22:48 -0800692 virtual void onDraw(const GrDrawState&,
693 const DrawInfo&,
joshualitt9176e2c2014-11-20 07:28:52 -0800694 const GrClipMaskManager::ScissorState&,
695 const GrDeviceCoordTexture* dstCopy) = 0;
joshualitt5478d422014-11-14 16:00:38 -0800696 // TODO copy in order drawbuffer onDrawRect to here
joshualitt9853cce2014-11-17 14:22:48 -0800697 virtual void onDrawRect(GrDrawState*,
698 const SkRect& rect,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000699 const SkRect* localRect,
joshualitt5478d422014-11-14 16:00:38 -0800700 const SkMatrix* localMatrix) = 0;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000701
joshualitt9853cce2014-11-17 14:22:48 -0800702 virtual void onStencilPath(const GrDrawState&,
703 const GrPath*,
joshualitt2c93efe2014-11-06 12:57:13 -0800704 const GrClipMaskManager::ScissorState&,
705 const GrStencilSettings&) = 0;
joshualitt9853cce2014-11-17 14:22:48 -0800706 virtual void onDrawPath(const GrDrawState&,
707 const GrPath*,
joshualitt2c93efe2014-11-06 12:57:13 -0800708 const GrClipMaskManager::ScissorState&,
709 const GrStencilSettings&,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000710 const GrDeviceCoordTexture* dstCopy) = 0;
joshualitt9853cce2014-11-17 14:22:48 -0800711 virtual void onDrawPaths(const GrDrawState&,
712 const GrPathRange*,
cdalton55b24af2014-11-25 11:00:56 -0800713 const void* indices,
714 PathIndexType,
715 const float transformValues[],
joshualitt2c93efe2014-11-06 12:57:13 -0800716 PathTransformType,
cdalton55b24af2014-11-25 11:00:56 -0800717 int count,
joshualitt2c93efe2014-11-06 12:57:13 -0800718 const GrClipMaskManager::ScissorState&,
719 const GrStencilSettings&,
720 const GrDeviceCoordTexture*) = 0;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000721
bsalomon63b21962014-11-05 07:05:34 -0800722 virtual void onClear(const SkIRect* rect, GrColor color, bool canIgnoreRect,
723 GrRenderTarget* renderTarget) = 0;
724
bsalomonf90a02b2014-11-26 12:28:00 -0800725 /** The subclass will get a chance to copy the surface for falling back to the default
726 implementation, which simply draws a rectangle (and fails if dst isn't a render target). It
727 should assume that any clipping has already been performed on the rect and point. It won't
728 be called if the copy can be skipped. */
729 virtual bool onCopySurface(GrSurface* dst,
730 GrSurface* src,
731 const SkIRect& srcRect,
732 const SkIPoint& dstPoint) = 0;
733
734 /** Indicates whether onCopySurface would succeed. It should assume that any clipping has
735 already been performed on the rect and point. It won't be called if the copy can be
736 skipped. */
737 virtual bool onCanCopySurface(const GrSurface* dst,
738 const GrSurface* src,
739 const SkIRect& srcRect,
740 const SkIPoint& dstPoint) = 0;
741 /**
742 * This will be called before allocating a texture to be a dst for onCopySurface. Only the
743 * dstDesc's config, flags, and origin need be set by the function. If the subclass cannot
744 * create a surface that would succeed its implementation of onCopySurface, it should return
745 * false. The base class will fall back to creating a render target to draw into using the src.
746 */
747 virtual bool onInitCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* dstDesc) = 0;
bsalomon63b21962014-11-05 07:05:34 -0800748
bsalomon@google.come3d70952012-03-13 12:40:53 +0000749 // helpers for reserving vertex and index space.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000750 bool reserveVertexSpace(size_t vertexSize,
bsalomon@google.come3d70952012-03-13 12:40:53 +0000751 int vertexCount,
752 void** vertices);
753 bool reserveIndexSpace(int indexCount, void** indices);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000754
bsalomon@google.come8262622011-11-07 02:30:51 +0000755 // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
756 // indicate non-indexed drawing.
joshualitt9853cce2014-11-17 14:22:48 -0800757 bool checkDraw(const GrDrawState&,
758 GrPrimitiveType type,
759 int startVertex,
760 int startIndex,
761 int vertexCount,
bsalomon@google.come8262622011-11-07 02:30:51 +0000762 int indexCount) const;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000763 // called when setting a new vert/idx source to unref prev vb/ib
764 void releasePreviousVertexSource();
765 void releasePreviousIndexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000766
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000767 // Check to see if this set of draw commands has been sent out
768 virtual bool isIssued(uint32_t drawID) { return true; }
joshualitt9853cce2014-11-17 14:22:48 -0800769 void getPathStencilSettingsForFilltype(GrPathRendering::FillType,
770 const GrStencilBuffer*,
771 GrStencilSettings*);
joshualitta7024152014-11-03 14:16:35 -0800772 virtual GrClipMaskManager* clipMaskManager() = 0;
joshualitt2c93efe2014-11-06 12:57:13 -0800773 virtual bool setupClip(const SkRect* devBounds,
774 GrDrawState::AutoRestoreEffects* are,
775 GrDrawState::AutoRestoreStencil* ars,
joshualitt9853cce2014-11-17 14:22:48 -0800776 GrDrawState*,
joshualitt2c93efe2014-11-06 12:57:13 -0800777 GrClipMaskManager::ScissorState* scissorState) = 0;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000778
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000779 enum {
780 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +0000781 };
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000782 SkSTArray<kPreallocGeoSrcStateStackCnt, GeometrySrcState, true> fGeoSrcStateStack;
783 const GrClipData* fClip;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000784 // The context owns us, not vice-versa, so this ptr is not ref'ed by DrawTarget.
785 GrContext* fContext;
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000786 // To keep track that we always have at least as many debug marker adds as removes
787 int fGpuTraceMarkerCount;
788 GrTraceMarkerSet fActiveTraceMarkers;
egdaniel3eee3832014-06-18 13:09:11 -0700789 GrTraceMarkerSet fStoredTraceMarkers;
reed@google.comfa35e3d2012-06-26 20:16:17 +0000790
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000791 typedef SkRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000792};
793
joshualitt329bf482014-10-29 12:31:28 -0700794/*
795 * This class is JUST for clip mask manager. Everyone else should just use draw target above.
796 */
joshualitt6db519c2014-10-29 08:48:18 -0700797class GrClipTarget : public GrDrawTarget {
798public:
joshualitt329bf482014-10-29 12:31:28 -0700799 GrClipTarget(GrContext* context) : INHERITED(context) {
800 fClipMaskManager.setClipTarget(this);
801 }
802
803 /* Clip mask manager needs access to the context.
804 * TODO we only need a very small subset of context in the CMM.
805 */
806 GrContext* getContext() { return INHERITED::getContext(); }
807 const GrContext* getContext() const { return INHERITED::getContext(); }
808
joshualitt6db519c2014-10-29 08:48:18 -0700809 /**
810 * Clip Mask Manager(and no one else) needs to clear private stencil bits.
811 * ClipTarget subclass sets clip bit in the stencil buffer. The subclass
812 * is free to clear the remaining bits to zero if masked clears are more
813 * expensive than clearing all bits.
814 */
815 virtual void clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* = NULL) = 0;
816
joshualitt3bdd7dc2014-10-31 08:27:39 -0700817 /**
818 * Release any resources that are cached but not currently in use. This
819 * is intended to give an application some recourse when resources are low.
820 */
821 virtual void purgeResources() SK_OVERRIDE {
822 // The clip mask manager can rebuild all its clip masks so just
823 // get rid of them all.
824 fClipMaskManager.purgeResources();
825 };
826
joshualitt329bf482014-10-29 12:31:28 -0700827protected:
828 GrClipMaskManager fClipMaskManager;
829
joshualitt6db519c2014-10-29 08:48:18 -0700830private:
joshualitta7024152014-11-03 14:16:35 -0800831 GrClipMaskManager* clipMaskManager() { return &fClipMaskManager; }
joshualitt329bf482014-10-29 12:31:28 -0700832
joshualitt2c93efe2014-11-06 12:57:13 -0800833 virtual bool setupClip(const SkRect* devBounds,
834 GrDrawState::AutoRestoreEffects* are,
835 GrDrawState::AutoRestoreStencil* ars,
joshualitt9853cce2014-11-17 14:22:48 -0800836 GrDrawState*,
joshualitt2c93efe2014-11-06 12:57:13 -0800837 GrClipMaskManager::ScissorState* scissorState) SK_OVERRIDE;
838
joshualitt6db519c2014-10-29 08:48:18 -0700839 typedef GrDrawTarget INHERITED;
840};
841
reed@google.comac10a2d2010-12-22 21:39:39 +0000842#endif