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