blob: 86aa3b43ea50107c18aa4c9f87e720bfe8ca3416 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef GrDrawTarget_DEFINED
12#define GrDrawTarget_DEFINED
13
reed@google.comac10a2d2010-12-22 21:39:39 +000014#include "GrClip.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000015#include "GrColor.h"
16#include "GrMatrix.h"
17#include "GrRefCnt.h"
18#include "GrRenderTarget.h"
19#include "GrSamplerState.h"
bsalomon@google.comd302f142011-03-03 13:54:13 +000020#include "GrStencil.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000021#include "GrTexture.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000022
Scroggo97c88c22011-05-11 14:05:25 +000023#include "SkXfermode.h"
24
reed@google.comac10a2d2010-12-22 21:39:39 +000025class GrTexture;
reed@google.comac10a2d2010-12-22 21:39:39 +000026class GrClipIterator;
27class GrVertexBuffer;
28class GrIndexBuffer;
29
30class GrDrawTarget : public GrRefCnt {
31public:
32 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000033 * Represents the draw target capabilities.
34 */
35 struct Caps {
36 Caps() { memset(this, 0, sizeof(Caps)); }
37 Caps(const Caps& c) { *this = c; }
38 Caps& operator= (const Caps& c) {
39 memcpy(this, &c, sizeof(Caps));
40 return *this;
41 }
42 void print() const;
43 bool f8BitPaletteSupport : 1;
44 bool fNPOTTextureSupport : 1;
45 bool fNPOTTextureTileSupport : 1;
46 bool fNPOTRenderTargetSupport : 1;
47 bool fTwoSidedStencilSupport : 1;
48 bool fStencilWrapOpsSupport : 1;
49 bool fHWAALineSupport : 1;
50 bool fShaderSupport : 1;
51 bool fShaderDerivativeSupport : 1;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000052 bool fGeometryShaderSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000053 bool fFSAASupport : 1;
54 bool fDualSourceBlendingSupport : 1;
55 bool fBufferLockSupport : 1;
bsalomon@google.coma3108262011-10-10 14:08:47 +000056 bool fSupportPerVertexCoverage : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000057 int fMinRenderTargetWidth;
58 int fMinRenderTargetHeight;
59 int fMaxRenderTargetSize;
60 int fMaxTextureSize;
61 };
62
63 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +000064 * Number of texture stages. Each stage takes as input a color and
65 * 2D texture coordinates. The color input to the first enabled stage is the
66 * per-vertex color or the constant color (setColor/setAlpha) if there are
67 * no per-vertex colors. For subsequent stages the input color is the output
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000068 * color from the previous enabled stage. The output color of each stage is
bsalomon@google.com5782d712011-01-21 21:03:59 +000069 * the input color modulated with the result of a texture lookup. Texture
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000070 * lookups are specified by a texture a sampler (setSamplerState). Texture
71 * coordinates for each stage come from the vertices based on a
72 * GrVertexLayout bitfield. The output fragment color is the output color of
73 * the last enabled stage. The presence or absence of texture coordinates
74 * for each stage in the vertex layout indicates whether a stage is enabled
75 * or not.
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000076 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000077 enum {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000078 kNumStages = 3,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000079 kMaxTexCoords = kNumStages
80 };
bsalomon@google.com5782d712011-01-21 21:03:59 +000081
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000082 /**
83 * The absolute maximum number of edges that may be specified for
84 * a single draw call when performing edge antialiasing. This is used for
85 * the size of several static buffers, so implementations of getMaxEdges()
86 * (below) should clamp to this value.
87 */
88 enum {
89 kMaxEdges = 32
90 };
91
bsalomon@google.comaeb21602011-08-30 18:13:44 +000092 /**
93 * When specifying edges as vertex data this enum specifies what type of
94 * edges are in use. The edges are always 4 GrScalars in memory, even when
95 * the edge type requires fewer than 4.
96 */
97 enum VertexEdgeType {
98 /* 1-pixel wide line
99 2D implicit line eq (a*x + b*y +c = 0). 4th component unused */
100 kHairLine_EdgeType,
101 /* 1-pixel wide quadratic
102 u^2-v canonical coords (only 2 components used) */
103 kHairQuad_EdgeType
104 };
105
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000106 /**
bsalomon@google.comffca4002011-02-22 20:34:01 +0000107 * Bitfield used to indicate which stages are in use.
reed@google.comac10a2d2010-12-22 21:39:39 +0000108 */
bsalomon@google.comffca4002011-02-22 20:34:01 +0000109 typedef int StageBitfield;
110 GR_STATIC_ASSERT(sizeof(StageBitfield)*8 >= kNumStages);
reed@google.comac10a2d2010-12-22 21:39:39 +0000111
112 /**
113 * Flags that affect rendering. Controlled using enable/disableState(). All
114 * default to disabled.
115 */
116 enum StateBits {
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000117 kDither_StateBit = 0x01, //<! Perform color dithering
118 kAntialias_StateBit = 0x02, //<! Perform anti-aliasing. The render-
reed@google.comac10a2d2010-12-22 21:39:39 +0000119 // target must support some form of AA
120 // (msaa, coverage sampling, etc). For
121 // GrGpu-created rendertarget/textures
122 // this is controlled by parameters
123 // passed to createTexture.
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000124 kClip_StateBit = 0x04, //<! Controls whether drawing is clipped
reed@google.comac10a2d2010-12-22 21:39:39 +0000125 // against the region specified by
126 // setClip.
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000127 kNoColorWrites_StateBit = 0x08, //<! If set it disables writing colors.
128 // Useful while performing stencil
129 // ops.
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000130 kEdgeAAConcave_StateBit = 0x10,//<! If set, edge AA will test edge
131 // pairs for convexity while
132 // rasterizing. Set this if the
133 // source polygon is non-convex.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000134
135 // subclass may use additional bits internally
136 kDummyStateBit,
137 kLastPublicStateBit = kDummyStateBit-1
138 };
139
140 enum DrawFace {
141 kBoth_DrawFace,
142 kCCW_DrawFace,
143 kCW_DrawFace,
reed@google.comac10a2d2010-12-22 21:39:39 +0000144 };
145
146 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000147 * Sets the stencil settings to use for the next draw.
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000148 * Changing the clip has the side-effect of possibly zeroing
149 * out the client settable stencil bits. So multipass algorithms
150 * using stencil should not change the clip between passes.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000151 * @param settings the stencil settings to use.
152 */
153 void setStencil(const GrStencilSettings& settings) {
154 fCurrDrawState.fStencilSettings = settings;
155 }
156
157 /**
158 * Shortcut to disable stencil testing and ops.
159 */
160 void disableStencil() {
161 fCurrDrawState.fStencilSettings.setDisabled();
162 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000163
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000164 class Edge {
165 public:
166 Edge() {}
167 Edge(float x, float y, float z) : fX(x), fY(y), fZ(z) {}
168 GrPoint intersect(const Edge& other) {
169 return GrPoint::Make(
170 (fY * other.fZ - other.fY * fZ) /
171 (fX * other.fY - other.fX * fY),
172 (fX * other.fZ - other.fX * fZ) /
173 (other.fX * fY - fX * other.fY));
174 }
175 float fX, fY, fZ;
176 };
177
reed@google.comac10a2d2010-12-22 21:39:39 +0000178protected:
reed@google.comac10a2d2010-12-22 21:39:39 +0000179
reed@google.com8195f672011-01-12 18:14:28 +0000180 struct DrState {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000181 DrState() {
182 // make sure any pad is zero for memcmp
183 // all DrState members should default to something
184 // valid by the memset
185 memset(this, 0, sizeof(DrState));
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000186
187 // memset exceptions
Scroggo97c88c22011-05-11 14:05:25 +0000188 fColorFilterXfermode = SkXfermode::kDstIn_Mode;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000189 fFirstCoverageStage = kNumStages;
190
191 // pedantic assertion that our ptrs will
192 // be NULL (0 ptr is mem addr 0)
bsalomon@google.comd302f142011-03-03 13:54:13 +0000193 GrAssert((intptr_t)(void*)NULL == 0LL);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000194
195 // default stencil setting should be disabled
bsalomon@google.comd302f142011-03-03 13:54:13 +0000196 GrAssert(fStencilSettings.isDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000197 fFirstCoverageStage = kNumStages;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000198 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000199 uint32_t fFlagBits;
bsalomon@google.comffca4002011-02-22 20:34:01 +0000200 GrBlendCoeff fSrcBlend;
201 GrBlendCoeff fDstBlend;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000202 GrColor fBlendConstant;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000203 GrTexture* fTextures[kNumStages];
204 GrSamplerState fSamplerStates[kNumStages];
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000205 int fFirstCoverageStage;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000206 GrRenderTarget* fRenderTarget;
207 GrColor fColor;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000208 DrawFace fDrawFace;
Scroggo97c88c22011-05-11 14:05:25 +0000209 GrColor fColorFilterColor;
210 SkXfermode::Mode fColorFilterXfermode;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000211
212 GrStencilSettings fStencilSettings;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000213 GrMatrix fViewMatrix;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000214 VertexEdgeType fVertexEdgeType;
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000215 Edge fEdgeAAEdges[kMaxEdges];
216 int fEdgeAANumEdges;
reed@google.com8195f672011-01-12 18:14:28 +0000217 bool operator ==(const DrState& s) const {
218 return 0 == memcmp(this, &s, sizeof(DrState));
reed@google.comac10a2d2010-12-22 21:39:39 +0000219 }
reed@google.com8195f672011-01-12 18:14:28 +0000220 bool operator !=(const DrState& s) const { return !(*this == s); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000221 };
222
223public:
224 ///////////////////////////////////////////////////////////////////////////
225
226 GrDrawTarget();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000227 virtual ~GrDrawTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000228
229 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000230 * Gets the capabilities of the draw target.
231 */
232 const Caps& getCaps() const { return fCaps; }
233
234 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000235 * Sets the current clip to the region specified by clip. All draws will be
236 * clipped against this clip if kClip_StateBit is enabled.
237 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000238 * Setting the clip may (or may not) zero out the client's stencil bits.
239 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000240 * @param description of the clipping region
241 */
242 void setClip(const GrClip& clip);
243
244 /**
245 * Gets the current clip.
246 *
247 * @return the clip.
248 */
249 const GrClip& getClip() const;
250
251 /**
252 * Sets the texture used at the next drawing call
253 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000254 * @param stage The texture stage for which the texture will be set
255 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000256 * @param texture The texture to set. Can be NULL though there is no advantage
257 * to settings a NULL texture if doing non-textured drawing
258 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000259 void setTexture(int stage, GrTexture* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000260
261 /**
262 * Retrieves the currently set texture.
263 *
264 * @return The currently set texture. The return value will be NULL if no
265 * texture has been set, NULL was most recently passed to
266 * setTexture, or the last setTexture was destroyed.
267 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000268 const GrTexture* getTexture(int stage) const;
269 GrTexture* getTexture(int stage);
reed@google.comac10a2d2010-12-22 21:39:39 +0000270
271 /**
272 * Sets the rendertarget used at the next drawing call
273 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000274 * @param target The render target to set.
reed@google.comac10a2d2010-12-22 21:39:39 +0000275 */
276 void setRenderTarget(GrRenderTarget* target);
277
278 /**
279 * Retrieves the currently set rendertarget.
280 *
281 * @return The currently set render target.
282 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000283 const GrRenderTarget* getRenderTarget() const;
284 GrRenderTarget* getRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000285
286 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000287 * Sets the sampler state for a stage used in subsequent draws.
reed@google.comac10a2d2010-12-22 21:39:39 +0000288 *
bsalomon@google.comd302f142011-03-03 13:54:13 +0000289 * The sampler state determines how texture coordinates are
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000290 * intepretted and used to sample the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +0000291 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000292 * @param stage the stage of the sampler to set
reed@google.comac10a2d2010-12-22 21:39:39 +0000293 * @param samplerState Specifies the sampler state.
294 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000295 void setSamplerState(int stage, const GrSamplerState& samplerState);
reed@google.comac10a2d2010-12-22 21:39:39 +0000296
297 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000298 * Concats the matrix of a stage's sampler.
reed@google.comac10a2d2010-12-22 21:39:39 +0000299 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000300 * @param stage the stage of the sampler to set
301 * @param matrix the matrix to concat
reed@google.comac10a2d2010-12-22 21:39:39 +0000302 */
bsalomon@google.com27847de2011-02-22 20:59:41 +0000303 void preConcatSamplerMatrix(int stage, const GrMatrix& matrix) {
304 GrAssert(stage >= 0 && stage < kNumStages);
305 fCurrDrawState.fSamplerStates[stage].preConcatMatrix(matrix);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000306 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000307
308 /**
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000309 * Shortcut for preConcatSamplerMatrix on all stages in mask with same
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000310 * matrix
311 */
312 void preConcatSamplerMatrices(int stageMask, const GrMatrix& matrix) {
313 for (int i = 0; i < kNumStages; ++i) {
314 if ((1 << i) & stageMask) {
315 this->preConcatSamplerMatrix(i, matrix);
316 }
317 }
318 }
319
320 /**
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000321 * Shortcut for preConcatSamplerMatrix on all enabled stages in mask with
322 * same matrix
323 *
324 * @param stage the stage of the sampler to set
325 * @param matrix the matrix to concat
326 */
327 void preConcatEnabledSamplerMatrices(const GrMatrix& matrix) {
328 StageBitfield stageMask = this->enabledStages();
329 this->preConcatSamplerMatrices(stageMask, matrix);
330 }
331
332 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000333 * Gets the matrix of a stage's sampler
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000334 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000335 * @param stage the stage to of sampler to get
336 * @return the sampler state's matrix
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000337 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000338 const GrMatrix& getSamplerMatrix(int stage) const {
339 return fCurrDrawState.fSamplerStates[stage].getMatrix();
340 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000341
342 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000343 * Sets the matrix of a stage's sampler
344 *
345 * @param stage the stage of sampler set
346 * @param matrix the matrix to set
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000347 */
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000348 void setSamplerMatrix(int stage, const GrMatrix& matrix) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000349 fCurrDrawState.fSamplerStates[stage].setMatrix(matrix);
350 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000351
352 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000353 * Sets the matrix applied to veretx positions.
354 *
355 * In the post-view-matrix space the rectangle [0,w]x[0,h]
356 * fully covers the render target. (w and h are the width and height of the
357 * the rendertarget.)
358 *
359 * @param m the matrix used to transform the vertex positions.
360 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000361 void setViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000362
363 /**
364 * Multiplies the current view matrix by a matrix
365 *
366 * After this call V' = V*m where V is the old view matrix,
367 * m is the parameter to this function, and V' is the new view matrix.
368 * (We consider positions to be column vectors so position vector p is
369 * transformed by matrix X as p' = X*p.)
370 *
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000371 * @param m the matrix used to modify the view matrix.
reed@google.comac10a2d2010-12-22 21:39:39 +0000372 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000373 void preConcatViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000374
375 /**
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000376 * Multiplies the current view matrix by a matrix
377 *
378 * After this call V' = m*V where V is the old view matrix,
379 * m is the parameter to this function, and V' is the new view matrix.
380 * (We consider positions to be column vectors so position vector p is
381 * transformed by matrix X as p' = X*p.)
382 *
383 * @param m the matrix used to modify the view matrix.
384 */
385 void postConcatViewMatrix(const GrMatrix& m);
386
387 /**
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000388 * Retrieves the current view matrix
389 * @return the current view matrix.
390 */
391 const GrMatrix& getViewMatrix() const;
392
393 /**
394 * Retrieves the inverse of the current view matrix.
395 *
396 * If the current view matrix is invertible, return true, and if matrix
397 * is non-null, copy the inverse into it. If the current view matrix is
398 * non-invertible, return false and ignore the matrix parameter.
399 *
400 * @param matrix if not null, will receive a copy of the current inverse.
401 */
402 bool getViewInverse(GrMatrix* matrix) const;
403
404 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000405 * Sets color for next draw to a premultiplied-alpha color.
406 *
407 * @param the color to set.
408 */
409 void setColor(GrColor);
410
411 /**
bsalomon@google.coma3108262011-10-10 14:08:47 +0000412 * Gets the currently set color.
413 * @return the current color.
414 */
415 GrColor getColor() const { return fCurrDrawState.fColor; }
416
417 /**
Scroggo97c88c22011-05-11 14:05:25 +0000418 * Add a color filter that can be represented by a color and a mode.
419 */
420 void setColorFilter(GrColor, SkXfermode::Mode);
421
422 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000423 * Sets the color to be used for the next draw to be
424 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
425 *
426 * @param alpha The alpha value to set as the color.
427 */
428 void setAlpha(uint8_t alpha);
429
430 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000431 * Controls whether clockwise, counterclockwise, or both faces are drawn.
432 * @param face the face(s) to draw.
reed@google.comac10a2d2010-12-22 21:39:39 +0000433 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000434 void setDrawFace(DrawFace face) { fCurrDrawState.fDrawFace = face; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000435
436 /**
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000437 * A common pattern is to compute a color with the initial stages and then
438 * modulate that color by a coverage value in later stage(s) (AA, mask-
439 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
440 * computed based on the pre-coverage-modulated color. The division of
441 * stages between color-computing and coverage-computing is specified by
442 * this method. Initially this is kNumStages (all stages are color-
443 * computing).
444 */
445 void setFirstCoverageStage(int firstCoverageStage) {
446 fCurrDrawState.fFirstCoverageStage = firstCoverageStage;
447 }
448
449 /**
450 * Gets the index of the first coverage-computing stage.
451 */
452 int getFirstCoverageStage() const {
453 return fCurrDrawState.fFirstCoverageStage;
454 }
455
456 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000457 * Gets whether the target is drawing clockwise, counterclockwise,
458 * or both faces.
459 * @return the current draw face(s).
reed@google.comac10a2d2010-12-22 21:39:39 +0000460 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000461 DrawFace getDrawFace() const { return fCurrDrawState.fDrawFace; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000462
463 /**
464 * Enable render state settings.
465 *
466 * @param flags bitfield of StateBits specifing the states to enable
467 */
468 void enableState(uint32_t stateBits);
469
470 /**
471 * Disable render state settings.
472 *
473 * @param flags bitfield of StateBits specifing the states to disable
474 */
475 void disableState(uint32_t stateBits);
476
477 bool isDitherState() const {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000478 return 0 != (fCurrDrawState.fFlagBits & kDither_StateBit);
479 }
480
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000481 bool isAntialiasState() const {
482 return 0 != (fCurrDrawState.fFlagBits & kAntialias_StateBit);
483 }
484
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000485 bool isClipState() const {
486 return 0 != (fCurrDrawState.fFlagBits & kClip_StateBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000487 }
488
bsalomon@google.comd302f142011-03-03 13:54:13 +0000489 bool isColorWriteDisabled() const {
490 return 0 != (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit);
491 }
492
reed@google.comac10a2d2010-12-22 21:39:39 +0000493 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000494 * Sets the blending function coeffecients.
495 *
496 * The blend function will be:
497 * D' = sat(S*srcCoef + D*dstCoef)
498 *
499 * where D is the existing destination color, S is the incoming source
500 * color, and D' is the new destination color that will be written. sat()
501 * is the saturation function.
502 *
503 * @param srcCoef coeffecient applied to the src color.
504 * @param dstCoef coeffecient applied to the dst color.
505 */
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000506 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff);
reed@google.comac10a2d2010-12-22 21:39:39 +0000507
508 /**
bsalomon@google.com080773c2011-03-15 19:09:25 +0000509 * Sets the blending function constant referenced by the following blending
510 * coeffecients:
511 * kConstC_BlendCoeff
512 * kIConstC_BlendCoeff
513 * kConstA_BlendCoeff
514 * kIConstA_BlendCoeff
515 *
516 * @param constant the constant to set
517 */
518 void setBlendConstant(GrColor constant) { fCurrDrawState.fBlendConstant = constant; }
519
520 /**
521 * Retrieves the last value set by setBlendConstant()
522 * @return the blending constant value
523 */
524 GrColor getBlendConstant() const { return fCurrDrawState.fBlendConstant; }
525
526 /**
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000527 * Determines if blend is effectively disabled.
528 *
529 * @return true if blend can be disabled without changing the rendering
530 * result given the current state including the vertex layout specified
531 * with the vertex source.
532 */
533 bool canDisableBlend() const;
534
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000535 /**
536 * Color alpha and coverage are two inputs to the drawing pipeline. For some
537 * blend modes it is safe to fold the coverage into constant or per-vertex
538 * color alpha value. For other blend modes they must be handled separately.
539 * Depending on features available in the underlying 3D API this may or may
540 * not be possible.
541 *
542 * This function looks at the current blend on the draw target and the draw
543 * target's capabilities to determine whether coverage can be handled
544 * correctly.
545 */
546 bool canApplyCoverage() const;
547
548 /**
549 * Determines whether incorporating partial pixel coverage into the constant
550 * color specified by setColor or per-vertex colors will give the right
551 * blending result.
552 */
553 bool canTweakAlphaForCoverage() const {
554 return CanTweakAlphaForCoverage(fCurrDrawState.fDstBlend);
555 }
556
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000557 /**
558 * Determines the interpretation per-vertex edge data when the
559 * kEdge_VertexLayoutBit is set (see below). When per-vertex edges are not
560 * specified the value of this setting has no effect.
561 */
562 void setVertexEdgeType(VertexEdgeType type) {
563 fCurrDrawState.fVertexEdgeType = type;
564 }
565
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000566 /**
bsalomon@google.com471d4712011-08-23 15:45:25 +0000567 * Given the current draw state, vertex layout, and hw support, will HW AA
568 * lines be used (if line primitive type is drawn)? (Note that lines are
569 * always 1 pixel wide)
570 */
571 virtual bool willUseHWAALines() const = 0;
572
573 /**
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000574 * Sets the edge data required for edge antialiasing.
575 *
576 * @param edges 3 * 6 float values, representing the edge
577 * equations in Ax + By + C form
578 */
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000579 void setEdgeAAData(const Edge* edges, int numEdges);
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000580
bsalomon@google.coma3108262011-10-10 14:08:47 +0000581 /**
582 * Used to save and restore the GrGpu's drawing state
583 */
584 struct SavedDrawState {
585 private:
586 DrState fState;
587 friend class GrDrawTarget;
588 };
589
590 /**
591 * Saves the current draw state. The state can be restored at a later time
592 * with restoreDrawState.
593 *
594 * See also AutoStateRestore class.
595 *
596 * @param state will hold the state after the function returns.
597 */
598 void saveCurrentDrawState(SavedDrawState* state) const;
599
600 /**
601 * Restores previously saved draw state. The client guarantees that state
602 * was previously passed to saveCurrentDrawState and that the rendertarget
603 * and texture set at save are still valid.
604 *
605 * See also AutoStateRestore class.
606 *
607 * @param state the previously saved state to restore.
608 */
609 void restoreDrawState(const SavedDrawState& state);
610
611 /**
612 * Copies the draw state from another target to this target.
613 *
614 * @param srcTarget draw target used as src of the draw state.
615 */
616 void copyDrawState(const GrDrawTarget& srcTarget);
617
618 /**
619 * The format of vertices is represented as a bitfield of flags.
620 * Flags that indicate the layout of vertex data. Vertices always contain
621 * positions and may also contain up to kMaxTexCoords sets of 2D texture
622 * coordinates, per-vertex colors, and per-vertex coverage. Each stage can
623 * use any of the texture coordinates as its input texture coordinates or it
624 * may use the positions as texture coordinates.
625 *
626 * If no texture coordinates are specified for a stage then the stage is
627 * disabled.
628 *
629 * Only one type of texture coord can be specified per stage. For
630 * example StageTexCoordVertexLayoutBit(0, 2) and
631 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
632 *
633 * The order in memory is always (position, texture coord 0, ..., color,
634 * coverage) with any unused fields omitted. Note that this means that if
635 * only texture coordinates 1 is referenced then there is no texture
636 * coordinates 0 and the order would be (position, texture coordinate 1
637 * [, color][, coverage]).
638 */
639
640 /**
641 * Generates a bit indicating that a texture stage uses texture coordinates
642 *
643 * @param stage the stage that will use texture coordinates.
644 * @param texCoordIdx the index of the texture coordinates to use
645 *
646 * @return the bit to add to a GrVertexLayout bitfield.
647 */
648 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
649 GrAssert(stage < kNumStages);
650 GrAssert(texCoordIdx < kMaxTexCoords);
651 return 1 << (stage + (texCoordIdx * kNumStages));
652 }
653
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000654private:
655 static const int TEX_COORD_BIT_CNT = kNumStages*kMaxTexCoords;
656public:
657 /**
658 * Generates a bit indicating that a texture stage uses the position
659 * as its texture coordinate.
660 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000661 * @param stage the stage that will use position as texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000662 * coordinates.
663 *
664 * @return the bit to add to a GrVertexLayout bitfield.
665 */
666 static int StagePosAsTexCoordVertexLayoutBit(int stage) {
667 GrAssert(stage < kNumStages);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000668 return (1 << (TEX_COORD_BIT_CNT + stage));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000669 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000670
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000671private:
672 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT + kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000673
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000674public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000675
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000676 /**
677 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000678 */
679 enum VertexLayoutBits {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000680 /* vertices have colors (GrColor) */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000681 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
bsalomon@google.coma3108262011-10-10 14:08:47 +0000682 /* vertices have coverage (GrColor where only the alpha chan is used */
683 kCoverage_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000684 /* Use text vertices. (Pos and tex coords may be a different type for
685 text [GrGpuTextVertex vs GrPoint].) */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000686 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 2),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000687
bsalomon@google.coma3108262011-10-10 14:08:47 +0000688 kEdge_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 3),
reed@google.comac10a2d2010-12-22 21:39:39 +0000689 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000690 kDummyVertexLayoutBit,
691 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000692 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000693 // make sure we haven't exceeded the number of bits in GrVertexLayout.
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000694 GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
reed@google.comac10a2d2010-12-22 21:39:39 +0000695
696 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000697 * There are three methods for specifying geometry (vertices and optionally
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000698 * indices) to the draw target. When indexed drawing the indices and vertices
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000699 * can use a different method. Once geometry is specified it can be used for
700 * multiple drawIndexed and drawNonIndexed calls.
701 *
702 * Sometimes it is necessary to perform a draw while upstack code has
703 * already specified geometry that it isn't finished with. There are push
704 * pop methods
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000705 *
706 * 1. Provide a cpu array (set*SourceToArray). This is useful when the
707 * caller's client has already provided vertex data in a format
708 * the time compatible with a GrVertexLayout. The array must contain the
709 * data at set*SourceToArray is called. The source stays in effect for
710 * drawIndexed & drawNonIndexed calls until set*SourceToArray is called
711 * again or one of the other two paths is chosen.
712 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000713 * 2. Reserve. This is most useful when the caller has data it must
714 * transform before drawing and is not long-lived. The caller requests
715 * that the draw target make room for some amount of vertex and/or index
716 * data. The target provides ptrs to hold the vertex and/or index data.
717 *
718 * The data is writable up until the next drawIndexed, drawNonIndexed,
719 * or pushGeometrySource At this point the data is frozen and the ptrs
720 * are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000721 *
722 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000723 * is long-lived. SetVertexSourceToBuffer and SetIndexSourceToBuffer are
724 * used to set the buffer and subsequent drawIndexed and drawNonIndexed
725 * calls use this source until another source is set.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000726 */
727
728 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000729 * Reserves space for vertices. Draw target will use reserved vertices at
730 * at the next draw.
reed@google.comac10a2d2010-12-22 21:39:39 +0000731 *
732 * If succeeds:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000733 * if vertexCount > 0, *vertices will be the array
reed@google.comac10a2d2010-12-22 21:39:39 +0000734 * of vertices to be filled by caller. The next draw will read
735 * these vertices.
736 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000737 * If a client does not already have a vertex buffer then this is the
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000738 * preferred way to allocate vertex data. It allows the subclass of
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000739 * GrDrawTarget to decide whether to put data in buffers, to group vertex
740 * data that uses the same state (e.g. for deferred rendering), etc.
reed@google.comac10a2d2010-12-22 21:39:39 +0000741 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000742 * After the next draw or pushGeometrySource the vertices ptr is no longer
743 * valid and the geometry data cannot be further modified. The contents
744 * that were put in the reserved space can be drawn by multiple draws,
745 * however.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000746 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000747 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000748 * @param vertexCount the number of vertices to reserve space for. Can be 0.
reed@google.comac10a2d2010-12-22 21:39:39 +0000749 * @param vertices will point to reserved vertex space if vertexCount is
750 * non-zero. Illegal to pass NULL if vertexCount > 0.
reed@google.comac10a2d2010-12-22 21:39:39 +0000751 *
752 * @return true if succeeded in allocating space for the vertices and false
753 * if not.
754 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000755 bool reserveVertexSpace(GrVertexLayout vertexLayout,
756 int vertexCount,
757 void** vertices);
758 /**
759 * Reserves space for indices. Draw target will use the reserved indices at
760 * the next indexed draw.
761 *
762 * If succeeds:
763 * if indexCount > 0, *indices will be the array
764 * of indices to be filled by caller. The next draw will read
765 * these indices.
766 *
767 * If a client does not already have a index buffer then this is the
768 * preferred way to allocate index data. It allows the subclass of
769 * GrDrawTarget to decide whether to put data in buffers, to group index
770 * data that uses the same state (e.g. for deferred rendering), etc.
771 *
772 * After the next indexed draw or pushGeometrySource the indices ptr is no
773 * longer valid and the geometry data cannot be further modified. The
774 * contents that were put in the reserved space can be drawn by multiple
775 * draws, however.
776 *
777 * @param indexCount the number of indices to reserve space for. Can be 0.
778 * @param indices will point to reserved index space if indexCount is
779 * non-zero. Illegal to pass NULL if indexCount > 0.
780 */
781
782 bool reserveIndexSpace(int indexCount, void** indices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000783 /**
784 * Provides hints to caller about the number of vertices and indices
785 * that can be allocated cheaply. This can be useful if caller is reserving
786 * space but doesn't know exactly how much geometry is needed.
787 *
788 * Also may hint whether the draw target should be flushed first. This is
789 * useful for deferred targets.
790 *
791 * @param vertexLayout layout of vertices caller would like to reserve
792 * @param vertexCount in: hint about how many vertices the caller would
793 * like to allocate.
794 * out: a hint about the number of vertices that can be
795 * allocated cheaply. Negative means no hint.
796 * Ignored if NULL.
797 * @param indexCount in: hint about how many indices the caller would
798 * like to allocate.
799 * out: a hint about the number of indices that can be
800 * allocated cheaply. Negative means no hint.
801 * Ignored if NULL.
802 *
803 * @return true if target should be flushed based on the input values.
804 */
805 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000806 int* vertexCount,
807 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000808
809 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000810 * Sets source of vertex data for the next draw. Array must contain
811 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000812 *
813 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000814 * @param size size of the vertex data.
815 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000816 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000817 void setVertexSourceToArray(GrVertexLayout vertexLayout,
818 const void* vertexArray,
819 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000820
821 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000822 * Sets source of index data for the next indexed draw. Array must contain
823 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000824 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000825 * @param array cpu array containing index data.
826 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000827 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000828 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000829
830 /**
831 * Sets source of vertex data for the next draw. Data does not have to be
832 * in the buffer until drawIndexed or drawNonIndexed.
833 *
834 * @param buffer vertex buffer containing vertex data. Must be
835 * unlocked before draw call.
836 * @param vertexLayout layout of the vertex data in the buffer.
837 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000838 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
839 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000840
841 /**
842 * Sets source of index data for the next indexed draw. Data does not have
843 * to be in the buffer until drawIndexed or drawNonIndexed.
844 *
845 * @param buffer index buffer containing indices. Must be unlocked
846 * before indexed draw call.
847 */
848 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000849
850 /**
851 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
852 * source to reserved, array, or buffer before next draw. May be able to free
853 * up temporary storage allocated by setVertexSourceToArray or
854 * reserveVertexSpace.
855 */
856 void resetVertexSource();
857
858 /**
859 * Resets index source. Indexed Drawing from reset indices is illegal. Set
860 * index source to reserved, array, or buffer before next indexed draw. May
861 * be able to free up temporary storage allocated by setIndexSourceToArray
862 * or reserveIndexSpace.
863 */
864 void resetIndexSource();
reed@google.comac10a2d2010-12-22 21:39:39 +0000865
866 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000867 * Pushes and resets the vertex/index sources. Any reserved vertex / index
868 * data is finalized (i.e. cannot be updated after the matching pop but can
869 * be drawn from). Must be balanced by a pop.
870 */
871 void pushGeometrySource();
872
873 /**
874 * Pops the vertex / index sources from the matching push.
875 */
876 void popGeometrySource();
877
878 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000879 * Draws indexed geometry using the current state and current vertex / index
880 * sources.
881 *
882 * @param type The type of primitives to draw.
883 * @param startVertex the vertex in the vertex array/buffer corresponding
884 * to index 0
885 * @param startIndex first index to read from index src.
886 * @param vertexCount one greater than the max index.
887 * @param indexCount the number of index elements to read. The index count
888 * is effectively trimmed to the last completely
889 * specified primitive.
890 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000891 void drawIndexed(GrPrimitiveType type,
892 int startVertex,
893 int startIndex,
894 int vertexCount,
895 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000896
897 /**
898 * Draws non-indexed geometry using the current state and current vertex
899 * sources.
900 *
901 * @param type The type of primitives to draw.
902 * @param startVertex the vertex in the vertex array/buffer corresponding
903 * to index 0
904 * @param vertexCount one greater than the max index.
905 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000906 void drawNonIndexed(GrPrimitiveType type,
907 int startVertex,
908 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000909
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000910 /**
911 * Helper function for drawing rects. This does not use the current index
912 * and vertex sources. After returning, the vertex and index sources may
913 * have changed. They should be reestablished before the next drawIndexed
914 * or drawNonIndexed. This cannot be called between reserving and releasing
915 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000916 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000917 * drawNonIndexed.
918 * @param rect the rect to draw
919 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.comffca4002011-02-22 20:34:01 +0000920 * @param stageEnableBitfield bitmask indicating which stages are enabled.
921 * Bit i indicates whether stage i is enabled.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000922 * @param srcRects specifies rects for stages enabled by stageEnableMask.
923 * if stageEnableMask bit i is 1, srcRects is not NULL,
924 * and srcRects[i] is not NULL, then srcRects[i] will be
925 * used as coordinates for stage i. Otherwise, if stage i
926 * is enabled then rect is used as the coordinates.
927 * @param srcMatrices optional matrices applied to srcRects. If
928 * srcRect[i] is non-NULL and srcMatrices[i] is
929 * non-NULL then srcRect[i] will be transformed by
930 * srcMatrix[i]. srcMatrices can be NULL when no
931 * srcMatrices are desired.
932 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000933 virtual void drawRect(const GrRect& rect,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000934 const GrMatrix* matrix,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000935 StageBitfield stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000936 const GrRect* srcRects[],
937 const GrMatrix* srcMatrices[]);
938
939 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000940 * Helper for drawRect when the caller doesn't need separate src rects or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000941 * matrices.
942 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000943 void drawSimpleRect(const GrRect& rect,
944 const GrMatrix* matrix,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000945 StageBitfield stageEnableBitfield) {
946 drawRect(rect, matrix, stageEnableBitfield, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000947 }
948
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000949 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000950 * Clear the render target. Ignores the clip and all other draw state
951 * (blend mode, stages, etc). Clears the whole thing if rect is NULL,
952 * otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000953 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000954 virtual void clear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000955
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000956 /**
957 * Returns the maximum number of edges that may be specified in a single
958 * draw call when performing edge antialiasing. This is usually limited
959 * by the number of fragment uniforms which may be uploaded. Must be a
960 * minimum of six, since a triangle's vertices each belong to two boundary
961 * edges which may be distinct.
962 */
963 virtual int getMaxEdges() const { return 6; }
964
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000965 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000966
967 class AutoStateRestore : ::GrNoncopyable {
968 public:
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000969 AutoStateRestore();
reed@google.comac10a2d2010-12-22 21:39:39 +0000970 AutoStateRestore(GrDrawTarget* target);
971 ~AutoStateRestore();
972
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000973 /**
974 * if this object is already saving state for param target then
975 * this does nothing. Otherise, it restores previously saved state on
976 * previous target (if any) and saves current state on param target.
977 */
978 void set(GrDrawTarget* target);
979
reed@google.comac10a2d2010-12-22 21:39:39 +0000980 private:
981 GrDrawTarget* fDrawTarget;
982 SavedDrawState fDrawState;
983 };
984
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000985 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000986
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000987 class AutoViewMatrixRestore : ::GrNoncopyable {
988 public:
989 AutoViewMatrixRestore() {
990 fDrawTarget = NULL;
991 }
992
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000993 AutoViewMatrixRestore(GrDrawTarget* target)
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000994 : fDrawTarget(target), fMatrix(fDrawTarget->getViewMatrix()) {
995 GrAssert(NULL != target);
996 }
997
998 void set(GrDrawTarget* target) {
999 GrAssert(NULL != target);
1000 if (NULL != fDrawTarget) {
1001 fDrawTarget->setViewMatrix(fMatrix);
1002 }
1003 fDrawTarget = target;
1004 fMatrix = target->getViewMatrix();
1005 }
1006
1007 ~AutoViewMatrixRestore() {
1008 if (NULL != fDrawTarget) {
1009 fDrawTarget->setViewMatrix(fMatrix);
1010 }
1011 }
1012
1013 private:
1014 GrDrawTarget* fDrawTarget;
1015 GrMatrix fMatrix;
1016 };
1017
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001018 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +00001019
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001020 /**
1021 * Sets the view matrix to I and preconcats all stage matrices enabled in
1022 * mask by the view inverse. Destructor undoes these changes.
1023 */
1024 class AutoDeviceCoordDraw : ::GrNoncopyable {
1025 public:
1026 AutoDeviceCoordDraw(GrDrawTarget* target, int stageMask);
1027 ~AutoDeviceCoordDraw();
1028 private:
1029 GrDrawTarget* fDrawTarget;
1030 GrMatrix fViewMatrix;
1031 GrMatrix fSamplerMatrices[kNumStages];
1032 int fStageMask;
1033 };
1034
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001035 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001036
reed@google.comac10a2d2010-12-22 21:39:39 +00001037 class AutoReleaseGeometry : ::GrNoncopyable {
1038 public:
1039 AutoReleaseGeometry(GrDrawTarget* target,
1040 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001041 int vertexCount,
1042 int indexCount);
1043 AutoReleaseGeometry();
1044 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001045 bool set(GrDrawTarget* target,
1046 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001047 int vertexCount,
1048 int indexCount);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001049 bool succeeded() const { return NULL != fTarget; }
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001050 void* vertices() const { GrAssert(this->succeeded()); return fVertices; }
1051 void* indices() const { GrAssert(this->succeeded()); return fIndices; }
reed@google.comac10a2d2010-12-22 21:39:39 +00001052 GrPoint* positions() const {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001053 return static_cast<GrPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +00001054 }
1055
1056 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001057 void reset();
1058
reed@google.comac10a2d2010-12-22 21:39:39 +00001059 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001060 void* fVertices;
1061 void* fIndices;
1062 };
1063
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001064 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +00001065
1066 class AutoClipRestore : ::GrNoncopyable {
1067 public:
1068 AutoClipRestore(GrDrawTarget* target) {
1069 fTarget = target;
1070 fClip = fTarget->getClip();
1071 }
1072
1073 ~AutoClipRestore() {
1074 fTarget->setClip(fClip);
1075 }
1076 private:
1077 GrDrawTarget* fTarget;
1078 GrClip fClip;
1079 };
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001080
1081 ////////////////////////////////////////////////////////////////////////////
1082
1083 class AutoGeometryPush : ::GrNoncopyable {
1084 public:
1085 AutoGeometryPush(GrDrawTarget* target) {
1086 GrAssert(NULL != target);
1087 fTarget = target;
1088 target->pushGeometrySource();
1089 }
1090 ~AutoGeometryPush() {
1091 fTarget->popGeometrySource();
1092 }
1093 private:
1094 GrDrawTarget* fTarget;
1095 };
reed@google.comac10a2d2010-12-22 21:39:39 +00001096
1097 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001098 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +00001099
reed@google.comac10a2d2010-12-22 21:39:39 +00001100 /**
1101 * Helper function to compute the size of a vertex from a vertex layout
1102 * @return size of a single vertex.
1103 */
1104 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001105
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001106 /**
1107 * Helper function for determining the index of texture coordinates that
1108 * is input for a texture stage. Note that a stage may instead use positions
1109 * as texture coordinates, in which case the result of the function is
1110 * indistinguishable from the case when the stage is disabled.
1111 *
1112 * @param stage the stage to query
1113 * @param vertexLayout layout to query
1114 *
1115 * @return the texture coordinate index or -1 if the stage doesn't use
1116 * separate (non-position) texture coordinates.
1117 */
1118 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +00001119
1120 /**
1121 * Helper function to compute the offset of texture coordinates in a vertex
1122 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +00001123 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001124 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +00001125 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001126 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +00001127
1128 /**
1129 * Helper function to compute the offset of the color in a vertex
1130 * @return offset of color in vertex layout or -1 if the
1131 * layout has no color.
1132 */
1133 static int VertexColorOffset(GrVertexLayout vertexLayout);
1134
bsalomon@google.coma3108262011-10-10 14:08:47 +00001135 /**
1136 * Helper function to compute the offset of the coverage in a vertex
1137 * @return offset of coverage in vertex layout or -1 if the
1138 * layout has no coverage.
1139 */
1140 static int VertexCoverageOffset(GrVertexLayout vertexLayout);
1141
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001142 /**
1143 * Helper function to compute the offset of the edge pts in a vertex
1144 * @return offset of edge in vertex layout or -1 if the
1145 * layout has no edge.
1146 */
1147 static int VertexEdgeOffset(GrVertexLayout vertexLayout);
1148
reed@google.comac10a2d2010-12-22 21:39:39 +00001149 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +00001150 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001151 * coordinates of some index.
1152 *
1153 * @param coordIndex the tex coord index to query
1154 * @param vertexLayout layout to query
1155 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001156 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001157 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +00001158 */
bsalomon@google.com5782d712011-01-21 21:03:59 +00001159 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001160 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001161
reed@google.comac10a2d2010-12-22 21:39:39 +00001162 /**
1163 * Helper function to determine if vertex layout contains either explicit or
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001164 * implicit texture coordinates for a stage.
reed@google.comac10a2d2010-12-22 21:39:39 +00001165 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001166 * @param stage the stage to query
1167 * @param vertexLayout layout to query
1168 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001169 * @return true if vertex specifies texture coordinates for the stage,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001170 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +00001171 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001172 static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +00001173
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001174 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +00001175 * Helper function to compute the size of each vertex and the offsets of
1176 * texture coordinates and color. Determines tex coord offsets by tex coord
1177 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001178 * by StageTexCoordVertexLayoutBit.)
1179 *
1180 * @param vertexLayout the layout to query
1181 * @param texCoordOffsetsByIdx after return it is the offset of each
1182 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +00001183 * index isn't used. (optional)
1184 * @param colorOffset after return it is the offset of the
1185 * color field in each vertex, or -1 if
1186 * there aren't per-vertex colors. (optional)
1187 * @param coverageOffset after return it is the offset of the
1188 * coverage field in each vertex, or -1 if
1189 * there aren't per-vertex coeverages.
1190 * (optional)
1191 * @param edgeOffset after return it is the offset of the
1192 * edge eq field in each vertex, or -1 if
1193 * there aren't per-vertex edge equations.
1194 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001195 * @return size of a single vertex
1196 */
1197 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
1198 int texCoordOffsetsByIdx[kMaxTexCoords],
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001199 int *colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +00001200 int *coverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001201 int* edgeOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001202
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001203 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +00001204 * Helper function to compute the size of each vertex and the offsets of
1205 * texture coordinates and color. Determines tex coord offsets by stage
1206 * rather than by index. (Each stage can be mapped to any t.c. index
1207 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001208 * tex coords then that stage's offset will be 0 (positions are always at 0).
1209 *
1210 * @param vertexLayout the layout to query
1211 * @param texCoordOffsetsByStage after return it is the offset of each
1212 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +00001213 * index isn't used. (optional)
1214 * @param colorOffset after return it is the offset of the
1215 * color field in each vertex, or -1 if
1216 * there aren't per-vertex colors.
1217 * (optional)
1218 * @param coverageOffset after return it is the offset of the
1219 * coverage field in each vertex, or -1 if
1220 * there aren't per-vertex coeverages.
1221 * (optional)
1222 * @param edgeOffset after return it is the offset of the
1223 * edge eq field in each vertex, or -1 if
1224 * there aren't per-vertex edge equations.
1225 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001226 * @return size of a single vertex
1227 */
1228 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
1229 int texCoordOffsetsByStage[kNumStages],
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001230 int *colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +00001231 int *coverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001232 int* edgeOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001233
1234 /**
1235 * Accessing positions, texture coords, or colors, of a vertex within an
1236 * array is a hassle involving casts and simple math. These helpers exist
1237 * to keep GrDrawTarget clients' code a bit nicer looking.
1238 */
1239
1240 /**
1241 * Gets a pointer to a GrPoint of a vertex's position or texture
1242 * coordinate.
1243 * @param vertices the vetex array
1244 * @param vertexIndex the index of the vertex in the array
1245 * @param vertexSize the size of each vertex in the array
1246 * @param offset the offset in bytes of the vertex component.
1247 * Defaults to zero (corresponding to vertex position)
1248 * @return pointer to the vertex component as a GrPoint
1249 */
bsalomon@google.comd302f142011-03-03 13:54:13 +00001250 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001251 int vertexIndex,
1252 int vertexSize,
1253 int offset = 0) {
1254 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001255 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001256 vertexIndex * vertexSize);
1257 }
1258 static const GrPoint* GetVertexPoint(const void* vertices,
1259 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001260 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001261 int offset = 0) {
1262 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001263 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001264 vertexIndex * vertexSize);
1265 }
1266
1267 /**
1268 * Gets a pointer to a GrColor inside a vertex within a vertex array.
1269 * @param vertices the vetex array
1270 * @param vertexIndex the index of the vertex in the array
1271 * @param vertexSize the size of each vertex in the array
1272 * @param offset the offset in bytes of the vertex color
1273 * @return pointer to the vertex component as a GrColor
1274 */
bsalomon@google.comd302f142011-03-03 13:54:13 +00001275 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001276 int vertexIndex,
1277 int vertexSize,
1278 int offset) {
1279 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001280 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001281 vertexIndex * vertexSize);
1282 }
1283 static const GrColor* GetVertexColor(const void* vertices,
1284 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001285 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001286 int offset) {
1287 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001288 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001289 vertexIndex * vertexSize);
1290 }
1291
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001292 static void VertexLayoutUnitTest();
1293
reed@google.comac10a2d2010-12-22 21:39:39 +00001294protected:
bsalomon@google.com471d4712011-08-23 15:45:25 +00001295
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001296 // Determines whether it is correct to apply partial pixel coverage
1297 // by multiplying the src color by the fractional coverage.
1298 static bool CanTweakAlphaForCoverage(GrBlendCoeff dstCoeff);
1299
bsalomon@google.com471d4712011-08-23 15:45:25 +00001300 // determines whether HW blending can be disabled or not
1301 static bool CanDisableBlend(GrVertexLayout layout, const DrState& state);
1302
1303 // determines whether HW AA lines can be used or not
1304 static bool CanUseHWAALines(GrVertexLayout layout, const DrState& state);
1305
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001306 enum GeometrySrcType {
1307 kNone_GeometrySrcType, //<! src has not been specified
1308 kReserved_GeometrySrcType, //<! src was set using reserve*Space
1309 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
1310 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
1311 };
1312
1313 struct GeometrySrcState {
1314 GeometrySrcType fVertexSrc;
1315 union {
1316 // valid if src type is buffer
1317 const GrVertexBuffer* fVertexBuffer;
1318 // valid if src type is reserved or array
1319 int fVertexCount;
1320 };
1321
1322 GeometrySrcType fIndexSrc;
1323 union {
1324 // valid if src type is buffer
1325 const GrIndexBuffer* fIndexBuffer;
1326 // valid if src type is reserved or array
1327 int fIndexCount;
1328 };
1329
1330 GrVertexLayout fVertexLayout;
1331 };
1332
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001333 // given a vertex layout and a draw state, will a stage be used?
1334 static bool StageWillBeUsed(int stage, GrVertexLayout layout,
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001335 const DrState& state) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001336 return NULL != state.fTextures[stage] && VertexUsesStage(stage, layout);
1337 }
1338
1339 bool isStageEnabled(int stage) const {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001340 return StageWillBeUsed(stage, this->getGeomSrc().fVertexLayout,
1341 fCurrDrawState);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001342 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001343
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001344 StageBitfield enabledStages() const {
1345 StageBitfield mask = 0;
1346 for (int s = 0; s < kNumStages; ++s) {
1347 mask |= this->isStageEnabled(s) ? 1 : 0;
1348 }
1349 return mask;
1350 }
1351
reed@google.comac10a2d2010-12-22 21:39:39 +00001352 // Helpers for GrDrawTarget subclasses that won't have private access to
1353 // SavedDrawState but need to peek at the state values.
reed@google.com8195f672011-01-12 18:14:28 +00001354 static DrState& accessSavedDrawState(SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +00001355 { return sds.fState; }
reed@google.com8195f672011-01-12 18:14:28 +00001356 static const DrState& accessSavedDrawState(const SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +00001357 { return sds.fState; }
1358
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001359 // implemented by subclass to allocate space for reserved geom
1360 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
1361 int vertexCount,
1362 void** vertices) = 0;
1363 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
1364 // implemented by subclass to handle release of reserved geom space
1365 virtual void releaseReservedVertexSpace() = 0;
1366 virtual void releaseReservedIndexSpace() = 0;
1367 // subclass must consume array contents when set
1368 virtual void onSetVertexSourceToArray(const void* vertexArray,
1369 int vertexCount) = 0;
1370 virtual void onSetIndexSourceToArray(const void* indexArray,
1371 int indexCount) = 0;
1372 // subclass is notified that geom source will be set away from an array
1373 virtual void releaseVertexArray() = 0;
1374 virtual void releaseIndexArray() = 0;
1375 // subclass overrides to be notified just before geo src state
1376 // is pushed/popped.
1377 virtual void geometrySourceWillPush() = 0;
1378 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
1379 // subclass called to perform drawing
1380 virtual void onDrawIndexed(GrPrimitiveType type,
1381 int startVertex,
1382 int startIndex,
1383 int vertexCount,
1384 int indexCount) = 0;
1385 virtual void onDrawNonIndexed(GrPrimitiveType type,
1386 int startVertex,
1387 int vertexCount) = 0;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001388 // subclass overrides to be notified when clip is set. Must call
1389 // INHERITED::clipwillBeSet
1390 virtual void clipWillBeSet(const GrClip& clip);
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001391
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001392 // Helpers for drawRect, protected so subclasses that override drawRect
1393 // can use them.
bsalomon@google.comffca4002011-02-22 20:34:01 +00001394 static GrVertexLayout GetRectVertexLayout(StageBitfield stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001395 const GrRect* srcRects[]);
1396
1397 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001398 const GrMatrix* matrix,
1399 const GrRect* srcRects[],
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001400 const GrMatrix* srcMatrices[],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001401 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001402 void* vertices);
1403
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001404 // accessor for derived classes
1405 const GeometrySrcState& getGeomSrc() const {
1406 return fGeoSrcStateStack.back();
1407 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001408
1409 GrClip fClip;
1410
reed@google.com8195f672011-01-12 18:14:28 +00001411 DrState fCurrDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001412
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001413 Caps fCaps;
1414
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001415private:
1416 // called when setting a new vert/idx source to unref prev vb/ib
1417 void releasePreviousVertexSource();
1418 void releasePreviousIndexSource();
1419
1420 enum {
1421 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +00001422 };
bsalomon@google.com92669012011-09-27 19:10:05 +00001423 SkSTArray<kPreallocGeoSrcStateStackCnt,
1424 GeometrySrcState, true> fGeoSrcStateStack;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001425
reed@google.comac10a2d2010-12-22 21:39:39 +00001426};
1427
1428#endif