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