reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame^] | 1 | /* |
| 2 | Copyright 2010 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #ifndef GrDrawTarget_DEFINED |
| 19 | #define GrDrawTarget_DEFINED |
| 20 | |
| 21 | #include "GrScalar.h" |
| 22 | #include "GrMatrix.h" |
| 23 | #include "GrColor.h" |
| 24 | #include "GrRefCnt.h" |
| 25 | #include "GrSamplerState.h" |
| 26 | #include "GrClip.h" |
| 27 | |
| 28 | class GrTexture; |
| 29 | class GrRenderTarget; |
| 30 | class GrClipIterator; |
| 31 | class GrVertexBuffer; |
| 32 | class GrIndexBuffer; |
| 33 | |
| 34 | class GrDrawTarget : public GrRefCnt { |
| 35 | public: |
| 36 | /** |
| 37 | * Geometric primitives used for drawing. |
| 38 | */ |
| 39 | enum PrimitiveType { |
| 40 | kTriangles_PrimitiveType, |
| 41 | kTriangleStrip_PrimitiveType, |
| 42 | kTriangleFan_PrimitiveType, |
| 43 | kPoints_PrimitiveType, |
| 44 | kLines_PrimitiveType, |
| 45 | kLineStrip_PrimitiveType |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * Flags that affect rendering. Controlled using enable/disableState(). All |
| 50 | * default to disabled. |
| 51 | */ |
| 52 | enum StateBits { |
| 53 | kDither_StateBit = 0x1,//<! Perform color dithering |
| 54 | kAntialias_StateBit = 0x2,//<! Perform anti-aliasing. The render- |
| 55 | // target must support some form of AA |
| 56 | // (msaa, coverage sampling, etc). For |
| 57 | // GrGpu-created rendertarget/textures |
| 58 | // this is controlled by parameters |
| 59 | // passed to createTexture. |
| 60 | kClip_StateBit = 0x4,//<! Controls whether drawing is clipped |
| 61 | // against the region specified by |
| 62 | // setClip. |
| 63 | }; |
| 64 | |
| 65 | /** |
| 66 | * Coeffecients for alpha-blending. |
| 67 | */ |
| 68 | enum BlendCoeff { |
| 69 | kZero_BlendCoeff, //<! 0 |
| 70 | kOne_BlendCoeff, //<! 1 |
| 71 | kSC_BlendCoeff, //<! src color |
| 72 | kISC_BlendCoeff, //<! one minus src color |
| 73 | kDC_BlendCoeff, //<! dst color |
| 74 | kIDC_BlendCoeff, //<! one minus dst color |
| 75 | kSA_BlendCoeff, //<! src alpha |
| 76 | kISA_BlendCoeff, //<! one minus src alpha |
| 77 | kDA_BlendCoeff, //<! dst alpha |
| 78 | kIDA_BlendCoeff, //<! one minus dst alpha |
| 79 | }; |
| 80 | |
| 81 | /** |
| 82 | * StencilPass |
| 83 | * |
| 84 | * Sets the stencil state for subsequent draw calls. Used to fill paths. |
| 85 | * |
| 86 | * Winding requires two passes when the GPU/API doesn't support separate |
| 87 | * stencil. |
| 88 | * |
| 89 | * The color pass for path fill is used to zero out stencil bits used for |
| 90 | * path filling. Every pixel covere by a winding/EO stencil pass must get |
| 91 | * covered by the color pass in order to leave stencil buffer in the correct |
| 92 | * state for the next path draw. |
| 93 | * |
| 94 | * NOTE: Stencil-based Winding fill has alias-to-zero problems. (e.g. A |
| 95 | * winding count of 128,256,512,etc with a 8 bit stencil buffer |
| 96 | * will be unfilled) |
| 97 | */ |
| 98 | enum StencilPass { |
| 99 | kNone_StencilPass, //<! Not drawing a path or clip. |
| 100 | kEvenOddStencil_StencilPass, //<! records in/out in stencil buffer |
| 101 | // using the Even/Odd fill rule. |
| 102 | kEvenOddColor_StencilPass, //<! writes colors to color target in |
| 103 | // pixels marked inside the fill by |
| 104 | // kEOFillStencil_StencilPass. Clears |
| 105 | // stencil in pixels covered by |
| 106 | // geometry. |
| 107 | kWindingStencil1_StencilPass, //<! records in/out in stencil buffer |
| 108 | // using the Winding fill rule. |
| 109 | kWindingStencil2_StencilPass, //<! records in/out in stencil buffer |
| 110 | // using the Winding fill rule. |
| 111 | // Run when single-stencil-pass winding |
| 112 | // not supported (i.e. no separate |
| 113 | // stencil support) |
| 114 | kWindingColor_StencilPass, //<! writes colors to color target in |
| 115 | // pixels marked inside the fill by |
| 116 | // kWindFillStencil_StencilPass. Clears |
| 117 | // stencil in pixels covered by |
| 118 | // geometry. |
| 119 | kDrawTargetCount_StencilPass //<! Subclass may extend this enum to use |
| 120 | // the stencil for other purposes (e.g. |
| 121 | // to do stencil-based clipping) |
| 122 | // This value is provided as basis for |
| 123 | // defining these extended enum values. |
| 124 | }; |
| 125 | |
| 126 | protected: |
| 127 | enum MatrixMode { |
| 128 | kModelView_MatrixMode = 0, |
| 129 | kTexture_MatrixMode, |
| 130 | |
| 131 | kMatrixModeCount |
| 132 | }; |
| 133 | |
| 134 | struct DrawState { |
| 135 | uint32_t fFlagBits; |
| 136 | BlendCoeff fSrcBlend; |
| 137 | BlendCoeff fDstBlend; |
| 138 | GrTexture* fTexture; |
| 139 | GrSamplerState fSamplerState; |
| 140 | GrRenderTarget* fRenderTarget; |
| 141 | GrColor fColor; |
| 142 | float fPointSize; |
| 143 | StencilPass fStencilPass; |
| 144 | bool fReverseFill; |
| 145 | GrMatrix fMatrixModeCache[kMatrixModeCount]; |
| 146 | bool operator ==(const DrawState& s) const { |
| 147 | return 0 == memcmp(this, &s, sizeof(DrawState)); |
| 148 | } |
| 149 | bool operator !=(const DrawState& s) const { return !(*this == s); } |
| 150 | }; |
| 151 | |
| 152 | public: |
| 153 | /////////////////////////////////////////////////////////////////////////// |
| 154 | |
| 155 | GrDrawTarget(); |
| 156 | |
| 157 | /** |
| 158 | * Sets the current clip to the region specified by clip. All draws will be |
| 159 | * clipped against this clip if kClip_StateBit is enabled. |
| 160 | * |
| 161 | * @param description of the clipping region |
| 162 | */ |
| 163 | void setClip(const GrClip& clip); |
| 164 | |
| 165 | /** |
| 166 | * Gets the current clip. |
| 167 | * |
| 168 | * @return the clip. |
| 169 | */ |
| 170 | const GrClip& getClip() const; |
| 171 | |
| 172 | /** |
| 173 | * Sets the texture used at the next drawing call |
| 174 | * |
| 175 | * @param texture The texture to set. Can be NULL though there is no advantage |
| 176 | * to settings a NULL texture if doing non-textured drawing |
| 177 | */ |
| 178 | void setTexture(GrTexture* texture); |
| 179 | |
| 180 | /** |
| 181 | * Retrieves the currently set texture. |
| 182 | * |
| 183 | * @return The currently set texture. The return value will be NULL if no |
| 184 | * texture has been set, NULL was most recently passed to |
| 185 | * setTexture, or the last setTexture was destroyed. |
| 186 | */ |
| 187 | GrTexture* currentTexture() const; |
| 188 | |
| 189 | /** |
| 190 | * Sets the rendertarget used at the next drawing call |
| 191 | * |
| 192 | * @param target The render target to set. Must be a valid rendertarget. |
| 193 | * That is it is a value that was returned by |
| 194 | * currentRenderTarget() or GrTexture::asRenderTarget(). |
| 195 | */ |
| 196 | void setRenderTarget(GrRenderTarget* target); |
| 197 | |
| 198 | /** |
| 199 | * Retrieves the currently set rendertarget. |
| 200 | * |
| 201 | * @return The currently set render target. |
| 202 | */ |
| 203 | GrRenderTarget* currentRenderTarget() const; |
| 204 | |
| 205 | /** |
| 206 | * Sets the sampler state for the next draw. |
| 207 | * |
| 208 | * The sampler state determines the address wrap modes and |
| 209 | * filtering |
| 210 | * |
| 211 | * @param samplerState Specifies the sampler state. |
| 212 | */ |
| 213 | void setSamplerState(const GrSamplerState& samplerState); |
| 214 | |
| 215 | /** |
| 216 | * Sets the matrix applied to texture coordinates. |
| 217 | * |
| 218 | * The post-matrix texture coordinates in the square [0,1]^2 cover the |
| 219 | * entire area of the texture. This means the full POT width when a NPOT |
| 220 | * texture is embedded in a POT width texture to meet the 3D API |
| 221 | * requirements. The texture matrix is applied both when the texture |
| 222 | * coordinates are explicit and when vertex positions are used as texture |
| 223 | * coordinates. In the latter case the texture matrix is applied to the |
| 224 | * pre-modelview position values. |
| 225 | * |
| 226 | * @param m the matrix used to transform the texture coordinates. |
| 227 | */ |
| 228 | void setTextureMatrix(const GrMatrix& m) { |
| 229 | this->loadMatrix(m, kTexture_MatrixMode); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Sets the matrix applied to veretx positions. |
| 234 | * |
| 235 | * In the post-view-matrix space the rectangle [0,w]x[0,h] |
| 236 | * fully covers the render target. (w and h are the width and height of the |
| 237 | * the rendertarget.) |
| 238 | * |
| 239 | * @param m the matrix used to transform the vertex positions. |
| 240 | */ |
| 241 | void setViewMatrix(const GrMatrix& m) { |
| 242 | this->loadMatrix(m, kModelView_MatrixMode); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Multiplies the current view matrix by a matrix |
| 247 | * |
| 248 | * After this call V' = V*m where V is the old view matrix, |
| 249 | * m is the parameter to this function, and V' is the new view matrix. |
| 250 | * (We consider positions to be column vectors so position vector p is |
| 251 | * transformed by matrix X as p' = X*p.) |
| 252 | * |
| 253 | * @param m the matrix used to modify the modelview matrix. |
| 254 | */ |
| 255 | void concatViewMatrix(const GrMatrix& m); |
| 256 | |
| 257 | /** |
| 258 | * Sets color for next draw to a premultiplied-alpha color. |
| 259 | * |
| 260 | * @param the color to set. |
| 261 | */ |
| 262 | void setColor(GrColor); |
| 263 | |
| 264 | /** |
| 265 | * Sets the color to be used for the next draw to be |
| 266 | * (r,g,b,a) = (alpha, alpha, alpha, alpha). |
| 267 | * |
| 268 | * @param alpha The alpha value to set as the color. |
| 269 | */ |
| 270 | void setAlpha(uint8_t alpha); |
| 271 | |
| 272 | /** |
| 273 | * Sets pass for path rendering |
| 274 | * |
| 275 | * @param pass of path rendering |
| 276 | */ |
| 277 | void setStencilPass(StencilPass pass); |
| 278 | |
| 279 | /** |
| 280 | * Reveses the in/out decision of the fill rule for path rendering. |
| 281 | * Only affects kEOFillColor_StencilPass and kWindingFillColor_StencilPass |
| 282 | * |
| 283 | * @param reverse true to reverse, false otherwise |
| 284 | */ |
| 285 | void setReverseFill(bool reverse); |
| 286 | |
| 287 | /** |
| 288 | * Enable render state settings. |
| 289 | * |
| 290 | * @param flags bitfield of StateBits specifing the states to enable |
| 291 | */ |
| 292 | void enableState(uint32_t stateBits); |
| 293 | |
| 294 | /** |
| 295 | * Disable render state settings. |
| 296 | * |
| 297 | * @param flags bitfield of StateBits specifing the states to disable |
| 298 | */ |
| 299 | void disableState(uint32_t stateBits); |
| 300 | |
| 301 | bool isDitherState() const { |
| 302 | return fCurrDrawState.fFlagBits & kDither_StateBit; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Sets the size of points used the next time points are drawn. |
| 307 | * |
| 308 | * @param the point size |
| 309 | */ |
| 310 | void setPointSize(float size); |
| 311 | |
| 312 | /** |
| 313 | * Sets the blending function coeffecients. |
| 314 | * |
| 315 | * The blend function will be: |
| 316 | * D' = sat(S*srcCoef + D*dstCoef) |
| 317 | * |
| 318 | * where D is the existing destination color, S is the incoming source |
| 319 | * color, and D' is the new destination color that will be written. sat() |
| 320 | * is the saturation function. |
| 321 | * |
| 322 | * @param srcCoef coeffecient applied to the src color. |
| 323 | * @param dstCoef coeffecient applied to the dst color. |
| 324 | */ |
| 325 | void setBlendFunc(BlendCoeff srcCoef, BlendCoeff dstCoef); |
| 326 | |
| 327 | /** |
| 328 | * Retrieves the current view matrix |
| 329 | * @param matrix will be the current view matrix after return. |
| 330 | */ |
| 331 | void getViewMatrix(GrMatrix* matrix) const; |
| 332 | |
| 333 | /** |
| 334 | * Retrieves the inverse of the current view matrix. |
| 335 | * |
| 336 | * If the current view matrix is invertible, return true, and if matrix |
| 337 | * is non-null, copy the inverse into it. If the current view matrix is |
| 338 | * non-invertible, return false and ignore the matrix parameter. |
| 339 | * |
| 340 | * @param matrix if not null, will receive a copy of the current inverse. |
| 341 | */ |
| 342 | bool getViewInverse(GrMatrix* matrix) const; |
| 343 | |
| 344 | /** |
| 345 | * Used to save and restore the GrGpu's drawing state |
| 346 | */ |
| 347 | struct SavedDrawState { |
| 348 | private: |
| 349 | DrawState fState; |
| 350 | friend class GrDrawTarget; |
| 351 | }; |
| 352 | |
| 353 | /** |
| 354 | * Saves the current draw state. The state can be restored at a later time |
| 355 | * with restoreDrawState. |
| 356 | * |
| 357 | * See also AutoStateRestore class. |
| 358 | * |
| 359 | * @param state will hold the state after the function returns. |
| 360 | */ |
| 361 | void saveCurrentDrawState(SavedDrawState* state) const; |
| 362 | |
| 363 | /** |
| 364 | * Restores previously saved draw state. The client guarantees that state |
| 365 | * was previously passed to saveCurrentDrawState and that the rendertarget |
| 366 | * and texture set at save are still valid. |
| 367 | * |
| 368 | * See also AutoStateRestore class. |
| 369 | * |
| 370 | * @param state the previously saved state to restore. |
| 371 | */ |
| 372 | void restoreDrawState(const SavedDrawState& state); |
| 373 | |
| 374 | /** |
| 375 | * Copies the draw state from another target to this target. |
| 376 | * |
| 377 | * @param srcTarget draw target used as src of the draw state. |
| 378 | */ |
| 379 | void copyDrawState(const GrDrawTarget& srcTarget); |
| 380 | |
| 381 | /** |
| 382 | * Flags that indicate the layout of vertex data. |
| 383 | * |
| 384 | * kSeparateTexCoord_VertexLayoutBit is incompatible with |
| 385 | * kPositionAsTexCoord_VertexLayoutBit. kTextFormat_VertexLayoutBit is |
| 386 | * incompatible with any other flags. |
| 387 | * |
| 388 | * When kTextFormat_VertexLayoutBit is set: |
| 389 | * Texture coordinates are separate. |
| 390 | * Positions and Texture coordinates are SkGpuTextVertex. |
| 391 | * For non-text vertices: |
| 392 | * Position and texture coordinates are GrPoints. |
| 393 | * Colors are GrColors. |
| 394 | * |
| 395 | * The order is always positions, texture coords, colors. |
| 396 | */ |
| 397 | enum VertexLayoutBits { |
| 398 | kSeparateTexCoord_VertexLayoutBit = 0x1, //<! vertices have texture |
| 399 | // coords that are not |
| 400 | // inferred from the |
| 401 | // positions |
| 402 | kPositionAsTexCoord_VertexLayoutBit = 0x2, //<! vertices use positions |
| 403 | // as texture coords. |
| 404 | kColor_VertexLayoutBit = 0x4, //<! vertices have colors |
| 405 | kTextFormat_VertexLayoutBit = 0x8, //<! vertices represent glyphs |
| 406 | // and therefore contain |
| 407 | // two GrGpuTextVertexs. |
| 408 | // One for pos and one for |
| 409 | // text coords. |
| 410 | // for below assert |
| 411 | kDummy, |
| 412 | kHighVertexLayoutBit = kDummy - 1 |
| 413 | }; |
| 414 | GR_STATIC_ASSERT(kHighVertexLayoutBit < (1 << 8*sizeof(GrVertexLayout))); |
| 415 | |
| 416 | /** |
| 417 | * Reserves space for vertices and/or indices. Draw target will use |
| 418 | * reserved vertices / indices at next draw. |
| 419 | * |
| 420 | * If succeeds: |
| 421 | * if vertexCount is nonzero, *vertices will be the array |
| 422 | * of vertices to be filled by caller. The next draw will read |
| 423 | * these vertices. |
| 424 | * |
| 425 | * if indecCount is nonzero, *indices will be the array of indices |
| 426 | * to be filled by caller. The next indexed draw will read from |
| 427 | * these indices. |
| 428 | * |
| 429 | * If a client does not already have a vertex buffer or cpu arrays then this |
| 430 | * is the preferred way to allocate vertex/index array. It allows the |
| 431 | * subclass of GrDrawTarget to decide whether to put data in buffers, to |
| 432 | * group vertex data that uses the same state (e.g. for deferred rendering), |
| 433 | * etc. |
| 434 | * |
| 435 | * This must be matched with a releaseReservedGeometry call after all |
| 436 | * draws that reference the reserved geometry data have been called. |
| 437 | * |
| 438 | * AutoGeometryRelease can be used to automatically call the release. |
| 439 | * |
| 440 | * @param vertexCount the number of vertices to reserve space for. Can be 0. |
| 441 | * @param indexCount the number of indices to reserve space for. Can be 0. |
| 442 | * @param vertexLayout the format of vertices (ignored if vertexCount == 0). |
| 443 | * @param vertices will point to reserved vertex space if vertexCount is |
| 444 | * non-zero. Illegal to pass NULL if vertexCount > 0. |
| 445 | * @param indices will point to reserved index space if indexCount is |
| 446 | * non-zero. Illegal to pass NULL if indexCount > 0. |
| 447 | * |
| 448 | * @return true if succeeded in allocating space for the vertices and false |
| 449 | * if not. |
| 450 | */ |
| 451 | bool reserveAndLockGeometry(GrVertexLayout vertexLayout, |
| 452 | uint32_t vertexCount, |
| 453 | uint32_t indexCount, |
| 454 | void** vertices, |
| 455 | void** indices); |
| 456 | /** |
| 457 | * Provides hints to caller about the number of vertices and indices |
| 458 | * that can be allocated cheaply. This can be useful if caller is reserving |
| 459 | * space but doesn't know exactly how much geometry is needed. |
| 460 | * |
| 461 | * Also may hint whether the draw target should be flushed first. This is |
| 462 | * useful for deferred targets. |
| 463 | * |
| 464 | * @param vertexLayout layout of vertices caller would like to reserve |
| 465 | * @param vertexCount in: hint about how many vertices the caller would |
| 466 | * like to allocate. |
| 467 | * out: a hint about the number of vertices that can be |
| 468 | * allocated cheaply. Negative means no hint. |
| 469 | * Ignored if NULL. |
| 470 | * @param indexCount in: hint about how many indices the caller would |
| 471 | * like to allocate. |
| 472 | * out: a hint about the number of indices that can be |
| 473 | * allocated cheaply. Negative means no hint. |
| 474 | * Ignored if NULL. |
| 475 | * |
| 476 | * @return true if target should be flushed based on the input values. |
| 477 | */ |
| 478 | virtual bool geometryHints(GrVertexLayout vertexLayout, |
| 479 | int32_t* vertexCount, |
| 480 | int32_t* indexCount) const; |
| 481 | |
| 482 | /** |
| 483 | * Releases reserved vertex/index data from reserveAndLockGeometry(). |
| 484 | */ |
| 485 | void releaseReservedGeometry(); |
| 486 | |
| 487 | /** |
| 488 | * Sets source of vertex data for the next draw. Data does not have to be |
| 489 | * in the array until drawIndexed or drawNonIndexed. |
| 490 | * |
| 491 | * @param array cpu array containing vertex data. |
| 492 | * @param vertexLayout layout of the vertex data in the array. |
| 493 | */ |
| 494 | void setVertexSourceToArray(const void* array, GrVertexLayout vertexLayout); |
| 495 | |
| 496 | /** |
| 497 | * Sets source of index data for the next indexed draw. Data does not have |
| 498 | * to be in the array until drawIndexed or drawNonIndexed. |
| 499 | * |
| 500 | * @param array cpu array containing index data. |
| 501 | */ |
| 502 | void setIndexSourceToArray(const void* array); |
| 503 | |
| 504 | /** |
| 505 | * Sets source of vertex data for the next draw. Data does not have to be |
| 506 | * in the buffer until drawIndexed or drawNonIndexed. |
| 507 | * |
| 508 | * @param buffer vertex buffer containing vertex data. Must be |
| 509 | * unlocked before draw call. |
| 510 | * @param vertexLayout layout of the vertex data in the buffer. |
| 511 | */ |
| 512 | void setVertexSourceToBuffer(const GrVertexBuffer* buffer, |
| 513 | GrVertexLayout vertexLayout); |
| 514 | |
| 515 | /** |
| 516 | * Sets source of index data for the next indexed draw. Data does not have |
| 517 | * to be in the buffer until drawIndexed or drawNonIndexed. |
| 518 | * |
| 519 | * @param buffer index buffer containing indices. Must be unlocked |
| 520 | * before indexed draw call. |
| 521 | */ |
| 522 | void setIndexSourceToBuffer(const GrIndexBuffer* buffer); |
| 523 | |
| 524 | /** |
| 525 | * Draws indexed geometry using the current state and current vertex / index |
| 526 | * sources. |
| 527 | * |
| 528 | * @param type The type of primitives to draw. |
| 529 | * @param startVertex the vertex in the vertex array/buffer corresponding |
| 530 | * to index 0 |
| 531 | * @param startIndex first index to read from index src. |
| 532 | * @param vertexCount one greater than the max index. |
| 533 | * @param indexCount the number of index elements to read. The index count |
| 534 | * is effectively trimmed to the last completely |
| 535 | * specified primitive. |
| 536 | */ |
| 537 | virtual void drawIndexed(PrimitiveType type, |
| 538 | uint32_t startVertex, |
| 539 | uint32_t startIndex, |
| 540 | uint32_t vertexCount, |
| 541 | uint32_t indexCount) = 0; |
| 542 | |
| 543 | /** |
| 544 | * Draws non-indexed geometry using the current state and current vertex |
| 545 | * sources. |
| 546 | * |
| 547 | * @param type The type of primitives to draw. |
| 548 | * @param startVertex the vertex in the vertex array/buffer corresponding |
| 549 | * to index 0 |
| 550 | * @param vertexCount one greater than the max index. |
| 551 | */ |
| 552 | virtual void drawNonIndexed(PrimitiveType type, |
| 553 | uint32_t startVertex, |
| 554 | uint32_t vertexCount) = 0; |
| 555 | |
| 556 | /////////////////////////////////////////////////////////////////////////// |
| 557 | |
| 558 | class AutoStateRestore : ::GrNoncopyable { |
| 559 | public: |
| 560 | AutoStateRestore(GrDrawTarget* target); |
| 561 | ~AutoStateRestore(); |
| 562 | |
| 563 | private: |
| 564 | GrDrawTarget* fDrawTarget; |
| 565 | SavedDrawState fDrawState; |
| 566 | }; |
| 567 | |
| 568 | /////////////////////////////////////////////////////////////////////////// |
| 569 | |
| 570 | class AutoReleaseGeometry : ::GrNoncopyable { |
| 571 | public: |
| 572 | AutoReleaseGeometry(GrDrawTarget* target, |
| 573 | GrVertexLayout vertexLayout, |
| 574 | uint32_t vertexCount, |
| 575 | uint32_t indexCount) { |
| 576 | fTarget = target; |
| 577 | fSuccess = fTarget->reserveAndLockGeometry(vertexLayout, |
| 578 | vertexCount, |
| 579 | indexCount, |
| 580 | &fVertices, |
| 581 | &fIndices); |
| 582 | } |
| 583 | ~AutoReleaseGeometry() { |
| 584 | if (fSuccess) { |
| 585 | fTarget->releaseReservedGeometry(); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | bool succeeded() const { return fSuccess; } |
| 590 | void* vertices() const { return fVertices; } |
| 591 | void* indices() const { return fIndices; } |
| 592 | |
| 593 | GrPoint* positions() const { |
| 594 | return static_cast<GrPoint*>(fVertices); |
| 595 | } |
| 596 | |
| 597 | private: |
| 598 | GrDrawTarget* fTarget; |
| 599 | bool fSuccess; |
| 600 | void* fVertices; |
| 601 | void* fIndices; |
| 602 | }; |
| 603 | |
| 604 | /////////////////////////////////////////////////////////////////////////// |
| 605 | |
| 606 | class AutoClipRestore : ::GrNoncopyable { |
| 607 | public: |
| 608 | AutoClipRestore(GrDrawTarget* target) { |
| 609 | fTarget = target; |
| 610 | fClip = fTarget->getClip(); |
| 611 | } |
| 612 | |
| 613 | ~AutoClipRestore() { |
| 614 | fTarget->setClip(fClip); |
| 615 | } |
| 616 | private: |
| 617 | GrDrawTarget* fTarget; |
| 618 | GrClip fClip; |
| 619 | }; |
| 620 | |
| 621 | //////////////////////////////////////////////////////////////////////////// |
| 622 | |
| 623 | /** |
| 624 | * Helper function to compute the size of a vertex from a vertex layout |
| 625 | * @return size of a single vertex. |
| 626 | */ |
| 627 | static size_t VertexSize(GrVertexLayout vertexLayout); |
| 628 | |
| 629 | /** |
| 630 | * Helper function to compute the offset of texture coordinates in a vertex |
| 631 | * @return offset of texture coordinates in vertex layout or -1 if the |
| 632 | * layout has no texture coordinates. |
| 633 | */ |
| 634 | static int VertexTexCoordOffset(GrVertexLayout vertexLayout); |
| 635 | |
| 636 | /** |
| 637 | * Helper function to compute the offset of the color in a vertex |
| 638 | * @return offset of color in vertex layout or -1 if the |
| 639 | * layout has no color. |
| 640 | */ |
| 641 | static int VertexColorOffset(GrVertexLayout vertexLayout); |
| 642 | |
| 643 | /** |
| 644 | * Helper function to compute vertex size and component offsets. |
| 645 | * @param texCoordOffset after return it is the offset of texture coords |
| 646 | * in vertex layout or -1 if the layout has no |
| 647 | * texture coords. |
| 648 | * @param colorOffset after return it is the offset of color in vertex |
| 649 | * layout or -1 if the layout has no color. |
| 650 | * @return size of a single vertex. |
| 651 | */ |
| 652 | static int VertexSizeAndOffsets(GrVertexLayout vertexLayout, |
| 653 | int* texCoordOffset, |
| 654 | int* colorOffset); |
| 655 | /** |
| 656 | * Helper function to determine if vertex layout contains either explicit or |
| 657 | * implicit texture coordinates. |
| 658 | * |
| 659 | * @return true if vertex specifies texture coordinates, false otherwise. |
| 660 | */ |
| 661 | static bool VertexHasTexCoords(GrVertexLayout vertexLayout); |
| 662 | |
| 663 | protected: |
| 664 | |
| 665 | // Helpers for GrDrawTarget subclasses that won't have private access to |
| 666 | // SavedDrawState but need to peek at the state values. |
| 667 | static DrawState& accessSavedDrawState(SavedDrawState& sds) |
| 668 | { return sds.fState; } |
| 669 | static const DrawState& accessSavedDrawState(const SavedDrawState& sds) |
| 670 | { return sds.fState; } |
| 671 | |
| 672 | // implemented by subclass |
| 673 | virtual bool acquireGeometryHelper(GrVertexLayout vertexLayout, |
| 674 | void** vertices, |
| 675 | void** indices) = 0; |
| 676 | |
| 677 | virtual void releaseGeometryHelper() = 0; |
| 678 | |
| 679 | virtual void clipWillChange(const GrClip& clip) = 0; |
| 680 | |
| 681 | enum GeometrySrcType { |
| 682 | kArray_GeometrySrcType, |
| 683 | kReserved_GeometrySrcType, |
| 684 | kBuffer_GeometrySrcType |
| 685 | }; |
| 686 | |
| 687 | struct { |
| 688 | bool fLocked; |
| 689 | uint32_t fVertexCount; |
| 690 | uint32_t fIndexCount; |
| 691 | } fReservedGeometry; |
| 692 | |
| 693 | struct GeometrySrc { |
| 694 | GeometrySrcType fVertexSrc; |
| 695 | union { |
| 696 | const GrVertexBuffer* fVertexBuffer; |
| 697 | const void* fVertexArray; |
| 698 | }; |
| 699 | GeometrySrcType fIndexSrc; |
| 700 | union { |
| 701 | const GrIndexBuffer* fIndexBuffer; |
| 702 | const void* fIndexArray; |
| 703 | }; |
| 704 | GrVertexLayout fVertexLayout; |
| 705 | } fGeometrySrc; |
| 706 | |
| 707 | GrClip fClip; |
| 708 | |
| 709 | DrawState fCurrDrawState; |
| 710 | |
| 711 | // set texture or modelview matrix |
| 712 | void loadMatrix(const GrMatrix&, MatrixMode); |
| 713 | |
| 714 | // not meant for outside usage. Could cause problems if calls between |
| 715 | // the save and restore mess with reserved geometry state. |
| 716 | class AutoGeometrySrcRestore { |
| 717 | public: |
| 718 | AutoGeometrySrcRestore(GrDrawTarget* target) { |
| 719 | fTarget = target; |
| 720 | fGeometrySrc = fTarget->fGeometrySrc; |
| 721 | } |
| 722 | ~AutoGeometrySrcRestore() { |
| 723 | fTarget->fGeometrySrc = fGeometrySrc; |
| 724 | } |
| 725 | private: |
| 726 | GrDrawTarget *fTarget; |
| 727 | GeometrySrc fGeometrySrc; |
| 728 | |
| 729 | AutoGeometrySrcRestore(); |
| 730 | AutoGeometrySrcRestore(const AutoGeometrySrcRestore&); |
| 731 | AutoGeometrySrcRestore& operator =(AutoGeometrySrcRestore&); |
| 732 | }; |
| 733 | |
| 734 | }; |
| 735 | |
| 736 | #endif |