blob: 3efa6bf3577a564b7bc383411e6d09a31caea074 [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
318 * on the draw state (if possible in the 3D API).
319 */
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000320 void stencilPath(const GrPath*, SkPath::FillType fill);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000321
322 /**
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000323 * Draws a path. Fill must not be a hairline. It will respect the HW
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000324 * antialias flag on the draw state (if possible in the 3D API).
325 */
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000326 void drawPath(const GrPath*, SkPath::FillType fill);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000327
328 /**
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000329 * Draws many paths. It will respect the HW
330 * antialias flag on the draw state (if possible in the 3D API).
331 *
cdaltonb85a0aa2014-07-21 15:32:44 -0700332 * @param pathRange Source of paths to draw from
333 * @param indices Array of indices into the the pathRange
334 * @param count Number of paths to draw (length of indices array)
335 * @param transforms Array of individual transforms, one for each path
336 * @param transformsType Type of transformations in the array. Array contains
bsalomondb1f1562014-08-12 06:58:16 -0700337 PathTransformSize(transformsType) * count elements
cdaltonb85a0aa2014-07-21 15:32:44 -0700338 * @param fill Fill type for drawing all the paths
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000339 */
cdaltonb85a0aa2014-07-21 15:32:44 -0700340 void drawPaths(const GrPathRange* pathRange,
341 const uint32_t indices[], int count,
342 const float transforms[], PathTransformType transformsType,
343 SkPath::FillType fill);
344
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000345 /**
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000346 * Helper function for drawing rects. It performs a geometry src push and pop
347 * and thus will finalize any reserved geometry.
skia.committer@gmail.com044679e2013-02-15 07:16:57 +0000348 *
bsalomon@google.comc7818882013-03-20 19:19:53 +0000349 * @param rect the rect to draw
bsalomon@google.comc7818882013-03-20 19:19:53 +0000350 * @param localRect optional rect that specifies local coords to map onto
351 * rect. If NULL then rect serves as the local coords.
352 * @param localMatrix optional matrix applied to localRect. If
skia.committer@gmail.com044679e2013-02-15 07:16:57 +0000353 * srcRect is non-NULL and srcMatrix is non-NULL
354 * then srcRect will be transformed by srcMatrix.
jvanverth@google.com39768252013-02-14 15:25:44 +0000355 * srcMatrix can be NULL when no srcMatrix is desired.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000356 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000357 void drawRect(const SkRect& rect,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000358 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000359 const SkMatrix* localMatrix) {
360 AutoGeometryPush agp(this);
bsalomon01c8da12014-08-04 09:21:30 -0700361 this->onDrawRect(rect, localRect, localMatrix);
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000362 }
jvanverth@google.com39768252013-02-14 15:25:44 +0000363
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000364 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000365 * Helper for drawRect when the caller doesn't need separate local rects or matrices.
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000366 */
bsalomon01c8da12014-08-04 09:21:30 -0700367 void drawSimpleRect(const SkRect& rect) {
368 this->drawRect(rect, NULL, NULL);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000369 }
bsalomon01c8da12014-08-04 09:21:30 -0700370 void drawSimpleRect(const SkIRect& irect) {
reed@google.com44699382013-10-31 17:28:30 +0000371 SkRect rect = SkRect::Make(irect);
bsalomon01c8da12014-08-04 09:21:30 -0700372 this->drawRect(rect, NULL, NULL);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000373 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000374
375 /**
bsalomon@google.com934c5702012-03-20 21:17:58 +0000376 * This call is used to draw multiple instances of some geometry with a
377 * given number of vertices (V) and indices (I) per-instance. The indices in
378 * the index source must have the form i[k+I] == i[k] + V. Also, all indices
379 * i[kI] ... i[(k+1)I-1] must be elements of the range kV ... (k+1)V-1. As a
380 * concrete example, the following index buffer for drawing a series of
381 * quads each as two triangles each satisfies these conditions with V=4 and
382 * I=6:
383 * (0,1,2,0,2,3, 4,5,6,4,6,7, 8,9,10,8,10,11, ...)
384 *
385 * The call assumes that the pattern of indices fills the entire index
386 * source. The size of the index buffer limits the number of instances that
387 * can be drawn by the GPU in a single draw. However, the caller may specify
388 * any (positive) number for instanceCount and if necessary multiple GPU
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000389 * draws will be issued. Moreover, when drawIndexedInstances is called
bsalomon@google.com934c5702012-03-20 21:17:58 +0000390 * multiple times it may be possible for GrDrawTarget to group them into a
391 * single GPU draw.
392 *
393 * @param type the type of primitives to draw
394 * @param instanceCount the number of instances to draw. Each instance
395 * consists of verticesPerInstance vertices indexed by
396 * indicesPerInstance indices drawn as the primitive
397 * type specified by type.
398 * @param verticesPerInstance The number of vertices in each instance (V
399 * in the above description).
400 * @param indicesPerInstance The number of indices in each instance (I
401 * in the above description).
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000402 * @param devBounds optional bounds hint. This is a promise from the caller,
403 * not a request for clipping.
bsalomon@google.com934c5702012-03-20 21:17:58 +0000404 */
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000405 void drawIndexedInstances(GrPrimitiveType type,
406 int instanceCount,
407 int verticesPerInstance,
408 int indicesPerInstance,
409 const SkRect* devBounds = NULL);
bsalomon@google.com934c5702012-03-20 21:17:58 +0000410
411 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000412 * Clear the current render target if one isn't passed in. Ignores the
413 * clip and all other draw state (blend mode, stages, etc). Clears the
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000414 * whole thing if rect is NULL, otherwise just the rect. If canIgnoreRect
415 * is set then the entire render target can be optionally cleared.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000416 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000417 virtual void clear(const SkIRect* rect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000418 GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000419 bool canIgnoreRect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000420 GrRenderTarget* renderTarget = NULL) = 0;
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000421
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000422 /**
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000423 * Discards the contents render target. NULL indicates that the current render target should
424 * be discarded.
425 **/
426 virtual void discard(GrRenderTarget* = NULL) = 0;
427
428 /**
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000429 * Called at start and end of gpu trace marking
430 * GR_CREATE_GPU_TRACE_MARKER(marker_str, target) will automatically call these at the start
431 * and end of a code block respectively
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000432 */
egdaniel3eee3832014-06-18 13:09:11 -0700433 void addGpuTraceMarker(const GrGpuTraceMarker* marker);
434 void removeGpuTraceMarker(const GrGpuTraceMarker* marker);
435
436 /**
437 * Takes the current active set of markers and stores them for later use. Any current marker
438 * in the active set is removed from the active set and the targets remove function is called.
439 * These functions do not work as a stack so you cannot call save a second time before calling
440 * restore. Also, it is assumed that when restore is called the current active set of markers
441 * is empty. When the stored markers are added back into the active set, the targets add marker
442 * is called.
443 */
444 void saveActiveTraceMarkers();
445 void restoreActiveTraceMarkers();
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000446
447 /**
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000448 * Copies a pixel rectangle from one surface to another. This call may finalize
449 * reserved vertex/index data (as though a draw call was made). The src pixels
450 * copied are specified by srcRect. They are copied to a rect of the same
451 * size in dst with top left at dstPoint. If the src rect is clipped by the
452 * src bounds then pixel values in the dst rect corresponding to area clipped
453 * by the src rect are not overwritten. This method can fail and return false
454 * depending on the type of surface, configs, etc, and the backend-specific
455 * limitations. If rect is clipped out entirely by the src or dst bounds then
456 * true is returned since there is no actual copy necessary to succeed.
457 */
458 bool copySurface(GrSurface* dst,
459 GrSurface* src,
460 const SkIRect& srcRect,
461 const SkIPoint& dstPoint);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000462 /**
463 * Function that determines whether a copySurface call would succeed without
464 * performing the copy.
465 */
466 bool canCopySurface(GrSurface* dst,
467 GrSurface* src,
468 const SkIRect& srcRect,
469 const SkIPoint& dstPoint);
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000470
robertphillips@google.comff175842012-05-14 19:31:39 +0000471 /**
bsalomon@google.comeb851172013-04-15 13:51:00 +0000472 * This is can be called before allocating a texture to be a dst for copySurface. It will
473 * populate the origin, config, and flags fields of the desc such that copySurface is more
474 * likely to succeed and be efficient.
475 */
bsalomonf2703d82014-10-28 14:33:06 -0700476 virtual void initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc);
bsalomon@google.comeb851172013-04-15 13:51:00 +0000477
478
479 /**
robertphillips@google.comff175842012-05-14 19:31:39 +0000480 * Release any resources that are cached but not currently in use. This
481 * is intended to give an application some recourse when resources are low.
482 */
483 virtual void purgeResources() {};
484
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000485 /**
486 * For subclass internal use to invoke a call to onDraw(). See DrawInfo below.
487 */
488 void executeDraw(const DrawInfo& info) { this->onDraw(info); }
489
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000490 /**
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000491 * For subclass internal use to invoke a call to onDrawPath().
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000492 */
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000493 void executeDrawPath(const GrPath* path, SkPath::FillType fill,
494 const GrDeviceCoordTexture* dstCopy) {
495 this->onDrawPath(path, fill, dstCopy);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000496 }
497
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000498 /**
499 * For subclass internal use to invoke a call to onDrawPaths().
500 */
cdaltonb85a0aa2014-07-21 15:32:44 -0700501 void executeDrawPaths(const GrPathRange* pathRange,
502 const uint32_t indices[], int count,
503 const float transforms[], PathTransformType transformsType,
504 SkPath::FillType fill,
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000505 const GrDeviceCoordTexture* dstCopy) {
cdaltonb85a0aa2014-07-21 15:32:44 -0700506 this->onDrawPaths(pathRange, indices, count, transforms, transformsType, fill, dstCopy);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000507 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000508 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000509
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000510 /**
511 * See AutoStateRestore below.
512 */
513 enum ASRInit {
514 kPreserve_ASRInit,
515 kReset_ASRInit
516 };
517
518 /**
519 * Saves off the current state and restores it in the destructor. It will
520 * install a new GrDrawState object on the target (setDrawState) and restore
521 * the previous one in the destructor. The caller should call drawState() to
522 * get the new draw state after the ASR is installed.
523 *
524 * GrDrawState* state = target->drawState();
525 * AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit).
526 * state->setRenderTarget(rt); // state refers to the GrDrawState set on
527 * // target before asr was initialized.
528 * // Therefore, rt is set on the GrDrawState
529 * // that will be restored after asr's
530 * // destructor rather than target's current
rmistry@google.comd6176b02012-08-23 18:14:13 +0000531 * // GrDrawState.
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000532 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000533 class AutoStateRestore : public ::SkNoncopyable {
reed@google.comac10a2d2010-12-22 21:39:39 +0000534 public:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000535 /**
536 * Default ASR will have no effect unless set() is subsequently called.
537 */
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000538 AutoStateRestore();
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000539
540 /**
541 * Saves the state on target. The state will be restored when the ASR
542 * is destroyed. If this constructor is used do not call set().
543 *
544 * @param init Should the newly installed GrDrawState be a copy of the
545 * previous state or a default-initialized GrDrawState.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000546 * @param viewMatrix Optional view matrix. If init = kPreserve then the draw state's
547 * matrix will be preconcat'ed with the param. All stages will be
548 updated to compensate for the matrix change. If init == kReset
549 then the draw state's matrix will be this matrix.
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000550 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000551 AutoStateRestore(GrDrawTarget* target, ASRInit init, const SkMatrix* viewMatrix = NULL);
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000552
reed@google.comac10a2d2010-12-22 21:39:39 +0000553 ~AutoStateRestore();
554
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000555 /**
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000556 * Saves the state on target. The state will be restored when the ASR
557 * is destroyed. This should only be called once per ASR object and only
558 * when the default constructor was used. For nested saves use multiple
559 * ASR objects.
560 *
561 * @param init Should the newly installed GrDrawState be a copy of the
562 * previous state or a default-initialized GrDrawState.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000563 * @param viewMatrix Optional view matrix. If init = kPreserve then the draw state's
564 * matrix will be preconcat'ed with the param. All stages will be
565 updated to compensate for the matrix change. If init == kReset
566 then the draw state's matrix will be this matrix.
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000567 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000568 void set(GrDrawTarget* target, ASRInit init, const SkMatrix* viewMatrix = NULL);
569
570 /**
571 * Like set() but makes the view matrix identity. When init is kReset it is as though
572 * NULL was passed to set's viewMatrix param. When init is kPreserve it is as though
573 * the inverse view matrix was passed. If kPreserve is passed and the draw state's matrix
574 * is not invertible then this may fail.
575 */
576 bool setIdentity(GrDrawTarget* target, ASRInit init);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000577
reed@google.comac10a2d2010-12-22 21:39:39 +0000578 private:
bsalomon@google.com137f1342013-05-29 21:27:53 +0000579 GrDrawTarget* fDrawTarget;
580 SkTLazy<GrDrawState> fTempState;
581 GrDrawState* fSavedState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000582 };
583
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000584 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000585
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000586 class AutoReleaseGeometry : public ::SkNoncopyable {
reed@google.comac10a2d2010-12-22 21:39:39 +0000587 public:
588 AutoReleaseGeometry(GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000589 int vertexCount,
590 int indexCount);
591 AutoReleaseGeometry();
592 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000593 bool set(GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000594 int vertexCount,
595 int indexCount);
bsalomon49f085d2014-09-05 13:34:00 -0700596 bool succeeded() const { return SkToBool(fTarget); }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000597 void* vertices() const { SkASSERT(this->succeeded()); return fVertices; }
598 void* indices() const { SkASSERT(this->succeeded()); return fIndices; }
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000599 SkPoint* positions() const {
600 return static_cast<SkPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +0000601 }
602
603 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000604 void reset();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000605
reed@google.comac10a2d2010-12-22 21:39:39 +0000606 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000607 void* fVertices;
608 void* fIndices;
609 };
610
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000611 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000612
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000613 class AutoClipRestore : public ::SkNoncopyable {
reed@google.comac10a2d2010-12-22 21:39:39 +0000614 public:
615 AutoClipRestore(GrDrawTarget* target) {
616 fTarget = target;
617 fClip = fTarget->getClip();
618 }
619
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000620 AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip);
621
reed@google.comac10a2d2010-12-22 21:39:39 +0000622 ~AutoClipRestore() {
623 fTarget->setClip(fClip);
624 }
625 private:
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000626 GrDrawTarget* fTarget;
627 const GrClipData* fClip;
628 SkTLazy<SkClipStack> fStack;
629 GrClipData fReplacementClip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000630 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000631
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000632 ////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000633
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000634 /**
635 * Saves the geometry src state at construction and restores in the destructor. It also saves
636 * and then restores the vertex attrib state.
637 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000638 class AutoGeometryPush : public ::SkNoncopyable {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000639 public:
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000640 AutoGeometryPush(GrDrawTarget* target)
641 : fAttribRestore(target->drawState()) {
bsalomon49f085d2014-09-05 13:34:00 -0700642 SkASSERT(target);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000643 fTarget = target;
644 target->pushGeometrySource();
645 }
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000646
647 ~AutoGeometryPush() { fTarget->popGeometrySource(); }
648
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000649 private:
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000650 GrDrawTarget* fTarget;
651 GrDrawState::AutoVertexAttribRestore fAttribRestore;
652 };
653
654 /**
655 * Combination of AutoGeometryPush and AutoStateRestore. The vertex attribs will be in default
656 * state regardless of ASRInit value.
657 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000658 class AutoGeometryAndStatePush : public ::SkNoncopyable {
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000659 public:
bsalomon@google.com137f1342013-05-29 21:27:53 +0000660 AutoGeometryAndStatePush(GrDrawTarget* target,
661 ASRInit init,
662 const SkMatrix* viewMatrix = NULL)
663 : fState(target, init, viewMatrix) {
bsalomon49f085d2014-09-05 13:34:00 -0700664 SkASSERT(target);
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000665 fTarget = target;
666 target->pushGeometrySource();
667 if (kPreserve_ASRInit == init) {
668 target->drawState()->setDefaultVertexAttribs();
669 }
670 }
671
672 ~AutoGeometryAndStatePush() { fTarget->popGeometrySource(); }
673
674 private:
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000675 AutoStateRestore fState;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000676 GrDrawTarget* fTarget;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000677 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000678
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000679 ///////////////////////////////////////////////////////////////////////////
680 // Draw execution tracking (for font atlases and other resources)
681 class DrawToken {
682 public:
683 DrawToken(GrDrawTarget* drawTarget, uint32_t drawID) :
684 fDrawTarget(drawTarget), fDrawID(drawID) {}
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000685
bsalomon49f085d2014-09-05 13:34:00 -0700686 bool isIssued() { return fDrawTarget && fDrawTarget->isIssued(fDrawID); }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000687
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000688 private:
689 GrDrawTarget* fDrawTarget;
690 uint32_t fDrawID; // this may wrap, but we're doing direct comparison
691 // so that should be okay
692 };
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000693
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000694 virtual DrawToken getCurrentDrawToken() { return DrawToken(this, 0); }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000695
reed@google.comac10a2d2010-12-22 21:39:39 +0000696protected:
egdaniel89af44a2014-09-26 06:15:04 -0700697 // Extend access to GrDrawState::convertToPEndeingExec to subclasses.
698 void convertDrawStateToPendingExec(GrDrawState* ds) {
bsalomon2a9ca782014-09-05 14:27:43 -0700699 ds->convertToPendingExec();
700 }
bsalomon@google.com471d4712011-08-23 15:45:25 +0000701
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000702 enum GeometrySrcType {
703 kNone_GeometrySrcType, //<! src has not been specified
704 kReserved_GeometrySrcType, //<! src was set using reserve*Space
705 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
706 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
707 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000708
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000709 struct GeometrySrcState {
710 GeometrySrcType fVertexSrc;
711 union {
712 // valid if src type is buffer
713 const GrVertexBuffer* fVertexBuffer;
714 // valid if src type is reserved or array
715 int fVertexCount;
716 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000717
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000718 GeometrySrcType fIndexSrc;
719 union {
720 // valid if src type is buffer
721 const GrIndexBuffer* fIndexBuffer;
722 // valid if src type is reserved or array
723 int fIndexCount;
724 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000725
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000726 size_t fVertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000727 };
bsalomon@google.com934c5702012-03-20 21:17:58 +0000728
729 int indexCountInCurrentSource() const {
730 const GeometrySrcState& src = this->getGeomSrc();
731 switch (src.fIndexSrc) {
732 case kNone_GeometrySrcType:
733 return 0;
734 case kReserved_GeometrySrcType:
735 case kArray_GeometrySrcType:
736 return src.fIndexCount;
737 case kBuffer_GeometrySrcType:
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000738 return static_cast<int>(src.fIndexBuffer->gpuMemorySize() / sizeof(uint16_t));
bsalomon@google.com934c5702012-03-20 21:17:58 +0000739 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000740 SkFAIL("Unexpected Index Source.");
bsalomon@google.com934c5702012-03-20 21:17:58 +0000741 return 0;
742 }
743 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000744
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000745 // This method is called by copySurface The srcRect is guaranteed to be entirely within the
746 // src bounds. Likewise, the dst rect implied by dstPoint and srcRect's width and height falls
747 // entirely within the dst. The default implementation will draw a rect from the src to the
748 // dst if the src is a texture and the dst is a render target and fail otherwise.
749 virtual bool onCopySurface(GrSurface* dst,
750 GrSurface* src,
751 const SkIRect& srcRect,
752 const SkIPoint& dstPoint);
753
754 // Called to determine whether an onCopySurface call would succeed or not. This is useful for
755 // proxy subclasses to test whether the copy would succeed without executing it yet. Derived
756 // classes must keep this consistent with their implementation of onCopySurface(). The inputs
757 // are the same as onCopySurface(), i.e. srcRect and dstPoint are clipped to be inside the src
758 // and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000759 virtual bool onCanCopySurface(GrSurface* dst,
760 GrSurface* src,
761 const SkIRect& srcRect,
762 const SkIPoint& dstPoint);
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000763
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000764 GrContext* getContext() { return fContext; }
765 const GrContext* getContext() const { return fContext; }
766
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000767 // A subclass may override this function if it wishes to be notified when the clip is changed.
768 // The override should call INHERITED::clipWillBeSet().
769 virtual void clipWillBeSet(const GrClipData* clipData);
770
771 // subclasses must call this in their destructors to ensure all vertex
772 // and index sources have been released (including those held by
773 // pushGeometrySource())
774 void releaseGeometry();
775
776 // accessors for derived classes
777 const GeometrySrcState& getGeomSrc() const { return fGeoSrcStateStack.back(); }
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000778 // it is preferable to call this rather than getGeomSrc()->fVertexSize because of the assert.
779 size_t getVertexSize() const {
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000780 // the vertex layout is only valid if a vertex source has been specified.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000781 SkASSERT(this->getGeomSrc().fVertexSrc != kNone_GeometrySrcType);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000782 return this->getGeomSrc().fVertexSize;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000783 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000784
bsalomon@google.combcce8922013-03-25 15:38:39 +0000785 // Subclass must initialize this in its constructor.
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000786 SkAutoTUnref<const GrDrawTargetCaps> fCaps;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000787
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000788 const GrTraceMarkerSet& getActiveTraceMarkers() { return fActiveTraceMarkers; }
789
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000790 /**
791 * Used to communicate draws to subclass's onDraw function.
792 */
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000793 class DrawInfo {
794 public:
795 DrawInfo(const DrawInfo& di) { (*this) = di; }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000796 DrawInfo& operator =(const DrawInfo& di);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000797
798 GrPrimitiveType primitiveType() const { return fPrimitiveType; }
799 int startVertex() const { return fStartVertex; }
800 int startIndex() const { return fStartIndex; }
801 int vertexCount() const { return fVertexCount; }
802 int indexCount() const { return fIndexCount; }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000803 int verticesPerInstance() const { return fVerticesPerInstance; }
804 int indicesPerInstance() const { return fIndicesPerInstance; }
805 int instanceCount() const { return fInstanceCount; }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000806
807 bool isIndexed() const { return fIndexCount > 0; }
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000808#ifdef SK_DEBUG
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000809 bool isInstanced() const; // this version is longer because of asserts
810#else
811 bool isInstanced() const { return fInstanceCount > 0; }
812#endif
813
814 // adds or remove instances
815 void adjustInstanceCount(int instanceOffset);
816 // shifts the start vertex
817 void adjustStartVertex(int vertexOffset);
818 // shifts the start index
819 void adjustStartIndex(int indexOffset);
820
821 void setDevBounds(const SkRect& bounds) {
822 fDevBoundsStorage = bounds;
823 fDevBounds = &fDevBoundsStorage;
824 }
825 const SkRect* getDevBounds() const { return fDevBounds; }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000826
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000827 // NULL if no copy of the dst is needed for the draw.
828 const GrDeviceCoordTexture* getDstCopy() const {
bsalomon49f085d2014-09-05 13:34:00 -0700829 if (fDstCopy.texture()) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000830 return &fDstCopy;
831 } else {
832 return NULL;
833 }
834 }
835
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000836 private:
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000837 DrawInfo() { fDevBounds = NULL; }
838
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000839 friend class GrDrawTarget;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000840
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000841 GrPrimitiveType fPrimitiveType;
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000842
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000843 int fStartVertex;
844 int fStartIndex;
845 int fVertexCount;
846 int fIndexCount;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000847
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000848 int fInstanceCount;
849 int fVerticesPerInstance;
850 int fIndicesPerInstance;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000851
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000852 SkRect fDevBoundsStorage;
853 SkRect* fDevBounds;
854
855 GrDeviceCoordTexture fDstCopy;
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000856 };
857
joshualitt65171342014-10-09 07:25:36 -0700858 // Makes a copy of the dst if it is necessary for the draw. Returns false if a copy is required
859 // but couldn't be made. Otherwise, returns true. This method needs to be protected because it
860 // needs to be accessed by GLPrograms to setup a correct drawstate
861 bool setupDstReadIfNecessary(DrawInfo* info) {
862 return this->setupDstReadIfNecessary(&info->fDstCopy, info->getDevBounds());
863 }
864 bool setupDstReadIfNecessary(GrDeviceCoordTexture* dstCopy, const SkRect* drawBounds);
865
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000866private:
867 // A subclass can optionally overload this function to be notified before
868 // vertex and index space is reserved.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000869 virtual void willReserveVertexAndIndexSpace(int vertexCount, int indexCount) {}
bsalomon@google.com97805382012-03-13 14:32:07 +0000870
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000871 // implemented by subclass to allocate space for reserved geom
jvanverth@google.coma6338982013-01-31 21:34:25 +0000872 virtual bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices) = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000873 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
874 // implemented by subclass to handle release of reserved geom space
875 virtual void releaseReservedVertexSpace() = 0;
876 virtual void releaseReservedIndexSpace() = 0;
877 // subclass must consume array contents when set
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000878 virtual void onSetVertexSourceToArray(const void* vertexArray, int vertexCount) = 0;
879 virtual void onSetIndexSourceToArray(const void* indexArray, int indexCount) = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000880 // subclass is notified that geom source will be set away from an array
881 virtual void releaseVertexArray() = 0;
882 virtual void releaseIndexArray() = 0;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000883 // subclass overrides to be notified just before geo src state is pushed/popped.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000884 virtual void geometrySourceWillPush() = 0;
885 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
886 // subclass called to perform drawing
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000887 virtual void onDraw(const DrawInfo&) = 0;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000888 // Implementation of drawRect. The geometry src and vertex attribs will already
889 // be saved before this is called and restored afterwards. A subclass may override
890 // this to perform more optimal rect rendering. Its draws should be funneled through
891 // one of the public GrDrawTarget draw methods (e.g. drawNonIndexed,
892 // drawIndexedInstances, ...). The base class draws a two triangle fan using
893 // drawNonIndexed from reserved vertex space.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000894 virtual void onDrawRect(const SkRect& rect,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000895 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000896 const SkMatrix* localMatrix);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000897
898 virtual void onStencilPath(const GrPath*, SkPath::FillType) = 0;
899 virtual void onDrawPath(const GrPath*, SkPath::FillType,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000900 const GrDeviceCoordTexture* dstCopy) = 0;
cdaltonb85a0aa2014-07-21 15:32:44 -0700901 virtual void onDrawPaths(const GrPathRange*,
902 const uint32_t indices[], int count,
903 const float transforms[], PathTransformType,
904 SkPath::FillType, const GrDeviceCoordTexture*) = 0;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000905
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000906 virtual void didAddGpuTraceMarker() = 0;
907 virtual void didRemoveGpuTraceMarker() = 0;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000908
bsalomon@google.come3d70952012-03-13 12:40:53 +0000909 // helpers for reserving vertex and index space.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000910 bool reserveVertexSpace(size_t vertexSize,
bsalomon@google.come3d70952012-03-13 12:40:53 +0000911 int vertexCount,
912 void** vertices);
913 bool reserveIndexSpace(int indexCount, void** indices);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000914
bsalomon@google.come8262622011-11-07 02:30:51 +0000915 // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
916 // indicate non-indexed drawing.
917 bool checkDraw(GrPrimitiveType type, int startVertex,
918 int startIndex, int vertexCount,
919 int indexCount) const;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000920 // called when setting a new vert/idx source to unref prev vb/ib
921 void releasePreviousVertexSource();
922 void releasePreviousIndexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000923
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000924 // Check to see if this set of draw commands has been sent out
925 virtual bool isIssued(uint32_t drawID) { return true; }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000926
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000927 enum {
928 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +0000929 };
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000930 SkSTArray<kPreallocGeoSrcStateStackCnt, GeometrySrcState, true> fGeoSrcStateStack;
931 const GrClipData* fClip;
932 GrDrawState* fDrawState;
933 GrDrawState fDefaultDrawState;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000934 // The context owns us, not vice-versa, so this ptr is not ref'ed by DrawTarget.
935 GrContext* fContext;
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000936 // To keep track that we always have at least as many debug marker adds as removes
937 int fGpuTraceMarkerCount;
938 GrTraceMarkerSet fActiveTraceMarkers;
egdaniel3eee3832014-06-18 13:09:11 -0700939 GrTraceMarkerSet fStoredTraceMarkers;
reed@google.comfa35e3d2012-06-26 20:16:17 +0000940
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000941 typedef SkRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000942};
943
joshualitt6db519c2014-10-29 08:48:18 -0700944class GrClipTarget : public GrDrawTarget {
945public:
946 GrClipTarget(GrContext* context) : INHERITED(context) {}
947 /**
948 * Clip Mask Manager(and no one else) needs to clear private stencil bits.
949 * ClipTarget subclass sets clip bit in the stencil buffer. The subclass
950 * is free to clear the remaining bits to zero if masked clears are more
951 * expensive than clearing all bits.
952 */
953 virtual void clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* = NULL) = 0;
954
955private:
956 typedef GrDrawTarget INHERITED;
957};
958
reed@google.comac10a2d2010-12-22 21:39:39 +0000959#endif