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