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