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