epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #ifndef GrDrawTarget_DEFINED |
| 12 | #define GrDrawTarget_DEFINED |
| 13 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 14 | #include "GrClip.h" |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 15 | #include "GrColor.h" |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 16 | #include "GrDrawState.h" |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 17 | #include "GrMatrix.h" |
| 18 | #include "GrRefCnt.h" |
| 19 | #include "GrRenderTarget.h" |
| 20 | #include "GrSamplerState.h" |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 21 | #include "GrStencil.h" |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 22 | #include "GrTexture.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 23 | |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 24 | #include "SkXfermode.h" |
| 25 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 26 | class GrTexture; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 27 | class GrClipIterator; |
| 28 | class GrVertexBuffer; |
| 29 | class GrIndexBuffer; |
| 30 | |
| 31 | class GrDrawTarget : public GrRefCnt { |
| 32 | public: |
| 33 | /** |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 34 | * 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.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 45 | bool fNPOTTextureTileSupport : 1; |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 46 | bool fTwoSidedStencilSupport : 1; |
| 47 | bool fStencilWrapOpsSupport : 1; |
| 48 | bool fHWAALineSupport : 1; |
| 49 | bool fShaderSupport : 1; |
| 50 | bool fShaderDerivativeSupport : 1; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 51 | bool fGeometryShaderSupport : 1; |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 52 | bool fFSAASupport : 1; |
| 53 | bool fDualSourceBlendingSupport : 1; |
| 54 | bool fBufferLockSupport : 1; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 55 | bool fSupportPerVertexCoverage : 1; |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 56 | int fMaxRenderTargetSize; |
| 57 | int fMaxTextureSize; |
| 58 | }; |
| 59 | |
bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 60 | typedef GrDrawState::StageMask StageMask; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 61 | |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame^] | 62 | /** |
| 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 | |
| 115 | public: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 116 | /////////////////////////////////////////////////////////////////////////// |
| 117 | |
| 118 | GrDrawTarget(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 119 | virtual ~GrDrawTarget(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 120 | |
| 121 | /** |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 122 | * Gets the capabilities of the draw target. |
| 123 | */ |
| 124 | const Caps& getCaps() const { return fCaps; } |
| 125 | |
| 126 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 127 | * 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.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 130 | * Setting the clip may (or may not) zero out the client's stencil bits. |
| 131 | * |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 132 | * @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.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame^] | 144 | * 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.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 423 | * Determines if blending will require a read of a dst given the current |
| 424 | * state set on the draw target |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 425 | * |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 426 | * @return true if the dst surface will be read at each pixel hit by the |
| 427 | * a draw operation. |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 428 | */ |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 429 | bool drawWillReadDst() const; |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 430 | |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 431 | /** |
| 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.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 449 | bool canTweakAlphaForCoverage() const; |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 450 | |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame^] | 451 | /** |
| 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.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 460 | /** |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 461 | * 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.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 465 | bool willUseHWAALines() const; |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 466 | |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame^] | 467 | /** |
| 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.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 474 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 475 | /** |
| 476 | * Used to save and restore the GrGpu's drawing state |
| 477 | */ |
| 478 | struct SavedDrawState { |
| 479 | private: |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 480 | GrDrawState fState; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 481 | 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.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 515 | * 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.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 518 | * 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.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 544 | GrAssert(stage < GrDrawState::kNumStages); |
| 545 | GrAssert(texCoordIdx < GrDrawState::kMaxTexCoords); |
| 546 | return 1 << (stage + (texCoordIdx * GrDrawState::kNumStages)); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 547 | } |
| 548 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 549 | private: |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 550 | static const int TEX_COORD_BIT_CNT = GrDrawState::kNumStages * |
| 551 | GrDrawState::kMaxTexCoords; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 552 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 553 | public: |
| 554 | /** |
| 555 | * Generates a bit indicating that a texture stage uses the position |
| 556 | * as its texture coordinate. |
| 557 | * |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 558 | * @param stage the stage that will use position as texture |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 559 | * coordinates. |
| 560 | * |
| 561 | * @return the bit to add to a GrVertexLayout bitfield. |
| 562 | */ |
| 563 | static int StagePosAsTexCoordVertexLayoutBit(int stage) { |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 564 | GrAssert(stage < GrDrawState::kNumStages); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 565 | return (1 << (TEX_COORD_BIT_CNT + stage)); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 566 | } |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 567 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 568 | private: |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 569 | static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT + |
| 570 | GrDrawState::kNumStages; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 571 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 572 | public: |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 573 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 574 | /** |
| 575 | * Additional Bits that can be specified in GrVertexLayout. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 576 | */ |
| 577 | enum VertexLayoutBits { |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 578 | /* vertices have colors (GrColor) */ |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 579 | kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0), |
bsalomon@google.com | e10f6fd | 2011-10-11 20:15:26 +0000 | [diff] [blame] | 580 | /* vertices have coverage (GrColor where all channels should have the |
| 581 | * same value) |
| 582 | */ |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 583 | kCoverage_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1), |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 584 | /* Use text vertices. (Pos and tex coords may be a different type for |
bsalomon@google.com | e10f6fd | 2011-10-11 20:15:26 +0000 | [diff] [blame] | 585 | * text [GrGpuTextVertex vs GrPoint].) |
| 586 | */ |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 587 | kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 2), |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 588 | |
bsalomon@google.com | e10f6fd | 2011-10-11 20:15:26 +0000 | [diff] [blame] | 589 | /* Each vertex specificies an edge. Distance to the edge is used to |
| 590 | * compute a coverage. See setVertexEdgeType(). |
| 591 | */ |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 592 | kEdge_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 3), |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 593 | // for below assert |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 594 | kDummyVertexLayoutBit, |
| 595 | kHighVertexLayoutBit = kDummyVertexLayoutBit - 1 |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 596 | }; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 597 | // make sure we haven't exceeded the number of bits in GrVertexLayout. |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 598 | GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout))); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 599 | |
| 600 | /** |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 601 | * There are three methods for specifying geometry (vertices and optionally |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 602 | * indices) to the draw target. When indexed drawing the indices and vertices |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 603 | * 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.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 609 | * |
| 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.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 617 | * 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.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 625 | * |
| 626 | * 3. Vertex and Index Buffers. This is most useful for geometry that will |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 627 | * 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.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 630 | */ |
| 631 | |
| 632 | /** |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 633 | * Reserves space for vertices. Draw target will use reserved vertices at |
| 634 | * at the next draw. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 635 | * |
| 636 | * If succeeds: |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 637 | * if vertexCount > 0, *vertices will be the array |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 638 | * of vertices to be filled by caller. The next draw will read |
| 639 | * these vertices. |
| 640 | * |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 641 | * If a client does not already have a vertex buffer then this is the |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 642 | * preferred way to allocate vertex data. It allows the subclass of |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 643 | * 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 645 | * |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 646 | * 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.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 650 | * |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 651 | * @param vertexLayout the format of vertices (ignored if vertexCount == 0). |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 652 | * @param vertexCount the number of vertices to reserve space for. Can be 0. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 653 | * @param vertices will point to reserved vertex space if vertexCount is |
| 654 | * non-zero. Illegal to pass NULL if vertexCount > 0. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 655 | * |
| 656 | * @return true if succeeded in allocating space for the vertices and false |
| 657 | * if not. |
| 658 | */ |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 659 | 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 687 | /** |
| 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.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 710 | int* vertexCount, |
| 711 | int* indexCount) const; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 712 | |
| 713 | /** |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 714 | * Sets source of vertex data for the next draw. Array must contain |
| 715 | * the vertex data when this is called. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 716 | * |
| 717 | * @param array cpu array containing vertex data. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 718 | * @param size size of the vertex data. |
| 719 | * @param vertexCount the number of vertices in the array. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 720 | */ |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 721 | void setVertexSourceToArray(GrVertexLayout vertexLayout, |
| 722 | const void* vertexArray, |
| 723 | int vertexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 724 | |
| 725 | /** |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 726 | * Sets source of index data for the next indexed draw. Array must contain |
| 727 | * the indices when this is called. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 728 | * |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 729 | * @param array cpu array containing index data. |
| 730 | * @param indexCount the number of indices in the array. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 731 | */ |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 732 | void setIndexSourceToArray(const void* indexArray, int indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 733 | |
| 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.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 742 | void setVertexSourceToBuffer(GrVertexLayout vertexLayout, |
| 743 | const GrVertexBuffer* buffer); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 744 | |
| 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.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 753 | |
| 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 769 | |
| 770 | /** |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 771 | * 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 783 | * 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.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 795 | void drawIndexed(GrPrimitiveType type, |
| 796 | int startVertex, |
| 797 | int startIndex, |
| 798 | int vertexCount, |
| 799 | int indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 800 | |
| 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.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 810 | void drawNonIndexed(GrPrimitiveType type, |
| 811 | int startVertex, |
| 812 | int vertexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 813 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 814 | /** |
| 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.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 820 | * optimizations if drawRect is used rather than drawIndexed or |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 821 | * drawNonIndexed. |
| 822 | * @param rect the rect to draw |
| 823 | * @param matrix optional matrix applied to rect (before viewMatrix) |
bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 824 | * @param stageMask bitmask indicating which stages are enabled. |
| 825 | * Bit i indicates whether stage i is enabled. |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 826 | * @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.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 837 | virtual void drawRect(const GrRect& rect, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 838 | const GrMatrix* matrix, |
bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 839 | StageMask stageMask, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 840 | const GrRect* srcRects[], |
| 841 | const GrMatrix* srcMatrices[]); |
| 842 | |
| 843 | /** |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 844 | * Helper for drawRect when the caller doesn't need separate src rects or |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 845 | * matrices. |
| 846 | */ |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 847 | void drawSimpleRect(const GrRect& rect, |
| 848 | const GrMatrix* matrix, |
bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 849 | StageMask stageEnableBitfield) { |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 850 | drawRect(rect, matrix, stageEnableBitfield, NULL, NULL); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 851 | } |
| 852 | |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 853 | /** |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 854 | * 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.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 857 | */ |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 858 | virtual void clear(const GrIRect* rect, GrColor color) = 0; |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 859 | |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 860 | /** |
| 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.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 869 | //////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 870 | |
| 871 | class AutoStateRestore : ::GrNoncopyable { |
| 872 | public: |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 873 | AutoStateRestore(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 874 | AutoStateRestore(GrDrawTarget* target); |
| 875 | ~AutoStateRestore(); |
| 876 | |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 877 | /** |
| 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 884 | private: |
| 885 | GrDrawTarget* fDrawTarget; |
| 886 | SavedDrawState fDrawState; |
| 887 | }; |
| 888 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 889 | //////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 890 | |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 891 | class AutoViewMatrixRestore : ::GrNoncopyable { |
| 892 | public: |
| 893 | AutoViewMatrixRestore() { |
| 894 | fDrawTarget = NULL; |
| 895 | } |
| 896 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 897 | AutoViewMatrixRestore(GrDrawTarget* target) |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame^] | 898 | : fDrawTarget(target), fMatrix(fDrawTarget->getViewMatrix()) { |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 899 | 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.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 922 | //////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 923 | |
bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 924 | /** |
| 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.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame^] | 930 | AutoDeviceCoordDraw(GrDrawTarget* target, StageMask stageMask); |
bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 931 | ~AutoDeviceCoordDraw(); |
| 932 | private: |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame^] | 933 | GrDrawTarget* fDrawTarget; |
| 934 | GrMatrix fViewMatrix; |
| 935 | GrMatrix fSamplerMatrices[GrDrawState::kNumStages]; |
| 936 | int fStageMask; |
bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 937 | }; |
| 938 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 939 | //////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 940 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 941 | class AutoReleaseGeometry : ::GrNoncopyable { |
| 942 | public: |
| 943 | AutoReleaseGeometry(GrDrawTarget* target, |
| 944 | GrVertexLayout vertexLayout, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 945 | int vertexCount, |
| 946 | int indexCount); |
| 947 | AutoReleaseGeometry(); |
| 948 | ~AutoReleaseGeometry(); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 949 | bool set(GrDrawTarget* target, |
| 950 | GrVertexLayout vertexLayout, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 951 | int vertexCount, |
| 952 | int indexCount); |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 953 | bool succeeded() const { return NULL != fTarget; } |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 954 | void* vertices() const { GrAssert(this->succeeded()); return fVertices; } |
| 955 | void* indices() const { GrAssert(this->succeeded()); return fIndices; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 956 | GrPoint* positions() const { |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 957 | return static_cast<GrPoint*>(this->vertices()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | private: |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 961 | void reset(); |
| 962 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 963 | GrDrawTarget* fTarget; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 964 | void* fVertices; |
| 965 | void* fIndices; |
| 966 | }; |
| 967 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 968 | //////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 969 | |
| 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.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 984 | |
| 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1000 | |
| 1001 | //////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1002 | // Helpers for picking apart vertex layouts |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1003 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1004 | /** |
| 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.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1009 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1010 | /** |
| 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1023 | |
| 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.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1027 | * layout has no texture coordinates. Will be 0 if positions are |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1028 | * used as texture coordinates for the stage. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1029 | */ |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1030 | static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1031 | |
| 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.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1039 | /** |
| 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.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1046 | /** |
| 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1053 | /** |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1054 | * Helper function to determine if vertex layout contains explicit texture |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1055 | * coordinates of some index. |
| 1056 | * |
| 1057 | * @param coordIndex the tex coord index to query |
| 1058 | * @param vertexLayout layout to query |
| 1059 | * |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1060 | * @return true if vertex specifies texture coordinates for the index, |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1061 | * false otherwise. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1062 | */ |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1063 | static bool VertexUsesTexCoordIdx(int coordIndex, |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1064 | GrVertexLayout vertexLayout); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1065 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1066 | /** |
| 1067 | * Helper function to determine if vertex layout contains either explicit or |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1068 | * implicit texture coordinates for a stage. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1069 | * |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1070 | * @param stage the stage to query |
| 1071 | * @param vertexLayout layout to query |
| 1072 | * |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1073 | * @return true if vertex specifies texture coordinates for the stage, |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1074 | * false otherwise. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1075 | */ |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1076 | static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1077 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1078 | /** |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1079 | * 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.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1082 | * 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.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1087 | * 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.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1099 | * @return size of a single vertex |
| 1100 | */ |
| 1101 | static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout, |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1102 | int texCoordOffsetsByIdx[GrDrawState::kMaxTexCoords], |
| 1103 | int *colorOffset, |
| 1104 | int *coverageOffset, |
| 1105 | int* edgeOffset); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1106 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1107 | /** |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1108 | * 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.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1112 | * 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.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1117 | * 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.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1130 | * @return size of a single vertex |
| 1131 | */ |
| 1132 | static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout, |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1133 | int texCoordOffsetsByStage[GrDrawState::kNumStages], |
| 1134 | int* colorOffset, |
| 1135 | int* coverageOffset, |
| 1136 | int* edgeOffset); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1137 | |
| 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.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1154 | static GrPoint* GetVertexPoint(void* vertices, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1155 | int vertexIndex, |
| 1156 | int vertexSize, |
| 1157 | int offset = 0) { |
| 1158 | intptr_t start = GrTCast<intptr_t>(vertices); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1159 | return GrTCast<GrPoint*>(start + offset + |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1160 | vertexIndex * vertexSize); |
| 1161 | } |
| 1162 | static const GrPoint* GetVertexPoint(const void* vertices, |
| 1163 | int vertexIndex, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1164 | int vertexSize, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1165 | int offset = 0) { |
| 1166 | intptr_t start = GrTCast<intptr_t>(vertices); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1167 | return GrTCast<const GrPoint*>(start + offset + |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1168 | 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.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1179 | static GrColor* GetVertexColor(void* vertices, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1180 | int vertexIndex, |
| 1181 | int vertexSize, |
| 1182 | int offset) { |
| 1183 | intptr_t start = GrTCast<intptr_t>(vertices); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1184 | return GrTCast<GrColor*>(start + offset + |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1185 | vertexIndex * vertexSize); |
| 1186 | } |
| 1187 | static const GrColor* GetVertexColor(const void* vertices, |
| 1188 | int vertexIndex, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1189 | int vertexSize, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1190 | int offset) { |
| 1191 | const intptr_t start = GrTCast<intptr_t>(vertices); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1192 | return GrTCast<const GrColor*>(start + offset + |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1193 | vertexIndex * vertexSize); |
| 1194 | } |
| 1195 | |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 1196 | static void VertexLayoutUnitTest(); |
| 1197 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1198 | protected: |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 1199 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 1200 | /** |
| 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.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 1236 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 1237 | // 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.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 1251 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1252 | 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.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 1279 | // given a vertex layout and a draw state, will a stage be used? |
| 1280 | static bool StageWillBeUsed(int stage, GrVertexLayout layout, |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1281 | const GrDrawState& state) { |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame^] | 1282 | return NULL != state.fTextures[stage] && VertexUsesStage(stage, layout); |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | bool isStageEnabled(int stage) const { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1286 | return StageWillBeUsed(stage, this->getGeomSrc().fVertexLayout, |
| 1287 | fCurrDrawState); |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 1288 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1289 | |
bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 1290 | StageMask enabledStages() const { |
| 1291 | StageMask mask = 0; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1292 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1293 | mask |= this->isStageEnabled(s) ? 1 : 0; |
| 1294 | } |
| 1295 | return mask; |
| 1296 | } |
| 1297 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1298 | // Helpers for GrDrawTarget subclasses that won't have private access to |
| 1299 | // SavedDrawState but need to peek at the state values. |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1300 | static GrDrawState& accessSavedDrawState(SavedDrawState& sds) |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1301 | { return sds.fState; } |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1302 | static const GrDrawState& accessSavedDrawState(const SavedDrawState& sds) |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1303 | { return sds.fState; } |
| 1304 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1305 | // 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.com | dea2f8d | 2011-08-01 15:51:05 +0000 | [diff] [blame] | 1334 | // subclass overrides to be notified when clip is set. Must call |
| 1335 | // INHERITED::clipwillBeSet |
| 1336 | virtual void clipWillBeSet(const GrClip& clip); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 1337 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1338 | // Helpers for drawRect, protected so subclasses that override drawRect |
| 1339 | // can use them. |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame^] | 1340 | static GrVertexLayout GetRectVertexLayout(StageMask stageEnableBitfield, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1341 | const GrRect* srcRects[]); |
| 1342 | |
| 1343 | static void SetRectVertices(const GrRect& rect, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1344 | const GrMatrix* matrix, |
| 1345 | const GrRect* srcRects[], |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1346 | const GrMatrix* srcMatrices[], |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1347 | GrVertexLayout layout, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1348 | void* vertices); |
| 1349 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1350 | // accessor for derived classes |
| 1351 | const GeometrySrcState& getGeomSrc() const { |
| 1352 | return fGeoSrcStateStack.back(); |
| 1353 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1354 | |
| 1355 | GrClip fClip; |
| 1356 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1357 | GrDrawState fCurrDrawState; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1358 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 1359 | Caps fCaps; |
| 1360 | |
bsalomon@google.com | 4a018bb | 2011-10-28 19:50:21 +0000 | [diff] [blame] | 1361 | // 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.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1365 | private: |
bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 1366 | // 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.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1371 | // 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1377 | }; |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 1378 | SkSTArray<kPreallocGeoSrcStateStackCnt, |
| 1379 | GeometrySrcState, true> fGeoSrcStateStack; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1380 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1381 | }; |
| 1382 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 1383 | GR_MAKE_BITFIELD_OPS(GrDrawTarget::BlendOptFlags); |
| 1384 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1385 | #endif |