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