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