blob: 8f5f09fd65874105a397da4c719e7b49f3b322c5 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
10#ifndef GrDrawTarget_DEFINED
11#define GrDrawTarget_DEFINED
12
bsalomon@google.com8d67c072012-12-13 20:38:14 +000013#include "GrClipData.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"
bsalomon@google.comb9086a02012-11-01 18:02:54 +000016#include "SkMatrix.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000017#include "GrRefCnt.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018
bsalomon@google.com8d67c072012-12-13 20:38:14 +000019#include "SkClipStack.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000020#include "SkPath.h"
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000021#include "SkTLazy.h"
robertphillips@google.coma2d71482012-08-01 20:08:47 +000022#include "SkTArray.h"
bsalomon@google.com8d67c072012-12-13 20:38:14 +000023#include "SkXfermode.h"
Scroggo97c88c22011-05-11 14:05:25 +000024
robertphillips@google.coma2d71482012-08-01 20:08:47 +000025class GrClipData;
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000026class GrDrawTargetCaps;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000027class GrPath;
reed@google.comac10a2d2010-12-22 21:39:39 +000028class GrVertexBuffer;
sugoi@google.com5f74cf82012-12-17 21:16:45 +000029class SkStrokeRec;
sugoi@google.com12b4e272012-12-06 20:13:11 +000030
reed@google.comac10a2d2010-12-22 21:39:39 +000031class GrDrawTarget : public GrRefCnt {
bsalomon@google.comf6601872012-08-28 21:11:35 +000032protected:
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000033 class DrawInfo;
34
bsalomon@google.comf6601872012-08-28 21:11:35 +000035public:
36 SK_DECLARE_INST_COUNT(GrDrawTarget)
37
reed@google.comac10a2d2010-12-22 21:39:39 +000038 ///////////////////////////////////////////////////////////////////////////
39
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000040 // The context may not be fully constructed and should not be used during GrDrawTarget
41 // construction.
42 GrDrawTarget(GrContext* context);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000043 virtual ~GrDrawTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +000044
45 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000046 * Gets the capabilities of the draw target.
47 */
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000048 const GrDrawTargetCaps* caps() const { return fCaps.get(); }
bsalomon@google.com18c9c192011-09-22 21:01:31 +000049
50 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000051 * Sets the current clip to the region specified by clip. All draws will be
52 * clipped against this clip if kClip_StateBit is enabled.
53 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000054 * Setting the clip may (or may not) zero out the client's stencil bits.
55 *
reed@google.comac10a2d2010-12-22 21:39:39 +000056 * @param description of the clipping region
57 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +000058 void setClip(const GrClipData* clip);
reed@google.comac10a2d2010-12-22 21:39:39 +000059
60 /**
61 * Gets the current clip.
62 *
63 * @return the clip.
64 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +000065 const GrClipData* getClip() const;
reed@google.comac10a2d2010-12-22 21:39:39 +000066
bsalomon@google.coma5d056a2012-03-27 15:59:58 +000067 /**
68 * Sets the draw state object for the draw target. Note that this does not
69 * make a copy. The GrDrawTarget will take a reference to passed object.
70 * Passing NULL will cause the GrDrawTarget to use its own internal draw
71 * state object rather than an externally provided one.
72 */
73 void setDrawState(GrDrawState* drawState);
74
75 /**
76 * Read-only access to the GrDrawTarget's current draw state.
77 */
78 const GrDrawState& getDrawState() const { return *fDrawState; }
79
80 /**
81 * Read-write access to the GrDrawTarget's current draw state. Note that
82 * this doesn't ref.
83 */
84 GrDrawState* drawState() { return fDrawState; }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +000085
bsalomon@google.com3d0835b2011-12-08 16:12:03 +000086 /**
bsalomon@google.comd46e2422011-09-23 17:40:07 +000087 * Color alpha and coverage are two inputs to the drawing pipeline. For some
88 * blend modes it is safe to fold the coverage into constant or per-vertex
89 * color alpha value. For other blend modes they must be handled separately.
90 * Depending on features available in the underlying 3D API this may or may
91 * not be possible.
92 *
bsalomon@google.come79c8152012-03-29 19:07:12 +000093 * This function considers the current draw state and the draw target's
94 * capabilities to determine whether coverage can be handled correctly. The
95 * following assumptions are made:
96 * 1. The caller intends to somehow specify coverage. This can be
97 * specified either by enabling a coverage stage on the GrDrawState or
98 * via the vertex layout.
bsalomon@google.com2b446732013-02-12 16:47:41 +000099 * 2. Other than enabling coverage stages or enabling coverage in the
100 * layout, the current configuration of the target's GrDrawState is as
101 * it will be at draw time.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000102 */
103 bool canApplyCoverage() const;
104
105 /**
bsalomon@google.com2b446732013-02-12 16:47:41 +0000106 * Given the current draw state and hw support, will HW AA lines be used (if
107 * a line primitive type is drawn)?
bsalomon@google.com471d4712011-08-23 15:45:25 +0000108 */
tomhudson@google.com93813632011-10-27 20:21:16 +0000109 bool willUseHWAALines() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000110
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000111 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000112 * There are three types of "sources" of geometry (vertices and indices) for
113 * draw calls made on the target. When performing an indexed draw, the
114 * indices and vertices can use different source types. Once a source is
bsalomon@google.com934c5702012-03-20 21:17:58 +0000115 * specified it can be used for multiple draws. However, the time at which
116 * the geometry data is no longer editable depends on the source type.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000117 *
118 * Sometimes it is necessary to perform a draw while upstack code has
bsalomon@google.come3d70952012-03-13 12:40:53 +0000119 * already specified geometry that it isn't finished with. So there are push
120 * and pop methods. This allows the client to push the sources, draw
121 * something using alternate sources, and then pop to restore the original
122 * sources.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000123 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000124 * Aside from pushes and pops, a source remains valid until another source
125 * is set or resetVertexSource / resetIndexSource is called. Drawing from
126 * a reset source is an error.
127 *
128 * The three types of sources are:
129 *
130 * 1. A cpu array (set*SourceToArray). This is useful when the caller
131 * already provided vertex data in a format compatible with a
132 * GrVertexLayout. The data in the array is consumed at the time that
133 * set*SourceToArray is called and subsequent edits to the array will not
134 * be reflected in draws.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000135 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000136 * 2. Reserve. This is most useful when the caller has data it must
137 * transform before drawing and is not long-lived. The caller requests
138 * that the draw target make room for some amount of vertex and/or index
139 * data. The target provides ptrs to hold the vertex and/or index data.
140 *
rmistry@google.comd6176b02012-08-23 18:14:13 +0000141 * The data is writable up until the next drawIndexed, drawNonIndexed,
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000142 * drawIndexedInstances, drawRect, copySurface, or pushGeometrySource. At
143 * this point the data is frozen and the ptrs are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000144 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000145 * Where the space is allocated and how it is uploaded to the GPU is
146 * subclass-dependent.
147 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000148 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.come3d70952012-03-13 12:40:53 +0000149 * is long-lived. When the data in the buffer is consumed depends on the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000150 * GrDrawTarget subclass. For deferred subclasses the caller has to
bsalomon@google.come3d70952012-03-13 12:40:53 +0000151 * guarantee that the data is still available in the buffers at playback.
152 * (TODO: Make this more automatic as we have done for read/write pixels)
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000153 *
154 * The size of each vertex is determined by querying the current GrDrawState.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000155 */
156
157 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000158 * Reserves space for vertices and/or indices. Zero can be specifed as
159 * either the vertex or index count if the caller desires to only reserve
rmistry@google.comd6176b02012-08-23 18:14:13 +0000160 * space for only indices or only vertices. If zero is specifed for
bsalomon@google.come3d70952012-03-13 12:40:53 +0000161 * vertexCount then the vertex source will be unmodified and likewise for
162 * indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000163 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000164 * If the function returns true then the reserve suceeded and the vertices
165 * and indices pointers will point to the space created.
reed@google.comac10a2d2010-12-22 21:39:39 +0000166 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000167 * If the target cannot make space for the request then this function will
168 * return false. If vertexCount was non-zero then upon failure the vertex
169 * source is reset and likewise for indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000170 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000171 * The pointers to the space allocated for vertices and indices remain valid
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000172 * until a drawIndexed, drawNonIndexed, drawIndexedInstances, drawRect,
173 * copySurface, or push/popGeomtrySource is called. At that point logically a
174 * snapshot of the data is made and the pointers are invalid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000175 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000176 * @param vertexCount the number of vertices to reserve space for. Can be
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000177 * 0. Vertex size is queried from the current GrDrawState.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000178 * @param indexCount the number of indices to reserve space for. Can be 0.
bsalomon@google.come3d70952012-03-13 12:40:53 +0000179 * @param vertices will point to reserved vertex space if vertexCount is
rmistry@google.comd6176b02012-08-23 18:14:13 +0000180 * non-zero. Illegal to pass NULL if vertexCount > 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000181 * @param indices will point to reserved index space if indexCount is
182 * non-zero. Illegal to pass NULL if indexCount > 0.
183 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000184 bool reserveVertexAndIndexSpace(int vertexCount,
bsalomon@google.com97805382012-03-13 14:32:07 +0000185 int indexCount,
186 void** vertices,
187 void** indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000188
reed@google.comac10a2d2010-12-22 21:39:39 +0000189 /**
190 * Provides hints to caller about the number of vertices and indices
191 * that can be allocated cheaply. This can be useful if caller is reserving
192 * space but doesn't know exactly how much geometry is needed.
193 *
194 * Also may hint whether the draw target should be flushed first. This is
195 * useful for deferred targets.
196 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000197 * @param vertexCount in: hint about how many vertices the caller would
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000198 * like to allocate. Vertex size is queried from the
199 * current GrDrawState.
reed@google.comac10a2d2010-12-22 21:39:39 +0000200 * out: a hint about the number of vertices that can be
201 * allocated cheaply. Negative means no hint.
202 * Ignored if NULL.
203 * @param indexCount in: hint about how many indices the caller would
204 * like to allocate.
205 * out: a hint about the number of indices that can be
206 * allocated cheaply. Negative means no hint.
207 * Ignored if NULL.
208 *
209 * @return true if target should be flushed based on the input values.
210 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000211 virtual bool geometryHints(int* vertexCount,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000212 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000213
214 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000215 * Sets source of vertex data for the next draw. Array must contain
216 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000217 *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000218 * @param vertexArray cpu array containing vertex data.
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000219 * @param vertexCount the number of vertices in the array. Vertex size is
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000220 * queried from the current GrDrawState.
reed@google.comac10a2d2010-12-22 21:39:39 +0000221 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000222 void setVertexSourceToArray(const void* vertexArray, int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000223
224 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000225 * Sets source of index data for the next indexed draw. Array must contain
226 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000227 *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000228 * @param indexArray cpu array containing index data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000229 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000230 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000231 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000232
233 /**
234 * Sets source of vertex data for the next draw. Data does not have to be
bsalomon@google.com934c5702012-03-20 21:17:58 +0000235 * in the buffer until drawIndexed, drawNonIndexed, or drawIndexedInstances.
reed@google.comac10a2d2010-12-22 21:39:39 +0000236 *
237 * @param buffer vertex buffer containing vertex data. Must be
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000238 * unlocked before draw call. Vertex size is queried
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000239 * from current GrDrawState.
reed@google.comac10a2d2010-12-22 21:39:39 +0000240 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000241 void setVertexSourceToBuffer(const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000242
243 /**
244 * Sets source of index data for the next indexed draw. Data does not have
bsalomon@google.com934c5702012-03-20 21:17:58 +0000245 * to be in the buffer until drawIndexed.
reed@google.comac10a2d2010-12-22 21:39:39 +0000246 *
247 * @param buffer index buffer containing indices. Must be unlocked
248 * before indexed draw call.
249 */
250 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000251
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000252 /**
253 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
254 * source to reserved, array, or buffer before next draw. May be able to free
255 * up temporary storage allocated by setVertexSourceToArray or
256 * reserveVertexSpace.
257 */
258 void resetVertexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000259
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000260 /**
261 * Resets index source. Indexed Drawing from reset indices is illegal. Set
262 * index source to reserved, array, or buffer before next indexed draw. May
263 * be able to free up temporary storage allocated by setIndexSourceToArray
264 * or reserveIndexSpace.
265 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000266 void resetIndexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000267
bsalomon@google.com97805382012-03-13 14:32:07 +0000268 /**
269 * Query to find out if the vertex or index source is reserved.
270 */
271 bool hasReservedVerticesOrIndices() const {
bsalomon@google.com73d98aa2012-03-13 14:41:19 +0000272 return kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc ||
bsalomon@google.com97805382012-03-13 14:32:07 +0000273 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
274 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000275
276 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000277 * Pushes and resets the vertex/index sources. Any reserved vertex / index
278 * data is finalized (i.e. cannot be updated after the matching pop but can
279 * be drawn from). Must be balanced by a pop.
280 */
281 void pushGeometrySource();
282
283 /**
284 * Pops the vertex / index sources from the matching push.
285 */
286 void popGeometrySource();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000287
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000288 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000289 * Draws indexed geometry using the current state and current vertex / index
290 * sources.
291 *
292 * @param type The type of primitives to draw.
293 * @param startVertex the vertex in the vertex array/buffer corresponding
294 * to index 0
295 * @param startIndex first index to read from index src.
296 * @param vertexCount one greater than the max index.
297 * @param indexCount the number of index elements to read. The index count
298 * is effectively trimmed to the last completely
299 * specified primitive.
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000300 * @param devBounds optional bounds hint. This is a promise from the caller,
301 * not a request for clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000302 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000303 void drawIndexed(GrPrimitiveType type,
304 int startVertex,
305 int startIndex,
306 int vertexCount,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000307 int indexCount,
308 const SkRect* devBounds = NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +0000309
310 /**
311 * Draws non-indexed geometry using the current state and current vertex
312 * sources.
313 *
314 * @param type The type of primitives to draw.
315 * @param startVertex the vertex in the vertex array/buffer corresponding
316 * to index 0
317 * @param vertexCount one greater than the max index.
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000318 * @param devBounds optional bounds hint. This is a promise from the caller,
319 * not a request for clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000320 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000321 void drawNonIndexed(GrPrimitiveType type,
322 int startVertex,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000323 int vertexCount,
324 const SkRect* devBounds = NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +0000325
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000326 /**
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000327 * Draws path into the stencil buffer. The fill must be either even/odd or
328 * winding (not inverse or hairline). It will respect the HW antialias flag
329 * on the draw state (if possible in the 3D API).
330 */
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000331 void stencilPath(const GrPath*, const SkStrokeRec& stroke, SkPath::FillType fill);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000332
333 /**
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000334 * Helper function for drawing rects. It performs a geometry src push and pop
335 * and thus will finalize any reserved geometry.
skia.committer@gmail.com044679e2013-02-15 07:16:57 +0000336 *
bsalomon@google.comc7818882013-03-20 19:19:53 +0000337 * @param rect the rect to draw
338 * @param matrix optional matrix applied to rect (before viewMatrix)
339 * @param localRect optional rect that specifies local coords to map onto
340 * rect. If NULL then rect serves as the local coords.
341 * @param localMatrix optional matrix applied to localRect. If
skia.committer@gmail.com044679e2013-02-15 07:16:57 +0000342 * srcRect is non-NULL and srcMatrix is non-NULL
343 * then srcRect will be transformed by srcMatrix.
jvanverth@google.com39768252013-02-14 15:25:44 +0000344 * srcMatrix can be NULL when no srcMatrix is desired.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000345 */
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000346 void drawRect(const GrRect& rect,
347 const SkMatrix* matrix,
348 const GrRect* localRect,
349 const SkMatrix* localMatrix) {
350 AutoGeometryPush agp(this);
351 this->onDrawRect(rect, matrix, localRect, localMatrix);
352 }
jvanverth@google.com39768252013-02-14 15:25:44 +0000353
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000354 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000355 * Helper for drawRect when the caller doesn't need separate local rects or matrices.
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000356 */
357 void drawSimpleRect(const GrRect& rect, const SkMatrix* matrix = NULL) {
bsalomon@google.comc7818882013-03-20 19:19:53 +0000358 drawRect(rect, matrix, NULL, NULL);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000359 }
360 void drawSimpleRect(const GrIRect& irect, const SkMatrix* matrix = NULL) {
361 SkRect rect = SkRect::MakeFromIRect(irect);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000362 this->drawRect(rect, matrix, NULL, NULL);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000363 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000364
365 /**
bsalomon@google.com934c5702012-03-20 21:17:58 +0000366 * This call is used to draw multiple instances of some geometry with a
367 * given number of vertices (V) and indices (I) per-instance. The indices in
368 * the index source must have the form i[k+I] == i[k] + V. Also, all indices
369 * i[kI] ... i[(k+1)I-1] must be elements of the range kV ... (k+1)V-1. As a
370 * concrete example, the following index buffer for drawing a series of
371 * quads each as two triangles each satisfies these conditions with V=4 and
372 * I=6:
373 * (0,1,2,0,2,3, 4,5,6,4,6,7, 8,9,10,8,10,11, ...)
374 *
375 * The call assumes that the pattern of indices fills the entire index
376 * source. The size of the index buffer limits the number of instances that
377 * can be drawn by the GPU in a single draw. However, the caller may specify
378 * any (positive) number for instanceCount and if necessary multiple GPU
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000379 * draws will be issued. Moreover, when drawIndexedInstances is called
bsalomon@google.com934c5702012-03-20 21:17:58 +0000380 * multiple times it may be possible for GrDrawTarget to group them into a
381 * single GPU draw.
382 *
383 * @param type the type of primitives to draw
384 * @param instanceCount the number of instances to draw. Each instance
385 * consists of verticesPerInstance vertices indexed by
386 * indicesPerInstance indices drawn as the primitive
387 * type specified by type.
388 * @param verticesPerInstance The number of vertices in each instance (V
389 * in the above description).
390 * @param indicesPerInstance The number of indices in each instance (I
391 * in the above description).
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000392 * @param devBounds optional bounds hint. This is a promise from the caller,
393 * not a request for clipping.
bsalomon@google.com934c5702012-03-20 21:17:58 +0000394 */
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000395 void drawIndexedInstances(GrPrimitiveType type,
396 int instanceCount,
397 int verticesPerInstance,
398 int indicesPerInstance,
399 const SkRect* devBounds = NULL);
bsalomon@google.com934c5702012-03-20 21:17:58 +0000400
401 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000402 * Clear the current render target if one isn't passed in. Ignores the
403 * clip and all other draw state (blend mode, stages, etc). Clears the
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000404 * whole thing if rect is NULL, otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000405 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000406 virtual void clear(const GrIRect* rect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000407 GrColor color,
408 GrRenderTarget* renderTarget = NULL) = 0;
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000409
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000410 /**
411 * Copies a pixel rectangle from one surface to another. This call may finalize
412 * reserved vertex/index data (as though a draw call was made). The src pixels
413 * copied are specified by srcRect. They are copied to a rect of the same
414 * size in dst with top left at dstPoint. If the src rect is clipped by the
415 * src bounds then pixel values in the dst rect corresponding to area clipped
416 * by the src rect are not overwritten. This method can fail and return false
417 * depending on the type of surface, configs, etc, and the backend-specific
418 * limitations. If rect is clipped out entirely by the src or dst bounds then
419 * true is returned since there is no actual copy necessary to succeed.
420 */
421 bool copySurface(GrSurface* dst,
422 GrSurface* src,
423 const SkIRect& srcRect,
424 const SkIPoint& dstPoint);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000425 /**
426 * Function that determines whether a copySurface call would succeed without
427 * performing the copy.
428 */
429 bool canCopySurface(GrSurface* dst,
430 GrSurface* src,
431 const SkIRect& srcRect,
432 const SkIPoint& dstPoint);
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000433
robertphillips@google.comff175842012-05-14 19:31:39 +0000434 /**
bsalomon@google.comeb851172013-04-15 13:51:00 +0000435 * This is can be called before allocating a texture to be a dst for copySurface. It will
436 * populate the origin, config, and flags fields of the desc such that copySurface is more
437 * likely to succeed and be efficient.
438 */
439 virtual void initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc);
440
441
442 /**
robertphillips@google.comff175842012-05-14 19:31:39 +0000443 * Release any resources that are cached but not currently in use. This
444 * is intended to give an application some recourse when resources are low.
445 */
446 virtual void purgeResources() {};
447
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000448 /**
449 * For subclass internal use to invoke a call to onDraw(). See DrawInfo below.
450 */
451 void executeDraw(const DrawInfo& info) { this->onDraw(info); }
452
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000453 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000454
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000455 /**
456 * See AutoStateRestore below.
457 */
458 enum ASRInit {
459 kPreserve_ASRInit,
460 kReset_ASRInit
461 };
462
463 /**
464 * Saves off the current state and restores it in the destructor. It will
465 * install a new GrDrawState object on the target (setDrawState) and restore
466 * the previous one in the destructor. The caller should call drawState() to
467 * get the new draw state after the ASR is installed.
468 *
469 * GrDrawState* state = target->drawState();
470 * AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit).
471 * state->setRenderTarget(rt); // state refers to the GrDrawState set on
472 * // target before asr was initialized.
473 * // Therefore, rt is set on the GrDrawState
474 * // that will be restored after asr's
475 * // destructor rather than target's current
rmistry@google.comd6176b02012-08-23 18:14:13 +0000476 * // GrDrawState.
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000477 */
reed@google.comac10a2d2010-12-22 21:39:39 +0000478 class AutoStateRestore : ::GrNoncopyable {
479 public:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000480 /**
481 * Default ASR will have no effect unless set() is subsequently called.
482 */
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000483 AutoStateRestore();
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000484
485 /**
486 * Saves the state on target. The state will be restored when the ASR
487 * is destroyed. If this constructor is used do not call set().
488 *
489 * @param init Should the newly installed GrDrawState be a copy of the
490 * previous state or a default-initialized GrDrawState.
491 */
492 AutoStateRestore(GrDrawTarget* target, ASRInit init);
493
reed@google.comac10a2d2010-12-22 21:39:39 +0000494 ~AutoStateRestore();
495
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000496 /**
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000497 * Saves the state on target. The state will be restored when the ASR
498 * is destroyed. This should only be called once per ASR object and only
499 * when the default constructor was used. For nested saves use multiple
500 * ASR objects.
501 *
502 * @param init Should the newly installed GrDrawState be a copy of the
503 * previous state or a default-initialized GrDrawState.
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000504 */
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000505 void set(GrDrawTarget* target, ASRInit init);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000506
reed@google.comac10a2d2010-12-22 21:39:39 +0000507 private:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000508 GrDrawTarget* fDrawTarget;
509 SkTLazy<GrDrawState> fTempState;
510 GrDrawState* fSavedState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000511 };
512
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000513 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000514
reed@google.comac10a2d2010-12-22 21:39:39 +0000515 class AutoReleaseGeometry : ::GrNoncopyable {
516 public:
517 AutoReleaseGeometry(GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000518 int vertexCount,
519 int indexCount);
520 AutoReleaseGeometry();
521 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000522 bool set(GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000523 int vertexCount,
524 int indexCount);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000525 bool succeeded() const { return NULL != fTarget; }
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000526 void* vertices() const { GrAssert(this->succeeded()); return fVertices; }
527 void* indices() const { GrAssert(this->succeeded()); return fIndices; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000528 GrPoint* positions() const {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000529 return static_cast<GrPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +0000530 }
531
532 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000533 void reset();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000534
reed@google.comac10a2d2010-12-22 21:39:39 +0000535 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000536 void* fVertices;
537 void* fIndices;
538 };
539
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000540 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000541
542 class AutoClipRestore : ::GrNoncopyable {
543 public:
544 AutoClipRestore(GrDrawTarget* target) {
545 fTarget = target;
546 fClip = fTarget->getClip();
547 }
548
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000549 AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip);
550
reed@google.comac10a2d2010-12-22 21:39:39 +0000551 ~AutoClipRestore() {
552 fTarget->setClip(fClip);
553 }
554 private:
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000555 GrDrawTarget* fTarget;
556 const GrClipData* fClip;
557 SkTLazy<SkClipStack> fStack;
558 GrClipData fReplacementClip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000559 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000560
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000561 ////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000562
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000563 /**
564 * Saves the geometry src state at construction and restores in the destructor. It also saves
565 * and then restores the vertex attrib state.
566 */
567 class AutoGeometryPush : ::GrNoncopyable {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000568 public:
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000569 AutoGeometryPush(GrDrawTarget* target)
570 : fAttribRestore(target->drawState()) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000571 GrAssert(NULL != target);
572 fTarget = target;
573 target->pushGeometrySource();
574 }
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000575
576 ~AutoGeometryPush() { fTarget->popGeometrySource(); }
577
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000578 private:
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000579 GrDrawTarget* fTarget;
580 GrDrawState::AutoVertexAttribRestore fAttribRestore;
581 };
582
583 /**
584 * Combination of AutoGeometryPush and AutoStateRestore. The vertex attribs will be in default
585 * state regardless of ASRInit value.
586 */
587 class AutoGeometryAndStatePush : ::GrNoncopyable {
588 public:
589 AutoGeometryAndStatePush(GrDrawTarget* target, ASRInit init)
590 : fState(target, init){
591 GrAssert(NULL != target);
592 fTarget = target;
593 target->pushGeometrySource();
594 if (kPreserve_ASRInit == init) {
595 target->drawState()->setDefaultVertexAttribs();
596 }
597 }
598
599 ~AutoGeometryAndStatePush() { fTarget->popGeometrySource(); }
600
601 private:
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000602 AutoStateRestore fState;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000603 GrDrawTarget* fTarget;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000604 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000605
reed@google.comac10a2d2010-12-22 21:39:39 +0000606protected:
bsalomon@google.com471d4712011-08-23 15:45:25 +0000607
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000608 enum GeometrySrcType {
609 kNone_GeometrySrcType, //<! src has not been specified
610 kReserved_GeometrySrcType, //<! src was set using reserve*Space
611 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
612 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
613 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000614
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000615 struct GeometrySrcState {
616 GeometrySrcType fVertexSrc;
617 union {
618 // valid if src type is buffer
619 const GrVertexBuffer* fVertexBuffer;
620 // valid if src type is reserved or array
621 int fVertexCount;
622 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000623
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000624 GeometrySrcType fIndexSrc;
625 union {
626 // valid if src type is buffer
627 const GrIndexBuffer* fIndexBuffer;
628 // valid if src type is reserved or array
629 int fIndexCount;
630 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000631
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000632 size_t fVertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000633 };
bsalomon@google.com934c5702012-03-20 21:17:58 +0000634
635 int indexCountInCurrentSource() const {
636 const GeometrySrcState& src = this->getGeomSrc();
637 switch (src.fIndexSrc) {
638 case kNone_GeometrySrcType:
639 return 0;
640 case kReserved_GeometrySrcType:
641 case kArray_GeometrySrcType:
642 return src.fIndexCount;
643 case kBuffer_GeometrySrcType:
644 return src.fIndexBuffer->sizeInBytes() / sizeof(uint16_t);
645 default:
646 GrCrash("Unexpected Index Source.");
647 return 0;
648 }
649 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000650
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000651 // This method is called by copySurface The srcRect is guaranteed to be entirely within the
652 // src bounds. Likewise, the dst rect implied by dstPoint and srcRect's width and height falls
653 // entirely within the dst. The default implementation will draw a rect from the src to the
654 // dst if the src is a texture and the dst is a render target and fail otherwise.
655 virtual bool onCopySurface(GrSurface* dst,
656 GrSurface* src,
657 const SkIRect& srcRect,
658 const SkIPoint& dstPoint);
659
660 // Called to determine whether an onCopySurface call would succeed or not. This is useful for
661 // proxy subclasses to test whether the copy would succeed without executing it yet. Derived
662 // classes must keep this consistent with their implementation of onCopySurface(). The inputs
663 // are the same as onCopySurface(), i.e. srcRect and dstPoint are clipped to be inside the src
664 // and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000665 virtual bool onCanCopySurface(GrSurface* dst,
666 GrSurface* src,
667 const SkIRect& srcRect,
668 const SkIPoint& dstPoint);
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000669
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000670 GrContext* getContext() { return fContext; }
671 const GrContext* getContext() const { return fContext; }
672
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000673 // A subclass may override this function if it wishes to be notified when the clip is changed.
674 // The override should call INHERITED::clipWillBeSet().
675 virtual void clipWillBeSet(const GrClipData* clipData);
676
677 // subclasses must call this in their destructors to ensure all vertex
678 // and index sources have been released (including those held by
679 // pushGeometrySource())
680 void releaseGeometry();
681
682 // accessors for derived classes
683 const GeometrySrcState& getGeomSrc() const { return fGeoSrcStateStack.back(); }
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000684 // it is preferable to call this rather than getGeomSrc()->fVertexSize because of the assert.
685 size_t getVertexSize() const {
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000686 // the vertex layout is only valid if a vertex source has been specified.
687 GrAssert(this->getGeomSrc().fVertexSrc != kNone_GeometrySrcType);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000688 return this->getGeomSrc().fVertexSize;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000689 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000690
bsalomon@google.combcce8922013-03-25 15:38:39 +0000691 // Subclass must initialize this in its constructor.
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000692 SkAutoTUnref<const GrDrawTargetCaps> fCaps;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000693
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000694 /**
695 * Used to communicate draws to subclass's onDraw function.
696 */
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000697 class DrawInfo {
698 public:
699 DrawInfo(const DrawInfo& di) { (*this) = di; }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000700 DrawInfo& operator =(const DrawInfo& di);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000701
702 GrPrimitiveType primitiveType() const { return fPrimitiveType; }
703 int startVertex() const { return fStartVertex; }
704 int startIndex() const { return fStartIndex; }
705 int vertexCount() const { return fVertexCount; }
706 int indexCount() const { return fIndexCount; }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000707 int verticesPerInstance() const { return fVerticesPerInstance; }
708 int indicesPerInstance() const { return fIndicesPerInstance; }
709 int instanceCount() const { return fInstanceCount; }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000710
711 bool isIndexed() const { return fIndexCount > 0; }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000712#if GR_DEBUG
713 bool isInstanced() const; // this version is longer because of asserts
714#else
715 bool isInstanced() const { return fInstanceCount > 0; }
716#endif
717
718 // adds or remove instances
719 void adjustInstanceCount(int instanceOffset);
720 // shifts the start vertex
721 void adjustStartVertex(int vertexOffset);
722 // shifts the start index
723 void adjustStartIndex(int indexOffset);
724
725 void setDevBounds(const SkRect& bounds) {
726 fDevBoundsStorage = bounds;
727 fDevBounds = &fDevBoundsStorage;
728 }
729 const SkRect* getDevBounds() const { return fDevBounds; }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000730
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000731 bool getDevIBounds(SkIRect* bounds) const {
732 if (NULL != fDevBounds) {
733 fDevBounds->roundOut(bounds);
734 return true;
735 } else {
736 return false;
737 }
738 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000739
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000740 // NULL if no copy of the dst is needed for the draw.
741 const GrDeviceCoordTexture* getDstCopy() const {
742 if (NULL != fDstCopy.texture()) {
743 return &fDstCopy;
744 } else {
745 return NULL;
746 }
747 }
748
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000749 private:
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000750 DrawInfo() { fDevBounds = NULL; }
751
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000752 friend class GrDrawTarget;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000753
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000754 GrPrimitiveType fPrimitiveType;
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000755
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000756 int fStartVertex;
757 int fStartIndex;
758 int fVertexCount;
759 int fIndexCount;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000760
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000761 int fInstanceCount;
762 int fVerticesPerInstance;
763 int fIndicesPerInstance;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000764
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000765 SkRect fDevBoundsStorage;
766 SkRect* fDevBounds;
767
768 GrDeviceCoordTexture fDstCopy;
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000769 };
770
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000771private:
772 // A subclass can optionally overload this function to be notified before
773 // vertex and index space is reserved.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000774 virtual void willReserveVertexAndIndexSpace(int vertexCount, int indexCount) {}
bsalomon@google.com97805382012-03-13 14:32:07 +0000775
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000776 // implemented by subclass to allocate space for reserved geom
jvanverth@google.coma6338982013-01-31 21:34:25 +0000777 virtual bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices) = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000778 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
779 // implemented by subclass to handle release of reserved geom space
780 virtual void releaseReservedVertexSpace() = 0;
781 virtual void releaseReservedIndexSpace() = 0;
782 // subclass must consume array contents when set
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000783 virtual void onSetVertexSourceToArray(const void* vertexArray, int vertexCount) = 0;
784 virtual void onSetIndexSourceToArray(const void* indexArray, int indexCount) = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000785 // subclass is notified that geom source will be set away from an array
786 virtual void releaseVertexArray() = 0;
787 virtual void releaseIndexArray() = 0;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000788 // subclass overrides to be notified just before geo src state is pushed/popped.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000789 virtual void geometrySourceWillPush() = 0;
790 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
791 // subclass called to perform drawing
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000792 virtual void onDraw(const DrawInfo&) = 0;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000793 // Implementation of drawRect. The geometry src and vertex attribs will already
794 // be saved before this is called and restored afterwards. A subclass may override
795 // this to perform more optimal rect rendering. Its draws should be funneled through
796 // one of the public GrDrawTarget draw methods (e.g. drawNonIndexed,
797 // drawIndexedInstances, ...). The base class draws a two triangle fan using
798 // drawNonIndexed from reserved vertex space.
799 virtual void onDrawRect(const GrRect& rect,
800 const SkMatrix* matrix,
801 const GrRect* localRect,
802 const SkMatrix* localMatrix);
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000803 virtual void onStencilPath(const GrPath*, const SkStrokeRec& stroke, SkPath::FillType fill) = 0;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000804
bsalomon@google.come3d70952012-03-13 12:40:53 +0000805 // helpers for reserving vertex and index space.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000806 bool reserveVertexSpace(size_t vertexSize,
bsalomon@google.come3d70952012-03-13 12:40:53 +0000807 int vertexCount,
808 void** vertices);
809 bool reserveIndexSpace(int indexCount, void** indices);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000810
bsalomon@google.come8262622011-11-07 02:30:51 +0000811 // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
812 // indicate non-indexed drawing.
813 bool checkDraw(GrPrimitiveType type, int startVertex,
814 int startIndex, int vertexCount,
815 int indexCount) const;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000816 // called when setting a new vert/idx source to unref prev vb/ib
817 void releasePreviousVertexSource();
818 void releasePreviousIndexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000819
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000820 // Makes a copy of the dst if it is necessary for the draw. Returns false if a copy is required
821 // but couldn't be made. Otherwise, returns true.
822 bool setupDstReadIfNecessary(DrawInfo* info);
823
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000824 enum {
825 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +0000826 };
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000827 SkSTArray<kPreallocGeoSrcStateStackCnt, GeometrySrcState, true> fGeoSrcStateStack;
828 const GrClipData* fClip;
829 GrDrawState* fDrawState;
830 GrDrawState fDefaultDrawState;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000831 // The context owns us, not vice-versa, so this ptr is not ref'ed by DrawTarget.
832 GrContext* fContext;
reed@google.comfa35e3d2012-06-26 20:16:17 +0000833
834 typedef GrRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000835};
836
reed@google.comac10a2d2010-12-22 21:39:39 +0000837#endif