blob: 5b1954609cf2fdcce13c41e05fd1493ddaf3e7e1 [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"
tomhudson@google.com93813632011-10-27 20:21:16 +000012#include "GrDrawState.h"
bsalomon@google.com934c5702012-03-20 21:17:58 +000013#include "GrIndexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
bsalomon@google.com8d67c072012-12-13 20:38:14 +000015#include "SkClipStack.h"
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000016#include "SkMatrix.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000017#include "SkPath.h"
robertphillips@google.coma2d71482012-08-01 20:08:47 +000018#include "SkTArray.h"
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000019#include "SkTLazy.h"
bsalomon@google.com8d67c072012-12-13 20:38:14 +000020#include "SkXfermode.h"
Scroggo97c88c22011-05-11 14:05:25 +000021
robertphillips@google.coma2d71482012-08-01 20:08:47 +000022class GrClipData;
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000023class GrDrawTargetCaps;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000024class GrPath;
reed@google.comac10a2d2010-12-22 21:39:39 +000025class GrVertexBuffer;
sugoi@google.com5f74cf82012-12-17 21:16:45 +000026class SkStrokeRec;
sugoi@google.com12b4e272012-12-06 20:13:11 +000027
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000028class GrDrawTarget : public SkRefCnt {
bsalomon@google.comf6601872012-08-28 21:11:35 +000029protected:
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000030 class DrawInfo;
31
bsalomon@google.comf6601872012-08-28 21:11:35 +000032public:
33 SK_DECLARE_INST_COUNT(GrDrawTarget)
34
reed@google.comac10a2d2010-12-22 21:39:39 +000035 ///////////////////////////////////////////////////////////////////////////
36
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000037 // The context may not be fully constructed and should not be used during GrDrawTarget
38 // construction.
39 GrDrawTarget(GrContext* context);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000040 virtual ~GrDrawTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +000041
42 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000043 * Gets the capabilities of the draw target.
44 */
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000045 const GrDrawTargetCaps* caps() const { return fCaps.get(); }
bsalomon@google.com18c9c192011-09-22 21:01:31 +000046
47 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000048 * Sets the current clip to the region specified by clip. All draws will be
49 * clipped against this clip if kClip_StateBit is enabled.
50 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000051 * Setting the clip may (or may not) zero out the client's stencil bits.
52 *
reed@google.comac10a2d2010-12-22 21:39:39 +000053 * @param description of the clipping region
54 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +000055 void setClip(const GrClipData* clip);
reed@google.comac10a2d2010-12-22 21:39:39 +000056
57 /**
58 * Gets the current clip.
59 *
60 * @return the clip.
61 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +000062 const GrClipData* getClip() const;
reed@google.comac10a2d2010-12-22 21:39:39 +000063
bsalomon@google.coma5d056a2012-03-27 15:59:58 +000064 /**
65 * Sets the draw state object for the draw target. Note that this does not
66 * make a copy. The GrDrawTarget will take a reference to passed object.
67 * Passing NULL will cause the GrDrawTarget to use its own internal draw
68 * state object rather than an externally provided one.
69 */
70 void setDrawState(GrDrawState* drawState);
71
72 /**
73 * Read-only access to the GrDrawTarget's current draw state.
74 */
75 const GrDrawState& getDrawState() const { return *fDrawState; }
76
77 /**
78 * Read-write access to the GrDrawTarget's current draw state. Note that
79 * this doesn't ref.
80 */
81 GrDrawState* drawState() { return fDrawState; }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +000082
bsalomon@google.com3d0835b2011-12-08 16:12:03 +000083 /**
bsalomon@google.comd46e2422011-09-23 17:40:07 +000084 * Color alpha and coverage are two inputs to the drawing pipeline. For some
85 * blend modes it is safe to fold the coverage into constant or per-vertex
86 * color alpha value. For other blend modes they must be handled separately.
87 * Depending on features available in the underlying 3D API this may or may
88 * not be possible.
89 *
bsalomon@google.come79c8152012-03-29 19:07:12 +000090 * This function considers the current draw state and the draw target's
91 * capabilities to determine whether coverage can be handled correctly. The
92 * following assumptions are made:
93 * 1. The caller intends to somehow specify coverage. This can be
94 * specified either by enabling a coverage stage on the GrDrawState or
95 * via the vertex layout.
bsalomon@google.com2b446732013-02-12 16:47:41 +000096 * 2. Other than enabling coverage stages or enabling coverage in the
97 * layout, the current configuration of the target's GrDrawState is as
98 * it will be at draw time.
bsalomon@google.comd46e2422011-09-23 17:40:07 +000099 */
100 bool canApplyCoverage() const;
101
102 /**
bsalomon@google.com2b446732013-02-12 16:47:41 +0000103 * Given the current draw state and hw support, will HW AA lines be used (if
104 * a line primitive type is drawn)?
bsalomon@google.com471d4712011-08-23 15:45:25 +0000105 */
tomhudson@google.com93813632011-10-27 20:21:16 +0000106 bool willUseHWAALines() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000107
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000108 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000109 * There are three types of "sources" of geometry (vertices and indices) for
110 * draw calls made on the target. When performing an indexed draw, the
111 * indices and vertices can use different source types. Once a source is
bsalomon@google.com934c5702012-03-20 21:17:58 +0000112 * specified it can be used for multiple draws. However, the time at which
113 * the geometry data is no longer editable depends on the source type.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000114 *
115 * Sometimes it is necessary to perform a draw while upstack code has
bsalomon@google.come3d70952012-03-13 12:40:53 +0000116 * already specified geometry that it isn't finished with. So there are push
117 * and pop methods. This allows the client to push the sources, draw
118 * something using alternate sources, and then pop to restore the original
119 * sources.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000120 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000121 * Aside from pushes and pops, a source remains valid until another source
122 * is set or resetVertexSource / resetIndexSource is called. Drawing from
123 * a reset source is an error.
124 *
125 * The three types of sources are:
126 *
127 * 1. A cpu array (set*SourceToArray). This is useful when the caller
128 * already provided vertex data in a format compatible with a
129 * GrVertexLayout. The data in the array is consumed at the time that
130 * set*SourceToArray is called and subsequent edits to the array will not
131 * be reflected in draws.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000132 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000133 * 2. Reserve. This is most useful when the caller has data it must
134 * transform before drawing and is not long-lived. The caller requests
135 * that the draw target make room for some amount of vertex and/or index
136 * data. The target provides ptrs to hold the vertex and/or index data.
137 *
rmistry@google.comd6176b02012-08-23 18:14:13 +0000138 * The data is writable up until the next drawIndexed, drawNonIndexed,
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000139 * drawIndexedInstances, drawRect, copySurface, or pushGeometrySource. At
140 * this point the data is frozen and the ptrs are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000141 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000142 * Where the space is allocated and how it is uploaded to the GPU is
143 * subclass-dependent.
144 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000145 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.come3d70952012-03-13 12:40:53 +0000146 * is long-lived. When the data in the buffer is consumed depends on the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000147 * GrDrawTarget subclass. For deferred subclasses the caller has to
bsalomon@google.come3d70952012-03-13 12:40:53 +0000148 * guarantee that the data is still available in the buffers at playback.
149 * (TODO: Make this more automatic as we have done for read/write pixels)
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000150 *
151 * The size of each vertex is determined by querying the current GrDrawState.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000152 */
153
154 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000155 * Reserves space for vertices and/or indices. Zero can be specifed as
156 * either the vertex or index count if the caller desires to only reserve
rmistry@google.comd6176b02012-08-23 18:14:13 +0000157 * space for only indices or only vertices. If zero is specifed for
bsalomon@google.come3d70952012-03-13 12:40:53 +0000158 * vertexCount then the vertex source will be unmodified and likewise for
159 * indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000160 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000161 * If the function returns true then the reserve suceeded and the vertices
162 * and indices pointers will point to the space created.
reed@google.comac10a2d2010-12-22 21:39:39 +0000163 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000164 * If the target cannot make space for the request then this function will
165 * return false. If vertexCount was non-zero then upon failure the vertex
166 * source is reset and likewise for indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000167 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000168 * The pointers to the space allocated for vertices and indices remain valid
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000169 * until a drawIndexed, drawNonIndexed, drawIndexedInstances, drawRect,
170 * copySurface, or push/popGeomtrySource is called. At that point logically a
171 * snapshot of the data is made and the pointers are invalid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000172 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000173 * @param vertexCount the number of vertices to reserve space for. Can be
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000174 * 0. Vertex size is queried from the current GrDrawState.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000175 * @param indexCount the number of indices to reserve space for. Can be 0.
bsalomon@google.come3d70952012-03-13 12:40:53 +0000176 * @param vertices will point to reserved vertex space if vertexCount is
rmistry@google.comd6176b02012-08-23 18:14:13 +0000177 * non-zero. Illegal to pass NULL if vertexCount > 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000178 * @param indices will point to reserved index space if indexCount is
179 * non-zero. Illegal to pass NULL if indexCount > 0.
180 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000181 bool reserveVertexAndIndexSpace(int vertexCount,
bsalomon@google.com97805382012-03-13 14:32:07 +0000182 int indexCount,
183 void** vertices,
184 void** indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000185
reed@google.comac10a2d2010-12-22 21:39:39 +0000186 /**
187 * Provides hints to caller about the number of vertices and indices
188 * that can be allocated cheaply. This can be useful if caller is reserving
189 * space but doesn't know exactly how much geometry is needed.
190 *
191 * Also may hint whether the draw target should be flushed first. This is
192 * useful for deferred targets.
193 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000194 * @param vertexCount in: hint about how many vertices the caller would
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000195 * like to allocate. Vertex size is queried from the
196 * current GrDrawState.
reed@google.comac10a2d2010-12-22 21:39:39 +0000197 * out: a hint about the number of vertices that can be
198 * allocated cheaply. Negative means no hint.
199 * Ignored if NULL.
200 * @param indexCount in: hint about how many indices the caller would
201 * like to allocate.
202 * out: a hint about the number of indices that can be
203 * allocated cheaply. Negative means no hint.
204 * Ignored if NULL.
205 *
206 * @return true if target should be flushed based on the input values.
207 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000208 virtual bool geometryHints(int* vertexCount,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000209 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000210
211 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000212 * Sets source of vertex data for the next draw. Array must contain
213 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000214 *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000215 * @param vertexArray cpu array containing vertex data.
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000216 * @param vertexCount the number of vertices in the array. Vertex size is
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000217 * queried from the current GrDrawState.
reed@google.comac10a2d2010-12-22 21:39:39 +0000218 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000219 void setVertexSourceToArray(const void* vertexArray, int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000220
221 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000222 * Sets source of index data for the next indexed draw. Array must contain
223 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000224 *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000225 * @param indexArray cpu array containing index data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000226 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000227 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000228 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000229
230 /**
231 * Sets source of vertex data for the next draw. Data does not have to be
bsalomon@google.com934c5702012-03-20 21:17:58 +0000232 * in the buffer until drawIndexed, drawNonIndexed, or drawIndexedInstances.
reed@google.comac10a2d2010-12-22 21:39:39 +0000233 *
234 * @param buffer vertex buffer containing vertex data. Must be
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000235 * unlocked before draw call. Vertex size is queried
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000236 * from current GrDrawState.
reed@google.comac10a2d2010-12-22 21:39:39 +0000237 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000238 void setVertexSourceToBuffer(const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000239
240 /**
241 * Sets source of index data for the next indexed draw. Data does not have
bsalomon@google.com934c5702012-03-20 21:17:58 +0000242 * to be in the buffer until drawIndexed.
reed@google.comac10a2d2010-12-22 21:39:39 +0000243 *
244 * @param buffer index buffer containing indices. Must be unlocked
245 * before indexed draw call.
246 */
247 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000248
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000249 /**
250 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
251 * source to reserved, array, or buffer before next draw. May be able to free
252 * up temporary storage allocated by setVertexSourceToArray or
253 * reserveVertexSpace.
254 */
255 void resetVertexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000256
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000257 /**
258 * Resets index source. Indexed Drawing from reset indices is illegal. Set
259 * index source to reserved, array, or buffer before next indexed draw. May
260 * be able to free up temporary storage allocated by setIndexSourceToArray
261 * or reserveIndexSpace.
262 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000263 void resetIndexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000264
bsalomon@google.com97805382012-03-13 14:32:07 +0000265 /**
266 * Query to find out if the vertex or index source is reserved.
267 */
268 bool hasReservedVerticesOrIndices() const {
bsalomon@google.com73d98aa2012-03-13 14:41:19 +0000269 return kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc ||
bsalomon@google.com97805382012-03-13 14:32:07 +0000270 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
271 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000272
273 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000274 * Pushes and resets the vertex/index sources. Any reserved vertex / index
275 * data is finalized (i.e. cannot be updated after the matching pop but can
276 * be drawn from). Must be balanced by a pop.
277 */
278 void pushGeometrySource();
279
280 /**
281 * Pops the vertex / index sources from the matching push.
282 */
283 void popGeometrySource();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000284
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000285 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000286 * Draws indexed geometry using the current state and current vertex / index
287 * sources.
288 *
289 * @param type The type of primitives to draw.
290 * @param startVertex the vertex in the vertex array/buffer corresponding
291 * to index 0
292 * @param startIndex first index to read from index src.
293 * @param vertexCount one greater than the max index.
294 * @param indexCount the number of index elements to read. The index count
295 * is effectively trimmed to the last completely
296 * specified primitive.
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000297 * @param devBounds optional bounds hint. This is a promise from the caller,
298 * not a request for clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000299 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000300 void drawIndexed(GrPrimitiveType type,
301 int startVertex,
302 int startIndex,
303 int vertexCount,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000304 int indexCount,
305 const SkRect* devBounds = NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +0000306
307 /**
308 * Draws non-indexed geometry using the current state and current vertex
309 * sources.
310 *
311 * @param type The type of primitives to draw.
312 * @param startVertex the vertex in the vertex array/buffer corresponding
313 * to index 0
314 * @param vertexCount one greater than the max index.
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000315 * @param devBounds optional bounds hint. This is a promise from the caller,
316 * not a request for clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000317 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000318 void drawNonIndexed(GrPrimitiveType type,
319 int startVertex,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000320 int vertexCount,
321 const SkRect* devBounds = NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +0000322
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000323 /**
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000324 * Draws path into the stencil buffer. The fill must be either even/odd or
325 * winding (not inverse or hairline). It will respect the HW antialias flag
326 * on the draw state (if possible in the 3D API).
327 */
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000328 void stencilPath(const GrPath*, const SkStrokeRec& stroke, SkPath::FillType fill);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000329
330 /**
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000331 * Helper function for drawing rects. It performs a geometry src push and pop
332 * and thus will finalize any reserved geometry.
skia.committer@gmail.com044679e2013-02-15 07:16:57 +0000333 *
bsalomon@google.comc7818882013-03-20 19:19:53 +0000334 * @param rect the rect to draw
335 * @param matrix optional matrix applied to rect (before viewMatrix)
336 * @param localRect optional rect that specifies local coords to map onto
337 * rect. If NULL then rect serves as the local coords.
338 * @param localMatrix optional matrix applied to localRect. If
skia.committer@gmail.com044679e2013-02-15 07:16:57 +0000339 * srcRect is non-NULL and srcMatrix is non-NULL
340 * then srcRect will be transformed by srcMatrix.
jvanverth@google.com39768252013-02-14 15:25:44 +0000341 * srcMatrix can be NULL when no srcMatrix is desired.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000342 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000343 void drawRect(const SkRect& rect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000344 const SkMatrix* matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000345 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000346 const SkMatrix* localMatrix) {
347 AutoGeometryPush agp(this);
348 this->onDrawRect(rect, matrix, localRect, localMatrix);
349 }
jvanverth@google.com39768252013-02-14 15:25:44 +0000350
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000351 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000352 * Helper for drawRect when the caller doesn't need separate local rects or matrices.
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000353 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000354 void drawSimpleRect(const SkRect& rect, const SkMatrix* matrix = NULL) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000355 this->drawRect(rect, matrix, NULL, NULL);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000356 }
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000357 void drawSimpleRect(const SkIRect& irect, const SkMatrix* matrix = NULL) {
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000358 SkRect rect = SkRect::MakeFromIRect(irect);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000359 this->drawRect(rect, matrix, NULL, NULL);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000360 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000361
362 /**
bsalomon@google.com934c5702012-03-20 21:17:58 +0000363 * This call is used to draw multiple instances of some geometry with a
364 * given number of vertices (V) and indices (I) per-instance. The indices in
365 * the index source must have the form i[k+I] == i[k] + V. Also, all indices
366 * i[kI] ... i[(k+1)I-1] must be elements of the range kV ... (k+1)V-1. As a
367 * concrete example, the following index buffer for drawing a series of
368 * quads each as two triangles each satisfies these conditions with V=4 and
369 * I=6:
370 * (0,1,2,0,2,3, 4,5,6,4,6,7, 8,9,10,8,10,11, ...)
371 *
372 * The call assumes that the pattern of indices fills the entire index
373 * source. The size of the index buffer limits the number of instances that
374 * can be drawn by the GPU in a single draw. However, the caller may specify
375 * any (positive) number for instanceCount and if necessary multiple GPU
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000376 * draws will be issued. Moreover, when drawIndexedInstances is called
bsalomon@google.com934c5702012-03-20 21:17:58 +0000377 * multiple times it may be possible for GrDrawTarget to group them into a
378 * single GPU draw.
379 *
380 * @param type the type of primitives to draw
381 * @param instanceCount the number of instances to draw. Each instance
382 * consists of verticesPerInstance vertices indexed by
383 * indicesPerInstance indices drawn as the primitive
384 * type specified by type.
385 * @param verticesPerInstance The number of vertices in each instance (V
386 * in the above description).
387 * @param indicesPerInstance The number of indices in each instance (I
388 * in the above description).
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000389 * @param devBounds optional bounds hint. This is a promise from the caller,
390 * not a request for clipping.
bsalomon@google.com934c5702012-03-20 21:17:58 +0000391 */
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000392 void drawIndexedInstances(GrPrimitiveType type,
393 int instanceCount,
394 int verticesPerInstance,
395 int indicesPerInstance,
396 const SkRect* devBounds = NULL);
bsalomon@google.com934c5702012-03-20 21:17:58 +0000397
398 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000399 * Clear the current render target if one isn't passed in. Ignores the
400 * clip and all other draw state (blend mode, stages, etc). Clears the
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000401 * whole thing if rect is NULL, otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000402 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000403 virtual void clear(const SkIRect* rect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000404 GrColor color,
405 GrRenderTarget* renderTarget = NULL) = 0;
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000406
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000407 /**
408 * Copies a pixel rectangle from one surface to another. This call may finalize
409 * reserved vertex/index data (as though a draw call was made). The src pixels
410 * copied are specified by srcRect. They are copied to a rect of the same
411 * size in dst with top left at dstPoint. If the src rect is clipped by the
412 * src bounds then pixel values in the dst rect corresponding to area clipped
413 * by the src rect are not overwritten. This method can fail and return false
414 * depending on the type of surface, configs, etc, and the backend-specific
415 * limitations. If rect is clipped out entirely by the src or dst bounds then
416 * true is returned since there is no actual copy necessary to succeed.
417 */
418 bool copySurface(GrSurface* dst,
419 GrSurface* src,
420 const SkIRect& srcRect,
421 const SkIPoint& dstPoint);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000422 /**
423 * Function that determines whether a copySurface call would succeed without
424 * performing the copy.
425 */
426 bool canCopySurface(GrSurface* dst,
427 GrSurface* src,
428 const SkIRect& srcRect,
429 const SkIPoint& dstPoint);
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000430
robertphillips@google.comff175842012-05-14 19:31:39 +0000431 /**
bsalomon@google.comeb851172013-04-15 13:51:00 +0000432 * This is can be called before allocating a texture to be a dst for copySurface. It will
433 * populate the origin, config, and flags fields of the desc such that copySurface is more
434 * likely to succeed and be efficient.
435 */
436 virtual void initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc);
437
438
439 /**
robertphillips@google.comff175842012-05-14 19:31:39 +0000440 * Release any resources that are cached but not currently in use. This
441 * is intended to give an application some recourse when resources are low.
442 */
443 virtual void purgeResources() {};
444
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000445 /**
446 * For subclass internal use to invoke a call to onDraw(). See DrawInfo below.
447 */
448 void executeDraw(const DrawInfo& info) { this->onDraw(info); }
449
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000450 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000451
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000452 /**
453 * See AutoStateRestore below.
454 */
455 enum ASRInit {
456 kPreserve_ASRInit,
457 kReset_ASRInit
458 };
459
460 /**
461 * Saves off the current state and restores it in the destructor. It will
462 * install a new GrDrawState object on the target (setDrawState) and restore
463 * the previous one in the destructor. The caller should call drawState() to
464 * get the new draw state after the ASR is installed.
465 *
466 * GrDrawState* state = target->drawState();
467 * AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit).
468 * state->setRenderTarget(rt); // state refers to the GrDrawState set on
469 * // target before asr was initialized.
470 * // Therefore, rt is set on the GrDrawState
471 * // that will be restored after asr's
472 * // destructor rather than target's current
rmistry@google.comd6176b02012-08-23 18:14:13 +0000473 * // GrDrawState.
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000474 */
reed@google.comac10a2d2010-12-22 21:39:39 +0000475 class AutoStateRestore : ::GrNoncopyable {
476 public:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000477 /**
478 * Default ASR will have no effect unless set() is subsequently called.
479 */
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000480 AutoStateRestore();
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000481
482 /**
483 * Saves the state on target. The state will be restored when the ASR
484 * is destroyed. If this constructor is used do not call set().
485 *
486 * @param init Should the newly installed GrDrawState be a copy of the
487 * previous state or a default-initialized GrDrawState.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000488 * @param viewMatrix Optional view matrix. If init = kPreserve then the draw state's
489 * matrix will be preconcat'ed with the param. All stages will be
490 updated to compensate for the matrix change. If init == kReset
491 then the draw state's matrix will be this matrix.
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000492 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000493 AutoStateRestore(GrDrawTarget* target, ASRInit init, const SkMatrix* viewMatrix = NULL);
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000494
reed@google.comac10a2d2010-12-22 21:39:39 +0000495 ~AutoStateRestore();
496
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000497 /**
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000498 * Saves the state on target. The state will be restored when the ASR
499 * is destroyed. This should only be called once per ASR object and only
500 * when the default constructor was used. For nested saves use multiple
501 * ASR objects.
502 *
503 * @param init Should the newly installed GrDrawState be a copy of the
504 * previous state or a default-initialized GrDrawState.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000505 * @param viewMatrix Optional view matrix. If init = kPreserve then the draw state's
506 * matrix will be preconcat'ed with the param. All stages will be
507 updated to compensate for the matrix change. If init == kReset
508 then the draw state's matrix will be this matrix.
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000509 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000510 void set(GrDrawTarget* target, ASRInit init, const SkMatrix* viewMatrix = NULL);
511
512 /**
513 * Like set() but makes the view matrix identity. When init is kReset it is as though
514 * NULL was passed to set's viewMatrix param. When init is kPreserve it is as though
515 * the inverse view matrix was passed. If kPreserve is passed and the draw state's matrix
516 * is not invertible then this may fail.
517 */
518 bool setIdentity(GrDrawTarget* target, ASRInit init);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000519
reed@google.comac10a2d2010-12-22 21:39:39 +0000520 private:
bsalomon@google.com137f1342013-05-29 21:27:53 +0000521 GrDrawTarget* fDrawTarget;
522 SkTLazy<GrDrawState> fTempState;
523 GrDrawState* fSavedState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000524 };
525
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000526 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000527
reed@google.comac10a2d2010-12-22 21:39:39 +0000528 class AutoReleaseGeometry : ::GrNoncopyable {
529 public:
530 AutoReleaseGeometry(GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000531 int vertexCount,
532 int indexCount);
533 AutoReleaseGeometry();
534 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000535 bool set(GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000536 int vertexCount,
537 int indexCount);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000538 bool succeeded() const { return NULL != fTarget; }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000539 void* vertices() const { SkASSERT(this->succeeded()); return fVertices; }
540 void* indices() const { SkASSERT(this->succeeded()); return fIndices; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000541 GrPoint* positions() const {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000542 return static_cast<GrPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +0000543 }
544
545 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000546 void reset();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000547
reed@google.comac10a2d2010-12-22 21:39:39 +0000548 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000549 void* fVertices;
550 void* fIndices;
551 };
552
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000553 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000554
555 class AutoClipRestore : ::GrNoncopyable {
556 public:
557 AutoClipRestore(GrDrawTarget* target) {
558 fTarget = target;
559 fClip = fTarget->getClip();
560 }
561
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000562 AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip);
563
reed@google.comac10a2d2010-12-22 21:39:39 +0000564 ~AutoClipRestore() {
565 fTarget->setClip(fClip);
566 }
567 private:
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000568 GrDrawTarget* fTarget;
569 const GrClipData* fClip;
570 SkTLazy<SkClipStack> fStack;
571 GrClipData fReplacementClip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000572 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000573
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000574 ////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000575
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000576 /**
577 * Saves the geometry src state at construction and restores in the destructor. It also saves
578 * and then restores the vertex attrib state.
579 */
580 class AutoGeometryPush : ::GrNoncopyable {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000581 public:
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000582 AutoGeometryPush(GrDrawTarget* target)
583 : fAttribRestore(target->drawState()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000584 SkASSERT(NULL != target);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000585 fTarget = target;
586 target->pushGeometrySource();
587 }
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000588
589 ~AutoGeometryPush() { fTarget->popGeometrySource(); }
590
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000591 private:
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000592 GrDrawTarget* fTarget;
593 GrDrawState::AutoVertexAttribRestore fAttribRestore;
594 };
595
596 /**
597 * Combination of AutoGeometryPush and AutoStateRestore. The vertex attribs will be in default
598 * state regardless of ASRInit value.
599 */
600 class AutoGeometryAndStatePush : ::GrNoncopyable {
601 public:
bsalomon@google.com137f1342013-05-29 21:27:53 +0000602 AutoGeometryAndStatePush(GrDrawTarget* target,
603 ASRInit init,
604 const SkMatrix* viewMatrix = NULL)
605 : fState(target, init, viewMatrix) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000606 SkASSERT(NULL != target);
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000607 fTarget = target;
608 target->pushGeometrySource();
609 if (kPreserve_ASRInit == init) {
610 target->drawState()->setDefaultVertexAttribs();
611 }
612 }
613
614 ~AutoGeometryAndStatePush() { fTarget->popGeometrySource(); }
615
616 private:
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000617 AutoStateRestore fState;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000618 GrDrawTarget* fTarget;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000619 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000620
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000621 ///////////////////////////////////////////////////////////////////////////
622 // Draw execution tracking (for font atlases and other resources)
623 class DrawToken {
624 public:
625 DrawToken(GrDrawTarget* drawTarget, uint32_t drawID) :
626 fDrawTarget(drawTarget), fDrawID(drawID) {}
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000627
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000628 bool isIssued() { return NULL != fDrawTarget && fDrawTarget->isIssued(fDrawID); }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000629
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000630 private:
631 GrDrawTarget* fDrawTarget;
632 uint32_t fDrawID; // this may wrap, but we're doing direct comparison
633 // so that should be okay
634 };
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000635
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000636 virtual DrawToken getCurrentDrawToken() { return DrawToken(this, 0); }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000637
reed@google.comac10a2d2010-12-22 21:39:39 +0000638protected:
bsalomon@google.com471d4712011-08-23 15:45:25 +0000639
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000640 enum GeometrySrcType {
641 kNone_GeometrySrcType, //<! src has not been specified
642 kReserved_GeometrySrcType, //<! src was set using reserve*Space
643 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
644 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
645 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000646
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000647 struct GeometrySrcState {
648 GeometrySrcType fVertexSrc;
649 union {
650 // valid if src type is buffer
651 const GrVertexBuffer* fVertexBuffer;
652 // valid if src type is reserved or array
653 int fVertexCount;
654 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000655
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000656 GeometrySrcType fIndexSrc;
657 union {
658 // valid if src type is buffer
659 const GrIndexBuffer* fIndexBuffer;
660 // valid if src type is reserved or array
661 int fIndexCount;
662 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000663
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000664 size_t fVertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000665 };
bsalomon@google.com934c5702012-03-20 21:17:58 +0000666
667 int indexCountInCurrentSource() const {
668 const GeometrySrcState& src = this->getGeomSrc();
669 switch (src.fIndexSrc) {
670 case kNone_GeometrySrcType:
671 return 0;
672 case kReserved_GeometrySrcType:
673 case kArray_GeometrySrcType:
674 return src.fIndexCount;
675 case kBuffer_GeometrySrcType:
676 return src.fIndexBuffer->sizeInBytes() / sizeof(uint16_t);
677 default:
678 GrCrash("Unexpected Index Source.");
679 return 0;
680 }
681 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000682
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000683 // This method is called by copySurface The srcRect is guaranteed to be entirely within the
684 // src bounds. Likewise, the dst rect implied by dstPoint and srcRect's width and height falls
685 // entirely within the dst. The default implementation will draw a rect from the src to the
686 // dst if the src is a texture and the dst is a render target and fail otherwise.
687 virtual bool onCopySurface(GrSurface* dst,
688 GrSurface* src,
689 const SkIRect& srcRect,
690 const SkIPoint& dstPoint);
691
692 // Called to determine whether an onCopySurface call would succeed or not. This is useful for
693 // proxy subclasses to test whether the copy would succeed without executing it yet. Derived
694 // classes must keep this consistent with their implementation of onCopySurface(). The inputs
695 // are the same as onCopySurface(), i.e. srcRect and dstPoint are clipped to be inside the src
696 // and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000697 virtual bool onCanCopySurface(GrSurface* dst,
698 GrSurface* src,
699 const SkIRect& srcRect,
700 const SkIPoint& dstPoint);
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000701
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000702 GrContext* getContext() { return fContext; }
703 const GrContext* getContext() const { return fContext; }
704
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000705 // A subclass may override this function if it wishes to be notified when the clip is changed.
706 // The override should call INHERITED::clipWillBeSet().
707 virtual void clipWillBeSet(const GrClipData* clipData);
708
709 // subclasses must call this in their destructors to ensure all vertex
710 // and index sources have been released (including those held by
711 // pushGeometrySource())
712 void releaseGeometry();
713
714 // accessors for derived classes
715 const GeometrySrcState& getGeomSrc() const { return fGeoSrcStateStack.back(); }
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000716 // it is preferable to call this rather than getGeomSrc()->fVertexSize because of the assert.
717 size_t getVertexSize() const {
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000718 // the vertex layout is only valid if a vertex source has been specified.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000719 SkASSERT(this->getGeomSrc().fVertexSrc != kNone_GeometrySrcType);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000720 return this->getGeomSrc().fVertexSize;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000721 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000722
bsalomon@google.combcce8922013-03-25 15:38:39 +0000723 // Subclass must initialize this in its constructor.
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000724 SkAutoTUnref<const GrDrawTargetCaps> fCaps;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000725
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000726 /**
727 * Used to communicate draws to subclass's onDraw function.
728 */
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000729 class DrawInfo {
730 public:
731 DrawInfo(const DrawInfo& di) { (*this) = di; }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000732 DrawInfo& operator =(const DrawInfo& di);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000733
734 GrPrimitiveType primitiveType() const { return fPrimitiveType; }
735 int startVertex() const { return fStartVertex; }
736 int startIndex() const { return fStartIndex; }
737 int vertexCount() const { return fVertexCount; }
738 int indexCount() const { return fIndexCount; }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000739 int verticesPerInstance() const { return fVerticesPerInstance; }
740 int indicesPerInstance() const { return fIndicesPerInstance; }
741 int instanceCount() const { return fInstanceCount; }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000742
743 bool isIndexed() const { return fIndexCount > 0; }
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000744#ifdef SK_DEBUG
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000745 bool isInstanced() const; // this version is longer because of asserts
746#else
747 bool isInstanced() const { return fInstanceCount > 0; }
748#endif
749
750 // adds or remove instances
751 void adjustInstanceCount(int instanceOffset);
752 // shifts the start vertex
753 void adjustStartVertex(int vertexOffset);
754 // shifts the start index
755 void adjustStartIndex(int indexOffset);
756
757 void setDevBounds(const SkRect& bounds) {
758 fDevBoundsStorage = bounds;
759 fDevBounds = &fDevBoundsStorage;
760 }
761 const SkRect* getDevBounds() const { return fDevBounds; }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000762
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000763 bool getDevIBounds(SkIRect* bounds) const {
764 if (NULL != fDevBounds) {
765 fDevBounds->roundOut(bounds);
766 return true;
767 } else {
768 return false;
769 }
770 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000771
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000772 // NULL if no copy of the dst is needed for the draw.
773 const GrDeviceCoordTexture* getDstCopy() const {
774 if (NULL != fDstCopy.texture()) {
775 return &fDstCopy;
776 } else {
777 return NULL;
778 }
779 }
780
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000781 private:
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000782 DrawInfo() { fDevBounds = NULL; }
783
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000784 friend class GrDrawTarget;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000785
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000786 GrPrimitiveType fPrimitiveType;
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000787
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000788 int fStartVertex;
789 int fStartIndex;
790 int fVertexCount;
791 int fIndexCount;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000792
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000793 int fInstanceCount;
794 int fVerticesPerInstance;
795 int fIndicesPerInstance;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000796
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000797 SkRect fDevBoundsStorage;
798 SkRect* fDevBounds;
799
800 GrDeviceCoordTexture fDstCopy;
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000801 };
802
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000803private:
804 // A subclass can optionally overload this function to be notified before
805 // vertex and index space is reserved.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000806 virtual void willReserveVertexAndIndexSpace(int vertexCount, int indexCount) {}
bsalomon@google.com97805382012-03-13 14:32:07 +0000807
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000808 // implemented by subclass to allocate space for reserved geom
jvanverth@google.coma6338982013-01-31 21:34:25 +0000809 virtual bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices) = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000810 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
811 // implemented by subclass to handle release of reserved geom space
812 virtual void releaseReservedVertexSpace() = 0;
813 virtual void releaseReservedIndexSpace() = 0;
814 // subclass must consume array contents when set
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000815 virtual void onSetVertexSourceToArray(const void* vertexArray, int vertexCount) = 0;
816 virtual void onSetIndexSourceToArray(const void* indexArray, int indexCount) = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000817 // subclass is notified that geom source will be set away from an array
818 virtual void releaseVertexArray() = 0;
819 virtual void releaseIndexArray() = 0;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000820 // subclass overrides to be notified just before geo src state is pushed/popped.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000821 virtual void geometrySourceWillPush() = 0;
822 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
823 // subclass called to perform drawing
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000824 virtual void onDraw(const DrawInfo&) = 0;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000825 // Implementation of drawRect. The geometry src and vertex attribs will already
826 // be saved before this is called and restored afterwards. A subclass may override
827 // this to perform more optimal rect rendering. Its draws should be funneled through
828 // one of the public GrDrawTarget draw methods (e.g. drawNonIndexed,
829 // drawIndexedInstances, ...). The base class draws a two triangle fan using
830 // drawNonIndexed from reserved vertex space.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000831 virtual void onDrawRect(const SkRect& rect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000832 const SkMatrix* matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000833 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000834 const SkMatrix* localMatrix);
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000835 virtual void onStencilPath(const GrPath*, const SkStrokeRec& stroke, SkPath::FillType fill) = 0;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000836
bsalomon@google.come3d70952012-03-13 12:40:53 +0000837 // helpers for reserving vertex and index space.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000838 bool reserveVertexSpace(size_t vertexSize,
bsalomon@google.come3d70952012-03-13 12:40:53 +0000839 int vertexCount,
840 void** vertices);
841 bool reserveIndexSpace(int indexCount, void** indices);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000842
bsalomon@google.come8262622011-11-07 02:30:51 +0000843 // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
844 // indicate non-indexed drawing.
845 bool checkDraw(GrPrimitiveType type, int startVertex,
846 int startIndex, int vertexCount,
847 int indexCount) const;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000848 // called when setting a new vert/idx source to unref prev vb/ib
849 void releasePreviousVertexSource();
850 void releasePreviousIndexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000851
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000852 // Makes a copy of the dst if it is necessary for the draw. Returns false if a copy is required
853 // but couldn't be made. Otherwise, returns true.
854 bool setupDstReadIfNecessary(DrawInfo* info);
855
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000856 // Check to see if this set of draw commands has been sent out
857 virtual bool isIssued(uint32_t drawID) { return true; }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000858
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000859 enum {
860 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +0000861 };
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000862 SkSTArray<kPreallocGeoSrcStateStackCnt, GeometrySrcState, true> fGeoSrcStateStack;
863 const GrClipData* fClip;
864 GrDrawState* fDrawState;
865 GrDrawState fDefaultDrawState;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000866 // The context owns us, not vice-versa, so this ptr is not ref'ed by DrawTarget.
867 GrContext* fContext;
reed@google.comfa35e3d2012-06-26 20:16:17 +0000868
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000869 typedef SkRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000870};
871
reed@google.comac10a2d2010-12-22 21:39:39 +0000872#endif