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