blob: 4bd9eee777d921ca7012cbd4befc26f49c0ddf8c [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"
tomhudson@google.com93813632011-10-27 20:21:16 +000016#include "GrDrawState.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000017#include "GrMatrix.h"
18#include "GrRefCnt.h"
19#include "GrRenderTarget.h"
20#include "GrSamplerState.h"
bsalomon@google.comd302f142011-03-03 13:54:13 +000021#include "GrStencil.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000022#include "GrTexture.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023
Scroggo97c88c22011-05-11 14:05:25 +000024#include "SkXfermode.h"
25
reed@google.comac10a2d2010-12-22 21:39:39 +000026class GrTexture;
reed@google.comac10a2d2010-12-22 21:39:39 +000027class GrClipIterator;
28class GrVertexBuffer;
29class GrIndexBuffer;
30
31class GrDrawTarget : public GrRefCnt {
32public:
33 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000034 * Represents the draw target capabilities.
35 */
36 struct Caps {
37 Caps() { memset(this, 0, sizeof(Caps)); }
38 Caps(const Caps& c) { *this = c; }
39 Caps& operator= (const Caps& c) {
40 memcpy(this, &c, sizeof(Caps));
41 return *this;
42 }
43 void print() const;
44 bool f8BitPaletteSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000045 bool fNPOTTextureTileSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000046 bool fTwoSidedStencilSupport : 1;
47 bool fStencilWrapOpsSupport : 1;
48 bool fHWAALineSupport : 1;
49 bool fShaderSupport : 1;
50 bool fShaderDerivativeSupport : 1;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000051 bool fGeometryShaderSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000052 bool fFSAASupport : 1;
53 bool fDualSourceBlendingSupport : 1;
54 bool fBufferLockSupport : 1;
bsalomon@google.coma3108262011-10-10 14:08:47 +000055 bool fSupportPerVertexCoverage : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000056 int fMaxRenderTargetSize;
57 int fMaxTextureSize;
58 };
59
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +000060 typedef GrDrawState::StageMask StageMask;
reed@google.comac10a2d2010-12-22 21:39:39 +000061
bsalomon@google.com3d0835b2011-12-08 16:12:03 +000062 /**
63 * Flags that affect rendering. Controlled using enable/disableState(). All
64 * default to disabled.
65 */
66 enum StateBits {
67 /**
68 * Perform dithering. TODO: Re-evaluate whether we need this bit
69 */
70 kDither_StateBit = 0x01,
71 /**
72 * Perform HW anti-aliasing. This means either HW FSAA, if supported
73 * by the render target, or smooth-line rendering if a line primitive
74 * is drawn and line smoothing is supported by the 3D API.
75 */
76 kHWAntialias_StateBit = 0x02,
77 /**
78 * Draws will respect the clip, otherwise the clip is ignored.
79 */
80 kClip_StateBit = 0x04,
81 /**
82 * Disables writing to the color buffer. Useful when performing stencil
83 * operations.
84 */
85 kNoColorWrites_StateBit = 0x08,
86 /**
87 * Modifies the behavior of edge AA specified by setEdgeAA. If set,
88 * will test edge pairs for convexity when rasterizing. Set this if the
89 * source polygon is non-convex.
90 */
91 kEdgeAAConcave_StateBit = 0x10,
92 // subclass may use additional bits internally
93 kDummyStateBit,
94 kLastPublicStateBit = kDummyStateBit-1
95 };
96
97 /**
98 * Sets the stencil settings to use for the next draw.
99 * Changing the clip has the side-effect of possibly zeroing
100 * out the client settable stencil bits. So multipass algorithms
101 * using stencil should not change the clip between passes.
102 * @param settings the stencil settings to use.
103 */
104 void setStencil(const GrStencilSettings& settings) {
105 fCurrDrawState.fStencilSettings = settings;
106 }
107
108 /**
109 * Shortcut to disable stencil testing and ops.
110 */
111 void disableStencil() {
112 fCurrDrawState.fStencilSettings.setDisabled();
113 }
114
115public:
reed@google.comac10a2d2010-12-22 21:39:39 +0000116 ///////////////////////////////////////////////////////////////////////////
117
118 GrDrawTarget();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000119 virtual ~GrDrawTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000120
121 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000122 * Gets the capabilities of the draw target.
123 */
124 const Caps& getCaps() const { return fCaps; }
125
126 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000127 * Sets the current clip to the region specified by clip. All draws will be
128 * clipped against this clip if kClip_StateBit is enabled.
129 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000130 * Setting the clip may (or may not) zero out the client's stencil bits.
131 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000132 * @param description of the clipping region
133 */
134 void setClip(const GrClip& clip);
135
136 /**
137 * Gets the current clip.
138 *
139 * @return the clip.
140 */
141 const GrClip& getClip() const;
142
143 /**
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000144 * Sets the texture used at the next drawing call
145 *
146 * @param stage The texture stage for which the texture will be set
147 *
148 * @param texture The texture to set. Can be NULL though there is no advantage
149 * to settings a NULL texture if doing non-textured drawing
150 */
151 void setTexture(int stage, GrTexture* texture);
152
153 /**
154 * Retrieves the currently set texture.
155 *
156 * @return The currently set texture. The return value will be NULL if no
157 * texture has been set, NULL was most recently passed to
158 * setTexture, or the last setTexture was destroyed.
159 */
160 const GrTexture* getTexture(int stage) const;
161 GrTexture* getTexture(int stage);
162
163 /**
164 * Sets the rendertarget used at the next drawing call
165 *
166 * @param target The render target to set.
167 */
168 void setRenderTarget(GrRenderTarget* target);
169
170 /**
171 * Retrieves the currently set rendertarget.
172 *
173 * @return The currently set render target.
174 */
175 const GrRenderTarget* getRenderTarget() const;
176 GrRenderTarget* getRenderTarget();
177
178 /**
179 * Sets the sampler state for a stage used in subsequent draws.
180 *
181 * The sampler state determines how texture coordinates are
182 * intepretted and used to sample the texture.
183 *
184 * @param stage the stage of the sampler to set
185 * @param samplerState Specifies the sampler state.
186 */
187 void setSamplerState(int stage, const GrSamplerState& samplerState);
188
189 /**
190 * Concats the matrix of a stage's sampler.
191 *
192 * @param stage the stage of the sampler to set
193 * @param matrix the matrix to concat
194 */
195 void preConcatSamplerMatrix(int stage, const GrMatrix& matrix) {
196 GrAssert(stage >= 0 && stage < GrDrawState::kNumStages);
197 fCurrDrawState.fSamplerStates[stage].preConcatMatrix(matrix);
198 }
199
200 /**
201 * Shortcut for preConcatSamplerMatrix on all stages in mask with same
202 * matrix
203 */
204 void preConcatSamplerMatrices(StageMask stageMask, const GrMatrix& matrix) {
205 for (int i = 0; i < GrDrawState::kNumStages; ++i) {
206 if ((1 << i) & stageMask) {
207 this->preConcatSamplerMatrix(i, matrix);
208 }
209 }
210 }
211
212 /**
213 * Shortcut for preConcatSamplerMatrix on all enabled stages in mask with
214 * same matrix
215 *
216 * @param stage the stage of the sampler to set
217 * @param matrix the matrix to concat
218 */
219 void preConcatEnabledSamplerMatrices(const GrMatrix& matrix) {
220 StageMask stageMask = this->enabledStages();
221 this->preConcatSamplerMatrices(stageMask, matrix);
222 }
223
224 /**
225 * Gets the matrix of a stage's sampler
226 *
227 * @param stage the stage to of sampler to get
228 * @return the sampler state's matrix
229 */
230 const GrMatrix& getSamplerMatrix(int stage) const {
231 return fCurrDrawState.fSamplerStates[stage].getMatrix();
232 }
233
234 /**
235 * Sets the matrix of a stage's sampler
236 *
237 * @param stage the stage of sampler set
238 * @param matrix the matrix to set
239 */
240 void setSamplerMatrix(int stage, const GrMatrix& matrix) {
241 fCurrDrawState.fSamplerStates[stage].setMatrix(matrix);
242 }
243
244 /**
245 * Sets the matrix applied to veretx positions.
246 *
247 * In the post-view-matrix space the rectangle [0,w]x[0,h]
248 * fully covers the render target. (w and h are the width and height of the
249 * the rendertarget.)
250 *
251 * @param m the matrix used to transform the vertex positions.
252 */
253 void setViewMatrix(const GrMatrix& m);
254
255 /**
256 * Multiplies the current view matrix by a matrix
257 *
258 * After this call V' = V*m where V is the old view matrix,
259 * m is the parameter to this function, and V' is the new view matrix.
260 * (We consider positions to be column vectors so position vector p is
261 * transformed by matrix X as p' = X*p.)
262 *
263 * @param m the matrix used to modify the view matrix.
264 */
265 void preConcatViewMatrix(const GrMatrix& m);
266
267 /**
268 * Multiplies the current view matrix by a matrix
269 *
270 * After this call V' = m*V where V is the old view matrix,
271 * m is the parameter to this function, and V' is the new view matrix.
272 * (We consider positions to be column vectors so position vector p is
273 * transformed by matrix X as p' = X*p.)
274 *
275 * @param m the matrix used to modify the view matrix.
276 */
277 void postConcatViewMatrix(const GrMatrix& m);
278
279 /**
280 * Retrieves the current view matrix
281 * @return the current view matrix.
282 */
283 const GrMatrix& getViewMatrix() const;
284
285 /**
286 * Retrieves the inverse of the current view matrix.
287 *
288 * If the current view matrix is invertible, return true, and if matrix
289 * is non-null, copy the inverse into it. If the current view matrix is
290 * non-invertible, return false and ignore the matrix parameter.
291 *
292 * @param matrix if not null, will receive a copy of the current inverse.
293 */
294 bool getViewInverse(GrMatrix* matrix) const;
295
296 /**
297 * Sets color for next draw to a premultiplied-alpha color.
298 *
299 * @param the color to set.
300 */
301 void setColor(GrColor);
302
303 /**
304 * Gets the currently set color.
305 * @return the current color.
306 */
307 GrColor getColor() const { return fCurrDrawState.fColor; }
308
309 /**
310 * Add a color filter that can be represented by a color and a mode.
311 */
312 void setColorFilter(GrColor, SkXfermode::Mode);
313
314 /**
315 * Sets the color to be used for the next draw to be
316 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
317 *
318 * @param alpha The alpha value to set as the color.
319 */
320 void setAlpha(uint8_t alpha);
321
322 /**
323 * Controls whether clockwise, counterclockwise, or both faces are drawn.
324 * @param face the face(s) to draw.
325 */
326 void setDrawFace(GrDrawState::DrawFace face) {
327 fCurrDrawState.fDrawFace = face;
328 }
329
330 /**
331 * A common pattern is to compute a color with the initial stages and then
332 * modulate that color by a coverage value in later stage(s) (AA, mask-
333 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
334 * computed based on the pre-coverage-modulated color. The division of
335 * stages between color-computing and coverage-computing is specified by
336 * this method. Initially this is GrDrawState::kNumStages (all stages
337 * are color-computing).
338 */
339 void setFirstCoverageStage(int firstCoverageStage) {
340 fCurrDrawState.fFirstCoverageStage = firstCoverageStage;
341 }
342
343 /**
344 * Gets the index of the first coverage-computing stage.
345 */
346 int getFirstCoverageStage() const {
347 return fCurrDrawState.fFirstCoverageStage;
348 }
349
350 /**
351 * Gets whether the target is drawing clockwise, counterclockwise,
352 * or both faces.
353 * @return the current draw face(s).
354 */
355 GrDrawState::DrawFace getDrawFace() const {
356 return fCurrDrawState.fDrawFace;
357 }
358
359 /**
360 * Enable render state settings.
361 *
362 * @param flags bitfield of StateBits specifing the states to enable
363 */
364 void enableState(uint32_t stateBits);
365
366 /**
367 * Disable render state settings.
368 *
369 * @param flags bitfield of StateBits specifing the states to disable
370 */
371 void disableState(uint32_t stateBits);
372
373 bool isDitherState() const {
374 return 0 != (fCurrDrawState.fFlagBits & kDither_StateBit);
375 }
376
377 bool isHWAntialiasState() const {
378 return 0 != (fCurrDrawState.fFlagBits & kHWAntialias_StateBit);
379 }
380
381 bool isClipState() const {
382 return 0 != (fCurrDrawState.fFlagBits & kClip_StateBit);
383 }
384
385 bool isColorWriteDisabled() const {
386 return 0 != (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit);
387 }
388
389 /**
390 * Sets the blending function coeffecients.
391 *
392 * The blend function will be:
393 * D' = sat(S*srcCoef + D*dstCoef)
394 *
395 * where D is the existing destination color, S is the incoming source
396 * color, and D' is the new destination color that will be written. sat()
397 * is the saturation function.
398 *
399 * @param srcCoef coeffecient applied to the src color.
400 * @param dstCoef coeffecient applied to the dst color.
401 */
402 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff);
403
404 /**
405 * Sets the blending function constant referenced by the following blending
406 * coeffecients:
407 * kConstC_BlendCoeff
408 * kIConstC_BlendCoeff
409 * kConstA_BlendCoeff
410 * kIConstA_BlendCoeff
411 *
412 * @param constant the constant to set
413 */
414 void setBlendConstant(GrColor constant) { fCurrDrawState.fBlendConstant = constant; }
415
416 /**
417 * Retrieves the last value set by setBlendConstant()
418 * @return the blending constant value
419 */
420 GrColor getBlendConstant() const { return fCurrDrawState.fBlendConstant; }
421
422 /**
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000423 * Determines if blending will require a read of a dst given the current
424 * state set on the draw target
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000425 *
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000426 * @return true if the dst surface will be read at each pixel hit by the
427 * a draw operation.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000428 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000429 bool drawWillReadDst() const;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000430
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000431 /**
432 * Color alpha and coverage are two inputs to the drawing pipeline. For some
433 * blend modes it is safe to fold the coverage into constant or per-vertex
434 * color alpha value. For other blend modes they must be handled separately.
435 * Depending on features available in the underlying 3D API this may or may
436 * not be possible.
437 *
438 * This function looks at the current blend on the draw target and the draw
439 * target's capabilities to determine whether coverage can be handled
440 * correctly.
441 */
442 bool canApplyCoverage() const;
443
444 /**
445 * Determines whether incorporating partial pixel coverage into the constant
446 * color specified by setColor or per-vertex colors will give the right
447 * blending result.
448 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000449 bool canTweakAlphaForCoverage() const;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000450
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000451 /**
452 * Determines the interpretation per-vertex edge data when the
453 * kEdge_VertexLayoutBit is set (see below). When per-vertex edges are not
454 * specified the value of this setting has no effect.
455 */
456 void setVertexEdgeType(GrDrawState::VertexEdgeType type) {
457 fCurrDrawState.fVertexEdgeType = type;
458 }
459
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000460 /**
bsalomon@google.com471d4712011-08-23 15:45:25 +0000461 * Given the current draw state, vertex layout, and hw support, will HW AA
462 * lines be used (if line primitive type is drawn)? (Note that lines are
463 * always 1 pixel wide)
464 */
tomhudson@google.com93813632011-10-27 20:21:16 +0000465 bool willUseHWAALines() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000466
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000467 /**
468 * Sets the edge data required for edge antialiasing.
469 *
470 * @param edges 3 * 6 float values, representing the edge
471 * equations in Ax + By + C form
472 */
473 void setEdgeAAData(const GrDrawState::Edge* edges, int numEdges);
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000474
bsalomon@google.coma3108262011-10-10 14:08:47 +0000475 /**
476 * Used to save and restore the GrGpu's drawing state
477 */
478 struct SavedDrawState {
479 private:
tomhudson@google.com93813632011-10-27 20:21:16 +0000480 GrDrawState fState;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000481 friend class GrDrawTarget;
482 };
483
484 /**
485 * Saves the current draw state. The state can be restored at a later time
486 * with restoreDrawState.
487 *
488 * See also AutoStateRestore class.
489 *
490 * @param state will hold the state after the function returns.
491 */
492 void saveCurrentDrawState(SavedDrawState* state) const;
493
494 /**
495 * Restores previously saved draw state. The client guarantees that state
496 * was previously passed to saveCurrentDrawState and that the rendertarget
497 * and texture set at save are still valid.
498 *
499 * See also AutoStateRestore class.
500 *
501 * @param state the previously saved state to restore.
502 */
503 void restoreDrawState(const SavedDrawState& state);
504
505 /**
506 * Copies the draw state from another target to this target.
507 *
508 * @param srcTarget draw target used as src of the draw state.
509 */
510 void copyDrawState(const GrDrawTarget& srcTarget);
511
512 /**
513 * The format of vertices is represented as a bitfield of flags.
514 * Flags that indicate the layout of vertex data. Vertices always contain
tomhudson@google.com93813632011-10-27 20:21:16 +0000515 * positions and may also contain up to GrDrawState::kMaxTexCoords sets
516 * of 2D texture * coordinates, per-vertex colors, and per-vertex coverage.
517 * Each stage can
bsalomon@google.coma3108262011-10-10 14:08:47 +0000518 * use any of the texture coordinates as its input texture coordinates or it
519 * may use the positions as texture coordinates.
520 *
521 * If no texture coordinates are specified for a stage then the stage is
522 * disabled.
523 *
524 * Only one type of texture coord can be specified per stage. For
525 * example StageTexCoordVertexLayoutBit(0, 2) and
526 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
527 *
528 * The order in memory is always (position, texture coord 0, ..., color,
529 * coverage) with any unused fields omitted. Note that this means that if
530 * only texture coordinates 1 is referenced then there is no texture
531 * coordinates 0 and the order would be (position, texture coordinate 1
532 * [, color][, coverage]).
533 */
534
535 /**
536 * Generates a bit indicating that a texture stage uses texture coordinates
537 *
538 * @param stage the stage that will use texture coordinates.
539 * @param texCoordIdx the index of the texture coordinates to use
540 *
541 * @return the bit to add to a GrVertexLayout bitfield.
542 */
543 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000544 GrAssert(stage < GrDrawState::kNumStages);
545 GrAssert(texCoordIdx < GrDrawState::kMaxTexCoords);
546 return 1 << (stage + (texCoordIdx * GrDrawState::kNumStages));
bsalomon@google.coma3108262011-10-10 14:08:47 +0000547 }
548
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000549private:
tomhudson@google.com93813632011-10-27 20:21:16 +0000550 static const int TEX_COORD_BIT_CNT = GrDrawState::kNumStages *
551 GrDrawState::kMaxTexCoords;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000552
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000553public:
554 /**
555 * Generates a bit indicating that a texture stage uses the position
556 * as its texture coordinate.
557 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000558 * @param stage the stage that will use position as texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000559 * coordinates.
560 *
561 * @return the bit to add to a GrVertexLayout bitfield.
562 */
563 static int StagePosAsTexCoordVertexLayoutBit(int stage) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000564 GrAssert(stage < GrDrawState::kNumStages);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000565 return (1 << (TEX_COORD_BIT_CNT + stage));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000566 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000567
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000568private:
tomhudson@google.com93813632011-10-27 20:21:16 +0000569 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT +
570 GrDrawState::kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000571
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000572public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000573
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000574 /**
575 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000576 */
577 enum VertexLayoutBits {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000578 /* vertices have colors (GrColor) */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000579 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000580 /* vertices have coverage (GrColor where all channels should have the
581 * same value)
582 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000583 kCoverage_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000584 /* Use text vertices. (Pos and tex coords may be a different type for
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000585 * text [GrGpuTextVertex vs GrPoint].)
586 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000587 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 2),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000588
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000589 /* Each vertex specificies an edge. Distance to the edge is used to
590 * compute a coverage. See setVertexEdgeType().
591 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000592 kEdge_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 3),
reed@google.comac10a2d2010-12-22 21:39:39 +0000593 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000594 kDummyVertexLayoutBit,
595 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000596 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000597 // make sure we haven't exceeded the number of bits in GrVertexLayout.
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000598 GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
reed@google.comac10a2d2010-12-22 21:39:39 +0000599
600 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000601 * There are three methods for specifying geometry (vertices and optionally
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000602 * indices) to the draw target. When indexed drawing the indices and vertices
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000603 * can use a different method. Once geometry is specified it can be used for
604 * multiple drawIndexed and drawNonIndexed calls.
605 *
606 * Sometimes it is necessary to perform a draw while upstack code has
607 * already specified geometry that it isn't finished with. There are push
608 * pop methods
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000609 *
610 * 1. Provide a cpu array (set*SourceToArray). This is useful when the
611 * caller's client has already provided vertex data in a format
612 * the time compatible with a GrVertexLayout. The array must contain the
613 * data at set*SourceToArray is called. The source stays in effect for
614 * drawIndexed & drawNonIndexed calls until set*SourceToArray is called
615 * again or one of the other two paths is chosen.
616 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000617 * 2. Reserve. This is most useful when the caller has data it must
618 * transform before drawing and is not long-lived. The caller requests
619 * that the draw target make room for some amount of vertex and/or index
620 * data. The target provides ptrs to hold the vertex and/or index data.
621 *
622 * The data is writable up until the next drawIndexed, drawNonIndexed,
623 * or pushGeometrySource At this point the data is frozen and the ptrs
624 * are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000625 *
626 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000627 * is long-lived. SetVertexSourceToBuffer and SetIndexSourceToBuffer are
628 * used to set the buffer and subsequent drawIndexed and drawNonIndexed
629 * calls use this source until another source is set.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000630 */
631
632 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000633 * Reserves space for vertices. Draw target will use reserved vertices at
634 * at the next draw.
reed@google.comac10a2d2010-12-22 21:39:39 +0000635 *
636 * If succeeds:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000637 * if vertexCount > 0, *vertices will be the array
reed@google.comac10a2d2010-12-22 21:39:39 +0000638 * of vertices to be filled by caller. The next draw will read
639 * these vertices.
640 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000641 * If a client does not already have a vertex buffer then this is the
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000642 * preferred way to allocate vertex data. It allows the subclass of
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000643 * GrDrawTarget to decide whether to put data in buffers, to group vertex
644 * data that uses the same state (e.g. for deferred rendering), etc.
reed@google.comac10a2d2010-12-22 21:39:39 +0000645 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000646 * After the next draw or pushGeometrySource the vertices ptr is no longer
647 * valid and the geometry data cannot be further modified. The contents
648 * that were put in the reserved space can be drawn by multiple draws,
649 * however.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000650 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000651 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000652 * @param vertexCount the number of vertices to reserve space for. Can be 0.
reed@google.comac10a2d2010-12-22 21:39:39 +0000653 * @param vertices will point to reserved vertex space if vertexCount is
654 * non-zero. Illegal to pass NULL if vertexCount > 0.
reed@google.comac10a2d2010-12-22 21:39:39 +0000655 *
656 * @return true if succeeded in allocating space for the vertices and false
657 * if not.
658 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000659 bool reserveVertexSpace(GrVertexLayout vertexLayout,
660 int vertexCount,
661 void** vertices);
662 /**
663 * Reserves space for indices. Draw target will use the reserved indices at
664 * the next indexed draw.
665 *
666 * If succeeds:
667 * if indexCount > 0, *indices will be the array
668 * of indices to be filled by caller. The next draw will read
669 * these indices.
670 *
671 * If a client does not already have a index buffer then this is the
672 * preferred way to allocate index data. It allows the subclass of
673 * GrDrawTarget to decide whether to put data in buffers, to group index
674 * data that uses the same state (e.g. for deferred rendering), etc.
675 *
676 * After the next indexed draw or pushGeometrySource the indices ptr is no
677 * longer valid and the geometry data cannot be further modified. The
678 * contents that were put in the reserved space can be drawn by multiple
679 * draws, however.
680 *
681 * @param indexCount the number of indices to reserve space for. Can be 0.
682 * @param indices will point to reserved index space if indexCount is
683 * non-zero. Illegal to pass NULL if indexCount > 0.
684 */
685
686 bool reserveIndexSpace(int indexCount, void** indices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000687 /**
688 * Provides hints to caller about the number of vertices and indices
689 * that can be allocated cheaply. This can be useful if caller is reserving
690 * space but doesn't know exactly how much geometry is needed.
691 *
692 * Also may hint whether the draw target should be flushed first. This is
693 * useful for deferred targets.
694 *
695 * @param vertexLayout layout of vertices caller would like to reserve
696 * @param vertexCount in: hint about how many vertices the caller would
697 * like to allocate.
698 * out: a hint about the number of vertices that can be
699 * allocated cheaply. Negative means no hint.
700 * Ignored if NULL.
701 * @param indexCount in: hint about how many indices the caller would
702 * like to allocate.
703 * out: a hint about the number of indices that can be
704 * allocated cheaply. Negative means no hint.
705 * Ignored if NULL.
706 *
707 * @return true if target should be flushed based on the input values.
708 */
709 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000710 int* vertexCount,
711 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000712
713 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000714 * Sets source of vertex data for the next draw. Array must contain
715 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000716 *
717 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000718 * @param size size of the vertex data.
719 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000720 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000721 void setVertexSourceToArray(GrVertexLayout vertexLayout,
722 const void* vertexArray,
723 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000724
725 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000726 * Sets source of index data for the next indexed draw. Array must contain
727 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000728 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000729 * @param array cpu array containing index data.
730 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000731 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000732 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000733
734 /**
735 * Sets source of vertex data for the next draw. Data does not have to be
736 * in the buffer until drawIndexed or drawNonIndexed.
737 *
738 * @param buffer vertex buffer containing vertex data. Must be
739 * unlocked before draw call.
740 * @param vertexLayout layout of the vertex data in the buffer.
741 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000742 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
743 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000744
745 /**
746 * Sets source of index data for the next indexed draw. Data does not have
747 * to be in the buffer until drawIndexed or drawNonIndexed.
748 *
749 * @param buffer index buffer containing indices. Must be unlocked
750 * before indexed draw call.
751 */
752 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000753
754 /**
755 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
756 * source to reserved, array, or buffer before next draw. May be able to free
757 * up temporary storage allocated by setVertexSourceToArray or
758 * reserveVertexSpace.
759 */
760 void resetVertexSource();
761
762 /**
763 * Resets index source. Indexed Drawing from reset indices is illegal. Set
764 * index source to reserved, array, or buffer before next indexed draw. May
765 * be able to free up temporary storage allocated by setIndexSourceToArray
766 * or reserveIndexSpace.
767 */
768 void resetIndexSource();
reed@google.comac10a2d2010-12-22 21:39:39 +0000769
770 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000771 * Pushes and resets the vertex/index sources. Any reserved vertex / index
772 * data is finalized (i.e. cannot be updated after the matching pop but can
773 * be drawn from). Must be balanced by a pop.
774 */
775 void pushGeometrySource();
776
777 /**
778 * Pops the vertex / index sources from the matching push.
779 */
780 void popGeometrySource();
781
782 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000783 * Draws indexed geometry using the current state and current vertex / index
784 * sources.
785 *
786 * @param type The type of primitives to draw.
787 * @param startVertex the vertex in the vertex array/buffer corresponding
788 * to index 0
789 * @param startIndex first index to read from index src.
790 * @param vertexCount one greater than the max index.
791 * @param indexCount the number of index elements to read. The index count
792 * is effectively trimmed to the last completely
793 * specified primitive.
794 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000795 void drawIndexed(GrPrimitiveType type,
796 int startVertex,
797 int startIndex,
798 int vertexCount,
799 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000800
801 /**
802 * Draws non-indexed geometry using the current state and current vertex
803 * sources.
804 *
805 * @param type The type of primitives to draw.
806 * @param startVertex the vertex in the vertex array/buffer corresponding
807 * to index 0
808 * @param vertexCount one greater than the max index.
809 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000810 void drawNonIndexed(GrPrimitiveType type,
811 int startVertex,
812 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000813
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000814 /**
815 * Helper function for drawing rects. This does not use the current index
816 * and vertex sources. After returning, the vertex and index sources may
817 * have changed. They should be reestablished before the next drawIndexed
818 * or drawNonIndexed. This cannot be called between reserving and releasing
819 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000820 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000821 * drawNonIndexed.
822 * @param rect the rect to draw
823 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000824 * @param stageMask bitmask indicating which stages are enabled.
825 * Bit i indicates whether stage i is enabled.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000826 * @param srcRects specifies rects for stages enabled by stageEnableMask.
827 * if stageEnableMask bit i is 1, srcRects is not NULL,
828 * and srcRects[i] is not NULL, then srcRects[i] will be
829 * used as coordinates for stage i. Otherwise, if stage i
830 * is enabled then rect is used as the coordinates.
831 * @param srcMatrices optional matrices applied to srcRects. If
832 * srcRect[i] is non-NULL and srcMatrices[i] is
833 * non-NULL then srcRect[i] will be transformed by
834 * srcMatrix[i]. srcMatrices can be NULL when no
835 * srcMatrices are desired.
836 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000837 virtual void drawRect(const GrRect& rect,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000838 const GrMatrix* matrix,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000839 StageMask stageMask,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000840 const GrRect* srcRects[],
841 const GrMatrix* srcMatrices[]);
842
843 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000844 * Helper for drawRect when the caller doesn't need separate src rects or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000845 * matrices.
846 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000847 void drawSimpleRect(const GrRect& rect,
848 const GrMatrix* matrix,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000849 StageMask stageEnableBitfield) {
bsalomon@google.comffca4002011-02-22 20:34:01 +0000850 drawRect(rect, matrix, stageEnableBitfield, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000851 }
852
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000853 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000854 * Clear the render target. Ignores the clip and all other draw state
855 * (blend mode, stages, etc). Clears the whole thing if rect is NULL,
856 * otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000857 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000858 virtual void clear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000859
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000860 /**
861 * Returns the maximum number of edges that may be specified in a single
862 * draw call when performing edge antialiasing. This is usually limited
863 * by the number of fragment uniforms which may be uploaded. Must be a
864 * minimum of six, since a triangle's vertices each belong to two boundary
865 * edges which may be distinct.
866 */
867 virtual int getMaxEdges() const { return 6; }
868
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000869 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000870
871 class AutoStateRestore : ::GrNoncopyable {
872 public:
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000873 AutoStateRestore();
reed@google.comac10a2d2010-12-22 21:39:39 +0000874 AutoStateRestore(GrDrawTarget* target);
875 ~AutoStateRestore();
876
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000877 /**
878 * if this object is already saving state for param target then
879 * this does nothing. Otherise, it restores previously saved state on
880 * previous target (if any) and saves current state on param target.
881 */
882 void set(GrDrawTarget* target);
883
reed@google.comac10a2d2010-12-22 21:39:39 +0000884 private:
885 GrDrawTarget* fDrawTarget;
886 SavedDrawState fDrawState;
887 };
888
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000889 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000890
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000891 class AutoViewMatrixRestore : ::GrNoncopyable {
892 public:
893 AutoViewMatrixRestore() {
894 fDrawTarget = NULL;
895 }
896
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000897 AutoViewMatrixRestore(GrDrawTarget* target)
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000898 : fDrawTarget(target), fMatrix(fDrawTarget->getViewMatrix()) {
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000899 GrAssert(NULL != target);
900 }
901
902 void set(GrDrawTarget* target) {
903 GrAssert(NULL != target);
904 if (NULL != fDrawTarget) {
905 fDrawTarget->setViewMatrix(fMatrix);
906 }
907 fDrawTarget = target;
908 fMatrix = target->getViewMatrix();
909 }
910
911 ~AutoViewMatrixRestore() {
912 if (NULL != fDrawTarget) {
913 fDrawTarget->setViewMatrix(fMatrix);
914 }
915 }
916
917 private:
918 GrDrawTarget* fDrawTarget;
919 GrMatrix fMatrix;
920 };
921
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000922 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000923
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000924 /**
925 * Sets the view matrix to I and preconcats all stage matrices enabled in
926 * mask by the view inverse. Destructor undoes these changes.
927 */
928 class AutoDeviceCoordDraw : ::GrNoncopyable {
929 public:
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000930 AutoDeviceCoordDraw(GrDrawTarget* target, StageMask stageMask);
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000931 ~AutoDeviceCoordDraw();
932 private:
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000933 GrDrawTarget* fDrawTarget;
934 GrMatrix fViewMatrix;
935 GrMatrix fSamplerMatrices[GrDrawState::kNumStages];
936 int fStageMask;
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000937 };
938
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000939 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000940
reed@google.comac10a2d2010-12-22 21:39:39 +0000941 class AutoReleaseGeometry : ::GrNoncopyable {
942 public:
943 AutoReleaseGeometry(GrDrawTarget* target,
944 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000945 int vertexCount,
946 int indexCount);
947 AutoReleaseGeometry();
948 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000949 bool set(GrDrawTarget* target,
950 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000951 int vertexCount,
952 int indexCount);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000953 bool succeeded() const { return NULL != fTarget; }
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000954 void* vertices() const { GrAssert(this->succeeded()); return fVertices; }
955 void* indices() const { GrAssert(this->succeeded()); return fIndices; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000956 GrPoint* positions() const {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000957 return static_cast<GrPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +0000958 }
959
960 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000961 void reset();
962
reed@google.comac10a2d2010-12-22 21:39:39 +0000963 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000964 void* fVertices;
965 void* fIndices;
966 };
967
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000968 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000969
970 class AutoClipRestore : ::GrNoncopyable {
971 public:
972 AutoClipRestore(GrDrawTarget* target) {
973 fTarget = target;
974 fClip = fTarget->getClip();
975 }
976
977 ~AutoClipRestore() {
978 fTarget->setClip(fClip);
979 }
980 private:
981 GrDrawTarget* fTarget;
982 GrClip fClip;
983 };
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000984
985 ////////////////////////////////////////////////////////////////////////////
986
987 class AutoGeometryPush : ::GrNoncopyable {
988 public:
989 AutoGeometryPush(GrDrawTarget* target) {
990 GrAssert(NULL != target);
991 fTarget = target;
992 target->pushGeometrySource();
993 }
994 ~AutoGeometryPush() {
995 fTarget->popGeometrySource();
996 }
997 private:
998 GrDrawTarget* fTarget;
999 };
reed@google.comac10a2d2010-12-22 21:39:39 +00001000
1001 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001002 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +00001003
reed@google.comac10a2d2010-12-22 21:39:39 +00001004 /**
1005 * Helper function to compute the size of a vertex from a vertex layout
1006 * @return size of a single vertex.
1007 */
1008 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001009
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001010 /**
1011 * Helper function for determining the index of texture coordinates that
1012 * is input for a texture stage. Note that a stage may instead use positions
1013 * as texture coordinates, in which case the result of the function is
1014 * indistinguishable from the case when the stage is disabled.
1015 *
1016 * @param stage the stage to query
1017 * @param vertexLayout layout to query
1018 *
1019 * @return the texture coordinate index or -1 if the stage doesn't use
1020 * separate (non-position) texture coordinates.
1021 */
1022 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +00001023
1024 /**
1025 * Helper function to compute the offset of texture coordinates in a vertex
1026 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +00001027 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001028 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +00001029 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001030 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +00001031
1032 /**
1033 * Helper function to compute the offset of the color in a vertex
1034 * @return offset of color in vertex layout or -1 if the
1035 * layout has no color.
1036 */
1037 static int VertexColorOffset(GrVertexLayout vertexLayout);
1038
bsalomon@google.coma3108262011-10-10 14:08:47 +00001039 /**
1040 * Helper function to compute the offset of the coverage in a vertex
1041 * @return offset of coverage in vertex layout or -1 if the
1042 * layout has no coverage.
1043 */
1044 static int VertexCoverageOffset(GrVertexLayout vertexLayout);
1045
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001046 /**
1047 * Helper function to compute the offset of the edge pts in a vertex
1048 * @return offset of edge in vertex layout or -1 if the
1049 * layout has no edge.
1050 */
1051 static int VertexEdgeOffset(GrVertexLayout vertexLayout);
1052
reed@google.comac10a2d2010-12-22 21:39:39 +00001053 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +00001054 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001055 * coordinates of some index.
1056 *
1057 * @param coordIndex the tex coord index to query
1058 * @param vertexLayout layout to query
1059 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001060 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001061 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +00001062 */
bsalomon@google.com5782d712011-01-21 21:03:59 +00001063 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001064 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001065
reed@google.comac10a2d2010-12-22 21:39:39 +00001066 /**
1067 * Helper function to determine if vertex layout contains either explicit or
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001068 * implicit texture coordinates for a stage.
reed@google.comac10a2d2010-12-22 21:39:39 +00001069 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001070 * @param stage the stage to query
1071 * @param vertexLayout layout to query
1072 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001073 * @return true if vertex specifies texture coordinates for the stage,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001074 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +00001075 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001076 static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +00001077
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001078 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +00001079 * Helper function to compute the size of each vertex and the offsets of
1080 * texture coordinates and color. Determines tex coord offsets by tex coord
1081 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001082 * by StageTexCoordVertexLayoutBit.)
1083 *
1084 * @param vertexLayout the layout to query
1085 * @param texCoordOffsetsByIdx after return it is the offset of each
1086 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +00001087 * index isn't used. (optional)
1088 * @param colorOffset after return it is the offset of the
1089 * color field in each vertex, or -1 if
1090 * there aren't per-vertex colors. (optional)
1091 * @param coverageOffset after return it is the offset of the
1092 * coverage field in each vertex, or -1 if
1093 * there aren't per-vertex coeverages.
1094 * (optional)
1095 * @param edgeOffset after return it is the offset of the
1096 * edge eq field in each vertex, or -1 if
1097 * there aren't per-vertex edge equations.
1098 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001099 * @return size of a single vertex
1100 */
1101 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +00001102 int texCoordOffsetsByIdx[GrDrawState::kMaxTexCoords],
1103 int *colorOffset,
1104 int *coverageOffset,
1105 int* edgeOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001106
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001107 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +00001108 * Helper function to compute the size of each vertex and the offsets of
1109 * texture coordinates and color. Determines tex coord offsets by stage
1110 * rather than by index. (Each stage can be mapped to any t.c. index
1111 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001112 * tex coords then that stage's offset will be 0 (positions are always at 0).
1113 *
1114 * @param vertexLayout the layout to query
1115 * @param texCoordOffsetsByStage after return it is the offset of each
1116 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +00001117 * index isn't used. (optional)
1118 * @param colorOffset after return it is the offset of the
1119 * color field in each vertex, or -1 if
1120 * there aren't per-vertex colors.
1121 * (optional)
1122 * @param coverageOffset after return it is the offset of the
1123 * coverage field in each vertex, or -1 if
1124 * there aren't per-vertex coeverages.
1125 * (optional)
1126 * @param edgeOffset after return it is the offset of the
1127 * edge eq field in each vertex, or -1 if
1128 * there aren't per-vertex edge equations.
1129 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001130 * @return size of a single vertex
1131 */
1132 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +00001133 int texCoordOffsetsByStage[GrDrawState::kNumStages],
1134 int* colorOffset,
1135 int* coverageOffset,
1136 int* edgeOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001137
1138 /**
1139 * Accessing positions, texture coords, or colors, of a vertex within an
1140 * array is a hassle involving casts and simple math. These helpers exist
1141 * to keep GrDrawTarget clients' code a bit nicer looking.
1142 */
1143
1144 /**
1145 * Gets a pointer to a GrPoint of a vertex's position or texture
1146 * coordinate.
1147 * @param vertices the vetex array
1148 * @param vertexIndex the index of the vertex in the array
1149 * @param vertexSize the size of each vertex in the array
1150 * @param offset the offset in bytes of the vertex component.
1151 * Defaults to zero (corresponding to vertex position)
1152 * @return pointer to the vertex component as a GrPoint
1153 */
bsalomon@google.comd302f142011-03-03 13:54:13 +00001154 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001155 int vertexIndex,
1156 int vertexSize,
1157 int offset = 0) {
1158 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001159 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001160 vertexIndex * vertexSize);
1161 }
1162 static const GrPoint* GetVertexPoint(const void* vertices,
1163 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001164 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001165 int offset = 0) {
1166 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001167 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001168 vertexIndex * vertexSize);
1169 }
1170
1171 /**
1172 * Gets a pointer to a GrColor inside a vertex within a vertex array.
1173 * @param vertices the vetex array
1174 * @param vertexIndex the index of the vertex in the array
1175 * @param vertexSize the size of each vertex in the array
1176 * @param offset the offset in bytes of the vertex color
1177 * @return pointer to the vertex component as a GrColor
1178 */
bsalomon@google.comd302f142011-03-03 13:54:13 +00001179 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001180 int vertexIndex,
1181 int vertexSize,
1182 int offset) {
1183 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001184 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001185 vertexIndex * vertexSize);
1186 }
1187 static const GrColor* GetVertexColor(const void* vertices,
1188 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001189 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001190 int offset) {
1191 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001192 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001193 vertexIndex * vertexSize);
1194 }
1195
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001196 static void VertexLayoutUnitTest();
1197
reed@google.comac10a2d2010-12-22 21:39:39 +00001198protected:
bsalomon@google.com471d4712011-08-23 15:45:25 +00001199
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001200 /**
1201 * Optimizations for blending / coverage to be applied based on the current
1202 * state.
1203 * Subclasses that actually draw (as opposed to those that just buffer for
1204 * playback) must implement the flags that replace the output color.
1205 */
1206 enum BlendOptFlags {
1207 /**
1208 * No optimization
1209 */
1210 kNone_BlendOpt = 0,
1211 /**
1212 * Don't draw at all
1213 */
1214 kSkipDraw_BlendOptFlag = 0x2,
1215 /**
1216 * Emit the src color, disable HW blending (replace dst with src)
1217 */
1218 kDisableBlend_BlendOptFlag = 0x4,
1219 /**
1220 * The coverage value does not have to be computed separately from
1221 * alpha, the the output color can be the modulation of the two.
1222 */
1223 kCoverageAsAlpha_BlendOptFlag = 0x1,
1224 /**
1225 * Instead of emitting a src color, emit coverage in the alpha channel
1226 * and r,g,b are "don't cares".
1227 */
1228 kEmitCoverage_BlendOptFlag = 0x10,
1229 /**
1230 * Emit transparent black instead of the src color, no need to compute
1231 * coverage.
1232 */
1233 kEmitTransBlack_BlendOptFlag = 0x8,
1234 };
1235 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
bsalomon@google.com471d4712011-08-23 15:45:25 +00001236
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001237 // Determines what optimizations can be applied based on the blend.
1238 // The coeffecients may have to be tweaked in order for the optimization
1239 // to work. srcCoeff and dstCoeff are optional params that receive the
1240 // tweaked coeffecients.
1241 // Normally the function looks at the current state to see if coverage
1242 // is enabled. By setting forceCoverage the caller can speculatively
1243 // determine the blend optimizations that would be used if there was
1244 // partial pixel coverage
1245 BlendOptFlags getBlendOpts(bool forceCoverage = false,
1246 GrBlendCoeff* srcCoeff = NULL,
1247 GrBlendCoeff* dstCoeff = NULL) const;
1248
1249 // determine if src alpha is guaranteed to be one for all src pixels
1250 bool srcAlphaWillBeOne() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +00001251
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001252 enum GeometrySrcType {
1253 kNone_GeometrySrcType, //<! src has not been specified
1254 kReserved_GeometrySrcType, //<! src was set using reserve*Space
1255 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
1256 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
1257 };
1258
1259 struct GeometrySrcState {
1260 GeometrySrcType fVertexSrc;
1261 union {
1262 // valid if src type is buffer
1263 const GrVertexBuffer* fVertexBuffer;
1264 // valid if src type is reserved or array
1265 int fVertexCount;
1266 };
1267
1268 GeometrySrcType fIndexSrc;
1269 union {
1270 // valid if src type is buffer
1271 const GrIndexBuffer* fIndexBuffer;
1272 // valid if src type is reserved or array
1273 int fIndexCount;
1274 };
1275
1276 GrVertexLayout fVertexLayout;
1277 };
1278
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001279 // given a vertex layout and a draw state, will a stage be used?
1280 static bool StageWillBeUsed(int stage, GrVertexLayout layout,
tomhudson@google.com93813632011-10-27 20:21:16 +00001281 const GrDrawState& state) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001282 return NULL != state.fTextures[stage] && VertexUsesStage(stage, layout);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001283 }
1284
1285 bool isStageEnabled(int stage) const {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001286 return StageWillBeUsed(stage, this->getGeomSrc().fVertexLayout,
1287 fCurrDrawState);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001288 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001289
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001290 StageMask enabledStages() const {
1291 StageMask mask = 0;
tomhudson@google.com93813632011-10-27 20:21:16 +00001292 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001293 mask |= this->isStageEnabled(s) ? 1 : 0;
1294 }
1295 return mask;
1296 }
1297
reed@google.comac10a2d2010-12-22 21:39:39 +00001298 // Helpers for GrDrawTarget subclasses that won't have private access to
1299 // SavedDrawState but need to peek at the state values.
tomhudson@google.com93813632011-10-27 20:21:16 +00001300 static GrDrawState& accessSavedDrawState(SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +00001301 { return sds.fState; }
tomhudson@google.com93813632011-10-27 20:21:16 +00001302 static const GrDrawState& accessSavedDrawState(const SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +00001303 { return sds.fState; }
1304
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001305 // implemented by subclass to allocate space for reserved geom
1306 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
1307 int vertexCount,
1308 void** vertices) = 0;
1309 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
1310 // implemented by subclass to handle release of reserved geom space
1311 virtual void releaseReservedVertexSpace() = 0;
1312 virtual void releaseReservedIndexSpace() = 0;
1313 // subclass must consume array contents when set
1314 virtual void onSetVertexSourceToArray(const void* vertexArray,
1315 int vertexCount) = 0;
1316 virtual void onSetIndexSourceToArray(const void* indexArray,
1317 int indexCount) = 0;
1318 // subclass is notified that geom source will be set away from an array
1319 virtual void releaseVertexArray() = 0;
1320 virtual void releaseIndexArray() = 0;
1321 // subclass overrides to be notified just before geo src state
1322 // is pushed/popped.
1323 virtual void geometrySourceWillPush() = 0;
1324 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
1325 // subclass called to perform drawing
1326 virtual void onDrawIndexed(GrPrimitiveType type,
1327 int startVertex,
1328 int startIndex,
1329 int vertexCount,
1330 int indexCount) = 0;
1331 virtual void onDrawNonIndexed(GrPrimitiveType type,
1332 int startVertex,
1333 int vertexCount) = 0;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001334 // subclass overrides to be notified when clip is set. Must call
1335 // INHERITED::clipwillBeSet
1336 virtual void clipWillBeSet(const GrClip& clip);
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001337
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001338 // Helpers for drawRect, protected so subclasses that override drawRect
1339 // can use them.
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001340 static GrVertexLayout GetRectVertexLayout(StageMask stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001341 const GrRect* srcRects[]);
1342
1343 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001344 const GrMatrix* matrix,
1345 const GrRect* srcRects[],
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001346 const GrMatrix* srcMatrices[],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001347 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001348 void* vertices);
1349
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001350 // accessor for derived classes
1351 const GeometrySrcState& getGeomSrc() const {
1352 return fGeoSrcStateStack.back();
1353 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001354
1355 GrClip fClip;
1356
tomhudson@google.com93813632011-10-27 20:21:16 +00001357 GrDrawState fCurrDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001358
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001359 Caps fCaps;
1360
bsalomon@google.com4a018bb2011-10-28 19:50:21 +00001361 // subclasses must call this in their destructors to ensure all vertex
1362 // and index sources have been released (including those held by
1363 // pushGeometrySource())
1364 void releaseGeometry();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001365private:
bsalomon@google.come8262622011-11-07 02:30:51 +00001366 // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
1367 // indicate non-indexed drawing.
1368 bool checkDraw(GrPrimitiveType type, int startVertex,
1369 int startIndex, int vertexCount,
1370 int indexCount) const;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001371 // called when setting a new vert/idx source to unref prev vb/ib
1372 void releasePreviousVertexSource();
1373 void releasePreviousIndexSource();
1374
1375 enum {
1376 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +00001377 };
bsalomon@google.com92669012011-09-27 19:10:05 +00001378 SkSTArray<kPreallocGeoSrcStateStackCnt,
1379 GeometrySrcState, true> fGeoSrcStateStack;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001380
reed@google.comac10a2d2010-12-22 21:39:39 +00001381};
1382
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001383GR_MAKE_BITFIELD_OPS(GrDrawTarget::BlendOptFlags);
1384
reed@google.comac10a2d2010-12-22 21:39:39 +00001385#endif