blob: c5058699b5ef880c2e2277099f7dae8e1c948d3e [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
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000103 /** When we're using coverage AA but the blend is incompatible (given gpu
104 * limitations) we should disable AA. */
105 bool shouldDisableCoverageAAForBlend() {
106 // Enable below if we should draw with AA even when it produces
107 // incorrect blending.
108 // return false;
109 return !this->canApplyCoverage();
110 }
111
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000112 /**
bsalomon@google.com2b446732013-02-12 16:47:41 +0000113 * Given the current draw state and hw support, will HW AA lines be used (if
114 * a line primitive type is drawn)?
bsalomon@google.com471d4712011-08-23 15:45:25 +0000115 */
tomhudson@google.com93813632011-10-27 20:21:16 +0000116 bool willUseHWAALines() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000117
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000118 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000119 * There are three types of "sources" of geometry (vertices and indices) for
120 * draw calls made on the target. When performing an indexed draw, the
121 * indices and vertices can use different source types. Once a source is
bsalomon@google.com934c5702012-03-20 21:17:58 +0000122 * specified it can be used for multiple draws. However, the time at which
123 * the geometry data is no longer editable depends on the source type.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000124 *
125 * Sometimes it is necessary to perform a draw while upstack code has
bsalomon@google.come3d70952012-03-13 12:40:53 +0000126 * already specified geometry that it isn't finished with. So there are push
127 * and pop methods. This allows the client to push the sources, draw
128 * something using alternate sources, and then pop to restore the original
129 * sources.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000130 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000131 * Aside from pushes and pops, a source remains valid until another source
132 * is set or resetVertexSource / resetIndexSource is called. Drawing from
133 * a reset source is an error.
134 *
135 * The three types of sources are:
136 *
137 * 1. A cpu array (set*SourceToArray). This is useful when the caller
138 * already provided vertex data in a format compatible with a
139 * GrVertexLayout. The data in the array is consumed at the time that
140 * set*SourceToArray is called and subsequent edits to the array will not
141 * be reflected in draws.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000142 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000143 * 2. Reserve. This is most useful when the caller has data it must
144 * transform before drawing and is not long-lived. The caller requests
145 * that the draw target make room for some amount of vertex and/or index
146 * data. The target provides ptrs to hold the vertex and/or index data.
147 *
rmistry@google.comd6176b02012-08-23 18:14:13 +0000148 * The data is writable up until the next drawIndexed, drawNonIndexed,
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000149 * drawIndexedInstances, drawRect, copySurface, or pushGeometrySource. At
150 * this point the data is frozen and the ptrs are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000151 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000152 * Where the space is allocated and how it is uploaded to the GPU is
153 * subclass-dependent.
154 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000155 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.come3d70952012-03-13 12:40:53 +0000156 * is long-lived. When the data in the buffer is consumed depends on the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000157 * GrDrawTarget subclass. For deferred subclasses the caller has to
bsalomon@google.come3d70952012-03-13 12:40:53 +0000158 * guarantee that the data is still available in the buffers at playback.
159 * (TODO: Make this more automatic as we have done for read/write pixels)
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000160 *
161 * The size of each vertex is determined by querying the current GrDrawState.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000162 */
163
164 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000165 * Reserves space for vertices and/or indices. Zero can be specifed as
166 * either the vertex or index count if the caller desires to only reserve
rmistry@google.comd6176b02012-08-23 18:14:13 +0000167 * space for only indices or only vertices. If zero is specifed for
bsalomon@google.come3d70952012-03-13 12:40:53 +0000168 * vertexCount then the vertex source will be unmodified and likewise for
169 * indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000170 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000171 * If the function returns true then the reserve suceeded and the vertices
172 * and indices pointers will point to the space created.
reed@google.comac10a2d2010-12-22 21:39:39 +0000173 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000174 * If the target cannot make space for the request then this function will
175 * return false. If vertexCount was non-zero then upon failure the vertex
176 * source is reset and likewise for indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000177 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000178 * The pointers to the space allocated for vertices and indices remain valid
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000179 * until a drawIndexed, drawNonIndexed, drawIndexedInstances, drawRect,
180 * copySurface, or push/popGeomtrySource is called. At that point logically a
181 * snapshot of the data is made and the pointers are invalid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000182 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000183 * @param vertexCount the number of vertices to reserve space for. Can be
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000184 * 0. Vertex size is queried from the current GrDrawState.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000185 * @param indexCount the number of indices to reserve space for. Can be 0.
bsalomon@google.come3d70952012-03-13 12:40:53 +0000186 * @param vertices will point to reserved vertex space if vertexCount is
rmistry@google.comd6176b02012-08-23 18:14:13 +0000187 * non-zero. Illegal to pass NULL if vertexCount > 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000188 * @param indices will point to reserved index space if indexCount is
189 * non-zero. Illegal to pass NULL if indexCount > 0.
190 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000191 bool reserveVertexAndIndexSpace(int vertexCount,
bsalomon@google.com97805382012-03-13 14:32:07 +0000192 int indexCount,
193 void** vertices,
194 void** indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000195
reed@google.comac10a2d2010-12-22 21:39:39 +0000196 /**
197 * Provides hints to caller about the number of vertices and indices
198 * that can be allocated cheaply. This can be useful if caller is reserving
199 * space but doesn't know exactly how much geometry is needed.
200 *
201 * Also may hint whether the draw target should be flushed first. This is
202 * useful for deferred targets.
203 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000204 * @param vertexCount in: hint about how many vertices the caller would
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000205 * like to allocate. Vertex size is queried from the
206 * current GrDrawState.
reed@google.comac10a2d2010-12-22 21:39:39 +0000207 * out: a hint about the number of vertices that can be
208 * allocated cheaply. Negative means no hint.
209 * Ignored if NULL.
210 * @param indexCount in: hint about how many indices the caller would
211 * like to allocate.
212 * out: a hint about the number of indices that can be
213 * allocated cheaply. Negative means no hint.
214 * Ignored if NULL.
215 *
216 * @return true if target should be flushed based on the input values.
217 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000218 virtual bool geometryHints(int* vertexCount,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000219 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000220
221 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000222 * Sets source of vertex data for the next draw. Array must contain
223 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000224 *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000225 * @param vertexArray cpu array containing vertex data.
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000226 * @param vertexCount the number of vertices in the array. Vertex size is
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000227 * queried from the current GrDrawState.
reed@google.comac10a2d2010-12-22 21:39:39 +0000228 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000229 void setVertexSourceToArray(const void* vertexArray, int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000230
231 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000232 * Sets source of index data for the next indexed draw. Array must contain
233 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000234 *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000235 * @param indexArray cpu array containing index data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000236 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000237 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000238 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000239
240 /**
241 * Sets source of vertex data for the next draw. Data does not have to be
bsalomon@google.com934c5702012-03-20 21:17:58 +0000242 * in the buffer until drawIndexed, drawNonIndexed, or drawIndexedInstances.
reed@google.comac10a2d2010-12-22 21:39:39 +0000243 *
244 * @param buffer vertex buffer containing vertex data. Must be
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000245 * unlocked before draw call. Vertex size is queried
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000246 * from current GrDrawState.
reed@google.comac10a2d2010-12-22 21:39:39 +0000247 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000248 void setVertexSourceToBuffer(const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000249
250 /**
251 * Sets source of index data for the next indexed draw. Data does not have
bsalomon@google.com934c5702012-03-20 21:17:58 +0000252 * to be in the buffer until drawIndexed.
reed@google.comac10a2d2010-12-22 21:39:39 +0000253 *
254 * @param buffer index buffer containing indices. Must be unlocked
255 * before indexed draw call.
256 */
257 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000258
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000259 /**
260 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
261 * source to reserved, array, or buffer before next draw. May be able to free
262 * up temporary storage allocated by setVertexSourceToArray or
263 * reserveVertexSpace.
264 */
265 void resetVertexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000266
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000267 /**
268 * Resets index source. Indexed Drawing from reset indices is illegal. Set
269 * index source to reserved, array, or buffer before next indexed draw. May
270 * be able to free up temporary storage allocated by setIndexSourceToArray
271 * or reserveIndexSpace.
272 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000273 void resetIndexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000274
bsalomon@google.com97805382012-03-13 14:32:07 +0000275 /**
276 * Query to find out if the vertex or index source is reserved.
277 */
278 bool hasReservedVerticesOrIndices() const {
bsalomon@google.com73d98aa2012-03-13 14:41:19 +0000279 return kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc ||
bsalomon@google.com97805382012-03-13 14:32:07 +0000280 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
281 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000282
283 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000284 * Pushes and resets the vertex/index sources. Any reserved vertex / index
285 * data is finalized (i.e. cannot be updated after the matching pop but can
286 * be drawn from). Must be balanced by a pop.
287 */
288 void pushGeometrySource();
289
290 /**
291 * Pops the vertex / index sources from the matching push.
292 */
293 void popGeometrySource();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000294
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000295 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000296 * Draws indexed geometry using the current state and current vertex / index
297 * sources.
298 *
299 * @param type The type of primitives to draw.
300 * @param startVertex the vertex in the vertex array/buffer corresponding
301 * to index 0
302 * @param startIndex first index to read from index src.
303 * @param vertexCount one greater than the max index.
304 * @param indexCount the number of index elements to read. The index count
305 * is effectively trimmed to the last completely
306 * specified primitive.
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000307 * @param devBounds optional bounds hint. This is a promise from the caller,
308 * not a request for clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000309 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000310 void drawIndexed(GrPrimitiveType type,
311 int startVertex,
312 int startIndex,
313 int vertexCount,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000314 int indexCount,
315 const SkRect* devBounds = NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +0000316
317 /**
318 * Draws non-indexed geometry using the current state and current vertex
319 * sources.
320 *
321 * @param type The type of primitives to draw.
322 * @param startVertex the vertex in the vertex array/buffer corresponding
323 * to index 0
324 * @param vertexCount one greater than the max index.
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000325 * @param devBounds optional bounds hint. This is a promise from the caller,
326 * not a request for clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000327 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000328 void drawNonIndexed(GrPrimitiveType type,
329 int startVertex,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000330 int vertexCount,
331 const SkRect* devBounds = NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +0000332
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000333 /**
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000334 * Draws path into the stencil buffer. The fill must be either even/odd or
335 * winding (not inverse or hairline). It will respect the HW antialias flag
336 * on the draw state (if possible in the 3D API).
337 */
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000338 void stencilPath(const GrPath*, SkPath::FillType fill);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000339
340 /**
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000341 * Draws a path. Fill must not be a hairline. It will respect the HW
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000342 * antialias flag on the draw state (if possible in the 3D API).
343 */
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000344 void drawPath(const GrPath*, SkPath::FillType fill);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000345
346 /**
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000347 * Helper function for drawing rects. It performs a geometry src push and pop
348 * and thus will finalize any reserved geometry.
skia.committer@gmail.com044679e2013-02-15 07:16:57 +0000349 *
bsalomon@google.comc7818882013-03-20 19:19:53 +0000350 * @param rect the rect to draw
351 * @param matrix optional matrix applied to rect (before viewMatrix)
352 * @param localRect optional rect that specifies local coords to map onto
353 * rect. If NULL then rect serves as the local coords.
354 * @param localMatrix optional matrix applied to localRect. If
skia.committer@gmail.com044679e2013-02-15 07:16:57 +0000355 * srcRect is non-NULL and srcMatrix is non-NULL
356 * then srcRect will be transformed by srcMatrix.
jvanverth@google.com39768252013-02-14 15:25:44 +0000357 * srcMatrix can be NULL when no srcMatrix is desired.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000358 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000359 void drawRect(const SkRect& rect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000360 const SkMatrix* matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000361 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000362 const SkMatrix* localMatrix) {
363 AutoGeometryPush agp(this);
364 this->onDrawRect(rect, matrix, localRect, localMatrix);
365 }
jvanverth@google.com39768252013-02-14 15:25:44 +0000366
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000367 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000368 * Helper for drawRect when the caller doesn't need separate local rects or matrices.
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000369 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000370 void drawSimpleRect(const SkRect& rect, const SkMatrix* matrix = NULL) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000371 this->drawRect(rect, matrix, NULL, NULL);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000372 }
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000373 void drawSimpleRect(const SkIRect& irect, const SkMatrix* matrix = NULL) {
reed@google.com44699382013-10-31 17:28:30 +0000374 SkRect rect = SkRect::Make(irect);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000375 this->drawRect(rect, matrix, NULL, NULL);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000376 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000377
378 /**
bsalomon@google.com934c5702012-03-20 21:17:58 +0000379 * This call is used to draw multiple instances of some geometry with a
380 * given number of vertices (V) and indices (I) per-instance. The indices in
381 * the index source must have the form i[k+I] == i[k] + V. Also, all indices
382 * i[kI] ... i[(k+1)I-1] must be elements of the range kV ... (k+1)V-1. As a
383 * concrete example, the following index buffer for drawing a series of
384 * quads each as two triangles each satisfies these conditions with V=4 and
385 * I=6:
386 * (0,1,2,0,2,3, 4,5,6,4,6,7, 8,9,10,8,10,11, ...)
387 *
388 * The call assumes that the pattern of indices fills the entire index
389 * source. The size of the index buffer limits the number of instances that
390 * can be drawn by the GPU in a single draw. However, the caller may specify
391 * any (positive) number for instanceCount and if necessary multiple GPU
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000392 * draws will be issued. Moreover, when drawIndexedInstances is called
bsalomon@google.com934c5702012-03-20 21:17:58 +0000393 * multiple times it may be possible for GrDrawTarget to group them into a
394 * single GPU draw.
395 *
396 * @param type the type of primitives to draw
397 * @param instanceCount the number of instances to draw. Each instance
398 * consists of verticesPerInstance vertices indexed by
399 * indicesPerInstance indices drawn as the primitive
400 * type specified by type.
401 * @param verticesPerInstance The number of vertices in each instance (V
402 * in the above description).
403 * @param indicesPerInstance The number of indices in each instance (I
404 * in the above description).
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000405 * @param devBounds optional bounds hint. This is a promise from the caller,
406 * not a request for clipping.
bsalomon@google.com934c5702012-03-20 21:17:58 +0000407 */
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000408 void drawIndexedInstances(GrPrimitiveType type,
409 int instanceCount,
410 int verticesPerInstance,
411 int indicesPerInstance,
412 const SkRect* devBounds = NULL);
bsalomon@google.com934c5702012-03-20 21:17:58 +0000413
414 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000415 * Clear the current render target if one isn't passed in. Ignores the
416 * clip and all other draw state (blend mode, stages, etc). Clears the
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000417 * whole thing if rect is NULL, otherwise just the rect. If canIgnoreRect
418 * is set then the entire render target can be optionally cleared.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000419 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000420 virtual void clear(const SkIRect* rect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000421 GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000422 bool canIgnoreRect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000423 GrRenderTarget* renderTarget = NULL) = 0;
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000424
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000425 /**
426 * Copies a pixel rectangle from one surface to another. This call may finalize
427 * reserved vertex/index data (as though a draw call was made). The src pixels
428 * copied are specified by srcRect. They are copied to a rect of the same
429 * size in dst with top left at dstPoint. If the src rect is clipped by the
430 * src bounds then pixel values in the dst rect corresponding to area clipped
431 * by the src rect are not overwritten. This method can fail and return false
432 * depending on the type of surface, configs, etc, and the backend-specific
433 * limitations. If rect is clipped out entirely by the src or dst bounds then
434 * true is returned since there is no actual copy necessary to succeed.
435 */
436 bool copySurface(GrSurface* dst,
437 GrSurface* src,
438 const SkIRect& srcRect,
439 const SkIPoint& dstPoint);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000440 /**
441 * Function that determines whether a copySurface call would succeed without
442 * performing the copy.
443 */
444 bool canCopySurface(GrSurface* dst,
445 GrSurface* src,
446 const SkIRect& srcRect,
447 const SkIPoint& dstPoint);
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000448
robertphillips@google.comff175842012-05-14 19:31:39 +0000449 /**
bsalomon@google.comeb851172013-04-15 13:51:00 +0000450 * This is can be called before allocating a texture to be a dst for copySurface. It will
451 * populate the origin, config, and flags fields of the desc such that copySurface is more
452 * likely to succeed and be efficient.
453 */
454 virtual void initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc);
455
456
457 /**
robertphillips@google.comff175842012-05-14 19:31:39 +0000458 * Release any resources that are cached but not currently in use. This
459 * is intended to give an application some recourse when resources are low.
460 */
461 virtual void purgeResources() {};
462
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000463 /**
464 * For subclass internal use to invoke a call to onDraw(). See DrawInfo below.
465 */
466 void executeDraw(const DrawInfo& info) { this->onDraw(info); }
467
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000468 /**
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000469 * For subclass internal use to invoke a call to onDrawPath().
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000470 */
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000471 void executeDrawPath(const GrPath* path, SkPath::FillType fill,
472 const GrDeviceCoordTexture* dstCopy) {
473 this->onDrawPath(path, fill, dstCopy);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000474 }
475
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000476 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000477
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000478 /**
479 * See AutoStateRestore below.
480 */
481 enum ASRInit {
482 kPreserve_ASRInit,
483 kReset_ASRInit
484 };
485
486 /**
487 * Saves off the current state and restores it in the destructor. It will
488 * install a new GrDrawState object on the target (setDrawState) and restore
489 * the previous one in the destructor. The caller should call drawState() to
490 * get the new draw state after the ASR is installed.
491 *
492 * GrDrawState* state = target->drawState();
493 * AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit).
494 * state->setRenderTarget(rt); // state refers to the GrDrawState set on
495 * // target before asr was initialized.
496 * // Therefore, rt is set on the GrDrawState
497 * // that will be restored after asr's
498 * // destructor rather than target's current
rmistry@google.comd6176b02012-08-23 18:14:13 +0000499 * // GrDrawState.
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000500 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000501 class AutoStateRestore : public ::SkNoncopyable {
reed@google.comac10a2d2010-12-22 21:39:39 +0000502 public:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000503 /**
504 * Default ASR will have no effect unless set() is subsequently called.
505 */
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000506 AutoStateRestore();
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000507
508 /**
509 * Saves the state on target. The state will be restored when the ASR
510 * is destroyed. If this constructor is used do not call set().
511 *
512 * @param init Should the newly installed GrDrawState be a copy of the
513 * previous state or a default-initialized GrDrawState.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000514 * @param viewMatrix Optional view matrix. If init = kPreserve then the draw state's
515 * matrix will be preconcat'ed with the param. All stages will be
516 updated to compensate for the matrix change. If init == kReset
517 then the draw state's matrix will be this matrix.
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000518 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000519 AutoStateRestore(GrDrawTarget* target, ASRInit init, const SkMatrix* viewMatrix = NULL);
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000520
reed@google.comac10a2d2010-12-22 21:39:39 +0000521 ~AutoStateRestore();
522
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000523 /**
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000524 * Saves the state on target. The state will be restored when the ASR
525 * is destroyed. This should only be called once per ASR object and only
526 * when the default constructor was used. For nested saves use multiple
527 * ASR objects.
528 *
529 * @param init Should the newly installed GrDrawState be a copy of the
530 * previous state or a default-initialized GrDrawState.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000531 * @param viewMatrix Optional view matrix. If init = kPreserve then the draw state's
532 * matrix will be preconcat'ed with the param. All stages will be
533 updated to compensate for the matrix change. If init == kReset
534 then the draw state's matrix will be this matrix.
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000535 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000536 void set(GrDrawTarget* target, ASRInit init, const SkMatrix* viewMatrix = NULL);
537
538 /**
539 * Like set() but makes the view matrix identity. When init is kReset it is as though
540 * NULL was passed to set's viewMatrix param. When init is kPreserve it is as though
541 * the inverse view matrix was passed. If kPreserve is passed and the draw state's matrix
542 * is not invertible then this may fail.
543 */
544 bool setIdentity(GrDrawTarget* target, ASRInit init);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000545
reed@google.comac10a2d2010-12-22 21:39:39 +0000546 private:
bsalomon@google.com137f1342013-05-29 21:27:53 +0000547 GrDrawTarget* fDrawTarget;
548 SkTLazy<GrDrawState> fTempState;
549 GrDrawState* fSavedState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000550 };
551
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000552 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000553
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000554 class AutoReleaseGeometry : public ::SkNoncopyable {
reed@google.comac10a2d2010-12-22 21:39:39 +0000555 public:
556 AutoReleaseGeometry(GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000557 int vertexCount,
558 int indexCount);
559 AutoReleaseGeometry();
560 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000561 bool set(GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000562 int vertexCount,
563 int indexCount);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000564 bool succeeded() const { return NULL != fTarget; }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000565 void* vertices() const { SkASSERT(this->succeeded()); return fVertices; }
566 void* indices() const { SkASSERT(this->succeeded()); return fIndices; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000567 GrPoint* positions() const {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000568 return static_cast<GrPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +0000569 }
570
571 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000572 void reset();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000573
reed@google.comac10a2d2010-12-22 21:39:39 +0000574 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000575 void* fVertices;
576 void* fIndices;
577 };
578
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000579 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000580
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000581 class AutoClipRestore : public ::SkNoncopyable {
reed@google.comac10a2d2010-12-22 21:39:39 +0000582 public:
583 AutoClipRestore(GrDrawTarget* target) {
584 fTarget = target;
585 fClip = fTarget->getClip();
586 }
587
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000588 AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip);
589
reed@google.comac10a2d2010-12-22 21:39:39 +0000590 ~AutoClipRestore() {
591 fTarget->setClip(fClip);
592 }
593 private:
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000594 GrDrawTarget* fTarget;
595 const GrClipData* fClip;
596 SkTLazy<SkClipStack> fStack;
597 GrClipData fReplacementClip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000598 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000599
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000600 ////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000601
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000602 /**
603 * Saves the geometry src state at construction and restores in the destructor. It also saves
604 * and then restores the vertex attrib state.
605 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000606 class AutoGeometryPush : public ::SkNoncopyable {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000607 public:
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000608 AutoGeometryPush(GrDrawTarget* target)
609 : fAttribRestore(target->drawState()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000610 SkASSERT(NULL != target);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000611 fTarget = target;
612 target->pushGeometrySource();
613 }
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000614
615 ~AutoGeometryPush() { fTarget->popGeometrySource(); }
616
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000617 private:
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000618 GrDrawTarget* fTarget;
619 GrDrawState::AutoVertexAttribRestore fAttribRestore;
620 };
621
622 /**
623 * Combination of AutoGeometryPush and AutoStateRestore. The vertex attribs will be in default
624 * state regardless of ASRInit value.
625 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000626 class AutoGeometryAndStatePush : public ::SkNoncopyable {
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000627 public:
bsalomon@google.com137f1342013-05-29 21:27:53 +0000628 AutoGeometryAndStatePush(GrDrawTarget* target,
629 ASRInit init,
630 const SkMatrix* viewMatrix = NULL)
631 : fState(target, init, viewMatrix) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000632 SkASSERT(NULL != target);
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000633 fTarget = target;
634 target->pushGeometrySource();
635 if (kPreserve_ASRInit == init) {
636 target->drawState()->setDefaultVertexAttribs();
637 }
638 }
639
640 ~AutoGeometryAndStatePush() { fTarget->popGeometrySource(); }
641
642 private:
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000643 AutoStateRestore fState;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000644 GrDrawTarget* fTarget;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000645 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000646
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000647 ///////////////////////////////////////////////////////////////////////////
648 // Draw execution tracking (for font atlases and other resources)
649 class DrawToken {
650 public:
651 DrawToken(GrDrawTarget* drawTarget, uint32_t drawID) :
652 fDrawTarget(drawTarget), fDrawID(drawID) {}
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000653
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000654 bool isIssued() { return NULL != fDrawTarget && fDrawTarget->isIssued(fDrawID); }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000655
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000656 private:
657 GrDrawTarget* fDrawTarget;
658 uint32_t fDrawID; // this may wrap, but we're doing direct comparison
659 // so that should be okay
660 };
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000661
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000662 virtual DrawToken getCurrentDrawToken() { return DrawToken(this, 0); }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000663
reed@google.comac10a2d2010-12-22 21:39:39 +0000664protected:
bsalomon@google.com471d4712011-08-23 15:45:25 +0000665
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000666 enum GeometrySrcType {
667 kNone_GeometrySrcType, //<! src has not been specified
668 kReserved_GeometrySrcType, //<! src was set using reserve*Space
669 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
670 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
671 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000672
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000673 struct GeometrySrcState {
674 GeometrySrcType fVertexSrc;
675 union {
676 // valid if src type is buffer
677 const GrVertexBuffer* fVertexBuffer;
678 // valid if src type is reserved or array
679 int fVertexCount;
680 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000681
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000682 GeometrySrcType fIndexSrc;
683 union {
684 // valid if src type is buffer
685 const GrIndexBuffer* fIndexBuffer;
686 // valid if src type is reserved or array
687 int fIndexCount;
688 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000689
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000690 size_t fVertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000691 };
bsalomon@google.com934c5702012-03-20 21:17:58 +0000692
693 int indexCountInCurrentSource() const {
694 const GeometrySrcState& src = this->getGeomSrc();
695 switch (src.fIndexSrc) {
696 case kNone_GeometrySrcType:
697 return 0;
698 case kReserved_GeometrySrcType:
699 case kArray_GeometrySrcType:
700 return src.fIndexCount;
701 case kBuffer_GeometrySrcType:
robertphillips@google.comadacc702013-10-14 21:53:24 +0000702 return static_cast<int>(src.fIndexBuffer->sizeInBytes() / sizeof(uint16_t));
bsalomon@google.com934c5702012-03-20 21:17:58 +0000703 default:
704 GrCrash("Unexpected Index Source.");
705 return 0;
706 }
707 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000708
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000709 // This method is called by copySurface The srcRect is guaranteed to be entirely within the
710 // src bounds. Likewise, the dst rect implied by dstPoint and srcRect's width and height falls
711 // entirely within the dst. The default implementation will draw a rect from the src to the
712 // dst if the src is a texture and the dst is a render target and fail otherwise.
713 virtual bool onCopySurface(GrSurface* dst,
714 GrSurface* src,
715 const SkIRect& srcRect,
716 const SkIPoint& dstPoint);
717
718 // Called to determine whether an onCopySurface call would succeed or not. This is useful for
719 // proxy subclasses to test whether the copy would succeed without executing it yet. Derived
720 // classes must keep this consistent with their implementation of onCopySurface(). The inputs
721 // are the same as onCopySurface(), i.e. srcRect and dstPoint are clipped to be inside the src
722 // and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000723 virtual bool onCanCopySurface(GrSurface* dst,
724 GrSurface* src,
725 const SkIRect& srcRect,
726 const SkIPoint& dstPoint);
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000727
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000728 GrContext* getContext() { return fContext; }
729 const GrContext* getContext() const { return fContext; }
730
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000731 // A subclass may override this function if it wishes to be notified when the clip is changed.
732 // The override should call INHERITED::clipWillBeSet().
733 virtual void clipWillBeSet(const GrClipData* clipData);
734
735 // subclasses must call this in their destructors to ensure all vertex
736 // and index sources have been released (including those held by
737 // pushGeometrySource())
738 void releaseGeometry();
739
740 // accessors for derived classes
741 const GeometrySrcState& getGeomSrc() const { return fGeoSrcStateStack.back(); }
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000742 // it is preferable to call this rather than getGeomSrc()->fVertexSize because of the assert.
743 size_t getVertexSize() const {
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000744 // the vertex layout is only valid if a vertex source has been specified.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000745 SkASSERT(this->getGeomSrc().fVertexSrc != kNone_GeometrySrcType);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000746 return this->getGeomSrc().fVertexSize;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000747 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000748
bsalomon@google.combcce8922013-03-25 15:38:39 +0000749 // Subclass must initialize this in its constructor.
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000750 SkAutoTUnref<const GrDrawTargetCaps> fCaps;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000751
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000752 /**
753 * Used to communicate draws to subclass's onDraw function.
754 */
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000755 class DrawInfo {
756 public:
757 DrawInfo(const DrawInfo& di) { (*this) = di; }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000758 DrawInfo& operator =(const DrawInfo& di);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000759
760 GrPrimitiveType primitiveType() const { return fPrimitiveType; }
761 int startVertex() const { return fStartVertex; }
762 int startIndex() const { return fStartIndex; }
763 int vertexCount() const { return fVertexCount; }
764 int indexCount() const { return fIndexCount; }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000765 int verticesPerInstance() const { return fVerticesPerInstance; }
766 int indicesPerInstance() const { return fIndicesPerInstance; }
767 int instanceCount() const { return fInstanceCount; }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000768
769 bool isIndexed() const { return fIndexCount > 0; }
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000770#ifdef SK_DEBUG
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000771 bool isInstanced() const; // this version is longer because of asserts
772#else
773 bool isInstanced() const { return fInstanceCount > 0; }
774#endif
775
776 // adds or remove instances
777 void adjustInstanceCount(int instanceOffset);
778 // shifts the start vertex
779 void adjustStartVertex(int vertexOffset);
780 // shifts the start index
781 void adjustStartIndex(int indexOffset);
782
783 void setDevBounds(const SkRect& bounds) {
784 fDevBoundsStorage = bounds;
785 fDevBounds = &fDevBoundsStorage;
786 }
787 const SkRect* getDevBounds() const { return fDevBounds; }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000788
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000789 // NULL if no copy of the dst is needed for the draw.
790 const GrDeviceCoordTexture* getDstCopy() const {
791 if (NULL != fDstCopy.texture()) {
792 return &fDstCopy;
793 } else {
794 return NULL;
795 }
796 }
797
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000798 private:
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000799 DrawInfo() { fDevBounds = NULL; }
800
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000801 friend class GrDrawTarget;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000802
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000803 GrPrimitiveType fPrimitiveType;
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000804
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000805 int fStartVertex;
806 int fStartIndex;
807 int fVertexCount;
808 int fIndexCount;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000809
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000810 int fInstanceCount;
811 int fVerticesPerInstance;
812 int fIndicesPerInstance;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000813
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000814 SkRect fDevBoundsStorage;
815 SkRect* fDevBounds;
816
817 GrDeviceCoordTexture fDstCopy;
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000818 };
819
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000820private:
821 // A subclass can optionally overload this function to be notified before
822 // vertex and index space is reserved.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000823 virtual void willReserveVertexAndIndexSpace(int vertexCount, int indexCount) {}
bsalomon@google.com97805382012-03-13 14:32:07 +0000824
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000825 // implemented by subclass to allocate space for reserved geom
jvanverth@google.coma6338982013-01-31 21:34:25 +0000826 virtual bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices) = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000827 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
828 // implemented by subclass to handle release of reserved geom space
829 virtual void releaseReservedVertexSpace() = 0;
830 virtual void releaseReservedIndexSpace() = 0;
831 // subclass must consume array contents when set
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000832 virtual void onSetVertexSourceToArray(const void* vertexArray, int vertexCount) = 0;
833 virtual void onSetIndexSourceToArray(const void* indexArray, int indexCount) = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000834 // subclass is notified that geom source will be set away from an array
835 virtual void releaseVertexArray() = 0;
836 virtual void releaseIndexArray() = 0;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000837 // subclass overrides to be notified just before geo src state is pushed/popped.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000838 virtual void geometrySourceWillPush() = 0;
839 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
840 // subclass called to perform drawing
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000841 virtual void onDraw(const DrawInfo&) = 0;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000842 // Implementation of drawRect. The geometry src and vertex attribs will already
843 // be saved before this is called and restored afterwards. A subclass may override
844 // this to perform more optimal rect rendering. Its draws should be funneled through
845 // one of the public GrDrawTarget draw methods (e.g. drawNonIndexed,
846 // drawIndexedInstances, ...). The base class draws a two triangle fan using
847 // drawNonIndexed from reserved vertex space.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000848 virtual void onDrawRect(const SkRect& rect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000849 const SkMatrix* matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000850 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000851 const SkMatrix* localMatrix);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000852
853 virtual void onStencilPath(const GrPath*, SkPath::FillType) = 0;
854 virtual void onDrawPath(const GrPath*, SkPath::FillType,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000855 const GrDeviceCoordTexture* dstCopy) = 0;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000856
bsalomon@google.come3d70952012-03-13 12:40:53 +0000857 // helpers for reserving vertex and index space.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000858 bool reserveVertexSpace(size_t vertexSize,
bsalomon@google.come3d70952012-03-13 12:40:53 +0000859 int vertexCount,
860 void** vertices);
861 bool reserveIndexSpace(int indexCount, void** indices);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000862
bsalomon@google.come8262622011-11-07 02:30:51 +0000863 // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
864 // indicate non-indexed drawing.
865 bool checkDraw(GrPrimitiveType type, int startVertex,
866 int startIndex, int vertexCount,
867 int indexCount) const;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000868 // called when setting a new vert/idx source to unref prev vb/ib
869 void releasePreviousVertexSource();
870 void releasePreviousIndexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000871
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000872 // Makes a copy of the dst if it is necessary for the draw. Returns false if a copy is required
873 // but couldn't be made. Otherwise, returns true.
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000874 bool setupDstReadIfNecessary(DrawInfo* info) {
875 return this->setupDstReadIfNecessary(&info->fDstCopy, info->getDevBounds());
876 }
877 bool setupDstReadIfNecessary(GrDeviceCoordTexture* dstCopy, const SkRect* drawBounds);
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000878
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000879 // Check to see if this set of draw commands has been sent out
880 virtual bool isIssued(uint32_t drawID) { return true; }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000881
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000882 enum {
883 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +0000884 };
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000885 SkSTArray<kPreallocGeoSrcStateStackCnt, GeometrySrcState, true> fGeoSrcStateStack;
886 const GrClipData* fClip;
887 GrDrawState* fDrawState;
888 GrDrawState fDefaultDrawState;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000889 // The context owns us, not vice-versa, so this ptr is not ref'ed by DrawTarget.
890 GrContext* fContext;
reed@google.comfa35e3d2012-06-26 20:16:17 +0000891
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000892 typedef SkRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000893};
894
reed@google.comac10a2d2010-12-22 21:39:39 +0000895#endif