blob: 955581b68368f7f3cddd766994a077b924e12d55 [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 /**
bsalomon89c62982014-11-03 12:08:42 -0800413 * Clear the passed in render target. Ignores the draw state and clip. Clears the whole thing if
414 * rect is NULL, otherwise just the rect. If canIgnoreRect is set then the entire render target
415 * can be optionally cleared.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000416 */
bsalomon89c62982014-11-03 12:08:42 -0800417 virtual void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect,
418 GrRenderTarget* renderTarget) = 0;
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000419
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000420 /**
bsalomon89c62982014-11-03 12:08:42 -0800421 * Discards the contents render target.
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000422 **/
bsalomon89c62982014-11-03 12:08:42 -0800423 virtual void discard(GrRenderTarget*) = 0;
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000424
425 /**
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000426 * Called at start and end of gpu trace marking
427 * GR_CREATE_GPU_TRACE_MARKER(marker_str, target) will automatically call these at the start
428 * and end of a code block respectively
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000429 */
egdaniel3eee3832014-06-18 13:09:11 -0700430 void addGpuTraceMarker(const GrGpuTraceMarker* marker);
431 void removeGpuTraceMarker(const GrGpuTraceMarker* marker);
432
433 /**
434 * Takes the current active set of markers and stores them for later use. Any current marker
435 * in the active set is removed from the active set and the targets remove function is called.
436 * These functions do not work as a stack so you cannot call save a second time before calling
437 * restore. Also, it is assumed that when restore is called the current active set of markers
438 * is empty. When the stored markers are added back into the active set, the targets add marker
439 * is called.
440 */
441 void saveActiveTraceMarkers();
442 void restoreActiveTraceMarkers();
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000443
444 /**
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000445 * Copies a pixel rectangle from one surface to another. This call may finalize
446 * reserved vertex/index data (as though a draw call was made). The src pixels
447 * copied are specified by srcRect. They are copied to a rect of the same
448 * size in dst with top left at dstPoint. If the src rect is clipped by the
449 * src bounds then pixel values in the dst rect corresponding to area clipped
450 * by the src rect are not overwritten. This method can fail and return false
451 * depending on the type of surface, configs, etc, and the backend-specific
452 * limitations. If rect is clipped out entirely by the src or dst bounds then
453 * true is returned since there is no actual copy necessary to succeed.
454 */
455 bool copySurface(GrSurface* dst,
456 GrSurface* src,
457 const SkIRect& srcRect,
458 const SkIPoint& dstPoint);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000459 /**
460 * Function that determines whether a copySurface call would succeed without
461 * performing the copy.
462 */
463 bool canCopySurface(GrSurface* dst,
464 GrSurface* src,
465 const SkIRect& srcRect,
466 const SkIPoint& dstPoint);
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000467
robertphillips@google.comff175842012-05-14 19:31:39 +0000468 /**
bsalomon@google.comeb851172013-04-15 13:51:00 +0000469 * This is can be called before allocating a texture to be a dst for copySurface. It will
470 * populate the origin, config, and flags fields of the desc such that copySurface is more
471 * likely to succeed and be efficient.
472 */
bsalomonf2703d82014-10-28 14:33:06 -0700473 virtual void initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc);
bsalomon@google.comeb851172013-04-15 13:51:00 +0000474
475
476 /**
robertphillips@google.comff175842012-05-14 19:31:39 +0000477 * Release any resources that are cached but not currently in use. This
478 * is intended to give an application some recourse when resources are low.
479 */
480 virtual void purgeResources() {};
481
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000482 /**
483 * For subclass internal use to invoke a call to onDraw(). See DrawInfo below.
484 */
485 void executeDraw(const DrawInfo& info) { this->onDraw(info); }
486
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000487 /**
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000488 * For subclass internal use to invoke a call to onDrawPath().
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000489 */
joshualitt92e496f2014-10-31 13:56:50 -0700490 void executeDrawPath(const GrPath* path, GrPathRendering::FillType fill,
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000491 const GrDeviceCoordTexture* dstCopy) {
492 this->onDrawPath(path, fill, dstCopy);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000493 }
494
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000495 /**
496 * For subclass internal use to invoke a call to onDrawPaths().
497 */
cdaltonb85a0aa2014-07-21 15:32:44 -0700498 void executeDrawPaths(const GrPathRange* pathRange,
499 const uint32_t indices[], int count,
500 const float transforms[], PathTransformType transformsType,
joshualitt92e496f2014-10-31 13:56:50 -0700501 GrPathRendering::FillType fill,
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000502 const GrDeviceCoordTexture* dstCopy) {
cdaltonb85a0aa2014-07-21 15:32:44 -0700503 this->onDrawPaths(pathRange, indices, count, transforms, transformsType, fill, dstCopy);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000504 }
joshualitt329bf482014-10-29 12:31:28 -0700505
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000506 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000507
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000508 /**
509 * See AutoStateRestore below.
510 */
511 enum ASRInit {
512 kPreserve_ASRInit,
513 kReset_ASRInit
514 };
515
516 /**
517 * Saves off the current state and restores it in the destructor. It will
518 * install a new GrDrawState object on the target (setDrawState) and restore
519 * the previous one in the destructor. The caller should call drawState() to
520 * get the new draw state after the ASR is installed.
521 *
522 * GrDrawState* state = target->drawState();
523 * AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit).
524 * state->setRenderTarget(rt); // state refers to the GrDrawState set on
525 * // target before asr was initialized.
526 * // Therefore, rt is set on the GrDrawState
527 * // that will be restored after asr's
528 * // destructor rather than target's current
rmistry@google.comd6176b02012-08-23 18:14:13 +0000529 * // GrDrawState.
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000530 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000531 class AutoStateRestore : public ::SkNoncopyable {
reed@google.comac10a2d2010-12-22 21:39:39 +0000532 public:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000533 /**
534 * Default ASR will have no effect unless set() is subsequently called.
535 */
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000536 AutoStateRestore();
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000537
538 /**
539 * Saves the state on target. The state will be restored when the ASR
540 * is destroyed. If this constructor is used do not call set().
541 *
542 * @param init Should the newly installed GrDrawState be a copy of the
543 * previous state or a default-initialized GrDrawState.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000544 * @param viewMatrix Optional view matrix. If init = kPreserve then the draw state's
545 * matrix will be preconcat'ed with the param. All stages will be
546 updated to compensate for the matrix change. If init == kReset
547 then the draw state's matrix will be this matrix.
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000548 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000549 AutoStateRestore(GrDrawTarget* target, ASRInit init, const SkMatrix* viewMatrix = NULL);
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000550
reed@google.comac10a2d2010-12-22 21:39:39 +0000551 ~AutoStateRestore();
552
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000553 /**
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000554 * Saves the state on target. The state will be restored when the ASR
555 * is destroyed. This should only be called once per ASR object and only
556 * when the default constructor was used. For nested saves use multiple
557 * ASR objects.
558 *
559 * @param init Should the newly installed GrDrawState be a copy of the
560 * previous state or a default-initialized GrDrawState.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000561 * @param viewMatrix Optional view matrix. If init = kPreserve then the draw state's
562 * matrix will be preconcat'ed with the param. All stages will be
563 updated to compensate for the matrix change. If init == kReset
564 then the draw state's matrix will be this matrix.
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000565 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000566 void set(GrDrawTarget* target, ASRInit init, const SkMatrix* viewMatrix = NULL);
567
568 /**
569 * Like set() but makes the view matrix identity. When init is kReset it is as though
570 * NULL was passed to set's viewMatrix param. When init is kPreserve it is as though
571 * the inverse view matrix was passed. If kPreserve is passed and the draw state's matrix
572 * is not invertible then this may fail.
573 */
574 bool setIdentity(GrDrawTarget* target, ASRInit init);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000575
reed@google.comac10a2d2010-12-22 21:39:39 +0000576 private:
bsalomon@google.com137f1342013-05-29 21:27:53 +0000577 GrDrawTarget* fDrawTarget;
578 SkTLazy<GrDrawState> fTempState;
579 GrDrawState* fSavedState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000580 };
581
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000582 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000583
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000584 class AutoReleaseGeometry : public ::SkNoncopyable {
reed@google.comac10a2d2010-12-22 21:39:39 +0000585 public:
586 AutoReleaseGeometry(GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000587 int vertexCount,
588 int indexCount);
589 AutoReleaseGeometry();
590 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000591 bool set(GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000592 int vertexCount,
593 int indexCount);
bsalomon49f085d2014-09-05 13:34:00 -0700594 bool succeeded() const { return SkToBool(fTarget); }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000595 void* vertices() const { SkASSERT(this->succeeded()); return fVertices; }
596 void* indices() const { SkASSERT(this->succeeded()); return fIndices; }
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000597 SkPoint* positions() const {
598 return static_cast<SkPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +0000599 }
600
601 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000602 void reset();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000603
reed@google.comac10a2d2010-12-22 21:39:39 +0000604 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000605 void* fVertices;
606 void* fIndices;
607 };
608
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000609 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000610
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000611 class AutoClipRestore : public ::SkNoncopyable {
reed@google.comac10a2d2010-12-22 21:39:39 +0000612 public:
613 AutoClipRestore(GrDrawTarget* target) {
614 fTarget = target;
615 fClip = fTarget->getClip();
616 }
617
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000618 AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip);
619
reed@google.comac10a2d2010-12-22 21:39:39 +0000620 ~AutoClipRestore() {
621 fTarget->setClip(fClip);
622 }
623 private:
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000624 GrDrawTarget* fTarget;
625 const GrClipData* fClip;
626 SkTLazy<SkClipStack> fStack;
627 GrClipData fReplacementClip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000628 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000629
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000630 ////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000631
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000632 /**
633 * Saves the geometry src state at construction and restores in the destructor. It also saves
634 * and then restores the vertex attrib state.
635 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000636 class AutoGeometryPush : public ::SkNoncopyable {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000637 public:
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000638 AutoGeometryPush(GrDrawTarget* target)
639 : fAttribRestore(target->drawState()) {
bsalomon49f085d2014-09-05 13:34:00 -0700640 SkASSERT(target);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000641 fTarget = target;
642 target->pushGeometrySource();
643 }
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000644
645 ~AutoGeometryPush() { fTarget->popGeometrySource(); }
646
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000647 private:
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000648 GrDrawTarget* fTarget;
649 GrDrawState::AutoVertexAttribRestore fAttribRestore;
650 };
651
652 /**
653 * Combination of AutoGeometryPush and AutoStateRestore. The vertex attribs will be in default
654 * state regardless of ASRInit value.
655 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000656 class AutoGeometryAndStatePush : public ::SkNoncopyable {
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000657 public:
bsalomon@google.com137f1342013-05-29 21:27:53 +0000658 AutoGeometryAndStatePush(GrDrawTarget* target,
659 ASRInit init,
660 const SkMatrix* viewMatrix = NULL)
661 : fState(target, init, viewMatrix) {
bsalomon49f085d2014-09-05 13:34:00 -0700662 SkASSERT(target);
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000663 fTarget = target;
664 target->pushGeometrySource();
665 if (kPreserve_ASRInit == init) {
666 target->drawState()->setDefaultVertexAttribs();
667 }
668 }
669
670 ~AutoGeometryAndStatePush() { fTarget->popGeometrySource(); }
671
672 private:
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000673 AutoStateRestore fState;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000674 GrDrawTarget* fTarget;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000675 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000676
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000677 ///////////////////////////////////////////////////////////////////////////
678 // Draw execution tracking (for font atlases and other resources)
679 class DrawToken {
680 public:
681 DrawToken(GrDrawTarget* drawTarget, uint32_t drawID) :
682 fDrawTarget(drawTarget), fDrawID(drawID) {}
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000683
bsalomon49f085d2014-09-05 13:34:00 -0700684 bool isIssued() { return fDrawTarget && fDrawTarget->isIssued(fDrawID); }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000685
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000686 private:
687 GrDrawTarget* fDrawTarget;
688 uint32_t fDrawID; // this may wrap, but we're doing direct comparison
689 // so that should be okay
690 };
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000691
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000692 virtual DrawToken getCurrentDrawToken() { return DrawToken(this, 0); }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000693
reed@google.comac10a2d2010-12-22 21:39:39 +0000694protected:
egdaniel89af44a2014-09-26 06:15:04 -0700695 // Extend access to GrDrawState::convertToPEndeingExec to subclasses.
696 void convertDrawStateToPendingExec(GrDrawState* ds) {
bsalomon2a9ca782014-09-05 14:27:43 -0700697 ds->convertToPendingExec();
698 }
bsalomon@google.com471d4712011-08-23 15:45:25 +0000699
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000700 enum GeometrySrcType {
701 kNone_GeometrySrcType, //<! src has not been specified
702 kReserved_GeometrySrcType, //<! src was set using reserve*Space
703 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
704 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
705 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000706
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000707 struct GeometrySrcState {
708 GeometrySrcType fVertexSrc;
709 union {
710 // valid if src type is buffer
711 const GrVertexBuffer* fVertexBuffer;
712 // valid if src type is reserved or array
713 int fVertexCount;
714 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000715
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000716 GeometrySrcType fIndexSrc;
717 union {
718 // valid if src type is buffer
719 const GrIndexBuffer* fIndexBuffer;
720 // valid if src type is reserved or array
721 int fIndexCount;
722 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000723
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000724 size_t fVertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000725 };
bsalomon@google.com934c5702012-03-20 21:17:58 +0000726
727 int indexCountInCurrentSource() const {
728 const GeometrySrcState& src = this->getGeomSrc();
729 switch (src.fIndexSrc) {
730 case kNone_GeometrySrcType:
731 return 0;
732 case kReserved_GeometrySrcType:
733 case kArray_GeometrySrcType:
734 return src.fIndexCount;
735 case kBuffer_GeometrySrcType:
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000736 return static_cast<int>(src.fIndexBuffer->gpuMemorySize() / sizeof(uint16_t));
bsalomon@google.com934c5702012-03-20 21:17:58 +0000737 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000738 SkFAIL("Unexpected Index Source.");
bsalomon@google.com934c5702012-03-20 21:17:58 +0000739 return 0;
740 }
741 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000742
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000743 // This method is called by copySurface The srcRect is guaranteed to be entirely within the
744 // src bounds. Likewise, the dst rect implied by dstPoint and srcRect's width and height falls
745 // entirely within the dst. The default implementation will draw a rect from the src to the
746 // dst if the src is a texture and the dst is a render target and fail otherwise.
747 virtual bool onCopySurface(GrSurface* dst,
748 GrSurface* src,
749 const SkIRect& srcRect,
750 const SkIPoint& dstPoint);
751
752 // Called to determine whether an onCopySurface call would succeed or not. This is useful for
753 // proxy subclasses to test whether the copy would succeed without executing it yet. Derived
754 // classes must keep this consistent with their implementation of onCopySurface(). The inputs
755 // are the same as onCopySurface(), i.e. srcRect and dstPoint are clipped to be inside the src
756 // and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000757 virtual bool onCanCopySurface(GrSurface* dst,
758 GrSurface* src,
759 const SkIRect& srcRect,
760 const SkIPoint& dstPoint);
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000761
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000762 GrContext* getContext() { return fContext; }
763 const GrContext* getContext() const { return fContext; }
764
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000765 // A subclass may override this function if it wishes to be notified when the clip is changed.
766 // The override should call INHERITED::clipWillBeSet().
767 virtual void clipWillBeSet(const GrClipData* clipData);
768
769 // subclasses must call this in their destructors to ensure all vertex
770 // and index sources have been released (including those held by
771 // pushGeometrySource())
772 void releaseGeometry();
773
774 // accessors for derived classes
775 const GeometrySrcState& getGeomSrc() const { return fGeoSrcStateStack.back(); }
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000776 // it is preferable to call this rather than getGeomSrc()->fVertexSize because of the assert.
777 size_t getVertexSize() const {
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000778 // the vertex layout is only valid if a vertex source has been specified.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000779 SkASSERT(this->getGeomSrc().fVertexSrc != kNone_GeometrySrcType);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000780 return this->getGeomSrc().fVertexSize;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000781 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000782
bsalomon@google.combcce8922013-03-25 15:38:39 +0000783 // Subclass must initialize this in its constructor.
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000784 SkAutoTUnref<const GrDrawTargetCaps> fCaps;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000785
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000786 const GrTraceMarkerSet& getActiveTraceMarkers() { return fActiveTraceMarkers; }
787
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000788 /**
789 * Used to communicate draws to subclass's onDraw function.
790 */
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000791 class DrawInfo {
792 public:
793 DrawInfo(const DrawInfo& di) { (*this) = di; }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000794 DrawInfo& operator =(const DrawInfo& di);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000795
796 GrPrimitiveType primitiveType() const { return fPrimitiveType; }
797 int startVertex() const { return fStartVertex; }
798 int startIndex() const { return fStartIndex; }
799 int vertexCount() const { return fVertexCount; }
800 int indexCount() const { return fIndexCount; }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000801 int verticesPerInstance() const { return fVerticesPerInstance; }
802 int indicesPerInstance() const { return fIndicesPerInstance; }
803 int instanceCount() const { return fInstanceCount; }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000804
805 bool isIndexed() const { return fIndexCount > 0; }
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000806#ifdef SK_DEBUG
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000807 bool isInstanced() const; // this version is longer because of asserts
808#else
809 bool isInstanced() const { return fInstanceCount > 0; }
810#endif
811
812 // adds or remove instances
813 void adjustInstanceCount(int instanceOffset);
814 // shifts the start vertex
815 void adjustStartVertex(int vertexOffset);
816 // shifts the start index
817 void adjustStartIndex(int indexOffset);
818
819 void setDevBounds(const SkRect& bounds) {
820 fDevBoundsStorage = bounds;
821 fDevBounds = &fDevBoundsStorage;
822 }
823 const SkRect* getDevBounds() const { return fDevBounds; }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000824
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000825 // NULL if no copy of the dst is needed for the draw.
826 const GrDeviceCoordTexture* getDstCopy() const {
bsalomon49f085d2014-09-05 13:34:00 -0700827 if (fDstCopy.texture()) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000828 return &fDstCopy;
829 } else {
830 return NULL;
831 }
832 }
833
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000834 private:
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000835 DrawInfo() { fDevBounds = NULL; }
836
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000837 friend class GrDrawTarget;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000838
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000839 GrPrimitiveType fPrimitiveType;
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000840
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000841 int fStartVertex;
842 int fStartIndex;
843 int fVertexCount;
844 int fIndexCount;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000845
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000846 int fInstanceCount;
847 int fVerticesPerInstance;
848 int fIndicesPerInstance;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000849
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000850 SkRect fDevBoundsStorage;
851 SkRect* fDevBounds;
852
853 GrDeviceCoordTexture fDstCopy;
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000854 };
855
joshualitt65171342014-10-09 07:25:36 -0700856 // Makes a copy of the dst if it is necessary for the draw. Returns false if a copy is required
857 // but couldn't be made. Otherwise, returns true. This method needs to be protected because it
858 // needs to be accessed by GLPrograms to setup a correct drawstate
859 bool setupDstReadIfNecessary(DrawInfo* info) {
860 return this->setupDstReadIfNecessary(&info->fDstCopy, info->getDevBounds());
861 }
862 bool setupDstReadIfNecessary(GrDeviceCoordTexture* dstCopy, const SkRect* drawBounds);
863
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000864private:
865 // A subclass can optionally overload this function to be notified before
866 // vertex and index space is reserved.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000867 virtual void willReserveVertexAndIndexSpace(int vertexCount, int indexCount) {}
bsalomon@google.com97805382012-03-13 14:32:07 +0000868
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000869 // implemented by subclass to allocate space for reserved geom
jvanverth@google.coma6338982013-01-31 21:34:25 +0000870 virtual bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices) = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000871 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
872 // implemented by subclass to handle release of reserved geom space
873 virtual void releaseReservedVertexSpace() = 0;
874 virtual void releaseReservedIndexSpace() = 0;
875 // subclass must consume array contents when set
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000876 virtual void onSetVertexSourceToArray(const void* vertexArray, int vertexCount) = 0;
877 virtual void onSetIndexSourceToArray(const void* indexArray, int indexCount) = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000878 // subclass is notified that geom source will be set away from an array
879 virtual void releaseVertexArray() = 0;
880 virtual void releaseIndexArray() = 0;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000881 // subclass overrides to be notified just before geo src state is pushed/popped.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000882 virtual void geometrySourceWillPush() = 0;
883 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
884 // subclass called to perform drawing
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000885 virtual void onDraw(const DrawInfo&) = 0;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000886 // Implementation of drawRect. The geometry src and vertex attribs will already
887 // be saved before this is called and restored afterwards. A subclass may override
888 // this to perform more optimal rect rendering. Its draws should be funneled through
889 // one of the public GrDrawTarget draw methods (e.g. drawNonIndexed,
890 // drawIndexedInstances, ...). The base class draws a two triangle fan using
891 // drawNonIndexed from reserved vertex space.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000892 virtual void onDrawRect(const SkRect& rect,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000893 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000894 const SkMatrix* localMatrix);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000895
joshualitt92e496f2014-10-31 13:56:50 -0700896 virtual void onStencilPath(const GrPath*, GrPathRendering::FillType) = 0;
897 virtual void onDrawPath(const GrPath*, GrPathRendering::FillType,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000898 const GrDeviceCoordTexture* dstCopy) = 0;
cdaltonb85a0aa2014-07-21 15:32:44 -0700899 virtual void onDrawPaths(const GrPathRange*,
900 const uint32_t indices[], int count,
901 const float transforms[], PathTransformType,
joshualitt92e496f2014-10-31 13:56:50 -0700902 GrPathRendering::FillType, const GrDeviceCoordTexture*) = 0;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000903
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000904 virtual void didAddGpuTraceMarker() = 0;
905 virtual void didRemoveGpuTraceMarker() = 0;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000906
bsalomon@google.come3d70952012-03-13 12:40:53 +0000907 // helpers for reserving vertex and index space.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000908 bool reserveVertexSpace(size_t vertexSize,
bsalomon@google.come3d70952012-03-13 12:40:53 +0000909 int vertexCount,
910 void** vertices);
911 bool reserveIndexSpace(int indexCount, void** indices);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000912
bsalomon@google.come8262622011-11-07 02:30:51 +0000913 // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
914 // indicate non-indexed drawing.
915 bool checkDraw(GrPrimitiveType type, int startVertex,
916 int startIndex, int vertexCount,
917 int indexCount) const;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000918 // called when setting a new vert/idx source to unref prev vb/ib
919 void releasePreviousVertexSource();
920 void releasePreviousIndexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000921
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000922 // Check to see if this set of draw commands has been sent out
923 virtual bool isIssued(uint32_t drawID) { return true; }
joshualitta3931272014-10-31 16:32:22 -0700924 virtual GrClipMaskManager* getClipMaskManager() = 0;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000925
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000926 enum {
927 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +0000928 };
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000929 SkSTArray<kPreallocGeoSrcStateStackCnt, GeometrySrcState, true> fGeoSrcStateStack;
930 const GrClipData* fClip;
931 GrDrawState* fDrawState;
932 GrDrawState fDefaultDrawState;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000933 // The context owns us, not vice-versa, so this ptr is not ref'ed by DrawTarget.
934 GrContext* fContext;
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000935 // To keep track that we always have at least as many debug marker adds as removes
936 int fGpuTraceMarkerCount;
937 GrTraceMarkerSet fActiveTraceMarkers;
egdaniel3eee3832014-06-18 13:09:11 -0700938 GrTraceMarkerSet fStoredTraceMarkers;
reed@google.comfa35e3d2012-06-26 20:16:17 +0000939
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000940 typedef SkRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000941};
942
joshualitt329bf482014-10-29 12:31:28 -0700943/*
944 * This class is JUST for clip mask manager. Everyone else should just use draw target above.
945 */
joshualitt6db519c2014-10-29 08:48:18 -0700946class GrClipTarget : public GrDrawTarget {
947public:
joshualitt329bf482014-10-29 12:31:28 -0700948 GrClipTarget(GrContext* context) : INHERITED(context) {
949 fClipMaskManager.setClipTarget(this);
950 }
951
952 /* Clip mask manager needs access to the context.
953 * TODO we only need a very small subset of context in the CMM.
954 */
955 GrContext* getContext() { return INHERITED::getContext(); }
956 const GrContext* getContext() const { return INHERITED::getContext(); }
957
joshualitt6db519c2014-10-29 08:48:18 -0700958 /**
959 * Clip Mask Manager(and no one else) needs to clear private stencil bits.
960 * ClipTarget subclass sets clip bit in the stencil buffer. The subclass
961 * is free to clear the remaining bits to zero if masked clears are more
962 * expensive than clearing all bits.
963 */
964 virtual void clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* = NULL) = 0;
965
joshualitt3bdd7dc2014-10-31 08:27:39 -0700966 /**
967 * Release any resources that are cached but not currently in use. This
968 * is intended to give an application some recourse when resources are low.
969 */
970 virtual void purgeResources() SK_OVERRIDE {
971 // The clip mask manager can rebuild all its clip masks so just
972 // get rid of them all.
973 fClipMaskManager.purgeResources();
974 };
975
joshualitt329bf482014-10-29 12:31:28 -0700976protected:
977 GrClipMaskManager fClipMaskManager;
978
joshualitt6db519c2014-10-29 08:48:18 -0700979private:
joshualitta3931272014-10-31 16:32:22 -0700980 GrClipMaskManager* getClipMaskManager() { return &fClipMaskManager; }
joshualitt329bf482014-10-29 12:31:28 -0700981
joshualitt6db519c2014-10-29 08:48:18 -0700982 typedef GrDrawTarget INHERITED;
983};
984
reed@google.comac10a2d2010-12-22 21:39:39 +0000985#endif