| 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" |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 16 | #include "GrDrawState.h" |
| bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 17 | #include "GrMatrix.h" |
| 18 | #include "GrRefCnt.h" |
| 19 | #include "GrRenderTarget.h" |
| 20 | #include "GrSamplerState.h" |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 21 | #include "GrStencil.h" |
| bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 22 | #include "GrTexture.h" |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 23 | |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 24 | #include "SkXfermode.h" |
| 25 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 26 | class GrTexture; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 27 | class GrClipIterator; |
| 28 | class GrVertexBuffer; |
| 29 | class GrIndexBuffer; |
| 30 | |
| 31 | class GrDrawTarget : public GrRefCnt { |
| 32 | public: |
| 33 | /** |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 34 | * Represents the draw target capabilities. |
| 35 | */ |
| 36 | struct Caps { |
| 37 | Caps() { memset(this, 0, sizeof(Caps)); } |
| 38 | Caps(const Caps& c) { *this = c; } |
| 39 | Caps& operator= (const Caps& c) { |
| 40 | memcpy(this, &c, sizeof(Caps)); |
| 41 | return *this; |
| 42 | } |
| 43 | void print() const; |
| 44 | bool f8BitPaletteSupport : 1; |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 45 | bool fNPOTTextureTileSupport : 1; |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 46 | bool fTwoSidedStencilSupport : 1; |
| 47 | bool fStencilWrapOpsSupport : 1; |
| 48 | bool fHWAALineSupport : 1; |
| 49 | bool fShaderSupport : 1; |
| 50 | bool fShaderDerivativeSupport : 1; |
| bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 51 | bool fGeometryShaderSupport : 1; |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 52 | bool fFSAASupport : 1; |
| 53 | bool fDualSourceBlendingSupport : 1; |
| 54 | bool fBufferLockSupport : 1; |
| bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 55 | bool fSupportPerVertexCoverage : 1; |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 56 | int fMaxRenderTargetSize; |
| 57 | int fMaxTextureSize; |
| 58 | }; |
| 59 | |
| bsalomon@google.com | 0fec61d | 2011-12-08 15:53:53 +0000 | [diff] [blame^] | 60 | // for convenience |
| bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 61 | typedef GrDrawState::StageMask StageMask; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 62 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 63 | /////////////////////////////////////////////////////////////////////////// |
| 64 | |
| 65 | GrDrawTarget(); |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 66 | virtual ~GrDrawTarget(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 67 | |
| 68 | /** |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 69 | * Gets the capabilities of the draw target. |
| 70 | */ |
| 71 | const Caps& getCaps() const { return fCaps; } |
| 72 | |
| 73 | /** |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 74 | * Sets the current clip to the region specified by clip. All draws will be |
| 75 | * clipped against this clip if kClip_StateBit is enabled. |
| 76 | * |
| bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 77 | * Setting the clip may (or may not) zero out the client's stencil bits. |
| 78 | * |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 79 | * @param description of the clipping region |
| 80 | */ |
| 81 | void setClip(const GrClip& clip); |
| 82 | |
| 83 | /** |
| 84 | * Gets the current clip. |
| 85 | * |
| 86 | * @return the clip. |
| 87 | */ |
| 88 | const GrClip& getClip() const; |
| 89 | |
| 90 | /** |
| bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 91 | * Determines if blending will require a read of a dst given the current |
| 92 | * state set on the draw target |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 93 | * |
| bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 94 | * @return true if the dst surface will be read at each pixel hit by the |
| 95 | * a draw operation. |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 96 | */ |
| bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 97 | bool drawWillReadDst() const; |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 98 | |
| bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 99 | /** |
| 100 | * Color alpha and coverage are two inputs to the drawing pipeline. For some |
| 101 | * blend modes it is safe to fold the coverage into constant or per-vertex |
| 102 | * color alpha value. For other blend modes they must be handled separately. |
| 103 | * Depending on features available in the underlying 3D API this may or may |
| 104 | * not be possible. |
| 105 | * |
| 106 | * This function looks at the current blend on the draw target and the draw |
| 107 | * target's capabilities to determine whether coverage can be handled |
| 108 | * correctly. |
| 109 | */ |
| 110 | bool canApplyCoverage() const; |
| 111 | |
| 112 | /** |
| 113 | * Determines whether incorporating partial pixel coverage into the constant |
| 114 | * color specified by setColor or per-vertex colors will give the right |
| 115 | * blending result. |
| 116 | */ |
| bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 117 | bool canTweakAlphaForCoverage() const; |
| bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 118 | |
| senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 119 | /** |
| bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 120 | * Given the current draw state, vertex layout, and hw support, will HW AA |
| 121 | * lines be used (if line primitive type is drawn)? (Note that lines are |
| 122 | * always 1 pixel wide) |
| 123 | */ |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 124 | bool willUseHWAALines() const; |
| bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 125 | |
| bsalomon@google.com | 0fec61d | 2011-12-08 15:53:53 +0000 | [diff] [blame^] | 126 | const GrDrawState& getDrawState() const { return fCurrDrawState; } |
| 127 | GrDrawState* drawState() { return &fCurrDrawState; } |
| 128 | |
| 129 | // Convenience Pass-thrus to GrDrawState. These are likely candidates for |
| 130 | // removal. |
| 131 | void setViewMatrix(const GrMatrix& m) { |
| 132 | this->drawState()->setViewMatrix(m); |
| 133 | } |
| 134 | GrMatrix* viewMatrix() { |
| 135 | return this->drawState()->viewMatrix(); |
| 136 | } |
| 137 | const GrMatrix& getViewMatrix() const { |
| 138 | return this->getDrawState().getViewMatrix(); |
| 139 | } |
| 140 | bool getViewInverse(GrMatrix* inv) const { |
| 141 | return this->getDrawState().getViewInverse(inv); |
| 142 | } |
| 143 | void setRenderTarget(GrRenderTarget* renderTarget) { |
| 144 | this->drawState()->setRenderTarget(renderTarget); |
| 145 | } |
| 146 | const GrRenderTarget* getRenderTarget() const { |
| 147 | return this->getDrawState().getRenderTarget(); |
| 148 | } |
| 149 | GrRenderTarget* getRenderTarget() { |
| 150 | return this->drawState()->getRenderTarget(); |
| 151 | } |
| 152 | void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) { |
| 153 | this->drawState()->setBlendFunc(srcCoeff, dstCoeff); |
| 154 | } |
| 155 | void setTexture(int stage, GrTexture* texture) { |
| 156 | this->drawState()->setTexture(stage, texture); |
| 157 | } |
| 158 | const GrTexture* getTexture(int stage) const { |
| 159 | return this->getDrawState().getTexture(stage); |
| 160 | } |
| 161 | GrTexture* getTexture(int stage) { |
| 162 | return this->drawState()->getTexture(stage); |
| 163 | } |
| 164 | // THIS WILL BE REMOVED AND REPLACED WITH DIRECT ACCESS TO SAMPLER |
| 165 | // IN A COMING REVISION. |
| 166 | void setSamplerState(int stage, const GrSamplerState& sampler) { |
| 167 | *(this->drawState()->sampler(stage)) = sampler; |
| 168 | } |
| 169 | const GrSamplerState& getSampler(int stage) const { |
| 170 | return this->getDrawState().getSampler(stage); |
| 171 | } |
| 172 | void preConcatSamplerMatrices(StageMask stageMask, |
| 173 | const GrMatrix& m) { |
| 174 | this->drawState()->preConcatSamplerMatrices(stageMask, m); |
| 175 | } |
| 176 | GrColor getColor() const { return this->getDrawState().getColor(); } |
| 177 | void setColor(GrColor color) { this->drawState()->setColor(color); } |
| 178 | void setFirstCoverageStage(int firstCoverageStage) { |
| 179 | this->drawState()->setFirstCoverageStage(firstCoverageStage); |
| 180 | } |
| 181 | int getFirstCoverageStage() const { |
| 182 | return this->getDrawState().getFirstCoverageStage(); |
| 183 | } |
| 184 | void setDrawFace(const GrDrawState::DrawFace face) { |
| 185 | this->drawState()->setDrawFace(face); |
| 186 | } |
| 187 | GrDrawState::DrawFace getDrawFace() const { |
| 188 | return this->getDrawState().getDrawFace(); |
| 189 | } |
| senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 190 | |
| bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 191 | /** |
| 192 | * Used to save and restore the GrGpu's drawing state |
| 193 | */ |
| 194 | struct SavedDrawState { |
| 195 | private: |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 196 | GrDrawState fState; |
| bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 197 | friend class GrDrawTarget; |
| 198 | }; |
| 199 | |
| 200 | /** |
| 201 | * Saves the current draw state. The state can be restored at a later time |
| 202 | * with restoreDrawState. |
| 203 | * |
| 204 | * See also AutoStateRestore class. |
| 205 | * |
| 206 | * @param state will hold the state after the function returns. |
| 207 | */ |
| 208 | void saveCurrentDrawState(SavedDrawState* state) const; |
| 209 | |
| 210 | /** |
| 211 | * Restores previously saved draw state. The client guarantees that state |
| 212 | * was previously passed to saveCurrentDrawState and that the rendertarget |
| 213 | * and texture set at save are still valid. |
| 214 | * |
| 215 | * See also AutoStateRestore class. |
| 216 | * |
| 217 | * @param state the previously saved state to restore. |
| 218 | */ |
| 219 | void restoreDrawState(const SavedDrawState& state); |
| 220 | |
| 221 | /** |
| 222 | * Copies the draw state from another target to this target. |
| 223 | * |
| 224 | * @param srcTarget draw target used as src of the draw state. |
| 225 | */ |
| 226 | void copyDrawState(const GrDrawTarget& srcTarget); |
| 227 | |
| 228 | /** |
| 229 | * The format of vertices is represented as a bitfield of flags. |
| 230 | * Flags that indicate the layout of vertex data. Vertices always contain |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 231 | * positions and may also contain up to GrDrawState::kMaxTexCoords sets |
| 232 | * of 2D texture * coordinates, per-vertex colors, and per-vertex coverage. |
| 233 | * Each stage can |
| bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 234 | * use any of the texture coordinates as its input texture coordinates or it |
| 235 | * may use the positions as texture coordinates. |
| 236 | * |
| 237 | * If no texture coordinates are specified for a stage then the stage is |
| 238 | * disabled. |
| 239 | * |
| 240 | * Only one type of texture coord can be specified per stage. For |
| 241 | * example StageTexCoordVertexLayoutBit(0, 2) and |
| 242 | * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified. |
| 243 | * |
| 244 | * The order in memory is always (position, texture coord 0, ..., color, |
| 245 | * coverage) with any unused fields omitted. Note that this means that if |
| 246 | * only texture coordinates 1 is referenced then there is no texture |
| 247 | * coordinates 0 and the order would be (position, texture coordinate 1 |
| 248 | * [, color][, coverage]). |
| 249 | */ |
| 250 | |
| 251 | /** |
| 252 | * Generates a bit indicating that a texture stage uses texture coordinates |
| 253 | * |
| 254 | * @param stage the stage that will use texture coordinates. |
| 255 | * @param texCoordIdx the index of the texture coordinates to use |
| 256 | * |
| 257 | * @return the bit to add to a GrVertexLayout bitfield. |
| 258 | */ |
| 259 | static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) { |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 260 | GrAssert(stage < GrDrawState::kNumStages); |
| 261 | GrAssert(texCoordIdx < GrDrawState::kMaxTexCoords); |
| 262 | return 1 << (stage + (texCoordIdx * GrDrawState::kNumStages)); |
| bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 265 | private: |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 266 | static const int TEX_COORD_BIT_CNT = GrDrawState::kNumStages * |
| 267 | GrDrawState::kMaxTexCoords; |
| bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 268 | |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 269 | public: |
| 270 | /** |
| 271 | * Generates a bit indicating that a texture stage uses the position |
| 272 | * as its texture coordinate. |
| 273 | * |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 274 | * @param stage the stage that will use position as texture |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 275 | * coordinates. |
| 276 | * |
| 277 | * @return the bit to add to a GrVertexLayout bitfield. |
| 278 | */ |
| 279 | static int StagePosAsTexCoordVertexLayoutBit(int stage) { |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 280 | GrAssert(stage < GrDrawState::kNumStages); |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 281 | return (1 << (TEX_COORD_BIT_CNT + stage)); |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 282 | } |
| bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 283 | |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 284 | private: |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 285 | static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT + |
| 286 | GrDrawState::kNumStages; |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 287 | |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 288 | public: |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 289 | |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 290 | /** |
| 291 | * Additional Bits that can be specified in GrVertexLayout. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 292 | */ |
| 293 | enum VertexLayoutBits { |
| bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 294 | /* vertices have colors (GrColor) */ |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 295 | kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0), |
| bsalomon@google.com | e10f6fd | 2011-10-11 20:15:26 +0000 | [diff] [blame] | 296 | /* vertices have coverage (GrColor where all channels should have the |
| 297 | * same value) |
| 298 | */ |
| bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 299 | kCoverage_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1), |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 300 | /* 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] | 301 | * text [GrGpuTextVertex vs GrPoint].) |
| 302 | */ |
| bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 303 | kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 2), |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 304 | |
| bsalomon@google.com | e10f6fd | 2011-10-11 20:15:26 +0000 | [diff] [blame] | 305 | /* Each vertex specificies an edge. Distance to the edge is used to |
| 306 | * compute a coverage. See setVertexEdgeType(). |
| 307 | */ |
| bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 308 | kEdge_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 3), |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 309 | // for below assert |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 310 | kDummyVertexLayoutBit, |
| 311 | kHighVertexLayoutBit = kDummyVertexLayoutBit - 1 |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 312 | }; |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 313 | // 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] | 314 | GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout))); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 315 | |
| 316 | /** |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 317 | * There are three methods for specifying geometry (vertices and optionally |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 318 | * 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] | 319 | * can use a different method. Once geometry is specified it can be used for |
| 320 | * multiple drawIndexed and drawNonIndexed calls. |
| 321 | * |
| 322 | * Sometimes it is necessary to perform a draw while upstack code has |
| 323 | * already specified geometry that it isn't finished with. There are push |
| 324 | * pop methods |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 325 | * |
| 326 | * 1. Provide a cpu array (set*SourceToArray). This is useful when the |
| 327 | * caller's client has already provided vertex data in a format |
| 328 | * the time compatible with a GrVertexLayout. The array must contain the |
| 329 | * data at set*SourceToArray is called. The source stays in effect for |
| 330 | * drawIndexed & drawNonIndexed calls until set*SourceToArray is called |
| 331 | * again or one of the other two paths is chosen. |
| 332 | * |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 333 | * 2. Reserve. This is most useful when the caller has data it must |
| 334 | * transform before drawing and is not long-lived. The caller requests |
| 335 | * that the draw target make room for some amount of vertex and/or index |
| 336 | * data. The target provides ptrs to hold the vertex and/or index data. |
| 337 | * |
| 338 | * The data is writable up until the next drawIndexed, drawNonIndexed, |
| 339 | * or pushGeometrySource At this point the data is frozen and the ptrs |
| 340 | * are no longer valid. |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 341 | * |
| 342 | * 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] | 343 | * is long-lived. SetVertexSourceToBuffer and SetIndexSourceToBuffer are |
| 344 | * used to set the buffer and subsequent drawIndexed and drawNonIndexed |
| 345 | * calls use this source until another source is set. |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 346 | */ |
| 347 | |
| 348 | /** |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 349 | * Reserves space for vertices. Draw target will use reserved vertices at |
| 350 | * at the next draw. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 351 | * |
| 352 | * If succeeds: |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 353 | * if vertexCount > 0, *vertices will be the array |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 354 | * of vertices to be filled by caller. The next draw will read |
| 355 | * these vertices. |
| 356 | * |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 357 | * 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] | 358 | * preferred way to allocate vertex data. It allows the subclass of |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 359 | * GrDrawTarget to decide whether to put data in buffers, to group vertex |
| 360 | * 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] | 361 | * |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 362 | * After the next draw or pushGeometrySource the vertices ptr is no longer |
| 363 | * valid and the geometry data cannot be further modified. The contents |
| 364 | * that were put in the reserved space can be drawn by multiple draws, |
| 365 | * however. |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 366 | * |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 367 | * @param vertexLayout the format of vertices (ignored if vertexCount == 0). |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 368 | * @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] | 369 | * @param vertices will point to reserved vertex space if vertexCount is |
| 370 | * non-zero. Illegal to pass NULL if vertexCount > 0. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 371 | * |
| 372 | * @return true if succeeded in allocating space for the vertices and false |
| 373 | * if not. |
| 374 | */ |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 375 | bool reserveVertexSpace(GrVertexLayout vertexLayout, |
| 376 | int vertexCount, |
| 377 | void** vertices); |
| 378 | /** |
| 379 | * Reserves space for indices. Draw target will use the reserved indices at |
| 380 | * the next indexed draw. |
| 381 | * |
| 382 | * If succeeds: |
| 383 | * if indexCount > 0, *indices will be the array |
| 384 | * of indices to be filled by caller. The next draw will read |
| 385 | * these indices. |
| 386 | * |
| 387 | * If a client does not already have a index buffer then this is the |
| 388 | * preferred way to allocate index data. It allows the subclass of |
| 389 | * GrDrawTarget to decide whether to put data in buffers, to group index |
| 390 | * data that uses the same state (e.g. for deferred rendering), etc. |
| 391 | * |
| 392 | * After the next indexed draw or pushGeometrySource the indices ptr is no |
| 393 | * longer valid and the geometry data cannot be further modified. The |
| 394 | * contents that were put in the reserved space can be drawn by multiple |
| 395 | * draws, however. |
| 396 | * |
| 397 | * @param indexCount the number of indices to reserve space for. Can be 0. |
| 398 | * @param indices will point to reserved index space if indexCount is |
| 399 | * non-zero. Illegal to pass NULL if indexCount > 0. |
| 400 | */ |
| 401 | |
| 402 | bool reserveIndexSpace(int indexCount, void** indices); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 403 | /** |
| 404 | * Provides hints to caller about the number of vertices and indices |
| 405 | * that can be allocated cheaply. This can be useful if caller is reserving |
| 406 | * space but doesn't know exactly how much geometry is needed. |
| 407 | * |
| 408 | * Also may hint whether the draw target should be flushed first. This is |
| 409 | * useful for deferred targets. |
| 410 | * |
| 411 | * @param vertexLayout layout of vertices caller would like to reserve |
| 412 | * @param vertexCount in: hint about how many vertices the caller would |
| 413 | * like to allocate. |
| 414 | * out: a hint about the number of vertices that can be |
| 415 | * allocated cheaply. Negative means no hint. |
| 416 | * Ignored if NULL. |
| 417 | * @param indexCount in: hint about how many indices the caller would |
| 418 | * like to allocate. |
| 419 | * out: a hint about the number of indices that can be |
| 420 | * allocated cheaply. Negative means no hint. |
| 421 | * Ignored if NULL. |
| 422 | * |
| 423 | * @return true if target should be flushed based on the input values. |
| 424 | */ |
| 425 | virtual bool geometryHints(GrVertexLayout vertexLayout, |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 426 | int* vertexCount, |
| 427 | int* indexCount) const; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 428 | |
| 429 | /** |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 430 | * Sets source of vertex data for the next draw. Array must contain |
| 431 | * the vertex data when this is called. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 432 | * |
| 433 | * @param array cpu array containing vertex data. |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 434 | * @param size size of the vertex data. |
| 435 | * @param vertexCount the number of vertices in the array. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 436 | */ |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 437 | void setVertexSourceToArray(GrVertexLayout vertexLayout, |
| 438 | const void* vertexArray, |
| 439 | int vertexCount); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 440 | |
| 441 | /** |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 442 | * Sets source of index data for the next indexed draw. Array must contain |
| 443 | * the indices when this is called. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 444 | * |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 445 | * @param array cpu array containing index data. |
| 446 | * @param indexCount the number of indices in the array. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 447 | */ |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 448 | void setIndexSourceToArray(const void* indexArray, int indexCount); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 449 | |
| 450 | /** |
| 451 | * Sets source of vertex data for the next draw. Data does not have to be |
| 452 | * in the buffer until drawIndexed or drawNonIndexed. |
| 453 | * |
| 454 | * @param buffer vertex buffer containing vertex data. Must be |
| 455 | * unlocked before draw call. |
| 456 | * @param vertexLayout layout of the vertex data in the buffer. |
| 457 | */ |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 458 | void setVertexSourceToBuffer(GrVertexLayout vertexLayout, |
| 459 | const GrVertexBuffer* buffer); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 460 | |
| 461 | /** |
| 462 | * Sets source of index data for the next indexed draw. Data does not have |
| 463 | * to be in the buffer until drawIndexed or drawNonIndexed. |
| 464 | * |
| 465 | * @param buffer index buffer containing indices. Must be unlocked |
| 466 | * before indexed draw call. |
| 467 | */ |
| 468 | void setIndexSourceToBuffer(const GrIndexBuffer* buffer); |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 469 | |
| 470 | /** |
| 471 | * Resets vertex source. Drawing from reset vertices is illegal. Set vertex |
| 472 | * source to reserved, array, or buffer before next draw. May be able to free |
| 473 | * up temporary storage allocated by setVertexSourceToArray or |
| 474 | * reserveVertexSpace. |
| 475 | */ |
| 476 | void resetVertexSource(); |
| 477 | |
| 478 | /** |
| 479 | * Resets index source. Indexed Drawing from reset indices is illegal. Set |
| 480 | * index source to reserved, array, or buffer before next indexed draw. May |
| 481 | * be able to free up temporary storage allocated by setIndexSourceToArray |
| 482 | * or reserveIndexSpace. |
| 483 | */ |
| 484 | void resetIndexSource(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 485 | |
| 486 | /** |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 487 | * Pushes and resets the vertex/index sources. Any reserved vertex / index |
| 488 | * data is finalized (i.e. cannot be updated after the matching pop but can |
| 489 | * be drawn from). Must be balanced by a pop. |
| 490 | */ |
| 491 | void pushGeometrySource(); |
| 492 | |
| 493 | /** |
| 494 | * Pops the vertex / index sources from the matching push. |
| 495 | */ |
| 496 | void popGeometrySource(); |
| 497 | |
| 498 | /** |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 499 | * Draws indexed geometry using the current state and current vertex / index |
| 500 | * sources. |
| 501 | * |
| 502 | * @param type The type of primitives to draw. |
| 503 | * @param startVertex the vertex in the vertex array/buffer corresponding |
| 504 | * to index 0 |
| 505 | * @param startIndex first index to read from index src. |
| 506 | * @param vertexCount one greater than the max index. |
| 507 | * @param indexCount the number of index elements to read. The index count |
| 508 | * is effectively trimmed to the last completely |
| 509 | * specified primitive. |
| 510 | */ |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 511 | void drawIndexed(GrPrimitiveType type, |
| 512 | int startVertex, |
| 513 | int startIndex, |
| 514 | int vertexCount, |
| 515 | int indexCount); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 516 | |
| 517 | /** |
| 518 | * Draws non-indexed geometry using the current state and current vertex |
| 519 | * sources. |
| 520 | * |
| 521 | * @param type The type of primitives to draw. |
| 522 | * @param startVertex the vertex in the vertex array/buffer corresponding |
| 523 | * to index 0 |
| 524 | * @param vertexCount one greater than the max index. |
| 525 | */ |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 526 | void drawNonIndexed(GrPrimitiveType type, |
| 527 | int startVertex, |
| 528 | int vertexCount); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 529 | |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 530 | /** |
| 531 | * Helper function for drawing rects. This does not use the current index |
| 532 | * and vertex sources. After returning, the vertex and index sources may |
| 533 | * have changed. They should be reestablished before the next drawIndexed |
| 534 | * or drawNonIndexed. This cannot be called between reserving and releasing |
| 535 | * geometry. The GrDrawTarget subclass may be able to perform additional |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 536 | * optimizations if drawRect is used rather than drawIndexed or |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 537 | * drawNonIndexed. |
| 538 | * @param rect the rect to draw |
| 539 | * @param matrix optional matrix applied to rect (before viewMatrix) |
| bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 540 | * @param stageMask bitmask indicating which stages are enabled. |
| 541 | * Bit i indicates whether stage i is enabled. |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 542 | * @param srcRects specifies rects for stages enabled by stageEnableMask. |
| 543 | * if stageEnableMask bit i is 1, srcRects is not NULL, |
| 544 | * and srcRects[i] is not NULL, then srcRects[i] will be |
| 545 | * used as coordinates for stage i. Otherwise, if stage i |
| 546 | * is enabled then rect is used as the coordinates. |
| 547 | * @param srcMatrices optional matrices applied to srcRects. If |
| 548 | * srcRect[i] is non-NULL and srcMatrices[i] is |
| 549 | * non-NULL then srcRect[i] will be transformed by |
| 550 | * srcMatrix[i]. srcMatrices can be NULL when no |
| 551 | * srcMatrices are desired. |
| 552 | */ |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 553 | virtual void drawRect(const GrRect& rect, |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 554 | const GrMatrix* matrix, |
| bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 555 | StageMask stageMask, |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 556 | const GrRect* srcRects[], |
| 557 | const GrMatrix* srcMatrices[]); |
| 558 | |
| 559 | /** |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 560 | * 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] | 561 | * matrices. |
| 562 | */ |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 563 | void drawSimpleRect(const GrRect& rect, |
| 564 | const GrMatrix* matrix, |
| bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 565 | StageMask stageEnableBitfield) { |
| bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 566 | drawRect(rect, matrix, stageEnableBitfield, NULL, NULL); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 569 | /** |
| bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 570 | * Clear the render target. Ignores the clip and all other draw state |
| 571 | * (blend mode, stages, etc). Clears the whole thing if rect is NULL, |
| 572 | * otherwise just the rect. |
| bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 573 | */ |
| bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 574 | virtual void clear(const GrIRect* rect, GrColor color) = 0; |
| bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 575 | |
| senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 576 | /** |
| 577 | * Returns the maximum number of edges that may be specified in a single |
| 578 | * draw call when performing edge antialiasing. This is usually limited |
| 579 | * by the number of fragment uniforms which may be uploaded. Must be a |
| 580 | * minimum of six, since a triangle's vertices each belong to two boundary |
| 581 | * edges which may be distinct. |
| 582 | */ |
| 583 | virtual int getMaxEdges() const { return 6; } |
| 584 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 585 | //////////////////////////////////////////////////////////////////////////// |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 586 | |
| 587 | class AutoStateRestore : ::GrNoncopyable { |
| 588 | public: |
| bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 589 | AutoStateRestore(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 590 | AutoStateRestore(GrDrawTarget* target); |
| 591 | ~AutoStateRestore(); |
| 592 | |
| bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 593 | /** |
| 594 | * if this object is already saving state for param target then |
| 595 | * this does nothing. Otherise, it restores previously saved state on |
| 596 | * previous target (if any) and saves current state on param target. |
| 597 | */ |
| 598 | void set(GrDrawTarget* target); |
| 599 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 600 | private: |
| 601 | GrDrawTarget* fDrawTarget; |
| 602 | SavedDrawState fDrawState; |
| 603 | }; |
| 604 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 605 | //////////////////////////////////////////////////////////////////////////// |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 606 | |
| bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 607 | class AutoViewMatrixRestore : ::GrNoncopyable { |
| 608 | public: |
| 609 | AutoViewMatrixRestore() { |
| 610 | fDrawTarget = NULL; |
| 611 | } |
| 612 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 613 | AutoViewMatrixRestore(GrDrawTarget* target) |
| bsalomon@google.com | 0fec61d | 2011-12-08 15:53:53 +0000 | [diff] [blame^] | 614 | : fDrawTarget(target), fMatrix(target->getViewMatrix()) { |
| bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 615 | GrAssert(NULL != target); |
| 616 | } |
| 617 | |
| 618 | void set(GrDrawTarget* target) { |
| 619 | GrAssert(NULL != target); |
| 620 | if (NULL != fDrawTarget) { |
| 621 | fDrawTarget->setViewMatrix(fMatrix); |
| 622 | } |
| 623 | fDrawTarget = target; |
| 624 | fMatrix = target->getViewMatrix(); |
| 625 | } |
| 626 | |
| 627 | ~AutoViewMatrixRestore() { |
| 628 | if (NULL != fDrawTarget) { |
| 629 | fDrawTarget->setViewMatrix(fMatrix); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | private: |
| 634 | GrDrawTarget* fDrawTarget; |
| 635 | GrMatrix fMatrix; |
| 636 | }; |
| 637 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 638 | //////////////////////////////////////////////////////////////////////////// |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 639 | |
| bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 640 | /** |
| 641 | * Sets the view matrix to I and preconcats all stage matrices enabled in |
| 642 | * mask by the view inverse. Destructor undoes these changes. |
| 643 | */ |
| 644 | class AutoDeviceCoordDraw : ::GrNoncopyable { |
| 645 | public: |
| bsalomon@google.com | 0fec61d | 2011-12-08 15:53:53 +0000 | [diff] [blame^] | 646 | AutoDeviceCoordDraw(GrDrawTarget* target, |
| 647 | GrDrawState::StageMask stageMask); |
| bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 648 | ~AutoDeviceCoordDraw(); |
| 649 | private: |
| bsalomon@google.com | 0fec61d | 2011-12-08 15:53:53 +0000 | [diff] [blame^] | 650 | GrDrawTarget* fDrawTarget; |
| 651 | GrMatrix fViewMatrix; |
| 652 | GrMatrix fSamplerMatrices[GrDrawState::kNumStages]; |
| 653 | GrDrawState::StageMask fStageMask; |
| bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 654 | }; |
| 655 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 656 | //////////////////////////////////////////////////////////////////////////// |
| bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 657 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 658 | class AutoReleaseGeometry : ::GrNoncopyable { |
| 659 | public: |
| 660 | AutoReleaseGeometry(GrDrawTarget* target, |
| 661 | GrVertexLayout vertexLayout, |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 662 | int vertexCount, |
| 663 | int indexCount); |
| 664 | AutoReleaseGeometry(); |
| 665 | ~AutoReleaseGeometry(); |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 666 | bool set(GrDrawTarget* target, |
| 667 | GrVertexLayout vertexLayout, |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 668 | int vertexCount, |
| 669 | int indexCount); |
| bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 670 | bool succeeded() const { return NULL != fTarget; } |
| bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 671 | void* vertices() const { GrAssert(this->succeeded()); return fVertices; } |
| 672 | void* indices() const { GrAssert(this->succeeded()); return fIndices; } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 673 | GrPoint* positions() const { |
| bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 674 | return static_cast<GrPoint*>(this->vertices()); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | private: |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 678 | void reset(); |
| 679 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 680 | GrDrawTarget* fTarget; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 681 | void* fVertices; |
| 682 | void* fIndices; |
| 683 | }; |
| 684 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 685 | //////////////////////////////////////////////////////////////////////////// |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 686 | |
| 687 | class AutoClipRestore : ::GrNoncopyable { |
| 688 | public: |
| 689 | AutoClipRestore(GrDrawTarget* target) { |
| 690 | fTarget = target; |
| 691 | fClip = fTarget->getClip(); |
| 692 | } |
| 693 | |
| 694 | ~AutoClipRestore() { |
| 695 | fTarget->setClip(fClip); |
| 696 | } |
| 697 | private: |
| 698 | GrDrawTarget* fTarget; |
| 699 | GrClip fClip; |
| 700 | }; |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 701 | |
| 702 | //////////////////////////////////////////////////////////////////////////// |
| 703 | |
| 704 | class AutoGeometryPush : ::GrNoncopyable { |
| 705 | public: |
| 706 | AutoGeometryPush(GrDrawTarget* target) { |
| 707 | GrAssert(NULL != target); |
| 708 | fTarget = target; |
| 709 | target->pushGeometrySource(); |
| 710 | } |
| 711 | ~AutoGeometryPush() { |
| 712 | fTarget->popGeometrySource(); |
| 713 | } |
| 714 | private: |
| 715 | GrDrawTarget* fTarget; |
| 716 | }; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 717 | |
| 718 | //////////////////////////////////////////////////////////////////////////// |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 719 | // Helpers for picking apart vertex layouts |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 720 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 721 | /** |
| 722 | * Helper function to compute the size of a vertex from a vertex layout |
| 723 | * @return size of a single vertex. |
| 724 | */ |
| 725 | static size_t VertexSize(GrVertexLayout vertexLayout); |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 726 | |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 727 | /** |
| 728 | * Helper function for determining the index of texture coordinates that |
| 729 | * is input for a texture stage. Note that a stage may instead use positions |
| 730 | * as texture coordinates, in which case the result of the function is |
| 731 | * indistinguishable from the case when the stage is disabled. |
| 732 | * |
| 733 | * @param stage the stage to query |
| 734 | * @param vertexLayout layout to query |
| 735 | * |
| 736 | * @return the texture coordinate index or -1 if the stage doesn't use |
| 737 | * separate (non-position) texture coordinates. |
| 738 | */ |
| 739 | static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 740 | |
| 741 | /** |
| 742 | * Helper function to compute the offset of texture coordinates in a vertex |
| 743 | * @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] | 744 | * layout has no texture coordinates. Will be 0 if positions are |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 745 | * used as texture coordinates for the stage. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 746 | */ |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 747 | static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 748 | |
| 749 | /** |
| 750 | * Helper function to compute the offset of the color in a vertex |
| 751 | * @return offset of color in vertex layout or -1 if the |
| 752 | * layout has no color. |
| 753 | */ |
| 754 | static int VertexColorOffset(GrVertexLayout vertexLayout); |
| 755 | |
| bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 756 | /** |
| 757 | * Helper function to compute the offset of the coverage in a vertex |
| 758 | * @return offset of coverage in vertex layout or -1 if the |
| 759 | * layout has no coverage. |
| 760 | */ |
| 761 | static int VertexCoverageOffset(GrVertexLayout vertexLayout); |
| 762 | |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 763 | /** |
| 764 | * Helper function to compute the offset of the edge pts in a vertex |
| 765 | * @return offset of edge in vertex layout or -1 if the |
| 766 | * layout has no edge. |
| 767 | */ |
| 768 | static int VertexEdgeOffset(GrVertexLayout vertexLayout); |
| 769 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 770 | /** |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 771 | * Helper function to determine if vertex layout contains explicit texture |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 772 | * coordinates of some index. |
| 773 | * |
| 774 | * @param coordIndex the tex coord index to query |
| 775 | * @param vertexLayout layout to query |
| 776 | * |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 777 | * @return true if vertex specifies texture coordinates for the index, |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 778 | * false otherwise. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 779 | */ |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 780 | static bool VertexUsesTexCoordIdx(int coordIndex, |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 781 | GrVertexLayout vertexLayout); |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 782 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 783 | /** |
| 784 | * Helper function to determine if vertex layout contains either explicit or |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 785 | * implicit texture coordinates for a stage. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 786 | * |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 787 | * @param stage the stage to query |
| 788 | * @param vertexLayout layout to query |
| 789 | * |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 790 | * @return true if vertex specifies texture coordinates for the stage, |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 791 | * false otherwise. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 792 | */ |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 793 | static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 794 | |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 795 | /** |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 796 | * Helper function to compute the size of each vertex and the offsets of |
| 797 | * texture coordinates and color. Determines tex coord offsets by tex coord |
| 798 | * 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] | 799 | * by StageTexCoordVertexLayoutBit.) |
| 800 | * |
| 801 | * @param vertexLayout the layout to query |
| 802 | * @param texCoordOffsetsByIdx after return it is the offset of each |
| 803 | * tex coord index in the vertex or -1 if |
| bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 804 | * index isn't used. (optional) |
| 805 | * @param colorOffset after return it is the offset of the |
| 806 | * color field in each vertex, or -1 if |
| 807 | * there aren't per-vertex colors. (optional) |
| 808 | * @param coverageOffset after return it is the offset of the |
| 809 | * coverage field in each vertex, or -1 if |
| 810 | * there aren't per-vertex coeverages. |
| 811 | * (optional) |
| 812 | * @param edgeOffset after return it is the offset of the |
| 813 | * edge eq field in each vertex, or -1 if |
| 814 | * there aren't per-vertex edge equations. |
| 815 | * (optional) |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 816 | * @return size of a single vertex |
| 817 | */ |
| 818 | static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout, |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 819 | int texCoordOffsetsByIdx[GrDrawState::kMaxTexCoords], |
| 820 | int *colorOffset, |
| 821 | int *coverageOffset, |
| 822 | int* edgeOffset); |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 823 | |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 824 | /** |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 825 | * Helper function to compute the size of each vertex and the offsets of |
| 826 | * texture coordinates and color. Determines tex coord offsets by stage |
| 827 | * rather than by index. (Each stage can be mapped to any t.c. index |
| 828 | * by StageTexCoordVertexLayoutBit.) If a stage uses positions for |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 829 | * tex coords then that stage's offset will be 0 (positions are always at 0). |
| 830 | * |
| 831 | * @param vertexLayout the layout to query |
| 832 | * @param texCoordOffsetsByStage after return it is the offset of each |
| 833 | * tex coord index in the vertex or -1 if |
| bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 834 | * index isn't used. (optional) |
| 835 | * @param colorOffset after return it is the offset of the |
| 836 | * color field in each vertex, or -1 if |
| 837 | * there aren't per-vertex colors. |
| 838 | * (optional) |
| 839 | * @param coverageOffset after return it is the offset of the |
| 840 | * coverage field in each vertex, or -1 if |
| 841 | * there aren't per-vertex coeverages. |
| 842 | * (optional) |
| 843 | * @param edgeOffset after return it is the offset of the |
| 844 | * edge eq field in each vertex, or -1 if |
| 845 | * there aren't per-vertex edge equations. |
| 846 | * (optional) |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 847 | * @return size of a single vertex |
| 848 | */ |
| 849 | static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout, |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 850 | int texCoordOffsetsByStage[GrDrawState::kNumStages], |
| 851 | int* colorOffset, |
| 852 | int* coverageOffset, |
| 853 | int* edgeOffset); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 854 | |
| 855 | /** |
| 856 | * Accessing positions, texture coords, or colors, of a vertex within an |
| 857 | * array is a hassle involving casts and simple math. These helpers exist |
| 858 | * to keep GrDrawTarget clients' code a bit nicer looking. |
| 859 | */ |
| 860 | |
| 861 | /** |
| 862 | * Gets a pointer to a GrPoint of a vertex's position or texture |
| 863 | * coordinate. |
| 864 | * @param vertices the vetex array |
| 865 | * @param vertexIndex the index of the vertex in the array |
| 866 | * @param vertexSize the size of each vertex in the array |
| 867 | * @param offset the offset in bytes of the vertex component. |
| 868 | * Defaults to zero (corresponding to vertex position) |
| 869 | * @return pointer to the vertex component as a GrPoint |
| 870 | */ |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 871 | static GrPoint* GetVertexPoint(void* vertices, |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 872 | int vertexIndex, |
| 873 | int vertexSize, |
| 874 | int offset = 0) { |
| 875 | intptr_t start = GrTCast<intptr_t>(vertices); |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 876 | return GrTCast<GrPoint*>(start + offset + |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 877 | vertexIndex * vertexSize); |
| 878 | } |
| 879 | static const GrPoint* GetVertexPoint(const void* vertices, |
| 880 | int vertexIndex, |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 881 | int vertexSize, |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 882 | int offset = 0) { |
| 883 | intptr_t start = GrTCast<intptr_t>(vertices); |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 884 | return GrTCast<const GrPoint*>(start + offset + |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 885 | vertexIndex * vertexSize); |
| 886 | } |
| 887 | |
| 888 | /** |
| 889 | * Gets a pointer to a GrColor inside a vertex within a vertex array. |
| 890 | * @param vertices the vetex array |
| 891 | * @param vertexIndex the index of the vertex in the array |
| 892 | * @param vertexSize the size of each vertex in the array |
| 893 | * @param offset the offset in bytes of the vertex color |
| 894 | * @return pointer to the vertex component as a GrColor |
| 895 | */ |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 896 | static GrColor* GetVertexColor(void* vertices, |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 897 | int vertexIndex, |
| 898 | int vertexSize, |
| 899 | int offset) { |
| 900 | intptr_t start = GrTCast<intptr_t>(vertices); |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 901 | return GrTCast<GrColor*>(start + offset + |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 902 | vertexIndex * vertexSize); |
| 903 | } |
| 904 | static const GrColor* GetVertexColor(const void* vertices, |
| 905 | int vertexIndex, |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 906 | int vertexSize, |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 907 | int offset) { |
| 908 | const intptr_t start = GrTCast<intptr_t>(vertices); |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 909 | return GrTCast<const GrColor*>(start + offset + |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 910 | vertexIndex * vertexSize); |
| 911 | } |
| 912 | |
| bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 913 | static void VertexLayoutUnitTest(); |
| 914 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 915 | protected: |
| bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 916 | |
| bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 917 | /** |
| 918 | * Optimizations for blending / coverage to be applied based on the current |
| 919 | * state. |
| 920 | * Subclasses that actually draw (as opposed to those that just buffer for |
| 921 | * playback) must implement the flags that replace the output color. |
| 922 | */ |
| 923 | enum BlendOptFlags { |
| 924 | /** |
| 925 | * No optimization |
| 926 | */ |
| 927 | kNone_BlendOpt = 0, |
| 928 | /** |
| 929 | * Don't draw at all |
| 930 | */ |
| 931 | kSkipDraw_BlendOptFlag = 0x2, |
| 932 | /** |
| 933 | * Emit the src color, disable HW blending (replace dst with src) |
| 934 | */ |
| 935 | kDisableBlend_BlendOptFlag = 0x4, |
| 936 | /** |
| 937 | * The coverage value does not have to be computed separately from |
| 938 | * alpha, the the output color can be the modulation of the two. |
| 939 | */ |
| 940 | kCoverageAsAlpha_BlendOptFlag = 0x1, |
| 941 | /** |
| 942 | * Instead of emitting a src color, emit coverage in the alpha channel |
| 943 | * and r,g,b are "don't cares". |
| 944 | */ |
| 945 | kEmitCoverage_BlendOptFlag = 0x10, |
| 946 | /** |
| 947 | * Emit transparent black instead of the src color, no need to compute |
| 948 | * coverage. |
| 949 | */ |
| 950 | kEmitTransBlack_BlendOptFlag = 0x8, |
| 951 | }; |
| 952 | GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags); |
| bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 953 | |
| bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 954 | // Determines what optimizations can be applied based on the blend. |
| 955 | // The coeffecients may have to be tweaked in order for the optimization |
| 956 | // to work. srcCoeff and dstCoeff are optional params that receive the |
| 957 | // tweaked coeffecients. |
| 958 | // Normally the function looks at the current state to see if coverage |
| 959 | // is enabled. By setting forceCoverage the caller can speculatively |
| 960 | // determine the blend optimizations that would be used if there was |
| 961 | // partial pixel coverage |
| 962 | BlendOptFlags getBlendOpts(bool forceCoverage = false, |
| 963 | GrBlendCoeff* srcCoeff = NULL, |
| 964 | GrBlendCoeff* dstCoeff = NULL) const; |
| 965 | |
| 966 | // determine if src alpha is guaranteed to be one for all src pixels |
| 967 | bool srcAlphaWillBeOne() const; |
| bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 968 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 969 | enum GeometrySrcType { |
| 970 | kNone_GeometrySrcType, //<! src has not been specified |
| 971 | kReserved_GeometrySrcType, //<! src was set using reserve*Space |
| 972 | kArray_GeometrySrcType, //<! src was set using set*SourceToArray |
| 973 | kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer |
| 974 | }; |
| 975 | |
| 976 | struct GeometrySrcState { |
| 977 | GeometrySrcType fVertexSrc; |
| 978 | union { |
| 979 | // valid if src type is buffer |
| 980 | const GrVertexBuffer* fVertexBuffer; |
| 981 | // valid if src type is reserved or array |
| 982 | int fVertexCount; |
| 983 | }; |
| 984 | |
| 985 | GeometrySrcType fIndexSrc; |
| 986 | union { |
| 987 | // valid if src type is buffer |
| 988 | const GrIndexBuffer* fIndexBuffer; |
| 989 | // valid if src type is reserved or array |
| 990 | int fIndexCount; |
| 991 | }; |
| 992 | |
| 993 | GrVertexLayout fVertexLayout; |
| 994 | }; |
| 995 | |
| bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 996 | // given a vertex layout and a draw state, will a stage be used? |
| 997 | static bool StageWillBeUsed(int stage, GrVertexLayout layout, |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 998 | const GrDrawState& state) { |
| bsalomon@google.com | 0fec61d | 2011-12-08 15:53:53 +0000 | [diff] [blame^] | 999 | return NULL != state.getTexture(stage) && |
| 1000 | VertexUsesStage(stage, layout); |
| bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | bool isStageEnabled(int stage) const { |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1004 | return StageWillBeUsed(stage, this->getGeomSrc().fVertexLayout, |
| 1005 | fCurrDrawState); |
| bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 1006 | } |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1007 | |
| bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 1008 | StageMask enabledStages() const { |
| 1009 | StageMask mask = 0; |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1010 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1011 | mask |= this->isStageEnabled(s) ? 1 : 0; |
| 1012 | } |
| 1013 | return mask; |
| 1014 | } |
| 1015 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1016 | // Helpers for GrDrawTarget subclasses that won't have private access to |
| 1017 | // SavedDrawState but need to peek at the state values. |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1018 | static GrDrawState& accessSavedDrawState(SavedDrawState& sds) |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1019 | { return sds.fState; } |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1020 | static const GrDrawState& accessSavedDrawState(const SavedDrawState& sds) |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1021 | { return sds.fState; } |
| 1022 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1023 | // implemented by subclass to allocate space for reserved geom |
| 1024 | virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout, |
| 1025 | int vertexCount, |
| 1026 | void** vertices) = 0; |
| 1027 | virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0; |
| 1028 | // implemented by subclass to handle release of reserved geom space |
| 1029 | virtual void releaseReservedVertexSpace() = 0; |
| 1030 | virtual void releaseReservedIndexSpace() = 0; |
| 1031 | // subclass must consume array contents when set |
| 1032 | virtual void onSetVertexSourceToArray(const void* vertexArray, |
| 1033 | int vertexCount) = 0; |
| 1034 | virtual void onSetIndexSourceToArray(const void* indexArray, |
| 1035 | int indexCount) = 0; |
| 1036 | // subclass is notified that geom source will be set away from an array |
| 1037 | virtual void releaseVertexArray() = 0; |
| 1038 | virtual void releaseIndexArray() = 0; |
| 1039 | // subclass overrides to be notified just before geo src state |
| 1040 | // is pushed/popped. |
| 1041 | virtual void geometrySourceWillPush() = 0; |
| 1042 | virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0; |
| 1043 | // subclass called to perform drawing |
| 1044 | virtual void onDrawIndexed(GrPrimitiveType type, |
| 1045 | int startVertex, |
| 1046 | int startIndex, |
| 1047 | int vertexCount, |
| 1048 | int indexCount) = 0; |
| 1049 | virtual void onDrawNonIndexed(GrPrimitiveType type, |
| 1050 | int startVertex, |
| 1051 | int vertexCount) = 0; |
| bsalomon@google.com | dea2f8d | 2011-08-01 15:51:05 +0000 | [diff] [blame] | 1052 | // subclass overrides to be notified when clip is set. Must call |
| 1053 | // INHERITED::clipwillBeSet |
| 1054 | virtual void clipWillBeSet(const GrClip& clip); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 1055 | |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1056 | // Helpers for drawRect, protected so subclasses that override drawRect |
| 1057 | // can use them. |
| bsalomon@google.com | 0fec61d | 2011-12-08 15:53:53 +0000 | [diff] [blame^] | 1058 | static GrVertexLayout GetRectVertexLayout(StageMask stageMask, |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1059 | const GrRect* srcRects[]); |
| 1060 | |
| 1061 | static void SetRectVertices(const GrRect& rect, |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1062 | const GrMatrix* matrix, |
| 1063 | const GrRect* srcRects[], |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1064 | const GrMatrix* srcMatrices[], |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1065 | GrVertexLayout layout, |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1066 | void* vertices); |
| 1067 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1068 | // accessor for derived classes |
| 1069 | const GeometrySrcState& getGeomSrc() const { |
| 1070 | return fGeoSrcStateStack.back(); |
| 1071 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1072 | |
| 1073 | GrClip fClip; |
| 1074 | |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1075 | GrDrawState fCurrDrawState; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1076 | |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 1077 | Caps fCaps; |
| 1078 | |
| bsalomon@google.com | 4a018bb | 2011-10-28 19:50:21 +0000 | [diff] [blame] | 1079 | // subclasses must call this in their destructors to ensure all vertex |
| 1080 | // and index sources have been released (including those held by |
| 1081 | // pushGeometrySource()) |
| 1082 | void releaseGeometry(); |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1083 | private: |
| bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 1084 | // called by drawIndexed and drawNonIndexed. Use a negative indexCount to |
| 1085 | // indicate non-indexed drawing. |
| 1086 | bool checkDraw(GrPrimitiveType type, int startVertex, |
| 1087 | int startIndex, int vertexCount, |
| 1088 | int indexCount) const; |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1089 | // called when setting a new vert/idx source to unref prev vb/ib |
| 1090 | void releasePreviousVertexSource(); |
| 1091 | void releasePreviousIndexSource(); |
| 1092 | |
| 1093 | enum { |
| 1094 | kPreallocGeoSrcStateStackCnt = 4, |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1095 | }; |
| bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 1096 | SkSTArray<kPreallocGeoSrcStateStackCnt, |
| 1097 | GeometrySrcState, true> fGeoSrcStateStack; |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1098 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1099 | }; |
| 1100 | |
| bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 1101 | GR_MAKE_BITFIELD_OPS(GrDrawTarget::BlendOptFlags); |
| 1102 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1103 | #endif |