blob: 47460ac527b56ac85d3efdccde2dc5f999e3ee11 [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"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000020#include "SkTypes.h"
bsalomon@google.com8d67c072012-12-13 20:38:14 +000021#include "SkXfermode.h"
Scroggo97c88c22011-05-11 14:05:25 +000022
robertphillips@google.coma2d71482012-08-01 20:08:47 +000023class GrClipData;
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000024class GrDrawTargetCaps;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000025class GrPath;
reed@google.comac10a2d2010-12-22 21:39:39 +000026class GrVertexBuffer;
sugoi@google.com5f74cf82012-12-17 21:16:45 +000027class SkStrokeRec;
sugoi@google.com12b4e272012-12-06 20:13:11 +000028
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000029class GrDrawTarget : public SkRefCnt {
bsalomon@google.comf6601872012-08-28 21:11:35 +000030protected:
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000031 class DrawInfo;
32
bsalomon@google.comf6601872012-08-28 21:11:35 +000033public:
34 SK_DECLARE_INST_COUNT(GrDrawTarget)
35
reed@google.comac10a2d2010-12-22 21:39:39 +000036 ///////////////////////////////////////////////////////////////////////////
37
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000038 // The context may not be fully constructed and should not be used during GrDrawTarget
39 // construction.
40 GrDrawTarget(GrContext* context);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000041 virtual ~GrDrawTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +000042
43 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000044 * Gets the capabilities of the draw target.
45 */
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000046 const GrDrawTargetCaps* caps() const { return fCaps.get(); }
bsalomon@google.com18c9c192011-09-22 21:01:31 +000047
48 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000049 * Sets the current clip to the region specified by clip. All draws will be
50 * clipped against this clip if kClip_StateBit is enabled.
51 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000052 * Setting the clip may (or may not) zero out the client's stencil bits.
53 *
reed@google.comac10a2d2010-12-22 21:39:39 +000054 * @param description of the clipping region
55 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +000056 void setClip(const GrClipData* clip);
reed@google.comac10a2d2010-12-22 21:39:39 +000057
58 /**
59 * Gets the current clip.
60 *
61 * @return the clip.
62 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +000063 const GrClipData* getClip() const;
reed@google.comac10a2d2010-12-22 21:39:39 +000064
bsalomon@google.coma5d056a2012-03-27 15:59:58 +000065 /**
66 * Sets the draw state object for the draw target. Note that this does not
67 * make a copy. The GrDrawTarget will take a reference to passed object.
68 * Passing NULL will cause the GrDrawTarget to use its own internal draw
69 * state object rather than an externally provided one.
70 */
71 void setDrawState(GrDrawState* drawState);
72
73 /**
74 * Read-only access to the GrDrawTarget's current draw state.
75 */
76 const GrDrawState& getDrawState() const { return *fDrawState; }
77
78 /**
79 * Read-write access to the GrDrawTarget's current draw state. Note that
80 * this doesn't ref.
81 */
82 GrDrawState* drawState() { return fDrawState; }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +000083
bsalomon@google.com3d0835b2011-12-08 16:12:03 +000084 /**
bsalomon@google.comd46e2422011-09-23 17:40:07 +000085 * Color alpha and coverage are two inputs to the drawing pipeline. For some
86 * blend modes it is safe to fold the coverage into constant or per-vertex
87 * color alpha value. For other blend modes they must be handled separately.
88 * Depending on features available in the underlying 3D API this may or may
89 * not be possible.
90 *
bsalomon@google.come79c8152012-03-29 19:07:12 +000091 * This function considers the current draw state and the draw target's
92 * capabilities to determine whether coverage can be handled correctly. The
93 * following assumptions are made:
94 * 1. The caller intends to somehow specify coverage. This can be
95 * specified either by enabling a coverage stage on the GrDrawState or
96 * via the vertex layout.
bsalomon@google.com2b446732013-02-12 16:47:41 +000097 * 2. Other than enabling coverage stages or enabling coverage in the
98 * layout, the current configuration of the target's GrDrawState is as
99 * it will be at draw time.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000100 */
101 bool canApplyCoverage() const;
102
103 /**
bsalomon@google.com2b446732013-02-12 16:47:41 +0000104 * Given the current draw state and hw support, will HW AA lines be used (if
105 * a line primitive type is drawn)?
bsalomon@google.com471d4712011-08-23 15:45:25 +0000106 */
tomhudson@google.com93813632011-10-27 20:21:16 +0000107 bool willUseHWAALines() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000108
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000109 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000110 * There are three types of "sources" of geometry (vertices and indices) for
111 * draw calls made on the target. When performing an indexed draw, the
112 * indices and vertices can use different source types. Once a source is
bsalomon@google.com934c5702012-03-20 21:17:58 +0000113 * specified it can be used for multiple draws. However, the time at which
114 * the geometry data is no longer editable depends on the source type.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000115 *
116 * Sometimes it is necessary to perform a draw while upstack code has
bsalomon@google.come3d70952012-03-13 12:40:53 +0000117 * already specified geometry that it isn't finished with. So there are push
118 * and pop methods. This allows the client to push the sources, draw
119 * something using alternate sources, and then pop to restore the original
120 * sources.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000121 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000122 * Aside from pushes and pops, a source remains valid until another source
123 * is set or resetVertexSource / resetIndexSource is called. Drawing from
124 * a reset source is an error.
125 *
126 * The three types of sources are:
127 *
128 * 1. A cpu array (set*SourceToArray). This is useful when the caller
129 * already provided vertex data in a format compatible with a
130 * GrVertexLayout. The data in the array is consumed at the time that
131 * set*SourceToArray is called and subsequent edits to the array will not
132 * be reflected in draws.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000133 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000134 * 2. Reserve. This is most useful when the caller has data it must
135 * transform before drawing and is not long-lived. The caller requests
136 * that the draw target make room for some amount of vertex and/or index
137 * data. The target provides ptrs to hold the vertex and/or index data.
138 *
rmistry@google.comd6176b02012-08-23 18:14:13 +0000139 * The data is writable up until the next drawIndexed, drawNonIndexed,
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000140 * drawIndexedInstances, drawRect, copySurface, or pushGeometrySource. At
141 * this point the data is frozen and the ptrs are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000142 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000143 * Where the space is allocated and how it is uploaded to the GPU is
144 * subclass-dependent.
145 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000146 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.come3d70952012-03-13 12:40:53 +0000147 * is long-lived. When the data in the buffer is consumed depends on the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000148 * GrDrawTarget subclass. For deferred subclasses the caller has to
bsalomon@google.come3d70952012-03-13 12:40:53 +0000149 * guarantee that the data is still available in the buffers at playback.
150 * (TODO: Make this more automatic as we have done for read/write pixels)
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000151 *
152 * The size of each vertex is determined by querying the current GrDrawState.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000153 */
154
155 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000156 * Reserves space for vertices and/or indices. Zero can be specifed as
157 * either the vertex or index count if the caller desires to only reserve
rmistry@google.comd6176b02012-08-23 18:14:13 +0000158 * space for only indices or only vertices. If zero is specifed for
bsalomon@google.come3d70952012-03-13 12:40:53 +0000159 * vertexCount then the vertex source will be unmodified and likewise for
160 * indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000161 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000162 * If the function returns true then the reserve suceeded and the vertices
163 * and indices pointers will point to the space created.
reed@google.comac10a2d2010-12-22 21:39:39 +0000164 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000165 * If the target cannot make space for the request then this function will
166 * return false. If vertexCount was non-zero then upon failure the vertex
167 * source is reset and likewise for indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000168 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000169 * The pointers to the space allocated for vertices and indices remain valid
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000170 * until a drawIndexed, drawNonIndexed, drawIndexedInstances, drawRect,
171 * copySurface, or push/popGeomtrySource is called. At that point logically a
172 * snapshot of the data is made and the pointers are invalid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000173 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000174 * @param vertexCount the number of vertices to reserve space for. Can be
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000175 * 0. Vertex size is queried from the current GrDrawState.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000176 * @param indexCount the number of indices to reserve space for. Can be 0.
bsalomon@google.come3d70952012-03-13 12:40:53 +0000177 * @param vertices will point to reserved vertex space if vertexCount is
rmistry@google.comd6176b02012-08-23 18:14:13 +0000178 * non-zero. Illegal to pass NULL if vertexCount > 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000179 * @param indices will point to reserved index space if indexCount is
180 * non-zero. Illegal to pass NULL if indexCount > 0.
181 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000182 bool reserveVertexAndIndexSpace(int vertexCount,
bsalomon@google.com97805382012-03-13 14:32:07 +0000183 int indexCount,
184 void** vertices,
185 void** indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000186
reed@google.comac10a2d2010-12-22 21:39:39 +0000187 /**
188 * Provides hints to caller about the number of vertices and indices
189 * that can be allocated cheaply. This can be useful if caller is reserving
190 * space but doesn't know exactly how much geometry is needed.
191 *
192 * Also may hint whether the draw target should be flushed first. This is
193 * useful for deferred targets.
194 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000195 * @param vertexCount in: hint about how many vertices the caller would
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000196 * like to allocate. Vertex size is queried from the
197 * current GrDrawState.
reed@google.comac10a2d2010-12-22 21:39:39 +0000198 * out: a hint about the number of vertices that can be
199 * allocated cheaply. Negative means no hint.
200 * Ignored if NULL.
201 * @param indexCount in: hint about how many indices the caller would
202 * like to allocate.
203 * out: a hint about the number of indices that can be
204 * allocated cheaply. Negative means no hint.
205 * Ignored if NULL.
206 *
207 * @return true if target should be flushed based on the input values.
208 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000209 virtual bool geometryHints(int* vertexCount,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000210 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000211
212 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000213 * Sets source of vertex data for the next draw. Array must contain
214 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000215 *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000216 * @param vertexArray cpu array containing vertex data.
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000217 * @param vertexCount the number of vertices in the array. Vertex size is
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000218 * queried from the current GrDrawState.
reed@google.comac10a2d2010-12-22 21:39:39 +0000219 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000220 void setVertexSourceToArray(const void* vertexArray, int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000221
222 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000223 * Sets source of index data for the next indexed draw. Array must contain
224 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000225 *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000226 * @param indexArray cpu array containing index data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000227 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000228 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000229 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000230
231 /**
232 * Sets source of vertex data for the next draw. Data does not have to be
bsalomon@google.com934c5702012-03-20 21:17:58 +0000233 * in the buffer until drawIndexed, drawNonIndexed, or drawIndexedInstances.
reed@google.comac10a2d2010-12-22 21:39:39 +0000234 *
235 * @param buffer vertex buffer containing vertex data. Must be
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000236 * unlocked before draw call. Vertex size is queried
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000237 * from current GrDrawState.
reed@google.comac10a2d2010-12-22 21:39:39 +0000238 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000239 void setVertexSourceToBuffer(const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000240
241 /**
242 * Sets source of index data for the next indexed draw. Data does not have
bsalomon@google.com934c5702012-03-20 21:17:58 +0000243 * to be in the buffer until drawIndexed.
reed@google.comac10a2d2010-12-22 21:39:39 +0000244 *
245 * @param buffer index buffer containing indices. Must be unlocked
246 * before indexed draw call.
247 */
248 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000249
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000250 /**
251 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
252 * source to reserved, array, or buffer before next draw. May be able to free
253 * up temporary storage allocated by setVertexSourceToArray or
254 * reserveVertexSpace.
255 */
256 void resetVertexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000257
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000258 /**
259 * Resets index source. Indexed Drawing from reset indices is illegal. Set
260 * index source to reserved, array, or buffer before next indexed draw. May
261 * be able to free up temporary storage allocated by setIndexSourceToArray
262 * or reserveIndexSpace.
263 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000264 void resetIndexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000265
bsalomon@google.com97805382012-03-13 14:32:07 +0000266 /**
267 * Query to find out if the vertex or index source is reserved.
268 */
269 bool hasReservedVerticesOrIndices() const {
bsalomon@google.com73d98aa2012-03-13 14:41:19 +0000270 return kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc ||
bsalomon@google.com97805382012-03-13 14:32:07 +0000271 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
272 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000273
274 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000275 * Pushes and resets the vertex/index sources. Any reserved vertex / index
276 * data is finalized (i.e. cannot be updated after the matching pop but can
277 * be drawn from). Must be balanced by a pop.
278 */
279 void pushGeometrySource();
280
281 /**
282 * Pops the vertex / index sources from the matching push.
283 */
284 void popGeometrySource();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000285
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000286 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000287 * Draws indexed geometry using the current state and current vertex / index
288 * sources.
289 *
290 * @param type The type of primitives to draw.
291 * @param startVertex the vertex in the vertex array/buffer corresponding
292 * to index 0
293 * @param startIndex first index to read from index src.
294 * @param vertexCount one greater than the max index.
295 * @param indexCount the number of index elements to read. The index count
296 * is effectively trimmed to the last completely
297 * specified primitive.
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000298 * @param devBounds optional bounds hint. This is a promise from the caller,
299 * not a request for clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000300 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000301 void drawIndexed(GrPrimitiveType type,
302 int startVertex,
303 int startIndex,
304 int vertexCount,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000305 int indexCount,
306 const SkRect* devBounds = NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +0000307
308 /**
309 * Draws non-indexed geometry using the current state and current vertex
310 * sources.
311 *
312 * @param type The type of primitives to draw.
313 * @param startVertex the vertex in the vertex array/buffer corresponding
314 * to index 0
315 * @param vertexCount one greater than the max index.
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000316 * @param devBounds optional bounds hint. This is a promise from the caller,
317 * not a request for clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000318 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000319 void drawNonIndexed(GrPrimitiveType type,
320 int startVertex,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000321 int vertexCount,
322 const SkRect* devBounds = NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +0000323
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000324 /**
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000325 * Draws path into the stencil buffer. The fill must be either even/odd or
326 * winding (not inverse or hairline). It will respect the HW antialias flag
327 * on the draw state (if possible in the 3D API).
328 */
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000329 void stencilPath(const GrPath*, const SkStrokeRec& stroke, SkPath::FillType fill);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000330
331 /**
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000332 * Helper function for drawing rects. It performs a geometry src push and pop
333 * and thus will finalize any reserved geometry.
skia.committer@gmail.com044679e2013-02-15 07:16:57 +0000334 *
bsalomon@google.comc7818882013-03-20 19:19:53 +0000335 * @param rect the rect to draw
336 * @param matrix optional matrix applied to rect (before viewMatrix)
337 * @param localRect optional rect that specifies local coords to map onto
338 * rect. If NULL then rect serves as the local coords.
339 * @param localMatrix optional matrix applied to localRect. If
skia.committer@gmail.com044679e2013-02-15 07:16:57 +0000340 * srcRect is non-NULL and srcMatrix is non-NULL
341 * then srcRect will be transformed by srcMatrix.
jvanverth@google.com39768252013-02-14 15:25:44 +0000342 * srcMatrix can be NULL when no srcMatrix is desired.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000343 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000344 void drawRect(const SkRect& rect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000345 const SkMatrix* matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000346 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000347 const SkMatrix* localMatrix) {
348 AutoGeometryPush agp(this);
349 this->onDrawRect(rect, matrix, localRect, localMatrix);
350 }
jvanverth@google.com39768252013-02-14 15:25:44 +0000351
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000352 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000353 * Helper for drawRect when the caller doesn't need separate local rects or matrices.
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000354 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000355 void drawSimpleRect(const SkRect& rect, const SkMatrix* matrix = NULL) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000356 this->drawRect(rect, matrix, NULL, NULL);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000357 }
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000358 void drawSimpleRect(const SkIRect& irect, const SkMatrix* matrix = NULL) {
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000359 SkRect rect = SkRect::MakeFromIRect(irect);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000360 this->drawRect(rect, matrix, NULL, NULL);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000361 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000362
363 /**
bsalomon@google.com934c5702012-03-20 21:17:58 +0000364 * This call is used to draw multiple instances of some geometry with a
365 * given number of vertices (V) and indices (I) per-instance. The indices in
366 * the index source must have the form i[k+I] == i[k] + V. Also, all indices
367 * i[kI] ... i[(k+1)I-1] must be elements of the range kV ... (k+1)V-1. As a
368 * concrete example, the following index buffer for drawing a series of
369 * quads each as two triangles each satisfies these conditions with V=4 and
370 * I=6:
371 * (0,1,2,0,2,3, 4,5,6,4,6,7, 8,9,10,8,10,11, ...)
372 *
373 * The call assumes that the pattern of indices fills the entire index
374 * source. The size of the index buffer limits the number of instances that
375 * can be drawn by the GPU in a single draw. However, the caller may specify
376 * any (positive) number for instanceCount and if necessary multiple GPU
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000377 * draws will be issued. Moreover, when drawIndexedInstances is called
bsalomon@google.com934c5702012-03-20 21:17:58 +0000378 * multiple times it may be possible for GrDrawTarget to group them into a
379 * single GPU draw.
380 *
381 * @param type the type of primitives to draw
382 * @param instanceCount the number of instances to draw. Each instance
383 * consists of verticesPerInstance vertices indexed by
384 * indicesPerInstance indices drawn as the primitive
385 * type specified by type.
386 * @param verticesPerInstance The number of vertices in each instance (V
387 * in the above description).
388 * @param indicesPerInstance The number of indices in each instance (I
389 * in the above description).
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000390 * @param devBounds optional bounds hint. This is a promise from the caller,
391 * not a request for clipping.
bsalomon@google.com934c5702012-03-20 21:17:58 +0000392 */
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000393 void drawIndexedInstances(GrPrimitiveType type,
394 int instanceCount,
395 int verticesPerInstance,
396 int indicesPerInstance,
397 const SkRect* devBounds = NULL);
bsalomon@google.com934c5702012-03-20 21:17:58 +0000398
399 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000400 * Clear the current render target if one isn't passed in. Ignores the
401 * clip and all other draw state (blend mode, stages, etc). Clears the
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000402 * whole thing if rect is NULL, otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000403 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000404 virtual void clear(const SkIRect* rect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000405 GrColor color,
406 GrRenderTarget* renderTarget = NULL) = 0;
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000407
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000408 /**
409 * Copies a pixel rectangle from one surface to another. This call may finalize
410 * reserved vertex/index data (as though a draw call was made). The src pixels
411 * copied are specified by srcRect. They are copied to a rect of the same
412 * size in dst with top left at dstPoint. If the src rect is clipped by the
413 * src bounds then pixel values in the dst rect corresponding to area clipped
414 * by the src rect are not overwritten. This method can fail and return false
415 * depending on the type of surface, configs, etc, and the backend-specific
416 * limitations. If rect is clipped out entirely by the src or dst bounds then
417 * true is returned since there is no actual copy necessary to succeed.
418 */
419 bool copySurface(GrSurface* dst,
420 GrSurface* src,
421 const SkIRect& srcRect,
422 const SkIPoint& dstPoint);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000423 /**
424 * Function that determines whether a copySurface call would succeed without
425 * performing the copy.
426 */
427 bool canCopySurface(GrSurface* dst,
428 GrSurface* src,
429 const SkIRect& srcRect,
430 const SkIPoint& dstPoint);
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000431
robertphillips@google.comff175842012-05-14 19:31:39 +0000432 /**
bsalomon@google.comeb851172013-04-15 13:51:00 +0000433 * This is can be called before allocating a texture to be a dst for copySurface. It will
434 * populate the origin, config, and flags fields of the desc such that copySurface is more
435 * likely to succeed and be efficient.
436 */
437 virtual void initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc);
438
439
440 /**
robertphillips@google.comff175842012-05-14 19:31:39 +0000441 * Release any resources that are cached but not currently in use. This
442 * is intended to give an application some recourse when resources are low.
443 */
444 virtual void purgeResources() {};
445
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000446 /**
447 * For subclass internal use to invoke a call to onDraw(). See DrawInfo below.
448 */
449 void executeDraw(const DrawInfo& info) { this->onDraw(info); }
450
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000451 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000452
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000453 /**
454 * See AutoStateRestore below.
455 */
456 enum ASRInit {
457 kPreserve_ASRInit,
458 kReset_ASRInit
459 };
460
461 /**
462 * Saves off the current state and restores it in the destructor. It will
463 * install a new GrDrawState object on the target (setDrawState) and restore
464 * the previous one in the destructor. The caller should call drawState() to
465 * get the new draw state after the ASR is installed.
466 *
467 * GrDrawState* state = target->drawState();
468 * AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit).
469 * state->setRenderTarget(rt); // state refers to the GrDrawState set on
470 * // target before asr was initialized.
471 * // Therefore, rt is set on the GrDrawState
472 * // that will be restored after asr's
473 * // destructor rather than target's current
rmistry@google.comd6176b02012-08-23 18:14:13 +0000474 * // GrDrawState.
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000475 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000476 class AutoStateRestore : public ::SkNoncopyable {
reed@google.comac10a2d2010-12-22 21:39:39 +0000477 public:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000478 /**
479 * Default ASR will have no effect unless set() is subsequently called.
480 */
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000481 AutoStateRestore();
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000482
483 /**
484 * Saves the state on target. The state will be restored when the ASR
485 * is destroyed. If this constructor is used do not call set().
486 *
487 * @param init Should the newly installed GrDrawState be a copy of the
488 * previous state or a default-initialized GrDrawState.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000489 * @param viewMatrix Optional view matrix. If init = kPreserve then the draw state's
490 * matrix will be preconcat'ed with the param. All stages will be
491 updated to compensate for the matrix change. If init == kReset
492 then the draw state's matrix will be this matrix.
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000493 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000494 AutoStateRestore(GrDrawTarget* target, ASRInit init, const SkMatrix* viewMatrix = NULL);
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000495
reed@google.comac10a2d2010-12-22 21:39:39 +0000496 ~AutoStateRestore();
497
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000498 /**
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000499 * Saves the state on target. The state will be restored when the ASR
500 * is destroyed. This should only be called once per ASR object and only
501 * when the default constructor was used. For nested saves use multiple
502 * ASR objects.
503 *
504 * @param init Should the newly installed GrDrawState be a copy of the
505 * previous state or a default-initialized GrDrawState.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000506 * @param viewMatrix Optional view matrix. If init = kPreserve then the draw state's
507 * matrix will be preconcat'ed with the param. All stages will be
508 updated to compensate for the matrix change. If init == kReset
509 then the draw state's matrix will be this matrix.
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000510 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000511 void set(GrDrawTarget* target, ASRInit init, const SkMatrix* viewMatrix = NULL);
512
513 /**
514 * Like set() but makes the view matrix identity. When init is kReset it is as though
515 * NULL was passed to set's viewMatrix param. When init is kPreserve it is as though
516 * the inverse view matrix was passed. If kPreserve is passed and the draw state's matrix
517 * is not invertible then this may fail.
518 */
519 bool setIdentity(GrDrawTarget* target, ASRInit init);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000520
reed@google.comac10a2d2010-12-22 21:39:39 +0000521 private:
bsalomon@google.com137f1342013-05-29 21:27:53 +0000522 GrDrawTarget* fDrawTarget;
523 SkTLazy<GrDrawState> fTempState;
524 GrDrawState* fSavedState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000525 };
526
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000527 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000528
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000529 class AutoReleaseGeometry : public ::SkNoncopyable {
reed@google.comac10a2d2010-12-22 21:39:39 +0000530 public:
531 AutoReleaseGeometry(GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000532 int vertexCount,
533 int indexCount);
534 AutoReleaseGeometry();
535 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000536 bool set(GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000537 int vertexCount,
538 int indexCount);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000539 bool succeeded() const { return NULL != fTarget; }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000540 void* vertices() const { SkASSERT(this->succeeded()); return fVertices; }
541 void* indices() const { SkASSERT(this->succeeded()); return fIndices; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000542 GrPoint* positions() const {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000543 return static_cast<GrPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +0000544 }
545
546 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000547 void reset();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000548
reed@google.comac10a2d2010-12-22 21:39:39 +0000549 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000550 void* fVertices;
551 void* fIndices;
552 };
553
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000554 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000555
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000556 class AutoClipRestore : public ::SkNoncopyable {
reed@google.comac10a2d2010-12-22 21:39:39 +0000557 public:
558 AutoClipRestore(GrDrawTarget* target) {
559 fTarget = target;
560 fClip = fTarget->getClip();
561 }
562
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000563 AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip);
564
reed@google.comac10a2d2010-12-22 21:39:39 +0000565 ~AutoClipRestore() {
566 fTarget->setClip(fClip);
567 }
568 private:
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000569 GrDrawTarget* fTarget;
570 const GrClipData* fClip;
571 SkTLazy<SkClipStack> fStack;
572 GrClipData fReplacementClip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000573 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000574
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000575 ////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000576
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000577 /**
578 * Saves the geometry src state at construction and restores in the destructor. It also saves
579 * and then restores the vertex attrib state.
580 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000581 class AutoGeometryPush : public ::SkNoncopyable {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000582 public:
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000583 AutoGeometryPush(GrDrawTarget* target)
584 : fAttribRestore(target->drawState()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000585 SkASSERT(NULL != target);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000586 fTarget = target;
587 target->pushGeometrySource();
588 }
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000589
590 ~AutoGeometryPush() { fTarget->popGeometrySource(); }
591
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000592 private:
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000593 GrDrawTarget* fTarget;
594 GrDrawState::AutoVertexAttribRestore fAttribRestore;
595 };
596
597 /**
598 * Combination of AutoGeometryPush and AutoStateRestore. The vertex attribs will be in default
599 * state regardless of ASRInit value.
600 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000601 class AutoGeometryAndStatePush : public ::SkNoncopyable {
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000602 public:
bsalomon@google.com137f1342013-05-29 21:27:53 +0000603 AutoGeometryAndStatePush(GrDrawTarget* target,
604 ASRInit init,
605 const SkMatrix* viewMatrix = NULL)
606 : fState(target, init, viewMatrix) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000607 SkASSERT(NULL != target);
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000608 fTarget = target;
609 target->pushGeometrySource();
610 if (kPreserve_ASRInit == init) {
611 target->drawState()->setDefaultVertexAttribs();
612 }
613 }
614
615 ~AutoGeometryAndStatePush() { fTarget->popGeometrySource(); }
616
617 private:
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000618 AutoStateRestore fState;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000619 GrDrawTarget* fTarget;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000620 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000621
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000622 ///////////////////////////////////////////////////////////////////////////
623 // Draw execution tracking (for font atlases and other resources)
624 class DrawToken {
625 public:
626 DrawToken(GrDrawTarget* drawTarget, uint32_t drawID) :
627 fDrawTarget(drawTarget), fDrawID(drawID) {}
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000628
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000629 bool isIssued() { return NULL != fDrawTarget && fDrawTarget->isIssued(fDrawID); }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000630
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000631 private:
632 GrDrawTarget* fDrawTarget;
633 uint32_t fDrawID; // this may wrap, but we're doing direct comparison
634 // so that should be okay
635 };
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000636
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000637 virtual DrawToken getCurrentDrawToken() { return DrawToken(this, 0); }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000638
reed@google.comac10a2d2010-12-22 21:39:39 +0000639protected:
bsalomon@google.com471d4712011-08-23 15:45:25 +0000640
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000641 enum GeometrySrcType {
642 kNone_GeometrySrcType, //<! src has not been specified
643 kReserved_GeometrySrcType, //<! src was set using reserve*Space
644 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
645 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
646 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000647
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000648 struct GeometrySrcState {
649 GeometrySrcType fVertexSrc;
650 union {
651 // valid if src type is buffer
652 const GrVertexBuffer* fVertexBuffer;
653 // valid if src type is reserved or array
654 int fVertexCount;
655 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000656
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000657 GeometrySrcType fIndexSrc;
658 union {
659 // valid if src type is buffer
660 const GrIndexBuffer* fIndexBuffer;
661 // valid if src type is reserved or array
662 int fIndexCount;
663 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000664
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000665 size_t fVertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000666 };
bsalomon@google.com934c5702012-03-20 21:17:58 +0000667
668 int indexCountInCurrentSource() const {
669 const GeometrySrcState& src = this->getGeomSrc();
670 switch (src.fIndexSrc) {
671 case kNone_GeometrySrcType:
672 return 0;
673 case kReserved_GeometrySrcType:
674 case kArray_GeometrySrcType:
675 return src.fIndexCount;
676 case kBuffer_GeometrySrcType:
677 return src.fIndexBuffer->sizeInBytes() / sizeof(uint16_t);
678 default:
679 GrCrash("Unexpected Index Source.");
680 return 0;
681 }
682 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000683
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000684 // This method is called by copySurface The srcRect is guaranteed to be entirely within the
685 // src bounds. Likewise, the dst rect implied by dstPoint and srcRect's width and height falls
686 // entirely within the dst. The default implementation will draw a rect from the src to the
687 // dst if the src is a texture and the dst is a render target and fail otherwise.
688 virtual bool onCopySurface(GrSurface* dst,
689 GrSurface* src,
690 const SkIRect& srcRect,
691 const SkIPoint& dstPoint);
692
693 // Called to determine whether an onCopySurface call would succeed or not. This is useful for
694 // proxy subclasses to test whether the copy would succeed without executing it yet. Derived
695 // classes must keep this consistent with their implementation of onCopySurface(). The inputs
696 // are the same as onCopySurface(), i.e. srcRect and dstPoint are clipped to be inside the src
697 // and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000698 virtual bool onCanCopySurface(GrSurface* dst,
699 GrSurface* src,
700 const SkIRect& srcRect,
701 const SkIPoint& dstPoint);
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000702
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000703 GrContext* getContext() { return fContext; }
704 const GrContext* getContext() const { return fContext; }
705
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000706 // A subclass may override this function if it wishes to be notified when the clip is changed.
707 // The override should call INHERITED::clipWillBeSet().
708 virtual void clipWillBeSet(const GrClipData* clipData);
709
710 // subclasses must call this in their destructors to ensure all vertex
711 // and index sources have been released (including those held by
712 // pushGeometrySource())
713 void releaseGeometry();
714
715 // accessors for derived classes
716 const GeometrySrcState& getGeomSrc() const { return fGeoSrcStateStack.back(); }
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000717 // it is preferable to call this rather than getGeomSrc()->fVertexSize because of the assert.
718 size_t getVertexSize() const {
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000719 // the vertex layout is only valid if a vertex source has been specified.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000720 SkASSERT(this->getGeomSrc().fVertexSrc != kNone_GeometrySrcType);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000721 return this->getGeomSrc().fVertexSize;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000722 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000723
bsalomon@google.combcce8922013-03-25 15:38:39 +0000724 // Subclass must initialize this in its constructor.
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000725 SkAutoTUnref<const GrDrawTargetCaps> fCaps;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000726
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000727 /**
728 * Used to communicate draws to subclass's onDraw function.
729 */
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000730 class DrawInfo {
731 public:
732 DrawInfo(const DrawInfo& di) { (*this) = di; }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000733 DrawInfo& operator =(const DrawInfo& di);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000734
735 GrPrimitiveType primitiveType() const { return fPrimitiveType; }
736 int startVertex() const { return fStartVertex; }
737 int startIndex() const { return fStartIndex; }
738 int vertexCount() const { return fVertexCount; }
739 int indexCount() const { return fIndexCount; }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000740 int verticesPerInstance() const { return fVerticesPerInstance; }
741 int indicesPerInstance() const { return fIndicesPerInstance; }
742 int instanceCount() const { return fInstanceCount; }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000743
744 bool isIndexed() const { return fIndexCount > 0; }
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000745#ifdef SK_DEBUG
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000746 bool isInstanced() const; // this version is longer because of asserts
747#else
748 bool isInstanced() const { return fInstanceCount > 0; }
749#endif
750
751 // adds or remove instances
752 void adjustInstanceCount(int instanceOffset);
753 // shifts the start vertex
754 void adjustStartVertex(int vertexOffset);
755 // shifts the start index
756 void adjustStartIndex(int indexOffset);
757
758 void setDevBounds(const SkRect& bounds) {
759 fDevBoundsStorage = bounds;
760 fDevBounds = &fDevBoundsStorage;
761 }
762 const SkRect* getDevBounds() const { return fDevBounds; }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000763
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000764 bool getDevIBounds(SkIRect* bounds) const {
765 if (NULL != fDevBounds) {
766 fDevBounds->roundOut(bounds);
767 return true;
768 } else {
769 return false;
770 }
771 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000772
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000773 // NULL if no copy of the dst is needed for the draw.
774 const GrDeviceCoordTexture* getDstCopy() const {
775 if (NULL != fDstCopy.texture()) {
776 return &fDstCopy;
777 } else {
778 return NULL;
779 }
780 }
781
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000782 private:
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000783 DrawInfo() { fDevBounds = NULL; }
784
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000785 friend class GrDrawTarget;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000786
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000787 GrPrimitiveType fPrimitiveType;
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000788
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000789 int fStartVertex;
790 int fStartIndex;
791 int fVertexCount;
792 int fIndexCount;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000793
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000794 int fInstanceCount;
795 int fVerticesPerInstance;
796 int fIndicesPerInstance;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000797
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000798 SkRect fDevBoundsStorage;
799 SkRect* fDevBounds;
800
801 GrDeviceCoordTexture fDstCopy;
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000802 };
803
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000804private:
805 // A subclass can optionally overload this function to be notified before
806 // vertex and index space is reserved.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000807 virtual void willReserveVertexAndIndexSpace(int vertexCount, int indexCount) {}
bsalomon@google.com97805382012-03-13 14:32:07 +0000808
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000809 // implemented by subclass to allocate space for reserved geom
jvanverth@google.coma6338982013-01-31 21:34:25 +0000810 virtual bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices) = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000811 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
812 // implemented by subclass to handle release of reserved geom space
813 virtual void releaseReservedVertexSpace() = 0;
814 virtual void releaseReservedIndexSpace() = 0;
815 // subclass must consume array contents when set
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000816 virtual void onSetVertexSourceToArray(const void* vertexArray, int vertexCount) = 0;
817 virtual void onSetIndexSourceToArray(const void* indexArray, int indexCount) = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000818 // subclass is notified that geom source will be set away from an array
819 virtual void releaseVertexArray() = 0;
820 virtual void releaseIndexArray() = 0;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000821 // subclass overrides to be notified just before geo src state is pushed/popped.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000822 virtual void geometrySourceWillPush() = 0;
823 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
824 // subclass called to perform drawing
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000825 virtual void onDraw(const DrawInfo&) = 0;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000826 // Implementation of drawRect. The geometry src and vertex attribs will already
827 // be saved before this is called and restored afterwards. A subclass may override
828 // this to perform more optimal rect rendering. Its draws should be funneled through
829 // one of the public GrDrawTarget draw methods (e.g. drawNonIndexed,
830 // drawIndexedInstances, ...). The base class draws a two triangle fan using
831 // drawNonIndexed from reserved vertex space.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000832 virtual void onDrawRect(const SkRect& rect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000833 const SkMatrix* matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000834 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000835 const SkMatrix* localMatrix);
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000836 virtual void onStencilPath(const GrPath*, const SkStrokeRec& stroke, SkPath::FillType fill) = 0;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000837
bsalomon@google.come3d70952012-03-13 12:40:53 +0000838 // helpers for reserving vertex and index space.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000839 bool reserveVertexSpace(size_t vertexSize,
bsalomon@google.come3d70952012-03-13 12:40:53 +0000840 int vertexCount,
841 void** vertices);
842 bool reserveIndexSpace(int indexCount, void** indices);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000843
bsalomon@google.come8262622011-11-07 02:30:51 +0000844 // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
845 // indicate non-indexed drawing.
846 bool checkDraw(GrPrimitiveType type, int startVertex,
847 int startIndex, int vertexCount,
848 int indexCount) const;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000849 // called when setting a new vert/idx source to unref prev vb/ib
850 void releasePreviousVertexSource();
851 void releasePreviousIndexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000852
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000853 // Makes a copy of the dst if it is necessary for the draw. Returns false if a copy is required
854 // but couldn't be made. Otherwise, returns true.
855 bool setupDstReadIfNecessary(DrawInfo* info);
856
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000857 // Check to see if this set of draw commands has been sent out
858 virtual bool isIssued(uint32_t drawID) { return true; }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000859
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000860 enum {
861 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +0000862 };
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000863 SkSTArray<kPreallocGeoSrcStateStackCnt, GeometrySrcState, true> fGeoSrcStateStack;
864 const GrClipData* fClip;
865 GrDrawState* fDrawState;
866 GrDrawState fDefaultDrawState;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000867 // The context owns us, not vice-versa, so this ptr is not ref'ed by DrawTarget.
868 GrContext* fContext;
reed@google.comfa35e3d2012-06-26 20:16:17 +0000869
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000870 typedef SkRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000871};
872
reed@google.comac10a2d2010-12-22 21:39:39 +0000873#endif