blob: 4545f74d256c6c7849d8e2f636295030c12ef508 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#ifndef GrDrawTarget_DEFINED
9#define GrDrawTarget_DEFINED
10
bsalomon@google.com8d67c072012-12-13 20:38:14 +000011#include "GrClipData.h"
joshualitt6db519c2014-10-29 08:48:18 -070012#include "GrClipMaskManager.h"
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +000013#include "GrContext.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000014#include "GrDrawState.h"
bsalomon@google.com934c5702012-03-20 21:17:58 +000015#include "GrIndexBuffer.h"
kkinnunenccdaa042014-08-20 01:36:23 -070016#include "GrPathRendering.h"
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +000017#include "GrTraceMarker.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018
bsalomon@google.com8d67c072012-12-13 20:38:14 +000019#include "SkClipStack.h"
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000020#include "SkMatrix.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000021#include "SkPath.h"
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +000022#include "SkStrokeRec.h"
robertphillips@google.coma2d71482012-08-01 20:08:47 +000023#include "SkTArray.h"
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000024#include "SkTLazy.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000025#include "SkTypes.h"
bsalomon@google.com8d67c072012-12-13 20:38:14 +000026#include "SkXfermode.h"
Scroggo97c88c22011-05-11 14:05:25 +000027
robertphillips@google.coma2d71482012-08-01 20:08:47 +000028class GrClipData;
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000029class GrDrawTargetCaps;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000030class GrPath;
cdaltonb85a0aa2014-07-21 15:32:44 -070031class GrPathRange;
reed@google.comac10a2d2010-12-22 21:39:39 +000032class GrVertexBuffer;
sugoi@google.com12b4e272012-12-06 20:13:11 +000033
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000034class GrDrawTarget : public SkRefCnt {
bsalomon@google.comf6601872012-08-28 21:11:35 +000035protected:
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000036 class DrawInfo;
37
bsalomon@google.comf6601872012-08-28 21:11:35 +000038public:
39 SK_DECLARE_INST_COUNT(GrDrawTarget)
40
kkinnunenccdaa042014-08-20 01:36:23 -070041 typedef GrPathRendering::PathTransformType PathTransformType ;
42
reed@google.comac10a2d2010-12-22 21:39:39 +000043 ///////////////////////////////////////////////////////////////////////////
44
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000045 // The context may not be fully constructed and should not be used during GrDrawTarget
46 // construction.
47 GrDrawTarget(GrContext* context);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000048 virtual ~GrDrawTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +000049
50 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000051 * Gets the capabilities of the draw target.
52 */
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000053 const GrDrawTargetCaps* caps() const { return fCaps.get(); }
bsalomon@google.com18c9c192011-09-22 21:01:31 +000054
55 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000056 * Sets the current clip to the region specified by clip. All draws will be
57 * clipped against this clip if kClip_StateBit is enabled.
58 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000059 * Setting the clip may (or may not) zero out the client's stencil bits.
60 *
reed@google.comac10a2d2010-12-22 21:39:39 +000061 * @param description of the clipping region
62 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +000063 void setClip(const GrClipData* clip);
reed@google.comac10a2d2010-12-22 21:39:39 +000064
65 /**
66 * Gets the current clip.
67 *
68 * @return the clip.
69 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +000070 const GrClipData* getClip() const;
reed@google.comac10a2d2010-12-22 21:39:39 +000071
bsalomon@google.coma5d056a2012-03-27 15:59:58 +000072 /**
73 * Sets the draw state object for the draw target. Note that this does not
74 * make a copy. The GrDrawTarget will take a reference to passed object.
75 * Passing NULL will cause the GrDrawTarget to use its own internal draw
76 * state object rather than an externally provided one.
77 */
78 void setDrawState(GrDrawState* drawState);
79
80 /**
81 * Read-only access to the GrDrawTarget's current draw state.
82 */
83 const GrDrawState& getDrawState() const { return *fDrawState; }
84
85 /**
86 * Read-write access to the GrDrawTarget's current draw state. Note that
87 * this doesn't ref.
88 */
89 GrDrawState* drawState() { return fDrawState; }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +000090
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +000091 /** When we're using coverage AA but the blend is incompatible (given gpu
92 * limitations) we should disable AA. */
bsalomon62c447d2014-08-08 08:08:50 -070093 bool shouldDisableCoverageAAForBlend() const {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +000094 // Enable below if we should draw with AA even when it produces
95 // incorrect blending.
96 // return false;
bsalomon62c447d2014-08-08 08:08:50 -070097 return !this->getDrawState().couldApplyCoverage(*this->caps());
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +000098 }
99
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000100 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000101 * There are three types of "sources" of geometry (vertices and indices) for
102 * draw calls made on the target. When performing an indexed draw, the
103 * indices and vertices can use different source types. Once a source is
bsalomon@google.com934c5702012-03-20 21:17:58 +0000104 * specified it can be used for multiple draws. However, the time at which
105 * the geometry data is no longer editable depends on the source type.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000106 *
107 * Sometimes it is necessary to perform a draw while upstack code has
bsalomon@google.come3d70952012-03-13 12:40:53 +0000108 * already specified geometry that it isn't finished with. So there are push
109 * and pop methods. This allows the client to push the sources, draw
110 * something using alternate sources, and then pop to restore the original
111 * sources.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000112 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000113 * Aside from pushes and pops, a source remains valid until another source
114 * is set or resetVertexSource / resetIndexSource is called. Drawing from
115 * a reset source is an error.
116 *
117 * The three types of sources are:
118 *
119 * 1. A cpu array (set*SourceToArray). This is useful when the caller
120 * already provided vertex data in a format compatible with a
121 * GrVertexLayout. The data in the array is consumed at the time that
122 * set*SourceToArray is called and subsequent edits to the array will not
123 * be reflected in draws.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000124 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000125 * 2. Reserve. This is most useful when the caller has data it must
126 * transform before drawing and is not long-lived. The caller requests
127 * that the draw target make room for some amount of vertex and/or index
128 * data. The target provides ptrs to hold the vertex and/or index data.
129 *
rmistry@google.comd6176b02012-08-23 18:14:13 +0000130 * The data is writable up until the next drawIndexed, drawNonIndexed,
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000131 * drawIndexedInstances, drawRect, copySurface, or pushGeometrySource. At
132 * this point the data is frozen and the ptrs are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000133 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000134 * Where the space is allocated and how it is uploaded to the GPU is
135 * subclass-dependent.
136 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000137 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.come3d70952012-03-13 12:40:53 +0000138 * is long-lived. When the data in the buffer is consumed depends on the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000139 * GrDrawTarget subclass. For deferred subclasses the caller has to
bsalomon@google.come3d70952012-03-13 12:40:53 +0000140 * guarantee that the data is still available in the buffers at playback.
141 * (TODO: Make this more automatic as we have done for read/write pixels)
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000142 *
143 * The size of each vertex is determined by querying the current GrDrawState.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000144 */
145
146 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000147 * Reserves space for vertices and/or indices. Zero can be specifed as
148 * either the vertex or index count if the caller desires to only reserve
rmistry@google.comd6176b02012-08-23 18:14:13 +0000149 * space for only indices or only vertices. If zero is specifed for
bsalomon@google.come3d70952012-03-13 12:40:53 +0000150 * vertexCount then the vertex source will be unmodified and likewise for
151 * indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000152 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000153 * If the function returns true then the reserve suceeded and the vertices
154 * and indices pointers will point to the space created.
reed@google.comac10a2d2010-12-22 21:39:39 +0000155 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000156 * If the target cannot make space for the request then this function will
157 * return false. If vertexCount was non-zero then upon failure the vertex
158 * source is reset and likewise for indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000159 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000160 * The pointers to the space allocated for vertices and indices remain valid
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000161 * until a drawIndexed, drawNonIndexed, drawIndexedInstances, drawRect,
162 * copySurface, or push/popGeomtrySource is called. At that point logically a
163 * snapshot of the data is made and the pointers are invalid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000164 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000165 * @param vertexCount the number of vertices to reserve space for. Can be
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000166 * 0. Vertex size is queried from the current GrDrawState.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000167 * @param indexCount the number of indices to reserve space for. Can be 0.
bsalomon@google.come3d70952012-03-13 12:40:53 +0000168 * @param vertices will point to reserved vertex space if vertexCount is
rmistry@google.comd6176b02012-08-23 18:14:13 +0000169 * non-zero. Illegal to pass NULL if vertexCount > 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000170 * @param indices will point to reserved index space if indexCount is
171 * non-zero. Illegal to pass NULL if indexCount > 0.
172 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000173 bool reserveVertexAndIndexSpace(int vertexCount,
bsalomon@google.com97805382012-03-13 14:32:07 +0000174 int indexCount,
175 void** vertices,
176 void** indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000177
reed@google.comac10a2d2010-12-22 21:39:39 +0000178 /**
179 * Provides hints to caller about the number of vertices and indices
180 * that can be allocated cheaply. This can be useful if caller is reserving
181 * space but doesn't know exactly how much geometry is needed.
182 *
183 * Also may hint whether the draw target should be flushed first. This is
184 * useful for deferred targets.
185 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000186 * @param vertexCount in: hint about how many vertices the caller would
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000187 * like to allocate. Vertex size is queried from the
188 * current GrDrawState.
reed@google.comac10a2d2010-12-22 21:39:39 +0000189 * out: a hint about the number of vertices that can be
190 * allocated cheaply. Negative means no hint.
191 * Ignored if NULL.
192 * @param indexCount in: hint about how many indices the caller would
193 * like to allocate.
194 * out: a hint about the number of indices that can be
195 * allocated cheaply. Negative means no hint.
196 * Ignored if NULL.
197 *
198 * @return true if target should be flushed based on the input values.
199 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000200 virtual bool geometryHints(int* vertexCount,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000201 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000202
203 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000204 * Sets source of vertex data for the next draw. Array must contain
205 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000206 *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000207 * @param vertexArray cpu array containing vertex data.
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000208 * @param vertexCount the number of vertices in the array. Vertex size is
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000209 * queried from the current GrDrawState.
reed@google.comac10a2d2010-12-22 21:39:39 +0000210 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000211 void setVertexSourceToArray(const void* vertexArray, int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000212
213 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000214 * Sets source of index data for the next indexed draw. Array must contain
215 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000216 *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000217 * @param indexArray cpu array containing index data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000218 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000219 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000220 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000221
222 /**
223 * Sets source of vertex data for the next draw. Data does not have to be
bsalomon@google.com934c5702012-03-20 21:17:58 +0000224 * in the buffer until drawIndexed, drawNonIndexed, or drawIndexedInstances.
reed@google.comac10a2d2010-12-22 21:39:39 +0000225 *
226 * @param buffer vertex buffer containing vertex data. Must be
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000227 * unlocked before draw call. Vertex size is queried
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000228 * from current GrDrawState.
reed@google.comac10a2d2010-12-22 21:39:39 +0000229 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000230 void setVertexSourceToBuffer(const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000231
232 /**
233 * Sets source of index data for the next indexed draw. Data does not have
bsalomon@google.com934c5702012-03-20 21:17:58 +0000234 * to be in the buffer until drawIndexed.
reed@google.comac10a2d2010-12-22 21:39:39 +0000235 *
236 * @param buffer index buffer containing indices. Must be unlocked
237 * before indexed draw call.
238 */
239 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000240
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000241 /**
242 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
243 * source to reserved, array, or buffer before next draw. May be able to free
244 * up temporary storage allocated by setVertexSourceToArray or
245 * reserveVertexSpace.
246 */
247 void resetVertexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000248
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000249 /**
250 * Resets index source. Indexed Drawing from reset indices is illegal. Set
251 * index source to reserved, array, or buffer before next indexed draw. May
252 * be able to free up temporary storage allocated by setIndexSourceToArray
253 * or reserveIndexSpace.
254 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000255 void resetIndexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000256
bsalomon@google.com97805382012-03-13 14:32:07 +0000257 /**
258 * Query to find out if the vertex or index source is reserved.
259 */
260 bool hasReservedVerticesOrIndices() const {
bsalomon@google.com73d98aa2012-03-13 14:41:19 +0000261 return kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc ||
bsalomon@google.com97805382012-03-13 14:32:07 +0000262 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
263 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000264
265 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000266 * Pushes and resets the vertex/index sources. Any reserved vertex / index
267 * data is finalized (i.e. cannot be updated after the matching pop but can
268 * be drawn from). Must be balanced by a pop.
269 */
270 void pushGeometrySource();
271
272 /**
273 * Pops the vertex / index sources from the matching push.
274 */
275 void popGeometrySource();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000276
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000277 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000278 * Draws indexed geometry using the current state and current vertex / index
279 * sources.
280 *
281 * @param type The type of primitives to draw.
282 * @param startVertex the vertex in the vertex array/buffer corresponding
283 * to index 0
284 * @param startIndex first index to read from index src.
285 * @param vertexCount one greater than the max index.
286 * @param indexCount the number of index elements to read. The index count
287 * is effectively trimmed to the last completely
288 * specified primitive.
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000289 * @param devBounds optional bounds hint. This is a promise from the caller,
290 * not a request for clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000291 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000292 void drawIndexed(GrPrimitiveType type,
293 int startVertex,
294 int startIndex,
295 int vertexCount,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000296 int indexCount,
297 const SkRect* devBounds = NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +0000298
299 /**
300 * Draws non-indexed geometry using the current state and current vertex
301 * sources.
302 *
303 * @param type The type of primitives to draw.
304 * @param startVertex the vertex in the vertex array/buffer corresponding
305 * to index 0
306 * @param vertexCount one greater than the max index.
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 drawNonIndexed(GrPrimitiveType type,
311 int startVertex,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000312 int vertexCount,
313 const SkRect* devBounds = NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +0000314
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000315 /**
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000316 * Draws path into the stencil buffer. The fill must be either even/odd or
317 * winding (not inverse or hairline). It will respect the HW antialias flag
joshualitt92e496f2014-10-31 13:56:50 -0700318 * on the draw state (if possible in the 3D API). Note, we will never have an inverse fill
319 * with stencil path
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000320 */
joshualitt92e496f2014-10-31 13:56:50 -0700321 void stencilPath(const GrPath*, GrPathRendering::FillType fill);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000322
323 /**
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000324 * Draws a path. Fill must not be a hairline. It will respect the HW
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000325 * antialias flag on the draw state (if possible in the 3D API).
326 */
joshualitt92e496f2014-10-31 13:56:50 -0700327 void drawPath(const GrPath*, GrPathRendering::FillType fill);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000328
329 /**
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000330 * Draws many paths. It will respect the HW
331 * antialias flag on the draw state (if possible in the 3D API).
332 *
cdaltonb85a0aa2014-07-21 15:32:44 -0700333 * @param pathRange Source of paths to draw from
334 * @param indices Array of indices into the the pathRange
335 * @param count Number of paths to draw (length of indices array)
336 * @param transforms Array of individual transforms, one for each path
337 * @param transformsType Type of transformations in the array. Array contains
bsalomondb1f1562014-08-12 06:58:16 -0700338 PathTransformSize(transformsType) * count elements
cdaltonb85a0aa2014-07-21 15:32:44 -0700339 * @param fill Fill type for drawing all the paths
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000340 */
cdaltonb85a0aa2014-07-21 15:32:44 -0700341 void drawPaths(const GrPathRange* pathRange,
342 const uint32_t indices[], int count,
343 const float transforms[], PathTransformType transformsType,
joshualitt92e496f2014-10-31 13:56:50 -0700344 GrPathRendering::FillType fill);
cdaltonb85a0aa2014-07-21 15:32:44 -0700345
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000346 /**
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
bsalomon@google.comc7818882013-03-20 19:19:53 +0000351 * @param localRect optional rect that specifies local coords to map onto
352 * rect. If NULL then rect serves as the local coords.
353 * @param localMatrix optional matrix applied to localRect. If
skia.committer@gmail.com044679e2013-02-15 07:16:57 +0000354 * srcRect is non-NULL and srcMatrix is non-NULL
355 * then srcRect will be transformed by srcMatrix.
jvanverth@google.com39768252013-02-14 15:25:44 +0000356 * srcMatrix can be NULL when no srcMatrix is desired.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000357 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000358 void drawRect(const SkRect& rect,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000359 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000360 const SkMatrix* localMatrix) {
361 AutoGeometryPush agp(this);
bsalomon01c8da12014-08-04 09:21:30 -0700362 this->onDrawRect(rect, localRect, localMatrix);
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000363 }
jvanverth@google.com39768252013-02-14 15:25:44 +0000364
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000365 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000366 * Helper for drawRect when the caller doesn't need separate local rects or matrices.
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000367 */
bsalomon01c8da12014-08-04 09:21:30 -0700368 void drawSimpleRect(const SkRect& rect) {
369 this->drawRect(rect, NULL, NULL);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000370 }
bsalomon01c8da12014-08-04 09:21:30 -0700371 void drawSimpleRect(const SkIRect& irect) {
reed@google.com44699382013-10-31 17:28:30 +0000372 SkRect rect = SkRect::Make(irect);
bsalomon01c8da12014-08-04 09:21:30 -0700373 this->drawRect(rect, NULL, NULL);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000374 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000375
376 /**
bsalomon@google.com934c5702012-03-20 21:17:58 +0000377 * This call is used to draw multiple instances of some geometry with a
378 * given number of vertices (V) and indices (I) per-instance. The indices in
379 * the index source must have the form i[k+I] == i[k] + V. Also, all indices
380 * i[kI] ... i[(k+1)I-1] must be elements of the range kV ... (k+1)V-1. As a
381 * concrete example, the following index buffer for drawing a series of
382 * quads each as two triangles each satisfies these conditions with V=4 and
383 * I=6:
384 * (0,1,2,0,2,3, 4,5,6,4,6,7, 8,9,10,8,10,11, ...)
385 *
386 * The call assumes that the pattern of indices fills the entire index
387 * source. The size of the index buffer limits the number of instances that
388 * can be drawn by the GPU in a single draw. However, the caller may specify
389 * any (positive) number for instanceCount and if necessary multiple GPU
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000390 * draws will be issued. Moreover, when drawIndexedInstances is called
bsalomon@google.com934c5702012-03-20 21:17:58 +0000391 * multiple times it may be possible for GrDrawTarget to group them into a
392 * single GPU draw.
393 *
394 * @param type the type of primitives to draw
395 * @param instanceCount the number of instances to draw. Each instance
396 * consists of verticesPerInstance vertices indexed by
397 * indicesPerInstance indices drawn as the primitive
398 * type specified by type.
399 * @param verticesPerInstance The number of vertices in each instance (V
400 * in the above description).
401 * @param indicesPerInstance The number of indices in each instance (I
402 * in the above description).
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000403 * @param devBounds optional bounds hint. This is a promise from the caller,
404 * not a request for clipping.
bsalomon@google.com934c5702012-03-20 21:17:58 +0000405 */
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000406 void drawIndexedInstances(GrPrimitiveType type,
407 int instanceCount,
408 int verticesPerInstance,
409 int indicesPerInstance,
410 const SkRect* devBounds = NULL);
bsalomon@google.com934c5702012-03-20 21:17:58 +0000411
412 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000413 * Clear the current render target if one isn't passed in. Ignores the
414 * clip and all other draw state (blend mode, stages, etc). Clears the
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000415 * whole thing if rect is NULL, otherwise just the rect. If canIgnoreRect
416 * is set then the entire render target can be optionally cleared.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000417 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000418 virtual void clear(const SkIRect* rect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000419 GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000420 bool canIgnoreRect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000421 GrRenderTarget* renderTarget = NULL) = 0;
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000422
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000423 /**
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000424 * Discards the contents render target. NULL indicates that the current render target should
425 * be discarded.
426 **/
427 virtual void discard(GrRenderTarget* = NULL) = 0;
428
429 /**
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000430 * Called at start and end of gpu trace marking
431 * GR_CREATE_GPU_TRACE_MARKER(marker_str, target) will automatically call these at the start
432 * and end of a code block respectively
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000433 */
egdaniel3eee3832014-06-18 13:09:11 -0700434 void addGpuTraceMarker(const GrGpuTraceMarker* marker);
435 void removeGpuTraceMarker(const GrGpuTraceMarker* marker);
436
437 /**
438 * Takes the current active set of markers and stores them for later use. Any current marker
439 * in the active set is removed from the active set and the targets remove function is called.
440 * These functions do not work as a stack so you cannot call save a second time before calling
441 * restore. Also, it is assumed that when restore is called the current active set of markers
442 * is empty. When the stored markers are added back into the active set, the targets add marker
443 * is called.
444 */
445 void saveActiveTraceMarkers();
446 void restoreActiveTraceMarkers();
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000447
448 /**
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000449 * Copies a pixel rectangle from one surface to another. This call may finalize
450 * reserved vertex/index data (as though a draw call was made). The src pixels
451 * copied are specified by srcRect. They are copied to a rect of the same
452 * size in dst with top left at dstPoint. If the src rect is clipped by the
453 * src bounds then pixel values in the dst rect corresponding to area clipped
454 * by the src rect are not overwritten. This method can fail and return false
455 * depending on the type of surface, configs, etc, and the backend-specific
456 * limitations. If rect is clipped out entirely by the src or dst bounds then
457 * true is returned since there is no actual copy necessary to succeed.
458 */
459 bool copySurface(GrSurface* dst,
460 GrSurface* src,
461 const SkIRect& srcRect,
462 const SkIPoint& dstPoint);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000463 /**
464 * Function that determines whether a copySurface call would succeed without
465 * performing the copy.
466 */
467 bool canCopySurface(GrSurface* dst,
468 GrSurface* src,
469 const SkIRect& srcRect,
470 const SkIPoint& dstPoint);
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000471
robertphillips@google.comff175842012-05-14 19:31:39 +0000472 /**
bsalomon@google.comeb851172013-04-15 13:51:00 +0000473 * This is can be called before allocating a texture to be a dst for copySurface. It will
474 * populate the origin, config, and flags fields of the desc such that copySurface is more
475 * likely to succeed and be efficient.
476 */
bsalomonf2703d82014-10-28 14:33:06 -0700477 virtual void initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc);
bsalomon@google.comeb851172013-04-15 13:51:00 +0000478
479
480 /**
robertphillips@google.comff175842012-05-14 19:31:39 +0000481 * Release any resources that are cached but not currently in use. This
482 * is intended to give an application some recourse when resources are low.
483 */
484 virtual void purgeResources() {};
485
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000486 /**
487 * For subclass internal use to invoke a call to onDraw(). See DrawInfo below.
488 */
489 void executeDraw(const DrawInfo& info) { this->onDraw(info); }
490
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000491 /**
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000492 * For subclass internal use to invoke a call to onDrawPath().
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000493 */
joshualitt92e496f2014-10-31 13:56:50 -0700494 void executeDrawPath(const GrPath* path, GrPathRendering::FillType fill,
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000495 const GrDeviceCoordTexture* dstCopy) {
496 this->onDrawPath(path, fill, dstCopy);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000497 }
498
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000499 /**
500 * For subclass internal use to invoke a call to onDrawPaths().
501 */
cdaltonb85a0aa2014-07-21 15:32:44 -0700502 void executeDrawPaths(const GrPathRange* pathRange,
503 const uint32_t indices[], int count,
504 const float transforms[], PathTransformType transformsType,
joshualitt92e496f2014-10-31 13:56:50 -0700505 GrPathRendering::FillType fill,
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000506 const GrDeviceCoordTexture* dstCopy) {
cdaltonb85a0aa2014-07-21 15:32:44 -0700507 this->onDrawPaths(pathRange, indices, count, transforms, transformsType, fill, dstCopy);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000508 }
joshualitt329bf482014-10-29 12:31:28 -0700509
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000510 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000511
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000512 /**
513 * See AutoStateRestore below.
514 */
515 enum ASRInit {
516 kPreserve_ASRInit,
517 kReset_ASRInit
518 };
519
520 /**
521 * Saves off the current state and restores it in the destructor. It will
522 * install a new GrDrawState object on the target (setDrawState) and restore
523 * the previous one in the destructor. The caller should call drawState() to
524 * get the new draw state after the ASR is installed.
525 *
526 * GrDrawState* state = target->drawState();
527 * AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit).
528 * state->setRenderTarget(rt); // state refers to the GrDrawState set on
529 * // target before asr was initialized.
530 * // Therefore, rt is set on the GrDrawState
531 * // that will be restored after asr's
532 * // destructor rather than target's current
rmistry@google.comd6176b02012-08-23 18:14:13 +0000533 * // GrDrawState.
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000534 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000535 class AutoStateRestore : public ::SkNoncopyable {
reed@google.comac10a2d2010-12-22 21:39:39 +0000536 public:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000537 /**
538 * Default ASR will have no effect unless set() is subsequently called.
539 */
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000540 AutoStateRestore();
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000541
542 /**
543 * Saves the state on target. The state will be restored when the ASR
544 * is destroyed. If this constructor is used do not call set().
545 *
546 * @param init Should the newly installed GrDrawState be a copy of the
547 * previous state or a default-initialized GrDrawState.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000548 * @param viewMatrix Optional view matrix. If init = kPreserve then the draw state's
549 * matrix will be preconcat'ed with the param. All stages will be
550 updated to compensate for the matrix change. If init == kReset
551 then the draw state's matrix will be this matrix.
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000552 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000553 AutoStateRestore(GrDrawTarget* target, ASRInit init, const SkMatrix* viewMatrix = NULL);
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000554
reed@google.comac10a2d2010-12-22 21:39:39 +0000555 ~AutoStateRestore();
556
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000557 /**
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000558 * Saves the state on target. The state will be restored when the ASR
559 * is destroyed. This should only be called once per ASR object and only
560 * when the default constructor was used. For nested saves use multiple
561 * ASR objects.
562 *
563 * @param init Should the newly installed GrDrawState be a copy of the
564 * previous state or a default-initialized GrDrawState.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000565 * @param viewMatrix Optional view matrix. If init = kPreserve then the draw state's
566 * matrix will be preconcat'ed with the param. All stages will be
567 updated to compensate for the matrix change. If init == kReset
568 then the draw state's matrix will be this matrix.
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000569 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000570 void set(GrDrawTarget* target, ASRInit init, const SkMatrix* viewMatrix = NULL);
571
572 /**
573 * Like set() but makes the view matrix identity. When init is kReset it is as though
574 * NULL was passed to set's viewMatrix param. When init is kPreserve it is as though
575 * the inverse view matrix was passed. If kPreserve is passed and the draw state's matrix
576 * is not invertible then this may fail.
577 */
578 bool setIdentity(GrDrawTarget* target, ASRInit init);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000579
reed@google.comac10a2d2010-12-22 21:39:39 +0000580 private:
bsalomon@google.com137f1342013-05-29 21:27:53 +0000581 GrDrawTarget* fDrawTarget;
582 SkTLazy<GrDrawState> fTempState;
583 GrDrawState* fSavedState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000584 };
585
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000586 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000587
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000588 class AutoReleaseGeometry : public ::SkNoncopyable {
reed@google.comac10a2d2010-12-22 21:39:39 +0000589 public:
590 AutoReleaseGeometry(GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000591 int vertexCount,
592 int indexCount);
593 AutoReleaseGeometry();
594 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000595 bool set(GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000596 int vertexCount,
597 int indexCount);
bsalomon49f085d2014-09-05 13:34:00 -0700598 bool succeeded() const { return SkToBool(fTarget); }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000599 void* vertices() const { SkASSERT(this->succeeded()); return fVertices; }
600 void* indices() const { SkASSERT(this->succeeded()); return fIndices; }
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000601 SkPoint* positions() const {
602 return static_cast<SkPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +0000603 }
604
605 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000606 void reset();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000607
reed@google.comac10a2d2010-12-22 21:39:39 +0000608 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000609 void* fVertices;
610 void* fIndices;
611 };
612
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000613 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000614
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000615 class AutoClipRestore : public ::SkNoncopyable {
reed@google.comac10a2d2010-12-22 21:39:39 +0000616 public:
617 AutoClipRestore(GrDrawTarget* target) {
618 fTarget = target;
619 fClip = fTarget->getClip();
620 }
621
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000622 AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip);
623
reed@google.comac10a2d2010-12-22 21:39:39 +0000624 ~AutoClipRestore() {
625 fTarget->setClip(fClip);
626 }
627 private:
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000628 GrDrawTarget* fTarget;
629 const GrClipData* fClip;
630 SkTLazy<SkClipStack> fStack;
631 GrClipData fReplacementClip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000632 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000633
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000634 ////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000635
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000636 /**
637 * Saves the geometry src state at construction and restores in the destructor. It also saves
638 * and then restores the vertex attrib state.
639 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000640 class AutoGeometryPush : public ::SkNoncopyable {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000641 public:
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000642 AutoGeometryPush(GrDrawTarget* target)
643 : fAttribRestore(target->drawState()) {
bsalomon49f085d2014-09-05 13:34:00 -0700644 SkASSERT(target);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000645 fTarget = target;
646 target->pushGeometrySource();
647 }
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000648
649 ~AutoGeometryPush() { fTarget->popGeometrySource(); }
650
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000651 private:
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000652 GrDrawTarget* fTarget;
653 GrDrawState::AutoVertexAttribRestore fAttribRestore;
654 };
655
656 /**
657 * Combination of AutoGeometryPush and AutoStateRestore. The vertex attribs will be in default
658 * state regardless of ASRInit value.
659 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000660 class AutoGeometryAndStatePush : public ::SkNoncopyable {
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000661 public:
bsalomon@google.com137f1342013-05-29 21:27:53 +0000662 AutoGeometryAndStatePush(GrDrawTarget* target,
663 ASRInit init,
664 const SkMatrix* viewMatrix = NULL)
665 : fState(target, init, viewMatrix) {
bsalomon49f085d2014-09-05 13:34:00 -0700666 SkASSERT(target);
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000667 fTarget = target;
668 target->pushGeometrySource();
669 if (kPreserve_ASRInit == init) {
670 target->drawState()->setDefaultVertexAttribs();
671 }
672 }
673
674 ~AutoGeometryAndStatePush() { fTarget->popGeometrySource(); }
675
676 private:
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000677 AutoStateRestore fState;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000678 GrDrawTarget* fTarget;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000679 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000680
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000681 ///////////////////////////////////////////////////////////////////////////
682 // Draw execution tracking (for font atlases and other resources)
683 class DrawToken {
684 public:
685 DrawToken(GrDrawTarget* drawTarget, uint32_t drawID) :
686 fDrawTarget(drawTarget), fDrawID(drawID) {}
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000687
bsalomon49f085d2014-09-05 13:34:00 -0700688 bool isIssued() { return fDrawTarget && fDrawTarget->isIssued(fDrawID); }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000689
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000690 private:
691 GrDrawTarget* fDrawTarget;
692 uint32_t fDrawID; // this may wrap, but we're doing direct comparison
693 // so that should be okay
694 };
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000695
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000696 virtual DrawToken getCurrentDrawToken() { return DrawToken(this, 0); }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000697
reed@google.comac10a2d2010-12-22 21:39:39 +0000698protected:
egdaniel89af44a2014-09-26 06:15:04 -0700699 // Extend access to GrDrawState::convertToPEndeingExec to subclasses.
700 void convertDrawStateToPendingExec(GrDrawState* ds) {
bsalomon2a9ca782014-09-05 14:27:43 -0700701 ds->convertToPendingExec();
702 }
bsalomon@google.com471d4712011-08-23 15:45:25 +0000703
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000704 enum GeometrySrcType {
705 kNone_GeometrySrcType, //<! src has not been specified
706 kReserved_GeometrySrcType, //<! src was set using reserve*Space
707 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
708 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
709 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000710
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000711 struct GeometrySrcState {
712 GeometrySrcType fVertexSrc;
713 union {
714 // valid if src type is buffer
715 const GrVertexBuffer* fVertexBuffer;
716 // valid if src type is reserved or array
717 int fVertexCount;
718 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000719
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000720 GeometrySrcType fIndexSrc;
721 union {
722 // valid if src type is buffer
723 const GrIndexBuffer* fIndexBuffer;
724 // valid if src type is reserved or array
725 int fIndexCount;
726 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000727
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000728 size_t fVertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000729 };
bsalomon@google.com934c5702012-03-20 21:17:58 +0000730
731 int indexCountInCurrentSource() const {
732 const GeometrySrcState& src = this->getGeomSrc();
733 switch (src.fIndexSrc) {
734 case kNone_GeometrySrcType:
735 return 0;
736 case kReserved_GeometrySrcType:
737 case kArray_GeometrySrcType:
738 return src.fIndexCount;
739 case kBuffer_GeometrySrcType:
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000740 return static_cast<int>(src.fIndexBuffer->gpuMemorySize() / sizeof(uint16_t));
bsalomon@google.com934c5702012-03-20 21:17:58 +0000741 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000742 SkFAIL("Unexpected Index Source.");
bsalomon@google.com934c5702012-03-20 21:17:58 +0000743 return 0;
744 }
745 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000746
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000747 // This method is called by copySurface The srcRect is guaranteed to be entirely within the
748 // src bounds. Likewise, the dst rect implied by dstPoint and srcRect's width and height falls
749 // entirely within the dst. The default implementation will draw a rect from the src to the
750 // dst if the src is a texture and the dst is a render target and fail otherwise.
751 virtual bool onCopySurface(GrSurface* dst,
752 GrSurface* src,
753 const SkIRect& srcRect,
754 const SkIPoint& dstPoint);
755
756 // Called to determine whether an onCopySurface call would succeed or not. This is useful for
757 // proxy subclasses to test whether the copy would succeed without executing it yet. Derived
758 // classes must keep this consistent with their implementation of onCopySurface(). The inputs
759 // are the same as onCopySurface(), i.e. srcRect and dstPoint are clipped to be inside the src
760 // and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000761 virtual bool onCanCopySurface(GrSurface* dst,
762 GrSurface* src,
763 const SkIRect& srcRect,
764 const SkIPoint& dstPoint);
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000765
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000766 GrContext* getContext() { return fContext; }
767 const GrContext* getContext() const { return fContext; }
768
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000769 // A subclass may override this function if it wishes to be notified when the clip is changed.
770 // The override should call INHERITED::clipWillBeSet().
771 virtual void clipWillBeSet(const GrClipData* clipData);
772
773 // subclasses must call this in their destructors to ensure all vertex
774 // and index sources have been released (including those held by
775 // pushGeometrySource())
776 void releaseGeometry();
777
778 // accessors for derived classes
779 const GeometrySrcState& getGeomSrc() const { return fGeoSrcStateStack.back(); }
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000780 // it is preferable to call this rather than getGeomSrc()->fVertexSize because of the assert.
781 size_t getVertexSize() const {
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000782 // the vertex layout is only valid if a vertex source has been specified.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000783 SkASSERT(this->getGeomSrc().fVertexSrc != kNone_GeometrySrcType);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000784 return this->getGeomSrc().fVertexSize;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000785 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000786
bsalomon@google.combcce8922013-03-25 15:38:39 +0000787 // Subclass must initialize this in its constructor.
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000788 SkAutoTUnref<const GrDrawTargetCaps> fCaps;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000789
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000790 const GrTraceMarkerSet& getActiveTraceMarkers() { return fActiveTraceMarkers; }
791
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000792 /**
793 * Used to communicate draws to subclass's onDraw function.
794 */
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000795 class DrawInfo {
796 public:
797 DrawInfo(const DrawInfo& di) { (*this) = di; }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000798 DrawInfo& operator =(const DrawInfo& di);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000799
800 GrPrimitiveType primitiveType() const { return fPrimitiveType; }
801 int startVertex() const { return fStartVertex; }
802 int startIndex() const { return fStartIndex; }
803 int vertexCount() const { return fVertexCount; }
804 int indexCount() const { return fIndexCount; }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000805 int verticesPerInstance() const { return fVerticesPerInstance; }
806 int indicesPerInstance() const { return fIndicesPerInstance; }
807 int instanceCount() const { return fInstanceCount; }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000808
809 bool isIndexed() const { return fIndexCount > 0; }
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000810#ifdef SK_DEBUG
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000811 bool isInstanced() const; // this version is longer because of asserts
812#else
813 bool isInstanced() const { return fInstanceCount > 0; }
814#endif
815
816 // adds or remove instances
817 void adjustInstanceCount(int instanceOffset);
818 // shifts the start vertex
819 void adjustStartVertex(int vertexOffset);
820 // shifts the start index
821 void adjustStartIndex(int indexOffset);
822
823 void setDevBounds(const SkRect& bounds) {
824 fDevBoundsStorage = bounds;
825 fDevBounds = &fDevBoundsStorage;
826 }
827 const SkRect* getDevBounds() const { return fDevBounds; }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000828
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000829 // NULL if no copy of the dst is needed for the draw.
830 const GrDeviceCoordTexture* getDstCopy() const {
bsalomon49f085d2014-09-05 13:34:00 -0700831 if (fDstCopy.texture()) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000832 return &fDstCopy;
833 } else {
834 return NULL;
835 }
836 }
837
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000838 private:
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000839 DrawInfo() { fDevBounds = NULL; }
840
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000841 friend class GrDrawTarget;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000842
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000843 GrPrimitiveType fPrimitiveType;
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000844
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000845 int fStartVertex;
846 int fStartIndex;
847 int fVertexCount;
848 int fIndexCount;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000849
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000850 int fInstanceCount;
851 int fVerticesPerInstance;
852 int fIndicesPerInstance;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000853
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000854 SkRect fDevBoundsStorage;
855 SkRect* fDevBounds;
856
857 GrDeviceCoordTexture fDstCopy;
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000858 };
859
joshualitt65171342014-10-09 07:25:36 -0700860 // Makes a copy of the dst if it is necessary for the draw. Returns false if a copy is required
861 // but couldn't be made. Otherwise, returns true. This method needs to be protected because it
862 // needs to be accessed by GLPrograms to setup a correct drawstate
863 bool setupDstReadIfNecessary(DrawInfo* info) {
864 return this->setupDstReadIfNecessary(&info->fDstCopy, info->getDevBounds());
865 }
866 bool setupDstReadIfNecessary(GrDeviceCoordTexture* dstCopy, const SkRect* drawBounds);
867
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000868private:
869 // A subclass can optionally overload this function to be notified before
870 // vertex and index space is reserved.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000871 virtual void willReserveVertexAndIndexSpace(int vertexCount, int indexCount) {}
bsalomon@google.com97805382012-03-13 14:32:07 +0000872
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000873 // implemented by subclass to allocate space for reserved geom
jvanverth@google.coma6338982013-01-31 21:34:25 +0000874 virtual bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices) = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000875 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
876 // implemented by subclass to handle release of reserved geom space
877 virtual void releaseReservedVertexSpace() = 0;
878 virtual void releaseReservedIndexSpace() = 0;
879 // subclass must consume array contents when set
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000880 virtual void onSetVertexSourceToArray(const void* vertexArray, int vertexCount) = 0;
881 virtual void onSetIndexSourceToArray(const void* indexArray, int indexCount) = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000882 // subclass is notified that geom source will be set away from an array
883 virtual void releaseVertexArray() = 0;
884 virtual void releaseIndexArray() = 0;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000885 // subclass overrides to be notified just before geo src state is pushed/popped.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000886 virtual void geometrySourceWillPush() = 0;
887 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
888 // subclass called to perform drawing
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000889 virtual void onDraw(const DrawInfo&) = 0;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000890 // Implementation of drawRect. The geometry src and vertex attribs will already
891 // be saved before this is called and restored afterwards. A subclass may override
892 // this to perform more optimal rect rendering. Its draws should be funneled through
893 // one of the public GrDrawTarget draw methods (e.g. drawNonIndexed,
894 // drawIndexedInstances, ...). The base class draws a two triangle fan using
895 // drawNonIndexed from reserved vertex space.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000896 virtual void onDrawRect(const SkRect& rect,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000897 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000898 const SkMatrix* localMatrix);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000899
joshualitt92e496f2014-10-31 13:56:50 -0700900 virtual void onStencilPath(const GrPath*, GrPathRendering::FillType) = 0;
901 virtual void onDrawPath(const GrPath*, GrPathRendering::FillType,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000902 const GrDeviceCoordTexture* dstCopy) = 0;
cdaltonb85a0aa2014-07-21 15:32:44 -0700903 virtual void onDrawPaths(const GrPathRange*,
904 const uint32_t indices[], int count,
905 const float transforms[], PathTransformType,
joshualitt92e496f2014-10-31 13:56:50 -0700906 GrPathRendering::FillType, const GrDeviceCoordTexture*) = 0;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000907
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000908 virtual void didAddGpuTraceMarker() = 0;
909 virtual void didRemoveGpuTraceMarker() = 0;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000910
bsalomon@google.come3d70952012-03-13 12:40:53 +0000911 // helpers for reserving vertex and index space.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000912 bool reserveVertexSpace(size_t vertexSize,
bsalomon@google.come3d70952012-03-13 12:40:53 +0000913 int vertexCount,
914 void** vertices);
915 bool reserveIndexSpace(int indexCount, void** indices);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000916
bsalomon@google.come8262622011-11-07 02:30:51 +0000917 // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
918 // indicate non-indexed drawing.
919 bool checkDraw(GrPrimitiveType type, int startVertex,
920 int startIndex, int vertexCount,
921 int indexCount) const;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000922 // called when setting a new vert/idx source to unref prev vb/ib
923 void releasePreviousVertexSource();
924 void releasePreviousIndexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000925
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000926 // Check to see if this set of draw commands has been sent out
927 virtual bool isIssued(uint32_t drawID) { return true; }
joshualitt329bf482014-10-29 12:31:28 -0700928 virtual GrClipMaskManager* getClipMaskManager() = 0;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000929
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000930 enum {
931 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +0000932 };
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000933 SkSTArray<kPreallocGeoSrcStateStackCnt, GeometrySrcState, true> fGeoSrcStateStack;
934 const GrClipData* fClip;
935 GrDrawState* fDrawState;
936 GrDrawState fDefaultDrawState;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000937 // The context owns us, not vice-versa, so this ptr is not ref'ed by DrawTarget.
938 GrContext* fContext;
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000939 // To keep track that we always have at least as many debug marker adds as removes
940 int fGpuTraceMarkerCount;
941 GrTraceMarkerSet fActiveTraceMarkers;
egdaniel3eee3832014-06-18 13:09:11 -0700942 GrTraceMarkerSet fStoredTraceMarkers;
reed@google.comfa35e3d2012-06-26 20:16:17 +0000943
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000944 typedef SkRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000945};
946
joshualitt329bf482014-10-29 12:31:28 -0700947/*
948 * This class is JUST for clip mask manager. Everyone else should just use draw target above.
949 */
joshualitt6db519c2014-10-29 08:48:18 -0700950class GrClipTarget : public GrDrawTarget {
951public:
joshualitt329bf482014-10-29 12:31:28 -0700952 GrClipTarget(GrContext* context) : INHERITED(context) {
953 fClipMaskManager.setClipTarget(this);
954 }
955
956 /* Clip mask manager needs access to the context.
957 * TODO we only need a very small subset of context in the CMM.
958 */
959 GrContext* getContext() { return INHERITED::getContext(); }
960 const GrContext* getContext() const { return INHERITED::getContext(); }
961
joshualitt6db519c2014-10-29 08:48:18 -0700962 /**
963 * Clip Mask Manager(and no one else) needs to clear private stencil bits.
964 * ClipTarget subclass sets clip bit in the stencil buffer. The subclass
965 * is free to clear the remaining bits to zero if masked clears are more
966 * expensive than clearing all bits.
967 */
968 virtual void clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* = NULL) = 0;
969
joshualitt3bdd7dc2014-10-31 08:27:39 -0700970 /**
971 * Release any resources that are cached but not currently in use. This
972 * is intended to give an application some recourse when resources are low.
973 */
974 virtual void purgeResources() SK_OVERRIDE {
975 // The clip mask manager can rebuild all its clip masks so just
976 // get rid of them all.
977 fClipMaskManager.purgeResources();
978 };
979
joshualitt329bf482014-10-29 12:31:28 -0700980protected:
981 GrClipMaskManager fClipMaskManager;
982
joshualitt6db519c2014-10-29 08:48:18 -0700983private:
joshualitt329bf482014-10-29 12:31:28 -0700984 GrClipMaskManager* getClipMaskManager() { return &fClipMaskManager; }
985
joshualitt6db519c2014-10-29 08:48:18 -0700986 typedef GrDrawTarget INHERITED;
987};
988
reed@google.comac10a2d2010-12-22 21:39:39 +0000989#endif