| 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 | |
| bsalomon@google.com | 8d67c07 | 2012-12-13 20:38:14 +0000 | [diff] [blame] | 14 | #include "GrClipData.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 | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 17 | #include "SkMatrix.h" |
| bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 18 | #include "GrRefCnt.h" |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 19 | |
| bsalomon@google.com | 8d67c07 | 2012-12-13 20:38:14 +0000 | [diff] [blame] | 20 | #include "SkClipStack.h" |
| sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 21 | #include "SkPath.h" |
| bsalomon@google.com | 46f7afb | 2012-01-18 19:51:55 +0000 | [diff] [blame] | 22 | #include "SkTLazy.h" |
| robertphillips@google.com | a2d7148 | 2012-08-01 20:08:47 +0000 | [diff] [blame] | 23 | #include "SkTArray.h" |
| bsalomon@google.com | 8d67c07 | 2012-12-13 20:38:14 +0000 | [diff] [blame] | 24 | #include "SkXfermode.h" |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 25 | |
| robertphillips@google.com | a2d7148 | 2012-08-01 20:08:47 +0000 | [diff] [blame] | 26 | class GrClipData; |
| bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 27 | class GrPath; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 28 | class GrVertexBuffer; |
| sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 29 | class SkStrokeRec; |
| sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 30 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 31 | class GrDrawTarget : public GrRefCnt { |
| bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 32 | protected: |
| 33 | /** This helper class allows GrDrawTarget subclasses to set the caps values without having to be |
| 34 | made a friend of GrDrawTarget::Caps. */ |
| 35 | class CapsInternals { |
| 36 | public: |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 37 | bool f8BitPaletteSupport : 1; |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 38 | bool fNPOTTextureTileSupport : 1; |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 39 | bool fTwoSidedStencilSupport : 1; |
| 40 | bool fStencilWrapOpsSupport : 1; |
| 41 | bool fHWAALineSupport : 1; |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 42 | bool fShaderDerivativeSupport : 1; |
| bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 43 | bool fGeometryShaderSupport : 1; |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 44 | bool fFSAASupport : 1; |
| 45 | bool fDualSourceBlendingSupport : 1; |
| 46 | bool fBufferLockSupport : 1; |
| bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 47 | bool fPathStencilingSupport : 1; |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 48 | int fMaxRenderTargetSize; |
| 49 | int fMaxTextureSize; |
| 50 | }; |
| 51 | |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 52 | class DrawInfo; |
| 53 | |
| bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 54 | public: |
| 55 | SK_DECLARE_INST_COUNT(GrDrawTarget) |
| 56 | |
| 57 | /** |
| 58 | * Represents the draw target capabilities. |
| 59 | */ |
| 60 | class Caps { |
| 61 | public: |
| 62 | Caps() { memset(this, 0, sizeof(Caps)); } |
| 63 | Caps(const Caps& c) { *this = c; } |
| 64 | Caps& operator= (const Caps& c) { |
| 65 | memcpy(this, &c, sizeof(Caps)); |
| 66 | return *this; |
| 67 | } |
| 68 | void print() const; |
| 69 | |
| 70 | bool eightBitPaletteSupport() const { return fInternals.f8BitPaletteSupport; } |
| 71 | bool npotTextureTileSupport() const { return fInternals.fNPOTTextureTileSupport; } |
| 72 | bool twoSidedStencilSupport() const { return fInternals.fTwoSidedStencilSupport; } |
| 73 | bool stencilWrapOpsSupport() const { return fInternals.fStencilWrapOpsSupport; } |
| 74 | bool hwAALineSupport() const { return fInternals.fHWAALineSupport; } |
| 75 | bool shaderDerivativeSupport() const { return fInternals.fShaderDerivativeSupport; } |
| 76 | bool geometryShaderSupport() const { return fInternals.fGeometryShaderSupport; } |
| 77 | bool fsaaSupport() const { return fInternals.fFSAASupport; } |
| 78 | bool dualSourceBlendingSupport() const { return fInternals.fDualSourceBlendingSupport; } |
| 79 | bool bufferLockSupport() const { return fInternals.fBufferLockSupport; } |
| 80 | bool pathStencilingSupport() const { return fInternals.fPathStencilingSupport; } |
| 81 | |
| 82 | int maxRenderTargetSize() const { return fInternals.fMaxRenderTargetSize; } |
| 83 | int maxTextureSize() const { return fInternals.fMaxTextureSize; } |
| 84 | private: |
| 85 | CapsInternals fInternals; |
| bsalomon@google.com | 0d82fe5 | 2012-08-28 21:39:15 +0000 | [diff] [blame] | 86 | friend class GrDrawTarget; // to set values of fInternals |
| bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 89 | /////////////////////////////////////////////////////////////////////////// |
| 90 | |
| 91 | GrDrawTarget(); |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 92 | virtual ~GrDrawTarget(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 93 | |
| 94 | /** |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 95 | * Gets the capabilities of the draw target. |
| 96 | */ |
| 97 | const Caps& getCaps() const { return fCaps; } |
| 98 | |
| 99 | /** |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 100 | * Sets the current clip to the region specified by clip. All draws will be |
| 101 | * clipped against this clip if kClip_StateBit is enabled. |
| 102 | * |
| bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 103 | * Setting the clip may (or may not) zero out the client's stencil bits. |
| 104 | * |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 105 | * @param description of the clipping region |
| 106 | */ |
| robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 107 | void setClip(const GrClipData* clip); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Gets the current clip. |
| 111 | * |
| 112 | * @return the clip. |
| 113 | */ |
| robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 114 | const GrClipData* getClip() const; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 115 | |
| bsalomon@google.com | a5d056a | 2012-03-27 15:59:58 +0000 | [diff] [blame] | 116 | /** |
| 117 | * Sets the draw state object for the draw target. Note that this does not |
| 118 | * make a copy. The GrDrawTarget will take a reference to passed object. |
| 119 | * Passing NULL will cause the GrDrawTarget to use its own internal draw |
| 120 | * state object rather than an externally provided one. |
| 121 | */ |
| 122 | void setDrawState(GrDrawState* drawState); |
| 123 | |
| 124 | /** |
| 125 | * Read-only access to the GrDrawTarget's current draw state. |
| 126 | */ |
| 127 | const GrDrawState& getDrawState() const { return *fDrawState; } |
| 128 | |
| 129 | /** |
| 130 | * Read-write access to the GrDrawTarget's current draw state. Note that |
| 131 | * this doesn't ref. |
| 132 | */ |
| 133 | GrDrawState* drawState() { return fDrawState; } |
| bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 134 | |
| bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 135 | /** |
| bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 136 | * Color alpha and coverage are two inputs to the drawing pipeline. For some |
| 137 | * blend modes it is safe to fold the coverage into constant or per-vertex |
| 138 | * color alpha value. For other blend modes they must be handled separately. |
| 139 | * Depending on features available in the underlying 3D API this may or may |
| 140 | * not be possible. |
| 141 | * |
| bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 142 | * This function considers the current draw state and the draw target's |
| 143 | * capabilities to determine whether coverage can be handled correctly. The |
| 144 | * following assumptions are made: |
| 145 | * 1. The caller intends to somehow specify coverage. This can be |
| 146 | * specified either by enabling a coverage stage on the GrDrawState or |
| 147 | * via the vertex layout. |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 148 | * 2. Other than enabling coverage stages, the current configuration of |
| bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 149 | * the target's GrDrawState is as it will be at draw time. |
| 150 | * 3. If a vertex source has not yet been specified then all stages with |
| 151 | * non-NULL textures will be referenced by the vertex layout. |
| bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 152 | */ |
| 153 | bool canApplyCoverage() const; |
| 154 | |
| 155 | /** |
| 156 | * Determines whether incorporating partial pixel coverage into the constant |
| 157 | * 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] | 158 | * blending result. If a vertex source has not yet been specified then |
| 159 | * the function assumes that all stages with non-NULL textures will be |
| 160 | * referenced by the vertex layout. |
| bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 161 | */ |
| bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 162 | bool canTweakAlphaForCoverage() const; |
| bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 163 | |
| senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 164 | /** |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 165 | * 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] | 166 | * (if line primitive type is drawn)? If a vertex source has not yet been |
| 167 | * specified then the function assumes that all stages with non-NULL |
| 168 | * textures will be referenced by the vertex layout. |
| bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 169 | */ |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 170 | bool willUseHWAALines() const; |
| bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 171 | |
| bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 172 | /** |
| bsalomon@google.com | e3d7095 | 2012-03-13 12:40:53 +0000 | [diff] [blame] | 173 | * There are three types of "sources" of geometry (vertices and indices) for |
| 174 | * draw calls made on the target. When performing an indexed draw, the |
| 175 | * 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] | 176 | * specified it can be used for multiple draws. However, the time at which |
| 177 | * 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] | 178 | * |
| 179 | * 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] | 180 | * already specified geometry that it isn't finished with. So there are push |
| 181 | * and pop methods. This allows the client to push the sources, draw |
| 182 | * something using alternate sources, and then pop to restore the original |
| 183 | * sources. |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 184 | * |
| bsalomon@google.com | e3d7095 | 2012-03-13 12:40:53 +0000 | [diff] [blame] | 185 | * Aside from pushes and pops, a source remains valid until another source |
| 186 | * is set or resetVertexSource / resetIndexSource is called. Drawing from |
| 187 | * a reset source is an error. |
| 188 | * |
| 189 | * The three types of sources are: |
| 190 | * |
| 191 | * 1. A cpu array (set*SourceToArray). This is useful when the caller |
| 192 | * already provided vertex data in a format compatible with a |
| 193 | * GrVertexLayout. The data in the array is consumed at the time that |
| 194 | * set*SourceToArray is called and subsequent edits to the array will not |
| 195 | * be reflected in draws. |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 196 | * |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 197 | * 2. Reserve. This is most useful when the caller has data it must |
| 198 | * transform before drawing and is not long-lived. The caller requests |
| 199 | * that the draw target make room for some amount of vertex and/or index |
| 200 | * data. The target provides ptrs to hold the vertex and/or index data. |
| 201 | * |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 202 | * The data is writable up until the next drawIndexed, drawNonIndexed, |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 203 | * drawIndexedInstances, or pushGeometrySource. At this point the data is |
| 204 | * frozen and the ptrs are no longer valid. |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 205 | * |
| bsalomon@google.com | e3d7095 | 2012-03-13 12:40:53 +0000 | [diff] [blame] | 206 | * Where the space is allocated and how it is uploaded to the GPU is |
| 207 | * subclass-dependent. |
| 208 | * |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 209 | * 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] | 210 | * 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] | 211 | * GrDrawTarget subclass. For deferred subclasses the caller has to |
| bsalomon@google.com | e3d7095 | 2012-03-13 12:40:53 +0000 | [diff] [blame] | 212 | * guarantee that the data is still available in the buffers at playback. |
| 213 | * (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] | 214 | */ |
| 215 | |
| 216 | /** |
| bsalomon@google.com | e3d7095 | 2012-03-13 12:40:53 +0000 | [diff] [blame] | 217 | * Reserves space for vertices and/or indices. Zero can be specifed as |
| 218 | * 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] | 219 | * 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] | 220 | * vertexCount then the vertex source will be unmodified and likewise for |
| 221 | * indexCount. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 222 | * |
| bsalomon@google.com | e3d7095 | 2012-03-13 12:40:53 +0000 | [diff] [blame] | 223 | * If the function returns true then the reserve suceeded and the vertices |
| 224 | * and indices pointers will point to the space created. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 225 | * |
| bsalomon@google.com | e3d7095 | 2012-03-13 12:40:53 +0000 | [diff] [blame] | 226 | * If the target cannot make space for the request then this function will |
| 227 | * return false. If vertexCount was non-zero then upon failure the vertex |
| 228 | * source is reset and likewise for indexCount. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 229 | * |
| bsalomon@google.com | e3d7095 | 2012-03-13 12:40:53 +0000 | [diff] [blame] | 230 | * 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] | 231 | * until a drawIndexed, drawNonIndexed, drawIndexedInstances, or push/ |
| 232 | * popGeomtrySource is called. At that point logically a snapshot of the |
| 233 | * data is made and the pointers are invalid. |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 234 | * |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 235 | * @param vertexLayout the format of vertices (ignored if vertexCount == 0). |
| bsalomon@google.com | e3d7095 | 2012-03-13 12:40:53 +0000 | [diff] [blame] | 236 | * @param vertexCount the number of vertices to reserve space for. Can be |
| 237 | * 0. |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 238 | * @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] | 239 | * @param vertices will point to reserved vertex space if vertexCount is |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 240 | * non-zero. Illegal to pass NULL if vertexCount > 0. |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 241 | * @param indices will point to reserved index space if indexCount is |
| 242 | * non-zero. Illegal to pass NULL if indexCount > 0. |
| 243 | */ |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 244 | bool reserveVertexAndIndexSpace(GrVertexLayout vertexLayout, |
| 245 | int vertexCount, |
| 246 | int indexCount, |
| 247 | void** vertices, |
| 248 | void** indices); |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 249 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 250 | /** |
| 251 | * Provides hints to caller about the number of vertices and indices |
| 252 | * that can be allocated cheaply. This can be useful if caller is reserving |
| 253 | * space but doesn't know exactly how much geometry is needed. |
| 254 | * |
| 255 | * Also may hint whether the draw target should be flushed first. This is |
| 256 | * useful for deferred targets. |
| 257 | * |
| jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 258 | * @param vertexSize size of vertices caller would like to reserve |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 259 | * @param vertexCount in: hint about how many vertices the caller would |
| 260 | * like to allocate. |
| 261 | * out: a hint about the number of vertices that can be |
| 262 | * allocated cheaply. Negative means no hint. |
| 263 | * Ignored if NULL. |
| 264 | * @param indexCount in: hint about how many indices the caller would |
| 265 | * like to allocate. |
| 266 | * out: a hint about the number of indices that can be |
| 267 | * allocated cheaply. Negative means no hint. |
| 268 | * Ignored if NULL. |
| 269 | * |
| 270 | * @return true if target should be flushed based on the input values. |
| 271 | */ |
| jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 272 | virtual bool geometryHints(size_t vertexSize, |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 273 | int* vertexCount, |
| 274 | int* indexCount) const; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 275 | |
| 276 | /** |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 277 | * Sets source of vertex data for the next draw. Array must contain |
| 278 | * the vertex data when this is called. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 279 | * |
| 280 | * @param array cpu array containing vertex data. |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 281 | * @param size size of the vertex data. |
| 282 | * @param vertexCount the number of vertices in the array. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 283 | */ |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 284 | void setVertexSourceToArray(GrVertexLayout vertexLayout, |
| 285 | const void* vertexArray, |
| 286 | int vertexCount); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 287 | |
| 288 | /** |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 289 | * Sets source of index data for the next indexed draw. Array must contain |
| 290 | * the indices when this is called. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 291 | * |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 292 | * @param array cpu array containing index data. |
| 293 | * @param indexCount the number of indices in the array. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 294 | */ |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 295 | void setIndexSourceToArray(const void* indexArray, int indexCount); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 296 | |
| 297 | /** |
| 298 | * 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] | 299 | * in the buffer until drawIndexed, drawNonIndexed, or drawIndexedInstances. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 300 | * |
| 301 | * @param buffer vertex buffer containing vertex data. Must be |
| 302 | * unlocked before draw call. |
| 303 | * @param vertexLayout layout of the vertex data in the buffer. |
| 304 | */ |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 305 | void setVertexSourceToBuffer(GrVertexLayout vertexLayout, |
| 306 | const GrVertexBuffer* buffer); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 307 | |
| 308 | /** |
| 309 | * 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] | 310 | * to be in the buffer until drawIndexed. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 311 | * |
| 312 | * @param buffer index buffer containing indices. Must be unlocked |
| 313 | * before indexed draw call. |
| 314 | */ |
| 315 | void setIndexSourceToBuffer(const GrIndexBuffer* buffer); |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 316 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 317 | /** |
| 318 | * Resets vertex source. Drawing from reset vertices is illegal. Set vertex |
| 319 | * source to reserved, array, or buffer before next draw. May be able to free |
| 320 | * up temporary storage allocated by setVertexSourceToArray or |
| 321 | * reserveVertexSpace. |
| 322 | */ |
| 323 | void resetVertexSource(); |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 324 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 325 | /** |
| 326 | * Resets index source. Indexed Drawing from reset indices is illegal. Set |
| 327 | * index source to reserved, array, or buffer before next indexed draw. May |
| 328 | * be able to free up temporary storage allocated by setIndexSourceToArray |
| 329 | * or reserveIndexSpace. |
| 330 | */ |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 331 | void resetIndexSource(); |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 332 | |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 333 | /** |
| 334 | * Query to find out if the vertex or index source is reserved. |
| 335 | */ |
| 336 | bool hasReservedVerticesOrIndices() const { |
| bsalomon@google.com | 73d98aa | 2012-03-13 14:41:19 +0000 | [diff] [blame] | 337 | return kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc || |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 338 | kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc; |
| 339 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 340 | |
| 341 | /** |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 342 | * Pushes and resets the vertex/index sources. Any reserved vertex / index |
| 343 | * data is finalized (i.e. cannot be updated after the matching pop but can |
| 344 | * be drawn from). Must be balanced by a pop. |
| 345 | */ |
| 346 | void pushGeometrySource(); |
| 347 | |
| 348 | /** |
| 349 | * Pops the vertex / index sources from the matching push. |
| 350 | */ |
| 351 | void popGeometrySource(); |
| bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 352 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 353 | /** |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 354 | * Draws indexed geometry using the current state and current vertex / index |
| 355 | * sources. |
| 356 | * |
| 357 | * @param type The type of primitives to draw. |
| 358 | * @param startVertex the vertex in the vertex array/buffer corresponding |
| 359 | * to index 0 |
| 360 | * @param startIndex first index to read from index src. |
| 361 | * @param vertexCount one greater than the max index. |
| 362 | * @param indexCount the number of index elements to read. The index count |
| 363 | * is effectively trimmed to the last completely |
| 364 | * specified primitive. |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 365 | * @param devBounds optional bounds hint. This is a promise from the caller, |
| 366 | * not a request for clipping. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 367 | */ |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 368 | void drawIndexed(GrPrimitiveType type, |
| 369 | int startVertex, |
| 370 | int startIndex, |
| 371 | int vertexCount, |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 372 | int indexCount, |
| 373 | const SkRect* devBounds = NULL); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 374 | |
| 375 | /** |
| 376 | * Draws non-indexed geometry using the current state and current vertex |
| 377 | * sources. |
| 378 | * |
| 379 | * @param type The type of primitives to draw. |
| 380 | * @param startVertex the vertex in the vertex array/buffer corresponding |
| 381 | * to index 0 |
| 382 | * @param vertexCount one greater than the max index. |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 383 | * @param devBounds optional bounds hint. This is a promise from the caller, |
| 384 | * not a request for clipping. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 385 | */ |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 386 | void drawNonIndexed(GrPrimitiveType type, |
| 387 | int startVertex, |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 388 | int vertexCount, |
| 389 | const SkRect* devBounds = NULL); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 390 | |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 391 | /** |
| bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 392 | * Draws path into the stencil buffer. The fill must be either even/odd or |
| 393 | * winding (not inverse or hairline). It will respect the HW antialias flag |
| 394 | * on the draw state (if possible in the 3D API). |
| 395 | */ |
| sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 396 | void stencilPath(const GrPath*, const SkStrokeRec& stroke, SkPath::FillType fill); |
| bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 397 | |
| 398 | /** |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 399 | * Helper function for drawing rects. This does not use the current index |
| 400 | * and vertex sources. After returning, the vertex and index sources may |
| 401 | * have changed. They should be reestablished before the next drawIndexed |
| 402 | * or drawNonIndexed. This cannot be called between reserving and releasing |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 403 | * geometry. |
| 404 | * |
| 405 | * A subclass may override this to perform more optimal rect rendering. Its |
| 406 | * draws should be funneled through one of the public GrDrawTarget draw methods |
| 407 | * (e.g. drawNonIndexed, drawIndexedInstances, ...). The base class draws a two |
| 408 | * triangle fan using drawNonIndexed from reserved vertex space. |
| 409 | * |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 410 | * @param rect the rect to draw |
| 411 | * @param matrix optional matrix applied to rect (before viewMatrix) |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 412 | * @param srcRects specifies rects for stages enabled by stageEnableMask. |
| 413 | * if stageEnableMask bit i is 1, srcRects is not NULL, |
| 414 | * and srcRects[i] is not NULL, then srcRects[i] will be |
| 415 | * used as coordinates for stage i. Otherwise, if stage i |
| 416 | * is enabled then rect is used as the coordinates. |
| 417 | * @param srcMatrices optional matrices applied to srcRects. If |
| 418 | * srcRect[i] is non-NULL and srcMatrices[i] is |
| 419 | * non-NULL then srcRect[i] will be transformed by |
| 420 | * srcMatrix[i]. srcMatrices can be NULL when no |
| 421 | * srcMatrices are desired. |
| 422 | */ |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 423 | virtual void drawRect(const GrRect& rect, |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 424 | const SkMatrix* matrix, |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 425 | const GrRect* srcRects[], |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 426 | const SkMatrix* srcMatrices[]); |
| bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 427 | /** |
| 428 | * Helper for drawRect when the caller doesn't need separate src rects or |
| 429 | * matrices. |
| 430 | */ |
| 431 | void drawSimpleRect(const GrRect& rect, const SkMatrix* matrix = NULL) { |
| 432 | drawRect(rect, matrix, NULL, NULL); |
| 433 | } |
| 434 | void drawSimpleRect(const GrIRect& irect, const SkMatrix* matrix = NULL) { |
| 435 | SkRect rect = SkRect::MakeFromIRect(irect); |
| 436 | this->drawRect(rect, matrix, NULL, NULL); |
| 437 | } |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 438 | |
| 439 | /** |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 440 | * This call is used to draw multiple instances of some geometry with a |
| 441 | * given number of vertices (V) and indices (I) per-instance. The indices in |
| 442 | * the index source must have the form i[k+I] == i[k] + V. Also, all indices |
| 443 | * i[kI] ... i[(k+1)I-1] must be elements of the range kV ... (k+1)V-1. As a |
| 444 | * concrete example, the following index buffer for drawing a series of |
| 445 | * quads each as two triangles each satisfies these conditions with V=4 and |
| 446 | * I=6: |
| 447 | * (0,1,2,0,2,3, 4,5,6,4,6,7, 8,9,10,8,10,11, ...) |
| 448 | * |
| 449 | * The call assumes that the pattern of indices fills the entire index |
| 450 | * source. The size of the index buffer limits the number of instances that |
| 451 | * can be drawn by the GPU in a single draw. However, the caller may specify |
| 452 | * any (positive) number for instanceCount and if necessary multiple GPU |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 453 | * draws will be issued. Moreover, when drawIndexedInstances is called |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 454 | * multiple times it may be possible for GrDrawTarget to group them into a |
| 455 | * single GPU draw. |
| 456 | * |
| 457 | * @param type the type of primitives to draw |
| 458 | * @param instanceCount the number of instances to draw. Each instance |
| 459 | * consists of verticesPerInstance vertices indexed by |
| 460 | * indicesPerInstance indices drawn as the primitive |
| 461 | * type specified by type. |
| 462 | * @param verticesPerInstance The number of vertices in each instance (V |
| 463 | * in the above description). |
| 464 | * @param indicesPerInstance The number of indices in each instance (I |
| 465 | * in the above description). |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 466 | * @param devBounds optional bounds hint. This is a promise from the caller, |
| 467 | * not a request for clipping. |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 468 | */ |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 469 | void drawIndexedInstances(GrPrimitiveType type, |
| 470 | int instanceCount, |
| 471 | int verticesPerInstance, |
| 472 | int indicesPerInstance, |
| 473 | const SkRect* devBounds = NULL); |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 474 | |
| 475 | /** |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 476 | * Clear the current render target if one isn't passed in. Ignores the |
| 477 | * 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] | 478 | * whole thing if rect is NULL, otherwise just the rect. |
| bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 479 | */ |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 480 | virtual void clear(const GrIRect* rect, |
| robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 481 | GrColor color, |
| 482 | GrRenderTarget* renderTarget = NULL) = 0; |
| bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 483 | |
| robertphillips@google.com | ff17584 | 2012-05-14 19:31:39 +0000 | [diff] [blame] | 484 | /** |
| 485 | * Release any resources that are cached but not currently in use. This |
| 486 | * is intended to give an application some recourse when resources are low. |
| 487 | */ |
| 488 | virtual void purgeResources() {}; |
| 489 | |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 490 | /** |
| 491 | * For subclass internal use to invoke a call to onDraw(). See DrawInfo below. |
| 492 | */ |
| 493 | void executeDraw(const DrawInfo& info) { this->onDraw(info); } |
| 494 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 495 | //////////////////////////////////////////////////////////////////////////// |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 496 | |
| bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 497 | /** |
| 498 | * See AutoStateRestore below. |
| 499 | */ |
| 500 | enum ASRInit { |
| 501 | kPreserve_ASRInit, |
| 502 | kReset_ASRInit |
| 503 | }; |
| 504 | |
| 505 | /** |
| 506 | * Saves off the current state and restores it in the destructor. It will |
| 507 | * install a new GrDrawState object on the target (setDrawState) and restore |
| 508 | * the previous one in the destructor. The caller should call drawState() to |
| 509 | * get the new draw state after the ASR is installed. |
| 510 | * |
| 511 | * GrDrawState* state = target->drawState(); |
| 512 | * AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit). |
| 513 | * state->setRenderTarget(rt); // state refers to the GrDrawState set on |
| 514 | * // target before asr was initialized. |
| 515 | * // Therefore, rt is set on the GrDrawState |
| 516 | * // that will be restored after asr's |
| 517 | * // destructor rather than target's current |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 518 | * // GrDrawState. |
| bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 519 | */ |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 520 | class AutoStateRestore : ::GrNoncopyable { |
| 521 | public: |
| bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 522 | /** |
| 523 | * Default ASR will have no effect unless set() is subsequently called. |
| 524 | */ |
| bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 525 | AutoStateRestore(); |
| bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 526 | |
| 527 | /** |
| 528 | * Saves the state on target. The state will be restored when the ASR |
| 529 | * is destroyed. If this constructor is used do not call set(). |
| 530 | * |
| 531 | * @param init Should the newly installed GrDrawState be a copy of the |
| 532 | * previous state or a default-initialized GrDrawState. |
| 533 | */ |
| 534 | AutoStateRestore(GrDrawTarget* target, ASRInit init); |
| 535 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 536 | ~AutoStateRestore(); |
| 537 | |
| bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 538 | /** |
| bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 539 | * Saves the state on target. The state will be restored when the ASR |
| 540 | * is destroyed. This should only be called once per ASR object and only |
| 541 | * when the default constructor was used. For nested saves use multiple |
| 542 | * ASR objects. |
| 543 | * |
| 544 | * @param init Should the newly installed GrDrawState be a copy of the |
| 545 | * previous state or a default-initialized GrDrawState. |
| bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 546 | */ |
| bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 547 | void set(GrDrawTarget* target, ASRInit init); |
| bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 548 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 549 | private: |
| bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 550 | GrDrawTarget* fDrawTarget; |
| 551 | SkTLazy<GrDrawState> fTempState; |
| 552 | GrDrawState* fSavedState; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 553 | }; |
| 554 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 555 | //////////////////////////////////////////////////////////////////////////// |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 556 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 557 | class AutoReleaseGeometry : ::GrNoncopyable { |
| 558 | public: |
| 559 | AutoReleaseGeometry(GrDrawTarget* target, |
| 560 | GrVertexLayout vertexLayout, |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 561 | int vertexCount, |
| 562 | int indexCount); |
| 563 | AutoReleaseGeometry(); |
| 564 | ~AutoReleaseGeometry(); |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 565 | bool set(GrDrawTarget* target, |
| 566 | GrVertexLayout vertexLayout, |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 567 | int vertexCount, |
| 568 | int indexCount); |
| bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 569 | bool succeeded() const { return NULL != fTarget; } |
| bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 570 | void* vertices() const { GrAssert(this->succeeded()); return fVertices; } |
| 571 | void* indices() const { GrAssert(this->succeeded()); return fIndices; } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 572 | GrPoint* positions() const { |
| bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 573 | return static_cast<GrPoint*>(this->vertices()); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | private: |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 577 | void reset(); |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 578 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 579 | GrDrawTarget* fTarget; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 580 | void* fVertices; |
| 581 | void* fIndices; |
| 582 | }; |
| 583 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 584 | //////////////////////////////////////////////////////////////////////////// |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 585 | |
| 586 | class AutoClipRestore : ::GrNoncopyable { |
| 587 | public: |
| 588 | AutoClipRestore(GrDrawTarget* target) { |
| 589 | fTarget = target; |
| 590 | fClip = fTarget->getClip(); |
| 591 | } |
| 592 | |
| bsalomon@google.com | 8d67c07 | 2012-12-13 20:38:14 +0000 | [diff] [blame] | 593 | AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip); |
| 594 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 595 | ~AutoClipRestore() { |
| 596 | fTarget->setClip(fClip); |
| 597 | } |
| 598 | private: |
| bsalomon@google.com | 8d67c07 | 2012-12-13 20:38:14 +0000 | [diff] [blame] | 599 | GrDrawTarget* fTarget; |
| 600 | const GrClipData* fClip; |
| 601 | SkTLazy<SkClipStack> fStack; |
| 602 | GrClipData fReplacementClip; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 603 | }; |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 604 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 605 | //////////////////////////////////////////////////////////////////////////// |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 606 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 607 | class AutoGeometryPush : ::GrNoncopyable { |
| 608 | public: |
| 609 | AutoGeometryPush(GrDrawTarget* target) { |
| 610 | GrAssert(NULL != target); |
| 611 | fTarget = target; |
| 612 | target->pushGeometrySource(); |
| 613 | } |
| 614 | ~AutoGeometryPush() { |
| 615 | fTarget->popGeometrySource(); |
| 616 | } |
| 617 | private: |
| 618 | GrDrawTarget* fTarget; |
| 619 | }; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 620 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 621 | protected: |
| bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 622 | |
| bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 623 | /** |
| 624 | * Optimizations for blending / coverage to be applied based on the current |
| 625 | * state. |
| 626 | * Subclasses that actually draw (as opposed to those that just buffer for |
| 627 | * playback) must implement the flags that replace the output color. |
| 628 | */ |
| 629 | enum BlendOptFlags { |
| 630 | /** |
| 631 | * No optimization |
| 632 | */ |
| 633 | kNone_BlendOpt = 0, |
| 634 | /** |
| 635 | * Don't draw at all |
| 636 | */ |
| 637 | kSkipDraw_BlendOptFlag = 0x2, |
| 638 | /** |
| 639 | * Emit the src color, disable HW blending (replace dst with src) |
| 640 | */ |
| 641 | kDisableBlend_BlendOptFlag = 0x4, |
| 642 | /** |
| 643 | * The coverage value does not have to be computed separately from |
| 644 | * alpha, the the output color can be the modulation of the two. |
| 645 | */ |
| 646 | kCoverageAsAlpha_BlendOptFlag = 0x1, |
| 647 | /** |
| 648 | * Instead of emitting a src color, emit coverage in the alpha channel |
| 649 | * and r,g,b are "don't cares". |
| 650 | */ |
| 651 | kEmitCoverage_BlendOptFlag = 0x10, |
| 652 | /** |
| 653 | * Emit transparent black instead of the src color, no need to compute |
| 654 | * coverage. |
| 655 | */ |
| 656 | kEmitTransBlack_BlendOptFlag = 0x8, |
| 657 | }; |
| 658 | GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags); |
| bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 659 | |
| skia.committer@gmail.com | 61b05dc | 2012-12-14 02:02:06 +0000 | [diff] [blame] | 660 | /** |
| bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 661 | * Determines what optimizations can be applied based on the blend. The coefficients may have |
| 662 | * to be tweaked in order for the optimization to work. srcCoeff and dstCoeff are optional |
| 663 | * params that receive the tweaked coefficients. Normally the function looks at the current |
| 664 | * state to see if coverage is enabled. By setting forceCoverage the caller can speculatively |
| 665 | * determine the blend optimizations that would be used if there was partial pixel coverage. |
| 666 | */ |
| bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 667 | BlendOptFlags getBlendOpts(bool forceCoverage = false, |
| 668 | GrBlendCoeff* srcCoeff = NULL, |
| 669 | GrBlendCoeff* dstCoeff = NULL) const; |
| 670 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 671 | enum GeometrySrcType { |
| 672 | kNone_GeometrySrcType, //<! src has not been specified |
| 673 | kReserved_GeometrySrcType, //<! src was set using reserve*Space |
| 674 | kArray_GeometrySrcType, //<! src was set using set*SourceToArray |
| 675 | kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer |
| 676 | }; |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 677 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 678 | struct GeometrySrcState { |
| 679 | GeometrySrcType fVertexSrc; |
| 680 | union { |
| 681 | // valid if src type is buffer |
| 682 | const GrVertexBuffer* fVertexBuffer; |
| 683 | // valid if src type is reserved or array |
| 684 | int fVertexCount; |
| 685 | }; |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 686 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 687 | GeometrySrcType fIndexSrc; |
| 688 | union { |
| 689 | // valid if src type is buffer |
| 690 | const GrIndexBuffer* fIndexBuffer; |
| 691 | // valid if src type is reserved or array |
| 692 | int fIndexCount; |
| 693 | }; |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 694 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 695 | GrVertexLayout fVertexLayout; |
| 696 | }; |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 697 | |
| 698 | int indexCountInCurrentSource() const { |
| 699 | const GeometrySrcState& src = this->getGeomSrc(); |
| 700 | switch (src.fIndexSrc) { |
| 701 | case kNone_GeometrySrcType: |
| 702 | return 0; |
| 703 | case kReserved_GeometrySrcType: |
| 704 | case kArray_GeometrySrcType: |
| 705 | return src.fIndexCount; |
| 706 | case kBuffer_GeometrySrcType: |
| 707 | return src.fIndexBuffer->sizeInBytes() / sizeof(uint16_t); |
| 708 | default: |
| 709 | GrCrash("Unexpected Index Source."); |
| 710 | return 0; |
| 711 | } |
| 712 | } |
| bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 713 | |
| bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 714 | // allows derived class to set the caps |
| 715 | CapsInternals* capsInternals() { return &fCaps.fInternals; } |
| 716 | |
| 717 | // A subclass may override this function if it wishes to be notified when the clip is changed. |
| 718 | // The override should call INHERITED::clipWillBeSet(). |
| 719 | virtual void clipWillBeSet(const GrClipData* clipData); |
| 720 | |
| 721 | // subclasses must call this in their destructors to ensure all vertex |
| 722 | // and index sources have been released (including those held by |
| 723 | // pushGeometrySource()) |
| 724 | void releaseGeometry(); |
| 725 | |
| 726 | // accessors for derived classes |
| 727 | const GeometrySrcState& getGeomSrc() const { return fGeoSrcStateStack.back(); } |
| 728 | // it is preferable to call this rather than getGeomSrc()->fVertexLayout because of the assert. |
| 729 | GrVertexLayout getVertexLayout() const { |
| 730 | // the vertex layout is only valid if a vertex source has been specified. |
| 731 | GrAssert(this->getGeomSrc().fVertexSrc != kNone_GeometrySrcType); |
| 732 | return this->getGeomSrc().fVertexLayout; |
| bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 733 | } |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 734 | |
| bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 735 | Caps fCaps; |
| 736 | |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 737 | /** |
| 738 | * Used to communicate draws to subclass's onDraw function. |
| 739 | */ |
| bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 740 | class DrawInfo { |
| 741 | public: |
| 742 | DrawInfo(const DrawInfo& di) { (*this) = di; } |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 743 | DrawInfo& operator =(const DrawInfo& di); |
| bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 744 | |
| 745 | GrPrimitiveType primitiveType() const { return fPrimitiveType; } |
| 746 | int startVertex() const { return fStartVertex; } |
| 747 | int startIndex() const { return fStartIndex; } |
| 748 | int vertexCount() const { return fVertexCount; } |
| 749 | int indexCount() const { return fIndexCount; } |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 750 | int verticesPerInstance() const { return fVerticesPerInstance; } |
| 751 | int indicesPerInstance() const { return fIndicesPerInstance; } |
| 752 | int instanceCount() const { return fInstanceCount; } |
| bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 753 | |
| 754 | bool isIndexed() const { return fIndexCount > 0; } |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 755 | #if GR_DEBUG |
| 756 | bool isInstanced() const; // this version is longer because of asserts |
| 757 | #else |
| 758 | bool isInstanced() const { return fInstanceCount > 0; } |
| 759 | #endif |
| 760 | |
| 761 | // adds or remove instances |
| 762 | void adjustInstanceCount(int instanceOffset); |
| 763 | // shifts the start vertex |
| 764 | void adjustStartVertex(int vertexOffset); |
| 765 | // shifts the start index |
| 766 | void adjustStartIndex(int indexOffset); |
| 767 | |
| 768 | void setDevBounds(const SkRect& bounds) { |
| 769 | fDevBoundsStorage = bounds; |
| 770 | fDevBounds = &fDevBoundsStorage; |
| 771 | } |
| 772 | const SkRect* getDevBounds() const { return fDevBounds; } |
| bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 773 | |
| 774 | private: |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 775 | DrawInfo() { fDevBounds = NULL; } |
| 776 | |
| bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 777 | friend class GrDrawTarget; |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 778 | |
| bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 779 | GrPrimitiveType fPrimitiveType; |
| 780 | |
| 781 | int fStartVertex; |
| 782 | int fStartIndex; |
| 783 | int fVertexCount; |
| 784 | int fIndexCount; |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 785 | |
| 786 | int fInstanceCount; |
| 787 | int fVerticesPerInstance; |
| 788 | int fIndicesPerInstance; |
| 789 | |
| 790 | SkRect fDevBoundsStorage; |
| 791 | SkRect* fDevBounds; |
| bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 792 | }; |
| 793 | |
| bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 794 | private: |
| 795 | // A subclass can optionally overload this function to be notified before |
| 796 | // vertex and index space is reserved. |
| jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 797 | virtual void willReserveVertexAndIndexSpace(size_t vertexSize, int vertexCount, int indexCount) {} |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 798 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 799 | // implemented by subclass to allocate space for reserved geom |
| jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 800 | virtual bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices) = 0; |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 801 | virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0; |
| 802 | // implemented by subclass to handle release of reserved geom space |
| 803 | virtual void releaseReservedVertexSpace() = 0; |
| 804 | virtual void releaseReservedIndexSpace() = 0; |
| 805 | // subclass must consume array contents when set |
| bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 806 | virtual void onSetVertexSourceToArray(const void* vertexArray, int vertexCount) = 0; |
| 807 | virtual void onSetIndexSourceToArray(const void* indexArray, int indexCount) = 0; |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 808 | // subclass is notified that geom source will be set away from an array |
| 809 | virtual void releaseVertexArray() = 0; |
| 810 | virtual void releaseIndexArray() = 0; |
| bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 811 | // subclass overrides to be notified just before geo src state is pushed/popped. |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 812 | virtual void geometrySourceWillPush() = 0; |
| 813 | virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0; |
| 814 | // subclass called to perform drawing |
| bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 815 | virtual void onDraw(const DrawInfo&) = 0; |
| sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 816 | virtual void onStencilPath(const GrPath*, const SkStrokeRec& stroke, SkPath::FillType fill) = 0; |
| bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 817 | |
| bsalomon@google.com | e3d7095 | 2012-03-13 12:40:53 +0000 | [diff] [blame] | 818 | // helpers for reserving vertex and index space. |
| 819 | bool reserveVertexSpace(GrVertexLayout vertexLayout, |
| 820 | int vertexCount, |
| 821 | void** vertices); |
| 822 | bool reserveIndexSpace(int indexCount, void** indices); |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 823 | |
| bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 824 | // called by drawIndexed and drawNonIndexed. Use a negative indexCount to |
| 825 | // indicate non-indexed drawing. |
| 826 | bool checkDraw(GrPrimitiveType type, int startVertex, |
| 827 | int startIndex, int vertexCount, |
| 828 | int indexCount) const; |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 829 | // called when setting a new vert/idx source to unref prev vb/ib |
| 830 | void releasePreviousVertexSource(); |
| 831 | void releasePreviousIndexSource(); |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 832 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 833 | enum { |
| 834 | kPreallocGeoSrcStateStackCnt = 4, |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 835 | }; |
| bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 836 | SkSTArray<kPreallocGeoSrcStateStackCnt, GeometrySrcState, true> fGeoSrcStateStack; |
| 837 | const GrClipData* fClip; |
| 838 | GrDrawState* fDrawState; |
| 839 | GrDrawState fDefaultDrawState; |
| reed@google.com | fa35e3d | 2012-06-26 20:16:17 +0000 | [diff] [blame] | 840 | |
| 841 | typedef GrRefCnt INHERITED; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 842 | }; |
| 843 | |
| bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 844 | GR_MAKE_BITFIELD_OPS(GrDrawTarget::BlendOptFlags); |
| 845 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 846 | #endif |