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 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 21 | #include "GrMatrix.h" |
| 22 | #include "GrColor.h" |
| 23 | #include "GrRefCnt.h" |
| 24 | #include "GrSamplerState.h" |
| 25 | #include "GrClip.h" |
| 26 | |
| 27 | class GrTexture; |
| 28 | class GrRenderTarget; |
| 29 | class GrClipIterator; |
| 30 | class GrVertexBuffer; |
| 31 | class GrIndexBuffer; |
| 32 | |
| 33 | class GrDrawTarget : public GrRefCnt { |
| 34 | public: |
| 35 | /** |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 36 | * Number of texture stages. Each stage takes as input a color and |
| 37 | * 2D texture coordinates. The color input to the first enabled stage is the |
| 38 | * per-vertex color or the constant color (setColor/setAlpha) if there are |
| 39 | * no per-vertex colors. For subsequent stages the input color is the output |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 40 | * color from the previous enabled stage. The output color of each stage is |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 41 | * the input color modulated with the result of a texture lookup. Texture |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 42 | * lookups are specified by a texture (setTexture), a texture matrix |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 43 | * (setTextureMatrix), and a sampler (setSamplerState). Texture coordinates |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 44 | * for each stage come from the vertices based on a GrVertexLayout bitfield. |
| 45 | * The output fragment color is the output color of the last enabled stage. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 46 | * The presence or absence of texture coordinates for each stage in the |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 47 | * vertex layout indicates whether a stage is enabled or not. |
| 48 | */ |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 49 | enum { |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 50 | kNumStages = 2, |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 51 | kMaxTexCoords = kNumStages |
| 52 | }; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 53 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 54 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 55 | * Geometric primitives used for drawing. |
| 56 | */ |
| 57 | enum PrimitiveType { |
| 58 | kTriangles_PrimitiveType, |
| 59 | kTriangleStrip_PrimitiveType, |
| 60 | kTriangleFan_PrimitiveType, |
| 61 | kPoints_PrimitiveType, |
| 62 | kLines_PrimitiveType, |
| 63 | kLineStrip_PrimitiveType |
| 64 | }; |
| 65 | |
| 66 | /** |
| 67 | * Flags that affect rendering. Controlled using enable/disableState(). All |
| 68 | * default to disabled. |
| 69 | */ |
| 70 | enum StateBits { |
| 71 | kDither_StateBit = 0x1,//<! Perform color dithering |
| 72 | kAntialias_StateBit = 0x2,//<! Perform anti-aliasing. The render- |
| 73 | // target must support some form of AA |
| 74 | // (msaa, coverage sampling, etc). For |
| 75 | // GrGpu-created rendertarget/textures |
| 76 | // this is controlled by parameters |
| 77 | // passed to createTexture. |
| 78 | kClip_StateBit = 0x4,//<! Controls whether drawing is clipped |
| 79 | // against the region specified by |
| 80 | // setClip. |
| 81 | }; |
| 82 | |
| 83 | /** |
| 84 | * Coeffecients for alpha-blending. |
| 85 | */ |
| 86 | enum BlendCoeff { |
| 87 | kZero_BlendCoeff, //<! 0 |
| 88 | kOne_BlendCoeff, //<! 1 |
| 89 | kSC_BlendCoeff, //<! src color |
| 90 | kISC_BlendCoeff, //<! one minus src color |
| 91 | kDC_BlendCoeff, //<! dst color |
| 92 | kIDC_BlendCoeff, //<! one minus dst color |
| 93 | kSA_BlendCoeff, //<! src alpha |
| 94 | kISA_BlendCoeff, //<! one minus src alpha |
| 95 | kDA_BlendCoeff, //<! dst alpha |
| 96 | kIDA_BlendCoeff, //<! one minus dst alpha |
| 97 | }; |
| 98 | |
| 99 | /** |
| 100 | * StencilPass |
| 101 | * |
| 102 | * Sets the stencil state for subsequent draw calls. Used to fill paths. |
| 103 | * |
| 104 | * Winding requires two passes when the GPU/API doesn't support separate |
| 105 | * stencil. |
| 106 | * |
| 107 | * The color pass for path fill is used to zero out stencil bits used for |
| 108 | * path filling. Every pixel covere by a winding/EO stencil pass must get |
| 109 | * covered by the color pass in order to leave stencil buffer in the correct |
| 110 | * state for the next path draw. |
| 111 | * |
| 112 | * NOTE: Stencil-based Winding fill has alias-to-zero problems. (e.g. A |
| 113 | * winding count of 128,256,512,etc with a 8 bit stencil buffer |
| 114 | * will be unfilled) |
| 115 | */ |
| 116 | enum StencilPass { |
| 117 | kNone_StencilPass, //<! Not drawing a path or clip. |
| 118 | kEvenOddStencil_StencilPass, //<! records in/out in stencil buffer |
| 119 | // using the Even/Odd fill rule. |
| 120 | kEvenOddColor_StencilPass, //<! writes colors to color target in |
| 121 | // pixels marked inside the fill by |
| 122 | // kEOFillStencil_StencilPass. Clears |
| 123 | // stencil in pixels covered by |
| 124 | // geometry. |
| 125 | kWindingStencil1_StencilPass, //<! records in/out in stencil buffer |
| 126 | // using the Winding fill rule. |
| 127 | kWindingStencil2_StencilPass, //<! records in/out in stencil buffer |
| 128 | // using the Winding fill rule. |
| 129 | // Run when single-stencil-pass winding |
| 130 | // not supported (i.e. no separate |
| 131 | // stencil support) |
| 132 | kWindingColor_StencilPass, //<! writes colors to color target in |
| 133 | // pixels marked inside the fill by |
| 134 | // kWindFillStencil_StencilPass. Clears |
| 135 | // stencil in pixels covered by |
| 136 | // geometry. |
| 137 | kDrawTargetCount_StencilPass //<! Subclass may extend this enum to use |
| 138 | // the stencil for other purposes (e.g. |
| 139 | // to do stencil-based clipping) |
| 140 | // This value is provided as basis for |
| 141 | // defining these extended enum values. |
| 142 | }; |
| 143 | |
| 144 | protected: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 145 | |
reed@google.com | 8195f67 | 2011-01-12 18:14:28 +0000 | [diff] [blame] | 146 | struct DrState { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 147 | uint32_t fFlagBits; |
| 148 | BlendCoeff fSrcBlend; |
| 149 | BlendCoeff fDstBlend; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 150 | GrTexture* fTextures[kNumStages]; |
| 151 | GrSamplerState fSamplerStates[kNumStages]; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 152 | GrRenderTarget* fRenderTarget; |
| 153 | GrColor fColor; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 154 | StencilPass fStencilPass; |
| 155 | bool fReverseFill; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 156 | GrMatrix fViewMatrix; |
| 157 | GrMatrix fTextureMatrices[kNumStages]; |
reed@google.com | 8195f67 | 2011-01-12 18:14:28 +0000 | [diff] [blame] | 158 | bool operator ==(const DrState& s) const { |
| 159 | return 0 == memcmp(this, &s, sizeof(DrState)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 160 | } |
reed@google.com | 8195f67 | 2011-01-12 18:14:28 +0000 | [diff] [blame] | 161 | bool operator !=(const DrState& s) const { return !(*this == s); } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | public: |
| 165 | /////////////////////////////////////////////////////////////////////////// |
| 166 | |
| 167 | GrDrawTarget(); |
| 168 | |
| 169 | /** |
| 170 | * Sets the current clip to the region specified by clip. All draws will be |
| 171 | * clipped against this clip if kClip_StateBit is enabled. |
| 172 | * |
| 173 | * @param description of the clipping region |
| 174 | */ |
| 175 | void setClip(const GrClip& clip); |
| 176 | |
| 177 | /** |
| 178 | * Gets the current clip. |
| 179 | * |
| 180 | * @return the clip. |
| 181 | */ |
| 182 | const GrClip& getClip() const; |
| 183 | |
| 184 | /** |
| 185 | * Sets the texture used at the next drawing call |
| 186 | * |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 187 | * @param stage The texture stage for which the texture will be set |
| 188 | * |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 189 | * @param texture The texture to set. Can be NULL though there is no advantage |
| 190 | * to settings a NULL texture if doing non-textured drawing |
| 191 | */ |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 192 | void setTexture(int stage, GrTexture* texture); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 193 | |
| 194 | /** |
| 195 | * Retrieves the currently set texture. |
| 196 | * |
| 197 | * @return The currently set texture. The return value will be NULL if no |
| 198 | * texture has been set, NULL was most recently passed to |
| 199 | * setTexture, or the last setTexture was destroyed. |
| 200 | */ |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 201 | const GrTexture* getTexture(int stage) const; |
| 202 | GrTexture* getTexture(int stage); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 203 | |
| 204 | /** |
| 205 | * Sets the rendertarget used at the next drawing call |
| 206 | * |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 207 | * @param target The render target to set. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 208 | */ |
| 209 | void setRenderTarget(GrRenderTarget* target); |
| 210 | |
| 211 | /** |
| 212 | * Retrieves the currently set rendertarget. |
| 213 | * |
| 214 | * @return The currently set render target. |
| 215 | */ |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 216 | const GrRenderTarget* getRenderTarget() const; |
| 217 | GrRenderTarget* getRenderTarget(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 218 | |
| 219 | /** |
| 220 | * Sets the sampler state for the next draw. |
| 221 | * |
| 222 | * The sampler state determines the address wrap modes and |
| 223 | * filtering |
| 224 | * |
| 225 | * @param samplerState Specifies the sampler state. |
| 226 | */ |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 227 | void setSamplerState(int stage, const GrSamplerState& samplerState); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 228 | |
| 229 | /** |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 230 | * Sets the matrix applied to texture coordinates for a stage. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 231 | * |
| 232 | * The post-matrix texture coordinates in the square [0,1]^2 cover the |
| 233 | * entire area of the texture. This means the full POT width when a NPOT |
| 234 | * texture is embedded in a POT width texture to meet the 3D API |
| 235 | * requirements. The texture matrix is applied both when the texture |
| 236 | * coordinates are explicit and when vertex positions are used as texture |
| 237 | * coordinates. In the latter case the texture matrix is applied to the |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 238 | * pre-view-matrix position values. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 239 | * |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 240 | * @param stage the stage for which to set a matrix. |
| 241 | * @param m the matrix used to transform the texture coordinates. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 242 | */ |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 243 | void setTextureMatrix(int stage, const GrMatrix& m); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 244 | |
| 245 | /** |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 246 | * Multiplies the current texture matrix for a stage by a matrix |
| 247 | * |
| 248 | * After this call T' = T*m where T is the old tex matrix, |
| 249 | * m is the parameter to this function, and T' is the new tex matrix. |
| 250 | * (We consider positions to be column vectors so tex cood vector t is |
| 251 | * transformed by matrix X as t' = X*t.) |
| 252 | * |
| 253 | * @param m the matrix used to modify the texture matrix matrix. |
| 254 | */ |
| 255 | void concatTextureMatrix(int stage, const GrMatrix& m); |
| 256 | |
| 257 | /** |
| 258 | * Retrieves the current texture matrix for a stage |
| 259 | * @param stage index of stage |
| 260 | * @return the stage's current texture matrix. |
| 261 | */ |
| 262 | const GrMatrix& getTextureMatrix(int stage) const; |
| 263 | |
| 264 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 265 | * Sets the matrix applied to veretx positions. |
| 266 | * |
| 267 | * In the post-view-matrix space the rectangle [0,w]x[0,h] |
| 268 | * fully covers the render target. (w and h are the width and height of the |
| 269 | * the rendertarget.) |
| 270 | * |
| 271 | * @param m the matrix used to transform the vertex positions. |
| 272 | */ |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 273 | void setViewMatrix(const GrMatrix& m); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 274 | |
| 275 | /** |
| 276 | * Multiplies the current view matrix by a matrix |
| 277 | * |
| 278 | * After this call V' = V*m where V is the old view matrix, |
| 279 | * m is the parameter to this function, and V' is the new view matrix. |
| 280 | * (We consider positions to be column vectors so position vector p is |
| 281 | * transformed by matrix X as p' = X*p.) |
| 282 | * |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 283 | * @param m the matrix used to modify the view matrix. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 284 | */ |
| 285 | void concatViewMatrix(const GrMatrix& m); |
| 286 | |
| 287 | /** |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 288 | * Retrieves the current view matrix |
| 289 | * @return the current view matrix. |
| 290 | */ |
| 291 | const GrMatrix& getViewMatrix() const; |
| 292 | |
| 293 | /** |
| 294 | * Retrieves the inverse of the current view matrix. |
| 295 | * |
| 296 | * If the current view matrix is invertible, return true, and if matrix |
| 297 | * is non-null, copy the inverse into it. If the current view matrix is |
| 298 | * non-invertible, return false and ignore the matrix parameter. |
| 299 | * |
| 300 | * @param matrix if not null, will receive a copy of the current inverse. |
| 301 | */ |
| 302 | bool getViewInverse(GrMatrix* matrix) const; |
| 303 | |
| 304 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 305 | * Sets color for next draw to a premultiplied-alpha color. |
| 306 | * |
| 307 | * @param the color to set. |
| 308 | */ |
| 309 | void setColor(GrColor); |
| 310 | |
| 311 | /** |
| 312 | * Sets the color to be used for the next draw to be |
| 313 | * (r,g,b,a) = (alpha, alpha, alpha, alpha). |
| 314 | * |
| 315 | * @param alpha The alpha value to set as the color. |
| 316 | */ |
| 317 | void setAlpha(uint8_t alpha); |
| 318 | |
| 319 | /** |
| 320 | * Sets pass for path rendering |
| 321 | * |
| 322 | * @param pass of path rendering |
| 323 | */ |
| 324 | void setStencilPass(StencilPass pass); |
| 325 | |
| 326 | /** |
| 327 | * Reveses the in/out decision of the fill rule for path rendering. |
| 328 | * Only affects kEOFillColor_StencilPass and kWindingFillColor_StencilPass |
| 329 | * |
| 330 | * @param reverse true to reverse, false otherwise |
| 331 | */ |
| 332 | void setReverseFill(bool reverse); |
| 333 | |
| 334 | /** |
| 335 | * Enable render state settings. |
| 336 | * |
| 337 | * @param flags bitfield of StateBits specifing the states to enable |
| 338 | */ |
| 339 | void enableState(uint32_t stateBits); |
| 340 | |
| 341 | /** |
| 342 | * Disable render state settings. |
| 343 | * |
| 344 | * @param flags bitfield of StateBits specifing the states to disable |
| 345 | */ |
| 346 | void disableState(uint32_t stateBits); |
| 347 | |
| 348 | bool isDitherState() const { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 349 | return 0 != (fCurrDrawState.fFlagBits & kDither_StateBit); |
| 350 | } |
| 351 | |
| 352 | bool isClipState() const { |
| 353 | return 0 != (fCurrDrawState.fFlagBits & kClip_StateBit); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 357 | * Sets the blending function coeffecients. |
| 358 | * |
| 359 | * The blend function will be: |
| 360 | * D' = sat(S*srcCoef + D*dstCoef) |
| 361 | * |
| 362 | * where D is the existing destination color, S is the incoming source |
| 363 | * color, and D' is the new destination color that will be written. sat() |
| 364 | * is the saturation function. |
| 365 | * |
| 366 | * @param srcCoef coeffecient applied to the src color. |
| 367 | * @param dstCoef coeffecient applied to the dst color. |
| 368 | */ |
| 369 | void setBlendFunc(BlendCoeff srcCoef, BlendCoeff dstCoef); |
| 370 | |
| 371 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 372 | * Used to save and restore the GrGpu's drawing state |
| 373 | */ |
| 374 | struct SavedDrawState { |
| 375 | private: |
reed@google.com | 8195f67 | 2011-01-12 18:14:28 +0000 | [diff] [blame] | 376 | DrState fState; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 377 | friend class GrDrawTarget; |
| 378 | }; |
| 379 | |
| 380 | /** |
| 381 | * Saves the current draw state. The state can be restored at a later time |
| 382 | * with restoreDrawState. |
| 383 | * |
| 384 | * See also AutoStateRestore class. |
| 385 | * |
| 386 | * @param state will hold the state after the function returns. |
| 387 | */ |
| 388 | void saveCurrentDrawState(SavedDrawState* state) const; |
| 389 | |
| 390 | /** |
| 391 | * Restores previously saved draw state. The client guarantees that state |
| 392 | * was previously passed to saveCurrentDrawState and that the rendertarget |
| 393 | * and texture set at save are still valid. |
| 394 | * |
| 395 | * See also AutoStateRestore class. |
| 396 | * |
| 397 | * @param state the previously saved state to restore. |
| 398 | */ |
| 399 | void restoreDrawState(const SavedDrawState& state); |
| 400 | |
| 401 | /** |
| 402 | * Copies the draw state from another target to this target. |
| 403 | * |
| 404 | * @param srcTarget draw target used as src of the draw state. |
| 405 | */ |
| 406 | void copyDrawState(const GrDrawTarget& srcTarget); |
| 407 | |
| 408 | /** |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 409 | * The format of vertices is represented as a bitfield of flags. |
| 410 | * Flags that indicate the layout of vertex data. Vertices always contain |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 411 | * positions and may also contain up to kMaxTexCoords sets of 2D texture |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 412 | * coordinates and per-vertex colors. Each stage can use any of the texture |
| 413 | * coordinates as its input texture coordinates or it may use the positions. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 414 | * |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 415 | * If no texture coordinates are specified for a stage then the stage is |
| 416 | * disabled. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 417 | * |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 418 | * Only one type of texture coord can be specified per stage. For |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 419 | * example StageTexCoordVertexLayoutBit(0, 2) and |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 420 | * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 421 | * |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 422 | * The order in memory is always (position, texture coord 0, ..., color) |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 423 | * with any unused fields omitted. Note that this means that if only texture |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 424 | * coordinates 1 is referenced then there is no texture coordinates 0 and |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 425 | * the order would be (position, texture coordinate 1[, color]). |
| 426 | */ |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 427 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 428 | /** |
| 429 | * Generates a bit indicating that a texture stage uses texture coordinates |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 430 | * |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 431 | * @param stage the stage that will use texture coordinates. |
| 432 | * @param texCoordIdx the index of the texture coordinates to use |
| 433 | * |
| 434 | * @return the bit to add to a GrVertexLayout bitfield. |
| 435 | */ |
| 436 | static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) { |
| 437 | GrAssert(stage < kNumStages); |
| 438 | GrAssert(texCoordIdx < kMaxTexCoords); |
| 439 | return 1 << (stage + (texCoordIdx * kNumStages)); |
| 440 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 441 | |
| 442 | /** |
| 443 | * Determines if blend is effectively disabled. |
| 444 | * |
| 445 | * @return true if blend can be disabled without changing the rendering |
| 446 | * result given the current state including the vertex layout specified |
| 447 | * with the vertex source. |
| 448 | */ |
| 449 | bool canDisableBlend() const; |
| 450 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 451 | private: |
| 452 | static const int TEX_COORD_BIT_CNT = kNumStages*kMaxTexCoords; |
| 453 | public: |
| 454 | /** |
| 455 | * Generates a bit indicating that a texture stage uses the position |
| 456 | * as its texture coordinate. |
| 457 | * |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 458 | * @param stage the stage that will use position as texture |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 459 | * coordinates. |
| 460 | * |
| 461 | * @return the bit to add to a GrVertexLayout bitfield. |
| 462 | */ |
| 463 | static int StagePosAsTexCoordVertexLayoutBit(int stage) { |
| 464 | GrAssert(stage < kNumStages); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 465 | return (1 << (TEX_COORD_BIT_CNT + stage)); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 466 | } |
| 467 | private: |
| 468 | static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT + kNumStages; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 469 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 470 | public: |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 471 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 472 | /** |
| 473 | * Additional Bits that can be specified in GrVertexLayout. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 474 | */ |
| 475 | enum VertexLayoutBits { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 476 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 477 | kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0), |
| 478 | //<! vertices have colors |
| 479 | kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1), |
| 480 | //<! use text vertices. (Pos |
| 481 | // and tex coords may be |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 482 | // a different type for |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 483 | // text [GrGpuTextVertex vs |
| 484 | // GrPoint].) |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 485 | // for below assert |
| 486 | kDummy, |
| 487 | kHighVertexLayoutBit = kDummy - 1 |
| 488 | }; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 489 | // make sure we haven't exceeded the number of bits in GrVertexLayout. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 490 | GR_STATIC_ASSERT(kHighVertexLayoutBit < (1 << 8*sizeof(GrVertexLayout))); |
| 491 | |
| 492 | /** |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 493 | * There are three paths for specifying geometry (vertices and optionally |
| 494 | * indices) to the draw target. When indexed drawing the indices and vertices |
| 495 | * can be each use a different path. |
| 496 | * |
| 497 | * 1. Provide a cpu array (set*SourceToArray). This is useful when the |
| 498 | * caller's client has already provided vertex data in a format |
| 499 | * the time compatible with a GrVertexLayout. The array must contain the |
| 500 | * data at set*SourceToArray is called. The source stays in effect for |
| 501 | * drawIndexed & drawNonIndexed calls until set*SourceToArray is called |
| 502 | * again or one of the other two paths is chosen. |
| 503 | * |
| 504 | * 2. Reserve and Lock. This is most useful when the caller has data it must |
| 505 | * transform before drawing and will not likely render it again. The |
| 506 | * caller requests that the draw target make room for some amount of |
| 507 | * vertex and/or index data. The target provides ptrs to hold the data |
| 508 | * data. The caller can write the data into the pts up until the first |
| 509 | * drawIndexed or drawNonIndexed call. At this point the data is frozen |
| 510 | * and the ptrs are no longer guaranteed to be valid. All subsequent |
| 511 | * drawIndexed & drawNonIndexed calls will use this data until |
| 512 | * releaseReserved geometry is called. This must be called before another |
| 513 | * source is set. |
| 514 | * |
| 515 | * 3. Vertex and Index Buffers. This is most useful for geometry that will |
| 516 | * be rendered multiple times. SetVertexSourceToBuffer & |
| 517 | * SetIndexSourceToBuffer are used to set the buffer and subsequent |
| 518 | * drawIndexed and drawNonIndexed calls use this source until another |
| 519 | * source is set. |
| 520 | */ |
| 521 | |
| 522 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 523 | * Reserves space for vertices and/or indices. Draw target will use |
| 524 | * reserved vertices / indices at next draw. |
| 525 | * |
| 526 | * If succeeds: |
| 527 | * if vertexCount is nonzero, *vertices will be the array |
| 528 | * of vertices to be filled by caller. The next draw will read |
| 529 | * these vertices. |
| 530 | * |
| 531 | * if indecCount is nonzero, *indices will be the array of indices |
| 532 | * to be filled by caller. The next indexed draw will read from |
| 533 | * these indices. |
| 534 | * |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 535 | * If a client does not already have a vertex buffer then this is the |
| 536 | * preferred way to allocate vertex/index array. It allows the subclass of |
| 537 | * GrDrawTarget to decide whether to put data in buffers, to group vertex |
| 538 | * 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] | 539 | * |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 540 | * Following the first draw after reserveAndLockGeometry the ptrs returned |
| 541 | * by releaseReservedGeometry are no longer valid and the geometry data |
| 542 | * cannot be further modified. The contents that were put in the reserved |
| 543 | * space can be drawn by multiple draws, however. |
| 544 | * |
| 545 | * reserveAndLockGeometry must be matched with a releaseReservedGeometry |
| 546 | * call after all draws that reference the reserved geometry data have |
| 547 | * been called. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 548 | * |
| 549 | * AutoGeometryRelease can be used to automatically call the release. |
| 550 | * |
| 551 | * @param vertexCount the number of vertices to reserve space for. Can be 0. |
| 552 | * @param indexCount the number of indices to reserve space for. Can be 0. |
| 553 | * @param vertexLayout the format of vertices (ignored if vertexCount == 0). |
| 554 | * @param vertices will point to reserved vertex space if vertexCount is |
| 555 | * non-zero. Illegal to pass NULL if vertexCount > 0. |
| 556 | * @param indices will point to reserved index space if indexCount is |
| 557 | * non-zero. Illegal to pass NULL if indexCount > 0. |
| 558 | * |
| 559 | * @return true if succeeded in allocating space for the vertices and false |
| 560 | * if not. |
| 561 | */ |
| 562 | bool reserveAndLockGeometry(GrVertexLayout vertexLayout, |
| 563 | uint32_t vertexCount, |
| 564 | uint32_t indexCount, |
| 565 | void** vertices, |
| 566 | void** indices); |
| 567 | /** |
| 568 | * Provides hints to caller about the number of vertices and indices |
| 569 | * that can be allocated cheaply. This can be useful if caller is reserving |
| 570 | * space but doesn't know exactly how much geometry is needed. |
| 571 | * |
| 572 | * Also may hint whether the draw target should be flushed first. This is |
| 573 | * useful for deferred targets. |
| 574 | * |
| 575 | * @param vertexLayout layout of vertices caller would like to reserve |
| 576 | * @param vertexCount in: hint about how many vertices the caller would |
| 577 | * like to allocate. |
| 578 | * out: a hint about the number of vertices that can be |
| 579 | * allocated cheaply. Negative means no hint. |
| 580 | * Ignored if NULL. |
| 581 | * @param indexCount in: hint about how many indices the caller would |
| 582 | * like to allocate. |
| 583 | * out: a hint about the number of indices that can be |
| 584 | * allocated cheaply. Negative means no hint. |
| 585 | * Ignored if NULL. |
| 586 | * |
| 587 | * @return true if target should be flushed based on the input values. |
| 588 | */ |
| 589 | virtual bool geometryHints(GrVertexLayout vertexLayout, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 590 | int* vertexCount, |
| 591 | int* indexCount) const; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 592 | |
| 593 | /** |
| 594 | * Releases reserved vertex/index data from reserveAndLockGeometry(). |
| 595 | */ |
| 596 | void releaseReservedGeometry(); |
| 597 | |
| 598 | /** |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 599 | * Sets source of vertex data for the next draw. Array must contain |
| 600 | * the vertex data when this is called. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 601 | * |
| 602 | * @param array cpu array containing vertex data. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 603 | * @param size size of the vertex data. |
| 604 | * @param vertexCount the number of vertices in the array. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 605 | */ |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 606 | void setVertexSourceToArray(GrVertexLayout vertexLayout, |
| 607 | const void* vertexArray, |
| 608 | int vertexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 609 | |
| 610 | /** |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 611 | * Sets source of index data for the next indexed draw. Array must contain |
| 612 | * the indices when this is called. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 613 | * |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 614 | * @param array cpu array containing index data. |
| 615 | * @param indexCount the number of indices in the array. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 616 | */ |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 617 | void setIndexSourceToArray(const void* indexArray, int indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 618 | |
| 619 | /** |
| 620 | * Sets source of vertex data for the next draw. Data does not have to be |
| 621 | * in the buffer until drawIndexed or drawNonIndexed. |
| 622 | * |
| 623 | * @param buffer vertex buffer containing vertex data. Must be |
| 624 | * unlocked before draw call. |
| 625 | * @param vertexLayout layout of the vertex data in the buffer. |
| 626 | */ |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 627 | void setVertexSourceToBuffer(GrVertexLayout vertexLayout, |
| 628 | const GrVertexBuffer* buffer); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 629 | |
| 630 | /** |
| 631 | * Sets source of index data for the next indexed draw. Data does not have |
| 632 | * to be in the buffer until drawIndexed or drawNonIndexed. |
| 633 | * |
| 634 | * @param buffer index buffer containing indices. Must be unlocked |
| 635 | * before indexed draw call. |
| 636 | */ |
| 637 | void setIndexSourceToBuffer(const GrIndexBuffer* buffer); |
| 638 | |
| 639 | /** |
| 640 | * Draws indexed geometry using the current state and current vertex / index |
| 641 | * sources. |
| 642 | * |
| 643 | * @param type The type of primitives to draw. |
| 644 | * @param startVertex the vertex in the vertex array/buffer corresponding |
| 645 | * to index 0 |
| 646 | * @param startIndex first index to read from index src. |
| 647 | * @param vertexCount one greater than the max index. |
| 648 | * @param indexCount the number of index elements to read. The index count |
| 649 | * is effectively trimmed to the last completely |
| 650 | * specified primitive. |
| 651 | */ |
| 652 | virtual void drawIndexed(PrimitiveType type, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 653 | int startVertex, |
| 654 | int startIndex, |
| 655 | int vertexCount, |
| 656 | int indexCount) = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 657 | |
| 658 | /** |
| 659 | * Draws non-indexed geometry using the current state and current vertex |
| 660 | * sources. |
| 661 | * |
| 662 | * @param type The type of primitives to draw. |
| 663 | * @param startVertex the vertex in the vertex array/buffer corresponding |
| 664 | * to index 0 |
| 665 | * @param vertexCount one greater than the max index. |
| 666 | */ |
| 667 | virtual void drawNonIndexed(PrimitiveType type, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 668 | int startVertex, |
| 669 | int vertexCount) = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 670 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 671 | /** |
| 672 | * Helper function for drawing rects. This does not use the current index |
| 673 | * and vertex sources. After returning, the vertex and index sources may |
| 674 | * have changed. They should be reestablished before the next drawIndexed |
| 675 | * or drawNonIndexed. This cannot be called between reserving and releasing |
| 676 | * geometry. The GrDrawTarget subclass may be able to perform additional |
| 677 | * optimizations if drawRect is used rather than drawIndexed or |
| 678 | * drawNonIndexed. |
| 679 | * @param rect the rect to draw |
| 680 | * @param matrix optional matrix applied to rect (before viewMatrix) |
| 681 | * @param stageEnableMask bitmask indicating which stages are enabled. |
| 682 | * Bit i indicates whether stage i is enabled. |
| 683 | * @param srcRects specifies rects for stages enabled by stageEnableMask. |
| 684 | * if stageEnableMask bit i is 1, srcRects is not NULL, |
| 685 | * and srcRects[i] is not NULL, then srcRects[i] will be |
| 686 | * used as coordinates for stage i. Otherwise, if stage i |
| 687 | * is enabled then rect is used as the coordinates. |
| 688 | * @param srcMatrices optional matrices applied to srcRects. If |
| 689 | * srcRect[i] is non-NULL and srcMatrices[i] is |
| 690 | * non-NULL then srcRect[i] will be transformed by |
| 691 | * srcMatrix[i]. srcMatrices can be NULL when no |
| 692 | * srcMatrices are desired. |
| 693 | */ |
| 694 | virtual void drawRect(const GrRect& rect, |
| 695 | const GrMatrix* matrix, |
| 696 | int stageEnableMask, |
| 697 | const GrRect* srcRects[], |
| 698 | const GrMatrix* srcMatrices[]); |
| 699 | |
| 700 | /** |
| 701 | * Helper for drawRect when the caller doesn't need separate src rects or |
| 702 | * matrices. |
| 703 | */ |
| 704 | void drawSimpleRect(const GrRect& rect, |
| 705 | const GrMatrix* matrix, |
| 706 | int stageEnableMask) { |
| 707 | drawRect(rect, matrix, stageEnableMask, NULL, NULL); |
| 708 | } |
| 709 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 710 | /////////////////////////////////////////////////////////////////////////// |
| 711 | |
| 712 | class AutoStateRestore : ::GrNoncopyable { |
| 713 | public: |
| 714 | AutoStateRestore(GrDrawTarget* target); |
| 715 | ~AutoStateRestore(); |
| 716 | |
| 717 | private: |
| 718 | GrDrawTarget* fDrawTarget; |
| 719 | SavedDrawState fDrawState; |
| 720 | }; |
| 721 | |
| 722 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 723 | |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 724 | class AutoViewMatrixRestore : ::GrNoncopyable { |
| 725 | public: |
| 726 | AutoViewMatrixRestore() { |
| 727 | fDrawTarget = NULL; |
| 728 | } |
| 729 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 730 | AutoViewMatrixRestore(GrDrawTarget* target) |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 731 | : fDrawTarget(target), fMatrix(fDrawTarget->getViewMatrix()) { |
| 732 | GrAssert(NULL != target); |
| 733 | } |
| 734 | |
| 735 | void set(GrDrawTarget* target) { |
| 736 | GrAssert(NULL != target); |
| 737 | if (NULL != fDrawTarget) { |
| 738 | fDrawTarget->setViewMatrix(fMatrix); |
| 739 | } |
| 740 | fDrawTarget = target; |
| 741 | fMatrix = target->getViewMatrix(); |
| 742 | } |
| 743 | |
| 744 | ~AutoViewMatrixRestore() { |
| 745 | if (NULL != fDrawTarget) { |
| 746 | fDrawTarget->setViewMatrix(fMatrix); |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | private: |
| 751 | GrDrawTarget* fDrawTarget; |
| 752 | GrMatrix fMatrix; |
| 753 | }; |
| 754 | |
| 755 | /////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 756 | |
| 757 | class AutoReleaseGeometry : ::GrNoncopyable { |
| 758 | public: |
| 759 | AutoReleaseGeometry(GrDrawTarget* target, |
| 760 | GrVertexLayout vertexLayout, |
| 761 | uint32_t vertexCount, |
| 762 | uint32_t indexCount) { |
| 763 | fTarget = target; |
| 764 | fSuccess = fTarget->reserveAndLockGeometry(vertexLayout, |
| 765 | vertexCount, |
| 766 | indexCount, |
| 767 | &fVertices, |
| 768 | &fIndices); |
| 769 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 770 | |
| 771 | AutoReleaseGeometry() { |
| 772 | fSuccess = false; |
| 773 | } |
| 774 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 775 | ~AutoReleaseGeometry() { |
| 776 | if (fSuccess) { |
| 777 | fTarget->releaseReservedGeometry(); |
| 778 | } |
| 779 | } |
| 780 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 781 | bool set(GrDrawTarget* target, |
| 782 | GrVertexLayout vertexLayout, |
| 783 | uint32_t vertexCount, |
| 784 | uint32_t indexCount) { |
| 785 | if (fSuccess) { |
| 786 | fTarget->releaseReservedGeometry(); |
| 787 | } |
| 788 | fTarget = target; |
| 789 | fSuccess = fTarget->reserveAndLockGeometry(vertexLayout, |
| 790 | vertexCount, |
| 791 | indexCount, |
| 792 | &fVertices, |
| 793 | &fIndices); |
| 794 | return fSuccess; |
| 795 | } |
| 796 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 797 | bool succeeded() const { return fSuccess; } |
| 798 | void* vertices() const { return fVertices; } |
| 799 | void* indices() const { return fIndices; } |
| 800 | |
| 801 | GrPoint* positions() const { |
| 802 | return static_cast<GrPoint*>(fVertices); |
| 803 | } |
| 804 | |
| 805 | private: |
| 806 | GrDrawTarget* fTarget; |
| 807 | bool fSuccess; |
| 808 | void* fVertices; |
| 809 | void* fIndices; |
| 810 | }; |
| 811 | |
| 812 | /////////////////////////////////////////////////////////////////////////// |
| 813 | |
| 814 | class AutoClipRestore : ::GrNoncopyable { |
| 815 | public: |
| 816 | AutoClipRestore(GrDrawTarget* target) { |
| 817 | fTarget = target; |
| 818 | fClip = fTarget->getClip(); |
| 819 | } |
| 820 | |
| 821 | ~AutoClipRestore() { |
| 822 | fTarget->setClip(fClip); |
| 823 | } |
| 824 | private: |
| 825 | GrDrawTarget* fTarget; |
| 826 | GrClip fClip; |
| 827 | }; |
| 828 | |
| 829 | //////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 830 | // Helpers for picking apart vertex layouts |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 831 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 832 | /** |
| 833 | * Helper function to compute the size of a vertex from a vertex layout |
| 834 | * @return size of a single vertex. |
| 835 | */ |
| 836 | static size_t VertexSize(GrVertexLayout vertexLayout); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 837 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 838 | /** |
| 839 | * Helper function for determining the index of texture coordinates that |
| 840 | * is input for a texture stage. Note that a stage may instead use positions |
| 841 | * as texture coordinates, in which case the result of the function is |
| 842 | * indistinguishable from the case when the stage is disabled. |
| 843 | * |
| 844 | * @param stage the stage to query |
| 845 | * @param vertexLayout layout to query |
| 846 | * |
| 847 | * @return the texture coordinate index or -1 if the stage doesn't use |
| 848 | * separate (non-position) texture coordinates. |
| 849 | */ |
| 850 | static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 851 | |
| 852 | /** |
| 853 | * Helper function to compute the offset of texture coordinates in a vertex |
| 854 | * @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] | 855 | * layout has no texture coordinates. Will be 0 if positions are |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 856 | * used as texture coordinates for the stage. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 857 | */ |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 858 | static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 859 | |
| 860 | /** |
| 861 | * Helper function to compute the offset of the color in a vertex |
| 862 | * @return offset of color in vertex layout or -1 if the |
| 863 | * layout has no color. |
| 864 | */ |
| 865 | static int VertexColorOffset(GrVertexLayout vertexLayout); |
| 866 | |
| 867 | /** |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 868 | * Helper function to determine if vertex layout contains explicit texture |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 869 | * coordinates of some index. |
| 870 | * |
| 871 | * @param coordIndex the tex coord index to query |
| 872 | * @param vertexLayout layout to query |
| 873 | * |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 874 | * @return true if vertex specifies texture coordinates for the index, |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 875 | * false otherwise. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 876 | */ |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 877 | static bool VertexUsesTexCoordIdx(int coordIndex, |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 878 | GrVertexLayout vertexLayout); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 879 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 880 | /** |
| 881 | * Helper function to determine if vertex layout contains either explicit or |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 882 | * implicit texture coordinates for a stage. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 883 | * |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 884 | * @param stage the stage to query |
| 885 | * @param vertexLayout layout to query |
| 886 | * |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 887 | * @return true if vertex specifies texture coordinates for the stage, |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 888 | * false otherwise. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 889 | */ |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 890 | static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 891 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 892 | /** |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 893 | * Helper function to compute the size of each vertex and the offsets of |
| 894 | * texture coordinates and color. Determines tex coord offsets by tex coord |
| 895 | * 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] | 896 | * by StageTexCoordVertexLayoutBit.) |
| 897 | * |
| 898 | * @param vertexLayout the layout to query |
| 899 | * @param texCoordOffsetsByIdx after return it is the offset of each |
| 900 | * tex coord index in the vertex or -1 if |
| 901 | * index isn't used. |
| 902 | * @return size of a single vertex |
| 903 | */ |
| 904 | static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout, |
| 905 | int texCoordOffsetsByIdx[kMaxTexCoords], |
| 906 | int *colorOffset); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 907 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 908 | /** |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 909 | * Helper function to compute the size of each vertex and the offsets of |
| 910 | * texture coordinates and color. Determines tex coord offsets by stage |
| 911 | * rather than by index. (Each stage can be mapped to any t.c. index |
| 912 | * by StageTexCoordVertexLayoutBit.) If a stage uses positions for |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 913 | * tex coords then that stage's offset will be 0 (positions are always at 0). |
| 914 | * |
| 915 | * @param vertexLayout the layout to query |
| 916 | * @param texCoordOffsetsByStage after return it is the offset of each |
| 917 | * tex coord index in the vertex or -1 if |
| 918 | * index isn't used. |
| 919 | * @return size of a single vertex |
| 920 | */ |
| 921 | static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout, |
| 922 | int texCoordOffsetsByStage[kNumStages], |
| 923 | int *colorOffset); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 924 | |
| 925 | /** |
| 926 | * Accessing positions, texture coords, or colors, of a vertex within an |
| 927 | * array is a hassle involving casts and simple math. These helpers exist |
| 928 | * to keep GrDrawTarget clients' code a bit nicer looking. |
| 929 | */ |
| 930 | |
| 931 | /** |
| 932 | * Gets a pointer to a GrPoint of a vertex's position or texture |
| 933 | * coordinate. |
| 934 | * @param vertices the vetex array |
| 935 | * @param vertexIndex the index of the vertex in the array |
| 936 | * @param vertexSize the size of each vertex in the array |
| 937 | * @param offset the offset in bytes of the vertex component. |
| 938 | * Defaults to zero (corresponding to vertex position) |
| 939 | * @return pointer to the vertex component as a GrPoint |
| 940 | */ |
| 941 | static GrPoint* GetVertexPoint(void* vertices, |
| 942 | int vertexIndex, |
| 943 | int vertexSize, |
| 944 | int offset = 0) { |
| 945 | intptr_t start = GrTCast<intptr_t>(vertices); |
| 946 | return GrTCast<GrPoint*>(start + offset + |
| 947 | vertexIndex * vertexSize); |
| 948 | } |
| 949 | static const GrPoint* GetVertexPoint(const void* vertices, |
| 950 | int vertexIndex, |
| 951 | int vertexSize, |
| 952 | int offset = 0) { |
| 953 | intptr_t start = GrTCast<intptr_t>(vertices); |
| 954 | return GrTCast<const GrPoint*>(start + offset + |
| 955 | vertexIndex * vertexSize); |
| 956 | } |
| 957 | |
| 958 | /** |
| 959 | * Gets a pointer to a GrColor inside a vertex within a vertex array. |
| 960 | * @param vertices the vetex array |
| 961 | * @param vertexIndex the index of the vertex in the array |
| 962 | * @param vertexSize the size of each vertex in the array |
| 963 | * @param offset the offset in bytes of the vertex color |
| 964 | * @return pointer to the vertex component as a GrColor |
| 965 | */ |
| 966 | static GrColor* GetVertexColor(void* vertices, |
| 967 | int vertexIndex, |
| 968 | int vertexSize, |
| 969 | int offset) { |
| 970 | intptr_t start = GrTCast<intptr_t>(vertices); |
| 971 | return GrTCast<GrColor*>(start + offset + |
| 972 | vertexIndex * vertexSize); |
| 973 | } |
| 974 | static const GrColor* GetVertexColor(const void* vertices, |
| 975 | int vertexIndex, |
| 976 | int vertexSize, |
| 977 | int offset) { |
| 978 | const intptr_t start = GrTCast<intptr_t>(vertices); |
| 979 | return GrTCast<const GrColor*>(start + offset + |
| 980 | vertexIndex * vertexSize); |
| 981 | } |
| 982 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 983 | protected: |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 984 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 985 | // Helpers for GrDrawTarget subclasses that won't have private access to |
| 986 | // SavedDrawState but need to peek at the state values. |
reed@google.com | 8195f67 | 2011-01-12 18:14:28 +0000 | [diff] [blame] | 987 | static DrState& accessSavedDrawState(SavedDrawState& sds) |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 988 | { return sds.fState; } |
reed@google.com | 8195f67 | 2011-01-12 18:14:28 +0000 | [diff] [blame] | 989 | static const DrState& accessSavedDrawState(const SavedDrawState& sds) |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 990 | { return sds.fState; } |
| 991 | |
| 992 | // implemented by subclass |
| 993 | virtual bool acquireGeometryHelper(GrVertexLayout vertexLayout, |
| 994 | void** vertices, |
| 995 | void** indices) = 0; |
| 996 | |
| 997 | virtual void releaseGeometryHelper() = 0; |
| 998 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 999 | // subclass overrides to be notified when clip is set. |
| 1000 | virtual void clipWillBeSet(const GrClip& clip) = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1001 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 1002 | virtual void setVertexSourceToArrayHelper(const void* vertexArray, |
| 1003 | int vertexCount) = 0; |
| 1004 | |
| 1005 | virtual void setIndexSourceToArrayHelper(const void* indexArray, |
| 1006 | int indexCount) = 0; |
| 1007 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 1008 | // Helpers for drawRect, protected so subclasses that override drawRect |
| 1009 | // can use them. |
| 1010 | static GrVertexLayout GetRectVertexLayout(int stageEnableMask, |
| 1011 | const GrRect* srcRects[]); |
| 1012 | |
| 1013 | static void SetRectVertices(const GrRect& rect, |
| 1014 | const GrMatrix* matrix, |
| 1015 | const GrRect* srcRects[], |
| 1016 | const GrMatrix* srcMatrices[], |
| 1017 | GrVertexLayout layout, |
| 1018 | void* vertices); |
| 1019 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1020 | enum GeometrySrcType { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 1021 | kReserved_GeometrySrcType, // src was set using reserveAndLockGeometry |
| 1022 | kArray_GeometrySrcType, // src was set using set*SourceToArray |
| 1023 | kBuffer_GeometrySrcType // src was set using set*SourceToBuffer |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1024 | }; |
| 1025 | |
| 1026 | struct { |
| 1027 | bool fLocked; |
| 1028 | uint32_t fVertexCount; |
| 1029 | uint32_t fIndexCount; |
| 1030 | } fReservedGeometry; |
| 1031 | |
| 1032 | struct GeometrySrc { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 1033 | GeometrySrcType fVertexSrc; |
| 1034 | const GrVertexBuffer* fVertexBuffer; // valid if src type is buffer |
| 1035 | GeometrySrcType fIndexSrc; |
| 1036 | const GrIndexBuffer* fIndexBuffer; // valid if src type is buffer |
| 1037 | GrVertexLayout fVertexLayout; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1038 | } fGeometrySrc; |
| 1039 | |
| 1040 | GrClip fClip; |
| 1041 | |
reed@google.com | 8195f67 | 2011-01-12 18:14:28 +0000 | [diff] [blame] | 1042 | DrState fCurrDrawState; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1043 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 1044 | // Not meant for external use. Only setVertexSourceToBuffer and |
| 1045 | // setIndexSourceToBuffer will work since GrDrawTarget subclasses don't |
| 1046 | // support nested reserveAndLockGeometry (and cpu arrays internally use the |
| 1047 | // same path). |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1048 | class AutoGeometrySrcRestore { |
| 1049 | public: |
| 1050 | AutoGeometrySrcRestore(GrDrawTarget* target) { |
| 1051 | fTarget = target; |
| 1052 | fGeometrySrc = fTarget->fGeometrySrc; |
| 1053 | } |
| 1054 | ~AutoGeometrySrcRestore() { |
| 1055 | fTarget->fGeometrySrc = fGeometrySrc; |
| 1056 | } |
| 1057 | private: |
| 1058 | GrDrawTarget *fTarget; |
| 1059 | GeometrySrc fGeometrySrc; |
| 1060 | |
| 1061 | AutoGeometrySrcRestore(); |
| 1062 | AutoGeometrySrcRestore(const AutoGeometrySrcRestore&); |
| 1063 | AutoGeometrySrcRestore& operator =(AutoGeometrySrcRestore&); |
| 1064 | }; |
| 1065 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1066 | private: |
| 1067 | void VertexLayoutUnitTest(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1068 | }; |
| 1069 | |
| 1070 | #endif |