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