epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #ifndef GrDrawTarget_DEFINED |
| 12 | #define GrDrawTarget_DEFINED |
| 13 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 14 | #include "GrClip.h" |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 15 | #include "GrColor.h" |
| 16 | #include "GrMatrix.h" |
| 17 | #include "GrRefCnt.h" |
| 18 | #include "GrRenderTarget.h" |
| 19 | #include "GrSamplerState.h" |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 20 | #include "GrStencil.h" |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 21 | #include "GrTexture.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 22 | |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 23 | #include "SkXfermode.h" |
| 24 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 25 | class GrTexture; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 26 | class GrClipIterator; |
| 27 | class GrVertexBuffer; |
| 28 | class GrIndexBuffer; |
| 29 | |
| 30 | class GrDrawTarget : public GrRefCnt { |
| 31 | public: |
| 32 | /** |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 33 | * Represents the draw target capabilities. |
| 34 | */ |
| 35 | struct Caps { |
| 36 | Caps() { memset(this, 0, sizeof(Caps)); } |
| 37 | Caps(const Caps& c) { *this = c; } |
| 38 | Caps& operator= (const Caps& c) { |
| 39 | memcpy(this, &c, sizeof(Caps)); |
| 40 | return *this; |
| 41 | } |
| 42 | void print() const; |
| 43 | bool f8BitPaletteSupport : 1; |
| 44 | bool fNPOTTextureSupport : 1; |
| 45 | bool fNPOTTextureTileSupport : 1; |
| 46 | bool fNPOTRenderTargetSupport : 1; |
| 47 | bool fTwoSidedStencilSupport : 1; |
| 48 | bool fStencilWrapOpsSupport : 1; |
| 49 | bool fHWAALineSupport : 1; |
| 50 | bool fShaderSupport : 1; |
| 51 | bool fShaderDerivativeSupport : 1; |
| 52 | bool fFSAASupport : 1; |
| 53 | bool fDualSourceBlendingSupport : 1; |
| 54 | bool fBufferLockSupport : 1; |
| 55 | int fMinRenderTargetWidth; |
| 56 | int fMinRenderTargetHeight; |
| 57 | int fMaxRenderTargetSize; |
| 58 | int fMaxTextureSize; |
| 59 | }; |
| 60 | |
| 61 | /** |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 62 | * Number of texture stages. Each stage takes as input a color and |
| 63 | * 2D texture coordinates. The color input to the first enabled stage is the |
| 64 | * per-vertex color or the constant color (setColor/setAlpha) if there are |
| 65 | * 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] | 66 | * 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] | 67 | * 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] | 68 | * lookups are specified by a texture a sampler (setSamplerState). Texture |
| 69 | * coordinates for each stage come from the vertices based on a |
| 70 | * GrVertexLayout bitfield. The output fragment color is the output color of |
| 71 | * the last enabled stage. The presence or absence of texture coordinates |
| 72 | * for each stage in the vertex layout indicates whether a stage is enabled |
| 73 | * or not. |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 74 | */ |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 75 | enum { |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 76 | kNumStages = 3, |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 77 | kMaxTexCoords = kNumStages |
| 78 | }; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 79 | |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 80 | /** |
| 81 | * The absolute maximum number of edges that may be specified for |
| 82 | * a single draw call when performing edge antialiasing. This is used for |
| 83 | * the size of several static buffers, so implementations of getMaxEdges() |
| 84 | * (below) should clamp to this value. |
| 85 | */ |
| 86 | enum { |
| 87 | kMaxEdges = 32 |
| 88 | }; |
| 89 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 90 | /** |
| 91 | * When specifying edges as vertex data this enum specifies what type of |
| 92 | * edges are in use. The edges are always 4 GrScalars in memory, even when |
| 93 | * the edge type requires fewer than 4. |
| 94 | */ |
| 95 | enum VertexEdgeType { |
| 96 | /* 1-pixel wide line |
| 97 | 2D implicit line eq (a*x + b*y +c = 0). 4th component unused */ |
| 98 | kHairLine_EdgeType, |
| 99 | /* 1-pixel wide quadratic |
| 100 | u^2-v canonical coords (only 2 components used) */ |
| 101 | kHairQuad_EdgeType |
| 102 | }; |
| 103 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 104 | /** |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 105 | * Bitfield used to indicate which stages are in use. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 106 | */ |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 107 | typedef int StageBitfield; |
| 108 | GR_STATIC_ASSERT(sizeof(StageBitfield)*8 >= kNumStages); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * Flags that affect rendering. Controlled using enable/disableState(). All |
| 112 | * default to disabled. |
| 113 | */ |
| 114 | enum StateBits { |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 115 | kDither_StateBit = 0x01, //<! Perform color dithering |
| 116 | kAntialias_StateBit = 0x02, //<! Perform anti-aliasing. The render- |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 117 | // target must support some form of AA |
| 118 | // (msaa, coverage sampling, etc). For |
| 119 | // GrGpu-created rendertarget/textures |
| 120 | // this is controlled by parameters |
| 121 | // passed to createTexture. |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 122 | kClip_StateBit = 0x04, //<! Controls whether drawing is clipped |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 123 | // against the region specified by |
| 124 | // setClip. |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 125 | kNoColorWrites_StateBit = 0x08, //<! If set it disables writing colors. |
| 126 | // Useful while performing stencil |
| 127 | // ops. |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 128 | kEdgeAAConcave_StateBit = 0x10,//<! If set, edge AA will test edge |
| 129 | // pairs for convexity while |
| 130 | // rasterizing. Set this if the |
| 131 | // source polygon is non-convex. |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 132 | |
| 133 | // subclass may use additional bits internally |
| 134 | kDummyStateBit, |
| 135 | kLastPublicStateBit = kDummyStateBit-1 |
| 136 | }; |
| 137 | |
| 138 | enum DrawFace { |
| 139 | kBoth_DrawFace, |
| 140 | kCCW_DrawFace, |
| 141 | kCW_DrawFace, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | /** |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 145 | * Sets the stencil settings to use for the next draw. |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 146 | * Changing the clip has the side-effect of possibly zeroing |
| 147 | * out the client settable stencil bits. So multipass algorithms |
| 148 | * using stencil should not change the clip between passes. |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 149 | * @param settings the stencil settings to use. |
| 150 | */ |
| 151 | void setStencil(const GrStencilSettings& settings) { |
| 152 | fCurrDrawState.fStencilSettings = settings; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Shortcut to disable stencil testing and ops. |
| 157 | */ |
| 158 | void disableStencil() { |
| 159 | fCurrDrawState.fStencilSettings.setDisabled(); |
| 160 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 161 | |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 162 | class Edge { |
| 163 | public: |
| 164 | Edge() {} |
| 165 | Edge(float x, float y, float z) : fX(x), fY(y), fZ(z) {} |
| 166 | GrPoint intersect(const Edge& other) { |
| 167 | return GrPoint::Make( |
| 168 | (fY * other.fZ - other.fY * fZ) / |
| 169 | (fX * other.fY - other.fX * fY), |
| 170 | (fX * other.fZ - other.fX * fZ) / |
| 171 | (other.fX * fY - fX * other.fY)); |
| 172 | } |
| 173 | float fX, fY, fZ; |
| 174 | }; |
| 175 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 176 | protected: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 177 | |
reed@google.com | 8195f67 | 2011-01-12 18:14:28 +0000 | [diff] [blame] | 178 | struct DrState { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 179 | DrState() { |
| 180 | // make sure any pad is zero for memcmp |
| 181 | // all DrState members should default to something |
| 182 | // valid by the memset |
| 183 | memset(this, 0, sizeof(DrState)); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 184 | |
| 185 | // memset exceptions |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 186 | fColorFilterXfermode = SkXfermode::kDstIn_Mode; |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 187 | fFirstCoverageStage = kNumStages; |
| 188 | |
| 189 | // pedantic assertion that our ptrs will |
| 190 | // be NULL (0 ptr is mem addr 0) |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 191 | GrAssert((intptr_t)(void*)NULL == 0LL); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 192 | |
| 193 | // default stencil setting should be disabled |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 194 | GrAssert(fStencilSettings.isDisabled()); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 195 | fFirstCoverageStage = kNumStages; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 196 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 197 | uint32_t fFlagBits; |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 198 | GrBlendCoeff fSrcBlend; |
| 199 | GrBlendCoeff fDstBlend; |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 200 | GrColor fBlendConstant; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 201 | GrTexture* fTextures[kNumStages]; |
| 202 | GrSamplerState fSamplerStates[kNumStages]; |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 203 | int fFirstCoverageStage; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 204 | GrRenderTarget* fRenderTarget; |
| 205 | GrColor fColor; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 206 | DrawFace fDrawFace; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 207 | GrColor fColorFilterColor; |
| 208 | SkXfermode::Mode fColorFilterXfermode; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 209 | |
| 210 | GrStencilSettings fStencilSettings; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 211 | GrMatrix fViewMatrix; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 212 | VertexEdgeType fVertexEdgeType; |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 213 | Edge fEdgeAAEdges[kMaxEdges]; |
| 214 | int fEdgeAANumEdges; |
reed@google.com | 8195f67 | 2011-01-12 18:14:28 +0000 | [diff] [blame] | 215 | bool operator ==(const DrState& s) const { |
| 216 | return 0 == memcmp(this, &s, sizeof(DrState)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 217 | } |
reed@google.com | 8195f67 | 2011-01-12 18:14:28 +0000 | [diff] [blame] | 218 | bool operator !=(const DrState& s) const { return !(*this == s); } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | public: |
| 222 | /////////////////////////////////////////////////////////////////////////// |
| 223 | |
| 224 | GrDrawTarget(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 225 | virtual ~GrDrawTarget(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 226 | |
| 227 | /** |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 228 | * Gets the capabilities of the draw target. |
| 229 | */ |
| 230 | const Caps& getCaps() const { return fCaps; } |
| 231 | |
| 232 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 233 | * Sets the current clip to the region specified by clip. All draws will be |
| 234 | * clipped against this clip if kClip_StateBit is enabled. |
| 235 | * |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 236 | * Setting the clip may (or may not) zero out the client's stencil bits. |
| 237 | * |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 238 | * @param description of the clipping region |
| 239 | */ |
| 240 | void setClip(const GrClip& clip); |
| 241 | |
| 242 | /** |
| 243 | * Gets the current clip. |
| 244 | * |
| 245 | * @return the clip. |
| 246 | */ |
| 247 | const GrClip& getClip() const; |
| 248 | |
| 249 | /** |
| 250 | * Sets the texture used at the next drawing call |
| 251 | * |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 252 | * @param stage The texture stage for which the texture will be set |
| 253 | * |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 254 | * @param texture The texture to set. Can be NULL though there is no advantage |
| 255 | * to settings a NULL texture if doing non-textured drawing |
| 256 | */ |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 257 | void setTexture(int stage, GrTexture* texture); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 258 | |
| 259 | /** |
| 260 | * Retrieves the currently set texture. |
| 261 | * |
| 262 | * @return The currently set texture. The return value will be NULL if no |
| 263 | * texture has been set, NULL was most recently passed to |
| 264 | * setTexture, or the last setTexture was destroyed. |
| 265 | */ |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 266 | const GrTexture* getTexture(int stage) const; |
| 267 | GrTexture* getTexture(int stage); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 268 | |
| 269 | /** |
| 270 | * Sets the rendertarget used at the next drawing call |
| 271 | * |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 272 | * @param target The render target to set. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 273 | */ |
| 274 | void setRenderTarget(GrRenderTarget* target); |
| 275 | |
| 276 | /** |
| 277 | * Retrieves the currently set rendertarget. |
| 278 | * |
| 279 | * @return The currently set render target. |
| 280 | */ |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 281 | const GrRenderTarget* getRenderTarget() const; |
| 282 | GrRenderTarget* getRenderTarget(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 283 | |
| 284 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 285 | * Sets the sampler state for a stage used in subsequent draws. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 286 | * |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 287 | * The sampler state determines how texture coordinates are |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 288 | * intepretted and used to sample the texture. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 289 | * |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 290 | * @param stage the stage of the sampler to set |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 291 | * @param samplerState Specifies the sampler state. |
| 292 | */ |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 293 | void setSamplerState(int stage, const GrSamplerState& samplerState); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 294 | |
| 295 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 296 | * Concats the matrix of a stage's sampler. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 297 | * |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 298 | * @param stage the stage of the sampler to set |
| 299 | * @param matrix the matrix to concat |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 300 | */ |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 301 | void preConcatSamplerMatrix(int stage, const GrMatrix& matrix) { |
| 302 | GrAssert(stage >= 0 && stage < kNumStages); |
| 303 | fCurrDrawState.fSamplerStates[stage].preConcatMatrix(matrix); |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 304 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 305 | |
| 306 | /** |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 307 | * Shortcut for preConcatSamplerMatrix on all stages in mask with same |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 308 | * matrix |
| 309 | */ |
| 310 | void preConcatSamplerMatrices(int stageMask, const GrMatrix& matrix) { |
| 311 | for (int i = 0; i < kNumStages; ++i) { |
| 312 | if ((1 << i) & stageMask) { |
| 313 | this->preConcatSamplerMatrix(i, matrix); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /** |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 319 | * Shortcut for preConcatSamplerMatrix on all enabled stages in mask with |
| 320 | * same matrix |
| 321 | * |
| 322 | * @param stage the stage of the sampler to set |
| 323 | * @param matrix the matrix to concat |
| 324 | */ |
| 325 | void preConcatEnabledSamplerMatrices(const GrMatrix& matrix) { |
| 326 | StageBitfield stageMask = this->enabledStages(); |
| 327 | this->preConcatSamplerMatrices(stageMask, matrix); |
| 328 | } |
| 329 | |
| 330 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 331 | * Gets the matrix of a stage's sampler |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 332 | * |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 333 | * @param stage the stage to of sampler to get |
| 334 | * @return the sampler state's matrix |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 335 | */ |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 336 | const GrMatrix& getSamplerMatrix(int stage) const { |
| 337 | return fCurrDrawState.fSamplerStates[stage].getMatrix(); |
| 338 | } |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 339 | |
| 340 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 341 | * Sets the matrix of a stage's sampler |
| 342 | * |
| 343 | * @param stage the stage of sampler set |
| 344 | * @param matrix the matrix to set |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 345 | */ |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 346 | void setSamplerMatrix(int stage, const GrMatrix& matrix) { |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 347 | fCurrDrawState.fSamplerStates[stage].setMatrix(matrix); |
| 348 | } |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 349 | |
| 350 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 351 | * Sets the matrix applied to veretx positions. |
| 352 | * |
| 353 | * In the post-view-matrix space the rectangle [0,w]x[0,h] |
| 354 | * fully covers the render target. (w and h are the width and height of the |
| 355 | * the rendertarget.) |
| 356 | * |
| 357 | * @param m the matrix used to transform the vertex positions. |
| 358 | */ |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 359 | void setViewMatrix(const GrMatrix& m); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 360 | |
| 361 | /** |
| 362 | * Multiplies the current view matrix by a matrix |
| 363 | * |
| 364 | * After this call V' = V*m where V is the old view matrix, |
| 365 | * m is the parameter to this function, and V' is the new view matrix. |
| 366 | * (We consider positions to be column vectors so position vector p is |
| 367 | * transformed by matrix X as p' = X*p.) |
| 368 | * |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 369 | * @param m the matrix used to modify the view matrix. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 370 | */ |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 371 | void preConcatViewMatrix(const GrMatrix& m); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 372 | |
| 373 | /** |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 374 | * Multiplies the current view matrix by a matrix |
| 375 | * |
| 376 | * After this call V' = m*V where V is the old view matrix, |
| 377 | * m is the parameter to this function, and V' is the new view matrix. |
| 378 | * (We consider positions to be column vectors so position vector p is |
| 379 | * transformed by matrix X as p' = X*p.) |
| 380 | * |
| 381 | * @param m the matrix used to modify the view matrix. |
| 382 | */ |
| 383 | void postConcatViewMatrix(const GrMatrix& m); |
| 384 | |
| 385 | /** |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 386 | * Retrieves the current view matrix |
| 387 | * @return the current view matrix. |
| 388 | */ |
| 389 | const GrMatrix& getViewMatrix() const; |
| 390 | |
| 391 | /** |
| 392 | * Retrieves the inverse of the current view matrix. |
| 393 | * |
| 394 | * If the current view matrix is invertible, return true, and if matrix |
| 395 | * is non-null, copy the inverse into it. If the current view matrix is |
| 396 | * non-invertible, return false and ignore the matrix parameter. |
| 397 | * |
| 398 | * @param matrix if not null, will receive a copy of the current inverse. |
| 399 | */ |
| 400 | bool getViewInverse(GrMatrix* matrix) const; |
| 401 | |
| 402 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 403 | * Sets color for next draw to a premultiplied-alpha color. |
| 404 | * |
| 405 | * @param the color to set. |
| 406 | */ |
| 407 | void setColor(GrColor); |
| 408 | |
| 409 | /** |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 410 | * Add a color filter that can be represented by a color and a mode. |
| 411 | */ |
| 412 | void setColorFilter(GrColor, SkXfermode::Mode); |
| 413 | |
| 414 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 415 | * Sets the color to be used for the next draw to be |
| 416 | * (r,g,b,a) = (alpha, alpha, alpha, alpha). |
| 417 | * |
| 418 | * @param alpha The alpha value to set as the color. |
| 419 | */ |
| 420 | void setAlpha(uint8_t alpha); |
| 421 | |
| 422 | /** |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 423 | * Controls whether clockwise, counterclockwise, or both faces are drawn. |
| 424 | * @param face the face(s) to draw. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 425 | */ |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 426 | void setDrawFace(DrawFace face) { fCurrDrawState.fDrawFace = face; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 427 | |
| 428 | /** |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 429 | * A common pattern is to compute a color with the initial stages and then |
| 430 | * modulate that color by a coverage value in later stage(s) (AA, mask- |
| 431 | * filters, glyph mask, etc). Color-filters, xfermodes, etc should be |
| 432 | * computed based on the pre-coverage-modulated color. The division of |
| 433 | * stages between color-computing and coverage-computing is specified by |
| 434 | * this method. Initially this is kNumStages (all stages are color- |
| 435 | * computing). |
| 436 | */ |
| 437 | void setFirstCoverageStage(int firstCoverageStage) { |
| 438 | fCurrDrawState.fFirstCoverageStage = firstCoverageStage; |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Gets the index of the first coverage-computing stage. |
| 443 | */ |
| 444 | int getFirstCoverageStage() const { |
| 445 | return fCurrDrawState.fFirstCoverageStage; |
| 446 | } |
| 447 | |
| 448 | /** |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 449 | * Gets whether the target is drawing clockwise, counterclockwise, |
| 450 | * or both faces. |
| 451 | * @return the current draw face(s). |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 452 | */ |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 453 | DrawFace getDrawFace() const { return fCurrDrawState.fDrawFace; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 454 | |
| 455 | /** |
| 456 | * Enable render state settings. |
| 457 | * |
| 458 | * @param flags bitfield of StateBits specifing the states to enable |
| 459 | */ |
| 460 | void enableState(uint32_t stateBits); |
| 461 | |
| 462 | /** |
| 463 | * Disable render state settings. |
| 464 | * |
| 465 | * @param flags bitfield of StateBits specifing the states to disable |
| 466 | */ |
| 467 | void disableState(uint32_t stateBits); |
| 468 | |
| 469 | bool isDitherState() const { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 470 | return 0 != (fCurrDrawState.fFlagBits & kDither_StateBit); |
| 471 | } |
| 472 | |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 473 | bool isAntialiasState() const { |
| 474 | return 0 != (fCurrDrawState.fFlagBits & kAntialias_StateBit); |
| 475 | } |
| 476 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 477 | bool isClipState() const { |
| 478 | return 0 != (fCurrDrawState.fFlagBits & kClip_StateBit); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 479 | } |
| 480 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 481 | bool isColorWriteDisabled() const { |
| 482 | return 0 != (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit); |
| 483 | } |
| 484 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 485 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 486 | * Sets the blending function coeffecients. |
| 487 | * |
| 488 | * The blend function will be: |
| 489 | * D' = sat(S*srcCoef + D*dstCoef) |
| 490 | * |
| 491 | * where D is the existing destination color, S is the incoming source |
| 492 | * color, and D' is the new destination color that will be written. sat() |
| 493 | * is the saturation function. |
| 494 | * |
| 495 | * @param srcCoef coeffecient applied to the src color. |
| 496 | * @param dstCoef coeffecient applied to the dst color. |
| 497 | */ |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 498 | void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 499 | |
| 500 | /** |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 501 | * Sets the blending function constant referenced by the following blending |
| 502 | * coeffecients: |
| 503 | * kConstC_BlendCoeff |
| 504 | * kIConstC_BlendCoeff |
| 505 | * kConstA_BlendCoeff |
| 506 | * kIConstA_BlendCoeff |
| 507 | * |
| 508 | * @param constant the constant to set |
| 509 | */ |
| 510 | void setBlendConstant(GrColor constant) { fCurrDrawState.fBlendConstant = constant; } |
| 511 | |
| 512 | /** |
| 513 | * Retrieves the last value set by setBlendConstant() |
| 514 | * @return the blending constant value |
| 515 | */ |
| 516 | GrColor getBlendConstant() const { return fCurrDrawState.fBlendConstant; } |
| 517 | |
| 518 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 519 | * Used to save and restore the GrGpu's drawing state |
| 520 | */ |
| 521 | struct SavedDrawState { |
| 522 | private: |
reed@google.com | 8195f67 | 2011-01-12 18:14:28 +0000 | [diff] [blame] | 523 | DrState fState; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 524 | friend class GrDrawTarget; |
| 525 | }; |
| 526 | |
| 527 | /** |
| 528 | * Saves the current draw state. The state can be restored at a later time |
| 529 | * with restoreDrawState. |
| 530 | * |
| 531 | * See also AutoStateRestore class. |
| 532 | * |
| 533 | * @param state will hold the state after the function returns. |
| 534 | */ |
| 535 | void saveCurrentDrawState(SavedDrawState* state) const; |
| 536 | |
| 537 | /** |
| 538 | * Restores previously saved draw state. The client guarantees that state |
| 539 | * was previously passed to saveCurrentDrawState and that the rendertarget |
| 540 | * and texture set at save are still valid. |
| 541 | * |
| 542 | * See also AutoStateRestore class. |
| 543 | * |
| 544 | * @param state the previously saved state to restore. |
| 545 | */ |
| 546 | void restoreDrawState(const SavedDrawState& state); |
| 547 | |
| 548 | /** |
| 549 | * Copies the draw state from another target to this target. |
| 550 | * |
| 551 | * @param srcTarget draw target used as src of the draw state. |
| 552 | */ |
| 553 | void copyDrawState(const GrDrawTarget& srcTarget); |
| 554 | |
| 555 | /** |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 556 | * The format of vertices is represented as a bitfield of flags. |
| 557 | * Flags that indicate the layout of vertex data. Vertices always contain |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 558 | * 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] | 559 | * coordinates and per-vertex colors. Each stage can use any of the texture |
| 560 | * 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] | 561 | * |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 562 | * If no texture coordinates are specified for a stage then the stage is |
| 563 | * disabled. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 564 | * |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 565 | * 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] | 566 | * example StageTexCoordVertexLayoutBit(0, 2) and |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 567 | * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 568 | * |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 569 | * The order in memory is always (position, texture coord 0, ..., color) |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 570 | * 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] | 571 | * 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] | 572 | * the order would be (position, texture coordinate 1[, color]). |
| 573 | */ |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 574 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 575 | /** |
| 576 | * Generates a bit indicating that a texture stage uses texture coordinates |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 577 | * |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 578 | * @param stage the stage that will use texture coordinates. |
| 579 | * @param texCoordIdx the index of the texture coordinates to use |
| 580 | * |
| 581 | * @return the bit to add to a GrVertexLayout bitfield. |
| 582 | */ |
| 583 | static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) { |
| 584 | GrAssert(stage < kNumStages); |
| 585 | GrAssert(texCoordIdx < kMaxTexCoords); |
| 586 | return 1 << (stage + (texCoordIdx * kNumStages)); |
| 587 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 588 | |
| 589 | /** |
| 590 | * Determines if blend is effectively disabled. |
| 591 | * |
| 592 | * @return true if blend can be disabled without changing the rendering |
| 593 | * result given the current state including the vertex layout specified |
| 594 | * with the vertex source. |
| 595 | */ |
| 596 | bool canDisableBlend() const; |
| 597 | |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame^] | 598 | /** |
| 599 | * Color alpha and coverage are two inputs to the drawing pipeline. For some |
| 600 | * blend modes it is safe to fold the coverage into constant or per-vertex |
| 601 | * color alpha value. For other blend modes they must be handled separately. |
| 602 | * Depending on features available in the underlying 3D API this may or may |
| 603 | * not be possible. |
| 604 | * |
| 605 | * This function looks at the current blend on the draw target and the draw |
| 606 | * target's capabilities to determine whether coverage can be handled |
| 607 | * correctly. |
| 608 | */ |
| 609 | bool canApplyCoverage() const; |
| 610 | |
| 611 | /** |
| 612 | * Determines whether incorporating partial pixel coverage into the constant |
| 613 | * color specified by setColor or per-vertex colors will give the right |
| 614 | * blending result. |
| 615 | */ |
| 616 | bool canTweakAlphaForCoverage() const { |
| 617 | return CanTweakAlphaForCoverage(fCurrDrawState.fDstBlend); |
| 618 | } |
| 619 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 620 | /** |
| 621 | * Determines the interpretation per-vertex edge data when the |
| 622 | * kEdge_VertexLayoutBit is set (see below). When per-vertex edges are not |
| 623 | * specified the value of this setting has no effect. |
| 624 | */ |
| 625 | void setVertexEdgeType(VertexEdgeType type) { |
| 626 | fCurrDrawState.fVertexEdgeType = type; |
| 627 | } |
| 628 | |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 629 | /** |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 630 | * Given the current draw state, vertex layout, and hw support, will HW AA |
| 631 | * lines be used (if line primitive type is drawn)? (Note that lines are |
| 632 | * always 1 pixel wide) |
| 633 | */ |
| 634 | virtual bool willUseHWAALines() const = 0; |
| 635 | |
| 636 | /** |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 637 | * Sets the edge data required for edge antialiasing. |
| 638 | * |
| 639 | * @param edges 3 * 6 float values, representing the edge |
| 640 | * equations in Ax + By + C form |
| 641 | */ |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 642 | void setEdgeAAData(const Edge* edges, int numEdges); |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 643 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 644 | private: |
| 645 | static const int TEX_COORD_BIT_CNT = kNumStages*kMaxTexCoords; |
| 646 | public: |
| 647 | /** |
| 648 | * Generates a bit indicating that a texture stage uses the position |
| 649 | * as its texture coordinate. |
| 650 | * |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 651 | * @param stage the stage that will use position as texture |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 652 | * coordinates. |
| 653 | * |
| 654 | * @return the bit to add to a GrVertexLayout bitfield. |
| 655 | */ |
| 656 | static int StagePosAsTexCoordVertexLayoutBit(int stage) { |
| 657 | GrAssert(stage < kNumStages); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 658 | return (1 << (TEX_COORD_BIT_CNT + stage)); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 659 | } |
| 660 | private: |
| 661 | static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT + kNumStages; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 662 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 663 | public: |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 664 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 665 | /** |
| 666 | * Additional Bits that can be specified in GrVertexLayout. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 667 | */ |
| 668 | enum VertexLayoutBits { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 669 | /* vertices have colors */ |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 670 | kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0), |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 671 | |
| 672 | /* Use text vertices. (Pos and tex coords may be a different type for |
| 673 | text [GrGpuTextVertex vs GrPoint].) */ |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 674 | kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1), |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 675 | |
| 676 | kEdge_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 2), |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 677 | // for below assert |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 678 | kDummyVertexLayoutBit, |
| 679 | kHighVertexLayoutBit = kDummyVertexLayoutBit - 1 |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 680 | }; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 681 | // 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] | 682 | GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout))); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 683 | |
| 684 | /** |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 685 | * There are three methods for specifying geometry (vertices and optionally |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 686 | * indices) to the draw target. When indexed drawing the indices and vertices |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 687 | * can use a different method. Once geometry is specified it can be used for |
| 688 | * multiple drawIndexed and drawNonIndexed calls. |
| 689 | * |
| 690 | * Sometimes it is necessary to perform a draw while upstack code has |
| 691 | * already specified geometry that it isn't finished with. There are push |
| 692 | * pop methods |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 693 | * |
| 694 | * 1. Provide a cpu array (set*SourceToArray). This is useful when the |
| 695 | * caller's client has already provided vertex data in a format |
| 696 | * the time compatible with a GrVertexLayout. The array must contain the |
| 697 | * data at set*SourceToArray is called. The source stays in effect for |
| 698 | * drawIndexed & drawNonIndexed calls until set*SourceToArray is called |
| 699 | * again or one of the other two paths is chosen. |
| 700 | * |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 701 | * 2. Reserve. This is most useful when the caller has data it must |
| 702 | * transform before drawing and is not long-lived. The caller requests |
| 703 | * that the draw target make room for some amount of vertex and/or index |
| 704 | * data. The target provides ptrs to hold the vertex and/or index data. |
| 705 | * |
| 706 | * The data is writable up until the next drawIndexed, drawNonIndexed, |
| 707 | * or pushGeometrySource At this point the data is frozen and the ptrs |
| 708 | * are no longer valid. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 709 | * |
| 710 | * 3. Vertex and Index Buffers. This is most useful for geometry that will |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 711 | * is long-lived. SetVertexSourceToBuffer and SetIndexSourceToBuffer are |
| 712 | * used to set the buffer and subsequent drawIndexed and drawNonIndexed |
| 713 | * calls use this source until another source is set. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 714 | */ |
| 715 | |
| 716 | /** |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 717 | * Reserves space for vertices. Draw target will use reserved vertices at |
| 718 | * at the next draw. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 719 | * |
| 720 | * If succeeds: |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 721 | * if vertexCount > 0, *vertices will be the array |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 722 | * of vertices to be filled by caller. The next draw will read |
| 723 | * these vertices. |
| 724 | * |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 725 | * If a client does not already have a vertex buffer then this is the |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 726 | * preferred way to allocate vertex data. It allows the subclass of |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 727 | * GrDrawTarget to decide whether to put data in buffers, to group vertex |
| 728 | * 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] | 729 | * |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 730 | * After the next draw or pushGeometrySource the vertices ptr is no longer |
| 731 | * valid and the geometry data cannot be further modified. The contents |
| 732 | * that were put in the reserved space can be drawn by multiple draws, |
| 733 | * however. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 734 | * |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 735 | * @param vertexLayout the format of vertices (ignored if vertexCount == 0). |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 736 | * @param vertexCount the number of vertices to reserve space for. Can be 0. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 737 | * @param vertices will point to reserved vertex space if vertexCount is |
| 738 | * non-zero. Illegal to pass NULL if vertexCount > 0. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 739 | * |
| 740 | * @return true if succeeded in allocating space for the vertices and false |
| 741 | * if not. |
| 742 | */ |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 743 | bool reserveVertexSpace(GrVertexLayout vertexLayout, |
| 744 | int vertexCount, |
| 745 | void** vertices); |
| 746 | /** |
| 747 | * Reserves space for indices. Draw target will use the reserved indices at |
| 748 | * the next indexed draw. |
| 749 | * |
| 750 | * If succeeds: |
| 751 | * if indexCount > 0, *indices will be the array |
| 752 | * of indices to be filled by caller. The next draw will read |
| 753 | * these indices. |
| 754 | * |
| 755 | * If a client does not already have a index buffer then this is the |
| 756 | * preferred way to allocate index data. It allows the subclass of |
| 757 | * GrDrawTarget to decide whether to put data in buffers, to group index |
| 758 | * data that uses the same state (e.g. for deferred rendering), etc. |
| 759 | * |
| 760 | * After the next indexed draw or pushGeometrySource the indices ptr is no |
| 761 | * longer valid and the geometry data cannot be further modified. The |
| 762 | * contents that were put in the reserved space can be drawn by multiple |
| 763 | * draws, however. |
| 764 | * |
| 765 | * @param indexCount the number of indices to reserve space for. Can be 0. |
| 766 | * @param indices will point to reserved index space if indexCount is |
| 767 | * non-zero. Illegal to pass NULL if indexCount > 0. |
| 768 | */ |
| 769 | |
| 770 | bool reserveIndexSpace(int indexCount, void** indices); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 771 | /** |
| 772 | * Provides hints to caller about the number of vertices and indices |
| 773 | * that can be allocated cheaply. This can be useful if caller is reserving |
| 774 | * space but doesn't know exactly how much geometry is needed. |
| 775 | * |
| 776 | * Also may hint whether the draw target should be flushed first. This is |
| 777 | * useful for deferred targets. |
| 778 | * |
| 779 | * @param vertexLayout layout of vertices caller would like to reserve |
| 780 | * @param vertexCount in: hint about how many vertices the caller would |
| 781 | * like to allocate. |
| 782 | * out: a hint about the number of vertices that can be |
| 783 | * allocated cheaply. Negative means no hint. |
| 784 | * Ignored if NULL. |
| 785 | * @param indexCount in: hint about how many indices the caller would |
| 786 | * like to allocate. |
| 787 | * out: a hint about the number of indices that can be |
| 788 | * allocated cheaply. Negative means no hint. |
| 789 | * Ignored if NULL. |
| 790 | * |
| 791 | * @return true if target should be flushed based on the input values. |
| 792 | */ |
| 793 | virtual bool geometryHints(GrVertexLayout vertexLayout, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 794 | int* vertexCount, |
| 795 | int* indexCount) const; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 796 | |
| 797 | /** |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 798 | * Sets source of vertex data for the next draw. Array must contain |
| 799 | * the vertex data when this is called. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 800 | * |
| 801 | * @param array cpu array containing vertex data. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 802 | * @param size size of the vertex data. |
| 803 | * @param vertexCount the number of vertices in the array. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 804 | */ |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 805 | void setVertexSourceToArray(GrVertexLayout vertexLayout, |
| 806 | const void* vertexArray, |
| 807 | int vertexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 808 | |
| 809 | /** |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 810 | * Sets source of index data for the next indexed draw. Array must contain |
| 811 | * the indices when this is called. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 812 | * |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 813 | * @param array cpu array containing index data. |
| 814 | * @param indexCount the number of indices in the array. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 815 | */ |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 816 | void setIndexSourceToArray(const void* indexArray, int indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 817 | |
| 818 | /** |
| 819 | * Sets source of vertex data for the next draw. Data does not have to be |
| 820 | * in the buffer until drawIndexed or drawNonIndexed. |
| 821 | * |
| 822 | * @param buffer vertex buffer containing vertex data. Must be |
| 823 | * unlocked before draw call. |
| 824 | * @param vertexLayout layout of the vertex data in the buffer. |
| 825 | */ |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 826 | void setVertexSourceToBuffer(GrVertexLayout vertexLayout, |
| 827 | const GrVertexBuffer* buffer); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 828 | |
| 829 | /** |
| 830 | * Sets source of index data for the next indexed draw. Data does not have |
| 831 | * to be in the buffer until drawIndexed or drawNonIndexed. |
| 832 | * |
| 833 | * @param buffer index buffer containing indices. Must be unlocked |
| 834 | * before indexed draw call. |
| 835 | */ |
| 836 | void setIndexSourceToBuffer(const GrIndexBuffer* buffer); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 837 | |
| 838 | /** |
| 839 | * Resets vertex source. Drawing from reset vertices is illegal. Set vertex |
| 840 | * source to reserved, array, or buffer before next draw. May be able to free |
| 841 | * up temporary storage allocated by setVertexSourceToArray or |
| 842 | * reserveVertexSpace. |
| 843 | */ |
| 844 | void resetVertexSource(); |
| 845 | |
| 846 | /** |
| 847 | * Resets index source. Indexed Drawing from reset indices is illegal. Set |
| 848 | * index source to reserved, array, or buffer before next indexed draw. May |
| 849 | * be able to free up temporary storage allocated by setIndexSourceToArray |
| 850 | * or reserveIndexSpace. |
| 851 | */ |
| 852 | void resetIndexSource(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 853 | |
| 854 | /** |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 855 | * Pushes and resets the vertex/index sources. Any reserved vertex / index |
| 856 | * data is finalized (i.e. cannot be updated after the matching pop but can |
| 857 | * be drawn from). Must be balanced by a pop. |
| 858 | */ |
| 859 | void pushGeometrySource(); |
| 860 | |
| 861 | /** |
| 862 | * Pops the vertex / index sources from the matching push. |
| 863 | */ |
| 864 | void popGeometrySource(); |
| 865 | |
| 866 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 867 | * Draws indexed geometry using the current state and current vertex / index |
| 868 | * sources. |
| 869 | * |
| 870 | * @param type The type of primitives to draw. |
| 871 | * @param startVertex the vertex in the vertex array/buffer corresponding |
| 872 | * to index 0 |
| 873 | * @param startIndex first index to read from index src. |
| 874 | * @param vertexCount one greater than the max index. |
| 875 | * @param indexCount the number of index elements to read. The index count |
| 876 | * is effectively trimmed to the last completely |
| 877 | * specified primitive. |
| 878 | */ |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 879 | void drawIndexed(GrPrimitiveType type, |
| 880 | int startVertex, |
| 881 | int startIndex, |
| 882 | int vertexCount, |
| 883 | int indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 884 | |
| 885 | /** |
| 886 | * Draws non-indexed geometry using the current state and current vertex |
| 887 | * sources. |
| 888 | * |
| 889 | * @param type The type of primitives to draw. |
| 890 | * @param startVertex the vertex in the vertex array/buffer corresponding |
| 891 | * to index 0 |
| 892 | * @param vertexCount one greater than the max index. |
| 893 | */ |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 894 | void drawNonIndexed(GrPrimitiveType type, |
| 895 | int startVertex, |
| 896 | int vertexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 897 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 898 | /** |
| 899 | * Helper function for drawing rects. This does not use the current index |
| 900 | * and vertex sources. After returning, the vertex and index sources may |
| 901 | * have changed. They should be reestablished before the next drawIndexed |
| 902 | * or drawNonIndexed. This cannot be called between reserving and releasing |
| 903 | * geometry. The GrDrawTarget subclass may be able to perform additional |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 904 | * optimizations if drawRect is used rather than drawIndexed or |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 905 | * drawNonIndexed. |
| 906 | * @param rect the rect to draw |
| 907 | * @param matrix optional matrix applied to rect (before viewMatrix) |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 908 | * @param stageEnableBitfield bitmask indicating which stages are enabled. |
| 909 | * Bit i indicates whether stage i is enabled. |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 910 | * @param srcRects specifies rects for stages enabled by stageEnableMask. |
| 911 | * if stageEnableMask bit i is 1, srcRects is not NULL, |
| 912 | * and srcRects[i] is not NULL, then srcRects[i] will be |
| 913 | * used as coordinates for stage i. Otherwise, if stage i |
| 914 | * is enabled then rect is used as the coordinates. |
| 915 | * @param srcMatrices optional matrices applied to srcRects. If |
| 916 | * srcRect[i] is non-NULL and srcMatrices[i] is |
| 917 | * non-NULL then srcRect[i] will be transformed by |
| 918 | * srcMatrix[i]. srcMatrices can be NULL when no |
| 919 | * srcMatrices are desired. |
| 920 | */ |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 921 | virtual void drawRect(const GrRect& rect, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 922 | const GrMatrix* matrix, |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 923 | StageBitfield stageEnableBitfield, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 924 | const GrRect* srcRects[], |
| 925 | const GrMatrix* srcMatrices[]); |
| 926 | |
| 927 | /** |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 928 | * 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] | 929 | * matrices. |
| 930 | */ |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 931 | void drawSimpleRect(const GrRect& rect, |
| 932 | const GrMatrix* matrix, |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 933 | StageBitfield stageEnableBitfield) { |
| 934 | drawRect(rect, matrix, stageEnableBitfield, NULL, NULL); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 935 | } |
| 936 | |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 937 | /** |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 938 | * Clear the render target. Ignores the clip and all other draw state |
| 939 | * (blend mode, stages, etc). Clears the whole thing if rect is NULL, |
| 940 | * otherwise just the rect. |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 941 | */ |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 942 | virtual void clear(const GrIRect* rect, GrColor color) = 0; |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 943 | |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 944 | /** |
| 945 | * Returns the maximum number of edges that may be specified in a single |
| 946 | * draw call when performing edge antialiasing. This is usually limited |
| 947 | * by the number of fragment uniforms which may be uploaded. Must be a |
| 948 | * minimum of six, since a triangle's vertices each belong to two boundary |
| 949 | * edges which may be distinct. |
| 950 | */ |
| 951 | virtual int getMaxEdges() const { return 6; } |
| 952 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 953 | //////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 954 | |
| 955 | class AutoStateRestore : ::GrNoncopyable { |
| 956 | public: |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 957 | AutoStateRestore(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 958 | AutoStateRestore(GrDrawTarget* target); |
| 959 | ~AutoStateRestore(); |
| 960 | |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 961 | /** |
| 962 | * if this object is already saving state for param target then |
| 963 | * this does nothing. Otherise, it restores previously saved state on |
| 964 | * previous target (if any) and saves current state on param target. |
| 965 | */ |
| 966 | void set(GrDrawTarget* target); |
| 967 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 968 | private: |
| 969 | GrDrawTarget* fDrawTarget; |
| 970 | SavedDrawState fDrawState; |
| 971 | }; |
| 972 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 973 | //////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 974 | |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 975 | class AutoViewMatrixRestore : ::GrNoncopyable { |
| 976 | public: |
| 977 | AutoViewMatrixRestore() { |
| 978 | fDrawTarget = NULL; |
| 979 | } |
| 980 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 981 | AutoViewMatrixRestore(GrDrawTarget* target) |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 982 | : fDrawTarget(target), fMatrix(fDrawTarget->getViewMatrix()) { |
| 983 | GrAssert(NULL != target); |
| 984 | } |
| 985 | |
| 986 | void set(GrDrawTarget* target) { |
| 987 | GrAssert(NULL != target); |
| 988 | if (NULL != fDrawTarget) { |
| 989 | fDrawTarget->setViewMatrix(fMatrix); |
| 990 | } |
| 991 | fDrawTarget = target; |
| 992 | fMatrix = target->getViewMatrix(); |
| 993 | } |
| 994 | |
| 995 | ~AutoViewMatrixRestore() { |
| 996 | if (NULL != fDrawTarget) { |
| 997 | fDrawTarget->setViewMatrix(fMatrix); |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | private: |
| 1002 | GrDrawTarget* fDrawTarget; |
| 1003 | GrMatrix fMatrix; |
| 1004 | }; |
| 1005 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1006 | //////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1007 | |
bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 1008 | /** |
| 1009 | * Sets the view matrix to I and preconcats all stage matrices enabled in |
| 1010 | * mask by the view inverse. Destructor undoes these changes. |
| 1011 | */ |
| 1012 | class AutoDeviceCoordDraw : ::GrNoncopyable { |
| 1013 | public: |
| 1014 | AutoDeviceCoordDraw(GrDrawTarget* target, int stageMask); |
| 1015 | ~AutoDeviceCoordDraw(); |
| 1016 | private: |
| 1017 | GrDrawTarget* fDrawTarget; |
| 1018 | GrMatrix fViewMatrix; |
| 1019 | GrMatrix fSamplerMatrices[kNumStages]; |
| 1020 | int fStageMask; |
| 1021 | }; |
| 1022 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1023 | //////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 1024 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1025 | class AutoReleaseGeometry : ::GrNoncopyable { |
| 1026 | public: |
| 1027 | AutoReleaseGeometry(GrDrawTarget* target, |
| 1028 | GrVertexLayout vertexLayout, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1029 | int vertexCount, |
| 1030 | int indexCount); |
| 1031 | AutoReleaseGeometry(); |
| 1032 | ~AutoReleaseGeometry(); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1033 | bool set(GrDrawTarget* target, |
| 1034 | GrVertexLayout vertexLayout, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1035 | int vertexCount, |
| 1036 | int indexCount); |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 1037 | bool succeeded() const { return NULL != fTarget; } |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 1038 | void* vertices() const { GrAssert(this->succeeded()); return fVertices; } |
| 1039 | void* indices() const { GrAssert(this->succeeded()); return fIndices; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1040 | GrPoint* positions() const { |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 1041 | return static_cast<GrPoint*>(this->vertices()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | private: |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1045 | void reset(); |
| 1046 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1047 | GrDrawTarget* fTarget; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1048 | void* fVertices; |
| 1049 | void* fIndices; |
| 1050 | }; |
| 1051 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1052 | //////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1053 | |
| 1054 | class AutoClipRestore : ::GrNoncopyable { |
| 1055 | public: |
| 1056 | AutoClipRestore(GrDrawTarget* target) { |
| 1057 | fTarget = target; |
| 1058 | fClip = fTarget->getClip(); |
| 1059 | } |
| 1060 | |
| 1061 | ~AutoClipRestore() { |
| 1062 | fTarget->setClip(fClip); |
| 1063 | } |
| 1064 | private: |
| 1065 | GrDrawTarget* fTarget; |
| 1066 | GrClip fClip; |
| 1067 | }; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1068 | |
| 1069 | //////////////////////////////////////////////////////////////////////////// |
| 1070 | |
| 1071 | class AutoGeometryPush : ::GrNoncopyable { |
| 1072 | public: |
| 1073 | AutoGeometryPush(GrDrawTarget* target) { |
| 1074 | GrAssert(NULL != target); |
| 1075 | fTarget = target; |
| 1076 | target->pushGeometrySource(); |
| 1077 | } |
| 1078 | ~AutoGeometryPush() { |
| 1079 | fTarget->popGeometrySource(); |
| 1080 | } |
| 1081 | private: |
| 1082 | GrDrawTarget* fTarget; |
| 1083 | }; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1084 | |
| 1085 | //////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1086 | // Helpers for picking apart vertex layouts |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1087 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1088 | /** |
| 1089 | * Helper function to compute the size of a vertex from a vertex layout |
| 1090 | * @return size of a single vertex. |
| 1091 | */ |
| 1092 | static size_t VertexSize(GrVertexLayout vertexLayout); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1093 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1094 | /** |
| 1095 | * Helper function for determining the index of texture coordinates that |
| 1096 | * is input for a texture stage. Note that a stage may instead use positions |
| 1097 | * as texture coordinates, in which case the result of the function is |
| 1098 | * indistinguishable from the case when the stage is disabled. |
| 1099 | * |
| 1100 | * @param stage the stage to query |
| 1101 | * @param vertexLayout layout to query |
| 1102 | * |
| 1103 | * @return the texture coordinate index or -1 if the stage doesn't use |
| 1104 | * separate (non-position) texture coordinates. |
| 1105 | */ |
| 1106 | static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1107 | |
| 1108 | /** |
| 1109 | * Helper function to compute the offset of texture coordinates in a vertex |
| 1110 | * @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] | 1111 | * layout has no texture coordinates. Will be 0 if positions are |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1112 | * used as texture coordinates for the stage. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1113 | */ |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1114 | static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1115 | |
| 1116 | /** |
| 1117 | * Helper function to compute the offset of the color in a vertex |
| 1118 | * @return offset of color in vertex layout or -1 if the |
| 1119 | * layout has no color. |
| 1120 | */ |
| 1121 | static int VertexColorOffset(GrVertexLayout vertexLayout); |
| 1122 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1123 | /** |
| 1124 | * Helper function to compute the offset of the edge pts in a vertex |
| 1125 | * @return offset of edge in vertex layout or -1 if the |
| 1126 | * layout has no edge. |
| 1127 | */ |
| 1128 | static int VertexEdgeOffset(GrVertexLayout vertexLayout); |
| 1129 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1130 | /** |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1131 | * Helper function to determine if vertex layout contains explicit texture |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1132 | * coordinates of some index. |
| 1133 | * |
| 1134 | * @param coordIndex the tex coord index to query |
| 1135 | * @param vertexLayout layout to query |
| 1136 | * |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1137 | * @return true if vertex specifies texture coordinates for the index, |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1138 | * false otherwise. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1139 | */ |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1140 | static bool VertexUsesTexCoordIdx(int coordIndex, |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1141 | GrVertexLayout vertexLayout); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1142 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1143 | /** |
| 1144 | * Helper function to determine if vertex layout contains either explicit or |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1145 | * implicit texture coordinates for a stage. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1146 | * |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1147 | * @param stage the stage to query |
| 1148 | * @param vertexLayout layout to query |
| 1149 | * |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1150 | * @return true if vertex specifies texture coordinates for the stage, |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1151 | * false otherwise. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1152 | */ |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1153 | static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1154 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1155 | /** |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1156 | * Helper function to compute the size of each vertex and the offsets of |
| 1157 | * texture coordinates and color. Determines tex coord offsets by tex coord |
| 1158 | * 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] | 1159 | * by StageTexCoordVertexLayoutBit.) |
| 1160 | * |
| 1161 | * @param vertexLayout the layout to query |
| 1162 | * @param texCoordOffsetsByIdx after return it is the offset of each |
| 1163 | * tex coord index in the vertex or -1 if |
| 1164 | * index isn't used. |
| 1165 | * @return size of a single vertex |
| 1166 | */ |
| 1167 | static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout, |
| 1168 | int texCoordOffsetsByIdx[kMaxTexCoords], |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1169 | int *colorOffset, |
| 1170 | int* edgeOffset); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1171 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1172 | /** |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1173 | * Helper function to compute the size of each vertex and the offsets of |
| 1174 | * texture coordinates and color. Determines tex coord offsets by stage |
| 1175 | * rather than by index. (Each stage can be mapped to any t.c. index |
| 1176 | * by StageTexCoordVertexLayoutBit.) If a stage uses positions for |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1177 | * tex coords then that stage's offset will be 0 (positions are always at 0). |
| 1178 | * |
| 1179 | * @param vertexLayout the layout to query |
| 1180 | * @param texCoordOffsetsByStage after return it is the offset of each |
| 1181 | * tex coord index in the vertex or -1 if |
| 1182 | * index isn't used. |
| 1183 | * @return size of a single vertex |
| 1184 | */ |
| 1185 | static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout, |
| 1186 | int texCoordOffsetsByStage[kNumStages], |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1187 | int *colorOffset, |
| 1188 | int* edgeOffset); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1189 | |
| 1190 | /** |
| 1191 | * Accessing positions, texture coords, or colors, of a vertex within an |
| 1192 | * array is a hassle involving casts and simple math. These helpers exist |
| 1193 | * to keep GrDrawTarget clients' code a bit nicer looking. |
| 1194 | */ |
| 1195 | |
| 1196 | /** |
| 1197 | * Gets a pointer to a GrPoint of a vertex's position or texture |
| 1198 | * coordinate. |
| 1199 | * @param vertices the vetex array |
| 1200 | * @param vertexIndex the index of the vertex in the array |
| 1201 | * @param vertexSize the size of each vertex in the array |
| 1202 | * @param offset the offset in bytes of the vertex component. |
| 1203 | * Defaults to zero (corresponding to vertex position) |
| 1204 | * @return pointer to the vertex component as a GrPoint |
| 1205 | */ |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1206 | static GrPoint* GetVertexPoint(void* vertices, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1207 | int vertexIndex, |
| 1208 | int vertexSize, |
| 1209 | int offset = 0) { |
| 1210 | intptr_t start = GrTCast<intptr_t>(vertices); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1211 | return GrTCast<GrPoint*>(start + offset + |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1212 | vertexIndex * vertexSize); |
| 1213 | } |
| 1214 | static const GrPoint* GetVertexPoint(const void* vertices, |
| 1215 | int vertexIndex, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1216 | int vertexSize, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1217 | int offset = 0) { |
| 1218 | intptr_t start = GrTCast<intptr_t>(vertices); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1219 | return GrTCast<const GrPoint*>(start + offset + |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1220 | vertexIndex * vertexSize); |
| 1221 | } |
| 1222 | |
| 1223 | /** |
| 1224 | * Gets a pointer to a GrColor inside a vertex within a vertex array. |
| 1225 | * @param vertices the vetex array |
| 1226 | * @param vertexIndex the index of the vertex in the array |
| 1227 | * @param vertexSize the size of each vertex in the array |
| 1228 | * @param offset the offset in bytes of the vertex color |
| 1229 | * @return pointer to the vertex component as a GrColor |
| 1230 | */ |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1231 | static GrColor* GetVertexColor(void* vertices, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1232 | int vertexIndex, |
| 1233 | int vertexSize, |
| 1234 | int offset) { |
| 1235 | intptr_t start = GrTCast<intptr_t>(vertices); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1236 | return GrTCast<GrColor*>(start + offset + |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1237 | vertexIndex * vertexSize); |
| 1238 | } |
| 1239 | static const GrColor* GetVertexColor(const void* vertices, |
| 1240 | int vertexIndex, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1241 | int vertexSize, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1242 | int offset) { |
| 1243 | const intptr_t start = GrTCast<intptr_t>(vertices); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1244 | return GrTCast<const GrColor*>(start + offset + |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1245 | vertexIndex * vertexSize); |
| 1246 | } |
| 1247 | |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 1248 | static void VertexLayoutUnitTest(); |
| 1249 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1250 | protected: |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 1251 | |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame^] | 1252 | // Determines whether it is correct to apply partial pixel coverage |
| 1253 | // by multiplying the src color by the fractional coverage. |
| 1254 | static bool CanTweakAlphaForCoverage(GrBlendCoeff dstCoeff); |
| 1255 | |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 1256 | // determines whether HW blending can be disabled or not |
| 1257 | static bool CanDisableBlend(GrVertexLayout layout, const DrState& state); |
| 1258 | |
| 1259 | // determines whether HW AA lines can be used or not |
| 1260 | static bool CanUseHWAALines(GrVertexLayout layout, const DrState& state); |
| 1261 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1262 | enum GeometrySrcType { |
| 1263 | kNone_GeometrySrcType, //<! src has not been specified |
| 1264 | kReserved_GeometrySrcType, //<! src was set using reserve*Space |
| 1265 | kArray_GeometrySrcType, //<! src was set using set*SourceToArray |
| 1266 | kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer |
| 1267 | }; |
| 1268 | |
| 1269 | struct GeometrySrcState { |
| 1270 | GeometrySrcType fVertexSrc; |
| 1271 | union { |
| 1272 | // valid if src type is buffer |
| 1273 | const GrVertexBuffer* fVertexBuffer; |
| 1274 | // valid if src type is reserved or array |
| 1275 | int fVertexCount; |
| 1276 | }; |
| 1277 | |
| 1278 | GeometrySrcType fIndexSrc; |
| 1279 | union { |
| 1280 | // valid if src type is buffer |
| 1281 | const GrIndexBuffer* fIndexBuffer; |
| 1282 | // valid if src type is reserved or array |
| 1283 | int fIndexCount; |
| 1284 | }; |
| 1285 | |
| 1286 | GrVertexLayout fVertexLayout; |
| 1287 | }; |
| 1288 | |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 1289 | // given a vertex layout and a draw state, will a stage be used? |
| 1290 | static bool StageWillBeUsed(int stage, GrVertexLayout layout, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1291 | const DrState& state) { |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 1292 | return NULL != state.fTextures[stage] && VertexUsesStage(stage, layout); |
| 1293 | } |
| 1294 | |
| 1295 | bool isStageEnabled(int stage) const { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1296 | return StageWillBeUsed(stage, this->getGeomSrc().fVertexLayout, |
| 1297 | fCurrDrawState); |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 1298 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1299 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1300 | StageBitfield enabledStages() const { |
| 1301 | StageBitfield mask = 0; |
| 1302 | for (int s = 0; s < kNumStages; ++s) { |
| 1303 | mask |= this->isStageEnabled(s) ? 1 : 0; |
| 1304 | } |
| 1305 | return mask; |
| 1306 | } |
| 1307 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1308 | // Helpers for GrDrawTarget subclasses that won't have private access to |
| 1309 | // SavedDrawState but need to peek at the state values. |
reed@google.com | 8195f67 | 2011-01-12 18:14:28 +0000 | [diff] [blame] | 1310 | static DrState& accessSavedDrawState(SavedDrawState& sds) |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1311 | { return sds.fState; } |
reed@google.com | 8195f67 | 2011-01-12 18:14:28 +0000 | [diff] [blame] | 1312 | static const DrState& accessSavedDrawState(const SavedDrawState& sds) |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1313 | { return sds.fState; } |
| 1314 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1315 | // implemented by subclass to allocate space for reserved geom |
| 1316 | virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout, |
| 1317 | int vertexCount, |
| 1318 | void** vertices) = 0; |
| 1319 | virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0; |
| 1320 | // implemented by subclass to handle release of reserved geom space |
| 1321 | virtual void releaseReservedVertexSpace() = 0; |
| 1322 | virtual void releaseReservedIndexSpace() = 0; |
| 1323 | // subclass must consume array contents when set |
| 1324 | virtual void onSetVertexSourceToArray(const void* vertexArray, |
| 1325 | int vertexCount) = 0; |
| 1326 | virtual void onSetIndexSourceToArray(const void* indexArray, |
| 1327 | int indexCount) = 0; |
| 1328 | // subclass is notified that geom source will be set away from an array |
| 1329 | virtual void releaseVertexArray() = 0; |
| 1330 | virtual void releaseIndexArray() = 0; |
| 1331 | // subclass overrides to be notified just before geo src state |
| 1332 | // is pushed/popped. |
| 1333 | virtual void geometrySourceWillPush() = 0; |
| 1334 | virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0; |
| 1335 | // subclass called to perform drawing |
| 1336 | virtual void onDrawIndexed(GrPrimitiveType type, |
| 1337 | int startVertex, |
| 1338 | int startIndex, |
| 1339 | int vertexCount, |
| 1340 | int indexCount) = 0; |
| 1341 | virtual void onDrawNonIndexed(GrPrimitiveType type, |
| 1342 | int startVertex, |
| 1343 | int vertexCount) = 0; |
bsalomon@google.com | dea2f8d | 2011-08-01 15:51:05 +0000 | [diff] [blame] | 1344 | // subclass overrides to be notified when clip is set. Must call |
| 1345 | // INHERITED::clipwillBeSet |
| 1346 | virtual void clipWillBeSet(const GrClip& clip); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 1347 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1348 | // Helpers for drawRect, protected so subclasses that override drawRect |
| 1349 | // can use them. |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 1350 | static GrVertexLayout GetRectVertexLayout(StageBitfield stageEnableBitfield, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1351 | const GrRect* srcRects[]); |
| 1352 | |
| 1353 | static void SetRectVertices(const GrRect& rect, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1354 | const GrMatrix* matrix, |
| 1355 | const GrRect* srcRects[], |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1356 | const GrMatrix* srcMatrices[], |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1357 | GrVertexLayout layout, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1358 | void* vertices); |
| 1359 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1360 | // accessor for derived classes |
| 1361 | const GeometrySrcState& getGeomSrc() const { |
| 1362 | return fGeoSrcStateStack.back(); |
| 1363 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1364 | |
| 1365 | GrClip fClip; |
| 1366 | |
reed@google.com | 8195f67 | 2011-01-12 18:14:28 +0000 | [diff] [blame] | 1367 | DrState fCurrDrawState; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1368 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 1369 | Caps fCaps; |
| 1370 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1371 | private: |
| 1372 | // called when setting a new vert/idx source to unref prev vb/ib |
| 1373 | void releasePreviousVertexSource(); |
| 1374 | void releasePreviousIndexSource(); |
| 1375 | |
| 1376 | enum { |
| 1377 | kPreallocGeoSrcStateStackCnt = 4, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1378 | }; |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 1379 | SkAlignedSTStorage<kPreallocGeoSrcStateStackCnt, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1380 | GeometrySrcState> |
| 1381 | fGeoSrcStateStackStorage; |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 1382 | SkTArray<GeometrySrcState, true> fGeoSrcStateStack; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1383 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1384 | }; |
| 1385 | |
| 1386 | #endif |