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 | #include "GrDrawTarget.h" |
| 12 | #include "GrGpuVertex.h" |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 13 | #include "GrRenderTarget.h" |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 14 | #include "GrTexture.h" |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 15 | #include "GrVertexBuffer.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 16 | |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 17 | #include "SkStrokeRec.h" |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 18 | |
reed@google.com | fa35e3d | 2012-06-26 20:16:17 +0000 | [diff] [blame] | 19 | SK_DEFINE_INST_COUNT(GrDrawTarget) |
| 20 | |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 21 | namespace { |
| 22 | |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 23 | /** |
| 24 | * This function generates some masks that we like to have known at compile |
| 25 | * time. When the number of stages or tex coords is bumped or the way bits |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 26 | * are defined in GrDrawTarget.h changes this function should be rerun to |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 27 | * generate the new masks. (We attempted to force the compiler to generate the |
| 28 | * masks using recursive templates but always wound up with static initializers |
| 29 | * under gcc, even if they were just a series of immediate->memory moves.) |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 30 | * |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 31 | */ |
| 32 | void gen_mask_arrays(GrVertexLayout* stageTexCoordMasks, |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 33 | GrVertexLayout* texCoordMasks) { |
| 34 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 35 | stageTexCoordMasks[s] = 0; |
| 36 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
| 37 | stageTexCoordMasks[s] |= GrDrawTarget::StageTexCoordVertexLayoutBit(s, t); |
| 38 | } |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 39 | } |
| 40 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
| 41 | texCoordMasks[t] = 0; |
| 42 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 43 | texCoordMasks[t] |= GrDrawTarget::StageTexCoordVertexLayoutBit(s, t); |
| 44 | } |
| 45 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 46 | } |
| 47 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 48 | /** |
skia.committer@gmail.com | 4e8ef33 | 2013-01-08 02:01:29 +0000 | [diff] [blame^] | 49 | * Uncomment and run the gen_globals function to generate |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 50 | * the code that declares the global masks. |
| 51 | * |
| 52 | * #if 0'ed out to avoid unused function warning. |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 53 | */ |
skia.committer@gmail.com | 4e8ef33 | 2013-01-08 02:01:29 +0000 | [diff] [blame^] | 54 | |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 55 | #if 0 |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 56 | void gen_globals() { |
| 57 | GrVertexLayout stageTexCoordMasks[GrDrawState::kNumStages]; |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 58 | GrVertexLayout texCoordMasks[GrDrawState::kMaxTexCoords]; |
bsalomon@google.com | 52544c7 | 2012-07-10 15:06:59 +0000 | [diff] [blame] | 59 | gen_mask_arrays(stageTexCoordMasks, texCoordMasks); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 60 | |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 61 | GrPrintf("const GrVertexLayout gStageTexCoordMasks[] = {\n"); |
| 62 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 63 | GrPrintf(" 0x%x,\n", stageTexCoordMasks[s]); |
| 64 | } |
| 65 | GrPrintf("};\n"); |
| 66 | GrPrintf("GR_STATIC_ASSERT(GrDrawState::kNumStages == GR_ARRAY_COUNT(gStageTexCoordMasks));\n\n"); |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 67 | GrPrintf("const GrVertexLayout gTexCoordMasks[] = {\n"); |
| 68 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
| 69 | GrPrintf(" 0x%x,\n", texCoordMasks[t]); |
| 70 | } |
| 71 | GrPrintf("};\n"); |
| 72 | GrPrintf("GR_STATIC_ASSERT(GrDrawState::kMaxTexCoords == GR_ARRAY_COUNT(gTexCoordMasks));\n"); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 73 | } |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 74 | #endif |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 75 | |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 76 | /* These values were generated by the above function */ |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 77 | |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 78 | const GrVertexLayout gStageTexCoordMasks[] = { |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 79 | 0x108421, |
| 80 | 0x210842, |
| 81 | 0x421084, |
| 82 | 0x842108, |
| 83 | 0x1084210, |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 84 | }; |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 85 | GR_STATIC_ASSERT(GrDrawState::kNumStages == GR_ARRAY_COUNT(gStageTexCoordMasks)); |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 86 | |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 87 | const GrVertexLayout gTexCoordMasks[] = { |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 88 | 0x1f, |
| 89 | 0x3e0, |
| 90 | 0x7c00, |
| 91 | 0xf8000, |
| 92 | 0x1f00000, |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 93 | }; |
| 94 | GR_STATIC_ASSERT(GrDrawState::kMaxTexCoords == GR_ARRAY_COUNT(gTexCoordMasks)); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 95 | |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 96 | #ifdef SK_DEBUG |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 97 | bool check_layout(GrVertexLayout layout) { |
| 98 | // can only have 1 or 0 bits set for each stage. |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 99 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 52544c7 | 2012-07-10 15:06:59 +0000 | [diff] [blame] | 100 | int stageBits = layout & gStageTexCoordMasks[s]; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 101 | if (stageBits && !GrIsPow2(stageBits)) { |
| 102 | return false; |
| 103 | } |
| 104 | } |
| 105 | return true; |
| 106 | } |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 107 | #endif |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 108 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 109 | int num_tex_coords(GrVertexLayout layout) { |
| 110 | int cnt = 0; |
| 111 | // figure out how many tex coordinates are present |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 112 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 113 | if (gTexCoordMasks[t] & layout) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 114 | ++cnt; |
| 115 | } |
| 116 | } |
| 117 | return cnt; |
| 118 | } |
| 119 | |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 120 | } //unnamed namespace |
| 121 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 122 | size_t GrDrawTarget::VertexSize(GrVertexLayout vertexLayout) { |
| 123 | GrAssert(check_layout(vertexLayout)); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 124 | |
| 125 | size_t vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ? |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 126 | sizeof(GrGpuTextVertex) : |
| 127 | sizeof(GrPoint); |
| 128 | |
| 129 | size_t size = vecSize; // position |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 130 | size += num_tex_coords(vertexLayout) * vecSize; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 131 | if (vertexLayout & kColor_VertexLayoutBit) { |
| 132 | size += sizeof(GrColor); |
| 133 | } |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 134 | if (vertexLayout & kCoverage_VertexLayoutBit) { |
| 135 | size += sizeof(GrColor); |
| 136 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 137 | if (vertexLayout & kEdge_VertexLayoutBit) { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 138 | size += 4 * sizeof(SkScalar); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 139 | } |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 140 | return size; |
| 141 | } |
| 142 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 143 | //////////////////////////////////////////////////////////////////////////////// |
| 144 | |
| 145 | /** |
| 146 | * Functions for computing offsets of various components from the layout |
| 147 | * bitfield. |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 148 | * |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 149 | * Order of vertex components: |
| 150 | * Position |
| 151 | * Tex Coord 0 |
| 152 | * ... |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 153 | * Tex Coord GrDrawState::kMaxTexCoords-1 |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 154 | * Color |
| 155 | * Coverage |
| 156 | */ |
| 157 | |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 158 | int GrDrawTarget::VertexStageCoordOffset(int stageIdx, GrVertexLayout vertexLayout) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 159 | GrAssert(check_layout(vertexLayout)); |
tomhudson@google.com | b213ed8 | 2012-06-25 15:22:12 +0000 | [diff] [blame] | 160 | |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 161 | if (!StageUsesTexCoords(vertexLayout, stageIdx)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 162 | return 0; |
| 163 | } |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 164 | int tcIdx = VertexTexCoordsForStage(stageIdx, vertexLayout); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 165 | if (tcIdx >= 0) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 166 | |
| 167 | int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ? |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 168 | sizeof(GrGpuTextVertex) : |
| 169 | sizeof(GrPoint); |
| 170 | int offset = vecSize; // position |
| 171 | // figure out how many tex coordinates are present and precede this one. |
| 172 | for (int t = 0; t < tcIdx; ++t) { |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 173 | if (gTexCoordMasks[t] & vertexLayout) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 174 | offset += vecSize; |
| 175 | } |
| 176 | } |
| 177 | return offset; |
| 178 | } |
| 179 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 180 | return -1; |
| 181 | } |
| 182 | |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 183 | int GrDrawTarget::VertexColorOffset(GrVertexLayout vertexLayout) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 184 | GrAssert(check_layout(vertexLayout)); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 185 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 186 | if (vertexLayout & kColor_VertexLayoutBit) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 187 | int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ? |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 188 | sizeof(GrGpuTextVertex) : |
| 189 | sizeof(GrPoint); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 190 | return vecSize * (num_tex_coords(vertexLayout) + 1); //+1 for pos |
| 191 | } |
| 192 | return -1; |
| 193 | } |
| 194 | |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 195 | int GrDrawTarget::VertexCoverageOffset(GrVertexLayout vertexLayout) { |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 196 | GrAssert(check_layout(vertexLayout)); |
| 197 | |
| 198 | if (vertexLayout & kCoverage_VertexLayoutBit) { |
| 199 | int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ? |
| 200 | sizeof(GrGpuTextVertex) : |
| 201 | sizeof(GrPoint); |
| 202 | |
| 203 | int offset = vecSize * (num_tex_coords(vertexLayout) + 1); |
| 204 | if (vertexLayout & kColor_VertexLayoutBit) { |
| 205 | offset += sizeof(GrColor); |
| 206 | } |
| 207 | return offset; |
| 208 | } |
| 209 | return -1; |
| 210 | } |
| 211 | |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 212 | int GrDrawTarget::VertexEdgeOffset(GrVertexLayout vertexLayout) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 213 | GrAssert(check_layout(vertexLayout)); |
| 214 | |
| 215 | // edge pts are after the pos, tex coords, and color |
| 216 | if (vertexLayout & kEdge_VertexLayoutBit) { |
| 217 | int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ? |
| 218 | sizeof(GrGpuTextVertex) : |
| 219 | sizeof(GrPoint); |
| 220 | int offset = vecSize * (num_tex_coords(vertexLayout) + 1); //+1 for pos |
| 221 | if (vertexLayout & kColor_VertexLayoutBit) { |
| 222 | offset += sizeof(GrColor); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 223 | } |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 224 | if (vertexLayout & kCoverage_VertexLayoutBit) { |
| 225 | offset += sizeof(GrColor); |
| 226 | } |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 227 | return offset; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 228 | } |
| 229 | return -1; |
| 230 | } |
| 231 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 232 | int GrDrawTarget::VertexSizeAndOffsetsByIdx( |
| 233 | GrVertexLayout vertexLayout, |
| 234 | int texCoordOffsetsByIdx[GrDrawState::kMaxTexCoords], |
| 235 | int* colorOffset, |
| 236 | int* coverageOffset, |
| 237 | int* edgeOffset) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 238 | GrAssert(check_layout(vertexLayout)); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 239 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 240 | int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ? |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 241 | sizeof(GrGpuTextVertex) : |
| 242 | sizeof(GrPoint); |
| 243 | int size = vecSize; // position |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 244 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 245 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 246 | if (gTexCoordMasks[t] & vertexLayout) { |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 247 | if (NULL != texCoordOffsetsByIdx) { |
| 248 | texCoordOffsetsByIdx[t] = size; |
| 249 | } |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 250 | size += vecSize; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 251 | } else { |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 252 | if (NULL != texCoordOffsetsByIdx) { |
| 253 | texCoordOffsetsByIdx[t] = -1; |
| 254 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 255 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 256 | } |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 257 | if (kColor_VertexLayoutBit & vertexLayout) { |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 258 | if (NULL != colorOffset) { |
| 259 | *colorOffset = size; |
| 260 | } |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 261 | size += sizeof(GrColor); |
| 262 | } else { |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 263 | if (NULL != colorOffset) { |
| 264 | *colorOffset = -1; |
| 265 | } |
| 266 | } |
| 267 | if (kCoverage_VertexLayoutBit & vertexLayout) { |
| 268 | if (NULL != coverageOffset) { |
| 269 | *coverageOffset = size; |
| 270 | } |
| 271 | size += sizeof(GrColor); |
| 272 | } else { |
| 273 | if (NULL != coverageOffset) { |
| 274 | *coverageOffset = -1; |
| 275 | } |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 276 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 277 | if (kEdge_VertexLayoutBit & vertexLayout) { |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 278 | if (NULL != edgeOffset) { |
| 279 | *edgeOffset = size; |
| 280 | } |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 281 | size += 4 * sizeof(SkScalar); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 282 | } else { |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 283 | if (NULL != edgeOffset) { |
| 284 | *edgeOffset = -1; |
| 285 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 286 | } |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 287 | return size; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 288 | } |
| 289 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 290 | int GrDrawTarget::VertexSizeAndOffsetsByStage( |
| 291 | GrVertexLayout vertexLayout, |
| 292 | int texCoordOffsetsByStage[GrDrawState::kNumStages], |
| 293 | int* colorOffset, |
| 294 | int* coverageOffset, |
| 295 | int* edgeOffset) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 296 | GrAssert(check_layout(vertexLayout)); |
| 297 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 298 | int texCoordOffsetsByIdx[GrDrawState::kMaxTexCoords]; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 299 | int size = VertexSizeAndOffsetsByIdx(vertexLayout, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 300 | (NULL == texCoordOffsetsByStage) ? |
| 301 | NULL : |
| 302 | texCoordOffsetsByIdx, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 303 | colorOffset, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 304 | coverageOffset, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 305 | edgeOffset); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 306 | if (NULL != texCoordOffsetsByStage) { |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 307 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
tomhudson@google.com | b213ed8 | 2012-06-25 15:22:12 +0000 | [diff] [blame] | 308 | int tcIdx = VertexTexCoordsForStage(s, vertexLayout); |
| 309 | texCoordOffsetsByStage[s] = |
| 310 | tcIdx < 0 ? 0 : texCoordOffsetsByIdx[tcIdx]; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 313 | return size; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 314 | } |
| 315 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 316 | //////////////////////////////////////////////////////////////////////////////// |
| 317 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 318 | bool GrDrawTarget::VertexUsesTexCoordIdx(int coordIndex, |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 319 | GrVertexLayout vertexLayout) { |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 320 | GrAssert(coordIndex < GrDrawState::kMaxTexCoords); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 321 | GrAssert(check_layout(vertexLayout)); |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 322 | return !!(gTexCoordMasks[coordIndex] & vertexLayout); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 323 | } |
| 324 | |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 325 | int GrDrawTarget::VertexTexCoordsForStage(int stageIdx, |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 326 | GrVertexLayout vertexLayout) { |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 327 | GrAssert(stageIdx < GrDrawState::kNumStages); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 328 | GrAssert(check_layout(vertexLayout)); |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 329 | int bit = vertexLayout & gStageTexCoordMasks[stageIdx]; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 330 | if (bit) { |
| 331 | // figure out which set of texture coordates is used |
| 332 | // bits are ordered T0S0, T0S1, T0S2, ..., T1S0, T1S1, ... |
| 333 | // and start at bit 0. |
| 334 | GR_STATIC_ASSERT(sizeof(GrVertexLayout) <= sizeof(uint32_t)); |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 335 | return (32 - SkCLZ(bit) - 1) / GrDrawState::kNumStages; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 336 | } |
| 337 | return -1; |
| 338 | } |
| 339 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 340 | //////////////////////////////////////////////////////////////////////////////// |
| 341 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 342 | void GrDrawTarget::VertexLayoutUnitTest() { |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 343 | // Ensure that our globals mask arrays are correct |
| 344 | GrVertexLayout stageTexCoordMasks[GrDrawState::kNumStages]; |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 345 | GrVertexLayout texCoordMasks[GrDrawState::kMaxTexCoords]; |
bsalomon@google.com | 52544c7 | 2012-07-10 15:06:59 +0000 | [diff] [blame] | 346 | gen_mask_arrays(stageTexCoordMasks, texCoordMasks); |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 347 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 348 | GrAssert(stageTexCoordMasks[s] == gStageTexCoordMasks[s]); |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 349 | } |
| 350 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
| 351 | GrAssert(texCoordMasks[t] == gTexCoordMasks[t]); |
| 352 | } |
| 353 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 354 | // not necessarily exhaustive |
| 355 | static bool run; |
| 356 | if (!run) { |
| 357 | run = true; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 358 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 359 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 360 | GrVertexLayout stageMask = 0; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 361 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 362 | stageMask |= StageTexCoordVertexLayoutBit(s,t); |
| 363 | } |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 364 | GrAssert(1 == GrDrawState::kMaxTexCoords || |
| 365 | !check_layout(stageMask)); |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 366 | GrAssert(gStageTexCoordMasks[s] == stageMask); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 367 | GrAssert(!check_layout(stageMask)); |
| 368 | } |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 369 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 370 | GrVertexLayout tcMask = 0; |
| 371 | GrAssert(!VertexUsesTexCoordIdx(t, 0)); |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 372 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 373 | tcMask |= StageTexCoordVertexLayoutBit(s,t); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 374 | GrAssert(sizeof(GrPoint) == VertexStageCoordOffset(s, tcMask)); |
| 375 | GrAssert(VertexUsesTexCoordIdx(t, tcMask)); |
| 376 | GrAssert(2*sizeof(GrPoint) == VertexSize(tcMask)); |
| 377 | GrAssert(t == VertexTexCoordsForStage(s, tcMask)); |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 378 | for (int s2 = s + 1; s2 < GrDrawState::kNumStages; ++s2) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 379 | GrAssert(-1 == VertexTexCoordsForStage(s2, tcMask)); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 380 | |
bsalomon@google.com | 1962832 | 2011-02-03 21:30:17 +0000 | [diff] [blame] | 381 | #if GR_DEBUG |
tomhudson@google.com | b213ed8 | 2012-06-25 15:22:12 +0000 | [diff] [blame] | 382 | GrVertexLayout posAsTex = tcMask; |
bsalomon@google.com | 1962832 | 2011-02-03 21:30:17 +0000 | [diff] [blame] | 383 | #endif |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 384 | GrAssert(0 == VertexStageCoordOffset(s2, posAsTex)); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 385 | GrAssert(2*sizeof(GrPoint) == VertexSize(posAsTex)); |
| 386 | GrAssert(-1 == VertexTexCoordsForStage(s2, posAsTex)); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 387 | GrAssert(-1 == VertexEdgeOffset(posAsTex)); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 388 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 389 | GrAssert(-1 == VertexEdgeOffset(tcMask)); |
| 390 | GrAssert(-1 == VertexColorOffset(tcMask)); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 391 | GrAssert(-1 == VertexCoverageOffset(tcMask)); |
bsalomon@google.com | 1962832 | 2011-02-03 21:30:17 +0000 | [diff] [blame] | 392 | #if GR_DEBUG |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 393 | GrVertexLayout withColor = tcMask | kColor_VertexLayoutBit; |
bsalomon@google.com | 1962832 | 2011-02-03 21:30:17 +0000 | [diff] [blame] | 394 | #endif |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 395 | GrAssert(-1 == VertexCoverageOffset(withColor)); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 396 | GrAssert(2*sizeof(GrPoint) == VertexColorOffset(withColor)); |
| 397 | GrAssert(2*sizeof(GrPoint) + sizeof(GrColor) == VertexSize(withColor)); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 398 | #if GR_DEBUG |
| 399 | GrVertexLayout withEdge = tcMask | kEdge_VertexLayoutBit; |
| 400 | #endif |
| 401 | GrAssert(-1 == VertexColorOffset(withEdge)); |
| 402 | GrAssert(2*sizeof(GrPoint) == VertexEdgeOffset(withEdge)); |
| 403 | GrAssert(4*sizeof(GrPoint) == VertexSize(withEdge)); |
| 404 | #if GR_DEBUG |
| 405 | GrVertexLayout withColorAndEdge = withColor | kEdge_VertexLayoutBit; |
| 406 | #endif |
| 407 | GrAssert(2*sizeof(GrPoint) == VertexColorOffset(withColorAndEdge)); |
| 408 | GrAssert(2*sizeof(GrPoint) + sizeof(GrColor) == VertexEdgeOffset(withColorAndEdge)); |
| 409 | GrAssert(4*sizeof(GrPoint) + sizeof(GrColor) == VertexSize(withColorAndEdge)); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 410 | #if GR_DEBUG |
| 411 | GrVertexLayout withCoverage = tcMask | kCoverage_VertexLayoutBit; |
| 412 | #endif |
| 413 | GrAssert(-1 == VertexColorOffset(withCoverage)); |
| 414 | GrAssert(2*sizeof(GrPoint) == VertexCoverageOffset(withCoverage)); |
| 415 | GrAssert(2*sizeof(GrPoint) + sizeof(GrColor) == VertexSize(withCoverage)); |
| 416 | #if GR_DEBUG |
| 417 | GrVertexLayout withCoverageAndColor = tcMask | kCoverage_VertexLayoutBit | |
| 418 | kColor_VertexLayoutBit; |
| 419 | #endif |
| 420 | GrAssert(2*sizeof(GrPoint) == VertexColorOffset(withCoverageAndColor)); |
| 421 | GrAssert(2*sizeof(GrPoint) + sizeof(GrColor) == VertexCoverageOffset(withCoverageAndColor)); |
| 422 | GrAssert(2*sizeof(GrPoint) + 2 * sizeof(GrColor) == VertexSize(withCoverageAndColor)); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 423 | } |
bsalomon@google.com | 35ff384 | 2011-12-15 16:58:19 +0000 | [diff] [blame] | 424 | GrAssert(gTexCoordMasks[t] == tcMask); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 425 | GrAssert(check_layout(tcMask)); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 426 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 427 | int stageOffsets[GrDrawState::kNumStages]; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 428 | int colorOffset; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 429 | int edgeOffset; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 430 | int coverageOffset; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 431 | int size; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 432 | size = VertexSizeAndOffsetsByStage(tcMask, |
| 433 | stageOffsets, &colorOffset, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 434 | &coverageOffset, &edgeOffset); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 435 | GrAssert(2*sizeof(GrPoint) == size); |
| 436 | GrAssert(-1 == colorOffset); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 437 | GrAssert(-1 == coverageOffset); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 438 | GrAssert(-1 == edgeOffset); |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 439 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 440 | GrAssert(sizeof(GrPoint) == stageOffsets[s]); |
| 441 | GrAssert(sizeof(GrPoint) == VertexStageCoordOffset(s, tcMask)); |
| 442 | } |
| 443 | } |
| 444 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | //////////////////////////////////////////////////////////////////////////////// |
| 448 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 449 | #define DEBUG_INVAL_BUFFER 0xdeadcafe |
| 450 | #define DEBUG_INVAL_START_IDX -1 |
| 451 | |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 452 | GrDrawTarget::GrDrawTarget() : fClip(NULL) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 453 | #if GR_DEBUG |
| 454 | VertexLayoutUnitTest(); |
| 455 | #endif |
bsalomon@google.com | a5d056a | 2012-03-27 15:59:58 +0000 | [diff] [blame] | 456 | fDrawState = &fDefaultDrawState; |
| 457 | // We assume that fDrawState always owns a ref to the object it points at. |
| 458 | fDefaultDrawState.ref(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 459 | GeometrySrcState& geoSrc = fGeoSrcStateStack.push_back(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 460 | #if GR_DEBUG |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 461 | geoSrc.fVertexCount = DEBUG_INVAL_START_IDX; |
| 462 | geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER; |
| 463 | geoSrc.fIndexCount = DEBUG_INVAL_START_IDX; |
| 464 | geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 465 | #endif |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 466 | geoSrc.fVertexSrc = kNone_GeometrySrcType; |
| 467 | geoSrc.fIndexSrc = kNone_GeometrySrcType; |
| 468 | } |
| 469 | |
| 470 | GrDrawTarget::~GrDrawTarget() { |
bsalomon@google.com | 4a018bb | 2011-10-28 19:50:21 +0000 | [diff] [blame] | 471 | GrAssert(1 == fGeoSrcStateStack.count()); |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 472 | SkDEBUGCODE(GeometrySrcState& geoSrc = fGeoSrcStateStack.back()); |
bsalomon@google.com | 4a018bb | 2011-10-28 19:50:21 +0000 | [diff] [blame] | 473 | GrAssert(kNone_GeometrySrcType == geoSrc.fIndexSrc); |
| 474 | GrAssert(kNone_GeometrySrcType == geoSrc.fVertexSrc); |
bsalomon@google.com | a5d056a | 2012-03-27 15:59:58 +0000 | [diff] [blame] | 475 | fDrawState->unref(); |
bsalomon@google.com | 4a018bb | 2011-10-28 19:50:21 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | void GrDrawTarget::releaseGeometry() { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 479 | int popCnt = fGeoSrcStateStack.count() - 1; |
| 480 | while (popCnt) { |
| 481 | this->popGeometrySource(); |
| 482 | --popCnt; |
| 483 | } |
bsalomon@google.com | 4a018bb | 2011-10-28 19:50:21 +0000 | [diff] [blame] | 484 | this->resetVertexSource(); |
| 485 | this->resetIndexSource(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 486 | } |
| 487 | |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 488 | void GrDrawTarget::setClip(const GrClipData* clip) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 489 | clipWillBeSet(clip); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 490 | fClip = clip; |
| 491 | } |
| 492 | |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 493 | const GrClipData* GrDrawTarget::getClip() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 494 | return fClip; |
| 495 | } |
| 496 | |
bsalomon@google.com | a5d056a | 2012-03-27 15:59:58 +0000 | [diff] [blame] | 497 | void GrDrawTarget::setDrawState(GrDrawState* drawState) { |
| 498 | GrAssert(NULL != fDrawState); |
| 499 | if (NULL == drawState) { |
| 500 | drawState = &fDefaultDrawState; |
| 501 | } |
| 502 | if (fDrawState != drawState) { |
| 503 | fDrawState->unref(); |
| 504 | drawState->ref(); |
| 505 | fDrawState = drawState; |
| 506 | } |
| 507 | } |
| 508 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 509 | bool GrDrawTarget::reserveVertexSpace(GrVertexLayout vertexLayout, |
| 510 | int vertexCount, |
| 511 | void** vertices) { |
| 512 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 513 | bool acquired = false; |
| 514 | if (vertexCount > 0) { |
| 515 | GrAssert(NULL != vertices); |
| 516 | this->releasePreviousVertexSource(); |
| 517 | geoSrc.fVertexSrc = kNone_GeometrySrcType; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 518 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 519 | acquired = this->onReserveVertexSpace(vertexLayout, |
| 520 | vertexCount, |
| 521 | vertices); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 522 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 523 | if (acquired) { |
| 524 | geoSrc.fVertexSrc = kReserved_GeometrySrcType; |
| 525 | geoSrc.fVertexCount = vertexCount; |
| 526 | geoSrc.fVertexLayout = vertexLayout; |
| 527 | } else if (NULL != vertices) { |
| 528 | *vertices = NULL; |
| 529 | } |
| 530 | return acquired; |
| 531 | } |
| 532 | |
| 533 | bool GrDrawTarget::reserveIndexSpace(int indexCount, |
| 534 | void** indices) { |
| 535 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 536 | bool acquired = false; |
| 537 | if (indexCount > 0) { |
| 538 | GrAssert(NULL != indices); |
| 539 | this->releasePreviousIndexSource(); |
| 540 | geoSrc.fIndexSrc = kNone_GeometrySrcType; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 541 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 542 | acquired = this->onReserveIndexSpace(indexCount, indices); |
| 543 | } |
| 544 | if (acquired) { |
| 545 | geoSrc.fIndexSrc = kReserved_GeometrySrcType; |
| 546 | geoSrc.fIndexCount = indexCount; |
| 547 | } else if (NULL != indices) { |
| 548 | *indices = NULL; |
| 549 | } |
| 550 | return acquired; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 551 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 552 | } |
| 553 | |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 554 | bool GrDrawTarget::StageUsesTexCoords(GrVertexLayout layout, int stageIdx) { |
| 555 | return SkToBool(layout & gStageTexCoordMasks[stageIdx]); |
tomhudson@google.com | b213ed8 | 2012-06-25 15:22:12 +0000 | [diff] [blame] | 556 | } |
| 557 | |
bsalomon@google.com | e3d7095 | 2012-03-13 12:40:53 +0000 | [diff] [blame] | 558 | bool GrDrawTarget::reserveVertexAndIndexSpace(GrVertexLayout vertexLayout, |
| 559 | int vertexCount, |
| 560 | int indexCount, |
| 561 | void** vertices, |
| 562 | void** indices) { |
bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 563 | this->willReserveVertexAndIndexSpace(vertexLayout, vertexCount, indexCount); |
bsalomon@google.com | e3d7095 | 2012-03-13 12:40:53 +0000 | [diff] [blame] | 564 | if (vertexCount) { |
| 565 | if (!this->reserveVertexSpace(vertexLayout, vertexCount, vertices)) { |
| 566 | if (indexCount) { |
| 567 | this->resetIndexSource(); |
| 568 | } |
| 569 | return false; |
| 570 | } |
| 571 | } |
| 572 | if (indexCount) { |
| 573 | if (!this->reserveIndexSpace(indexCount, indices)) { |
| 574 | if (vertexCount) { |
| 575 | this->resetVertexSource(); |
| 576 | } |
| 577 | return false; |
| 578 | } |
| 579 | } |
| 580 | return true; |
| 581 | } |
| 582 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 583 | bool GrDrawTarget::geometryHints(GrVertexLayout vertexLayout, |
| 584 | int32_t* vertexCount, |
| 585 | int32_t* indexCount) const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 586 | if (NULL != vertexCount) { |
| 587 | *vertexCount = -1; |
| 588 | } |
| 589 | if (NULL != indexCount) { |
| 590 | *indexCount = -1; |
| 591 | } |
| 592 | return false; |
| 593 | } |
| 594 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 595 | void GrDrawTarget::releasePreviousVertexSource() { |
| 596 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 597 | switch (geoSrc.fVertexSrc) { |
| 598 | case kNone_GeometrySrcType: |
| 599 | break; |
| 600 | case kArray_GeometrySrcType: |
| 601 | this->releaseVertexArray(); |
| 602 | break; |
| 603 | case kReserved_GeometrySrcType: |
| 604 | this->releaseReservedVertexSpace(); |
| 605 | break; |
| 606 | case kBuffer_GeometrySrcType: |
| 607 | geoSrc.fVertexBuffer->unref(); |
| 608 | #if GR_DEBUG |
| 609 | geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER; |
| 610 | #endif |
| 611 | break; |
| 612 | default: |
| 613 | GrCrash("Unknown Vertex Source Type."); |
| 614 | break; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | void GrDrawTarget::releasePreviousIndexSource() { |
| 619 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 620 | switch (geoSrc.fIndexSrc) { |
| 621 | case kNone_GeometrySrcType: // these two don't require |
| 622 | break; |
| 623 | case kArray_GeometrySrcType: |
| 624 | this->releaseIndexArray(); |
| 625 | break; |
| 626 | case kReserved_GeometrySrcType: |
| 627 | this->releaseReservedIndexSpace(); |
| 628 | break; |
| 629 | case kBuffer_GeometrySrcType: |
| 630 | geoSrc.fIndexBuffer->unref(); |
| 631 | #if GR_DEBUG |
| 632 | geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER; |
| 633 | #endif |
| 634 | break; |
| 635 | default: |
| 636 | GrCrash("Unknown Index Source Type."); |
| 637 | break; |
| 638 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 639 | } |
| 640 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 641 | void GrDrawTarget::setVertexSourceToArray(GrVertexLayout vertexLayout, |
| 642 | const void* vertexArray, |
| 643 | int vertexCount) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 644 | this->releasePreviousVertexSource(); |
| 645 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 646 | geoSrc.fVertexSrc = kArray_GeometrySrcType; |
| 647 | geoSrc.fVertexLayout = vertexLayout; |
| 648 | geoSrc.fVertexCount = vertexCount; |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 649 | this->onSetVertexSourceToArray(vertexArray, vertexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 650 | } |
| 651 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 652 | void GrDrawTarget::setIndexSourceToArray(const void* indexArray, |
| 653 | int indexCount) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 654 | this->releasePreviousIndexSource(); |
| 655 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 656 | geoSrc.fIndexSrc = kArray_GeometrySrcType; |
| 657 | geoSrc.fIndexCount = indexCount; |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 658 | this->onSetIndexSourceToArray(indexArray, indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 659 | } |
| 660 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 661 | void GrDrawTarget::setVertexSourceToBuffer(GrVertexLayout vertexLayout, |
| 662 | const GrVertexBuffer* buffer) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 663 | this->releasePreviousVertexSource(); |
| 664 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 665 | geoSrc.fVertexSrc = kBuffer_GeometrySrcType; |
| 666 | geoSrc.fVertexBuffer = buffer; |
| 667 | buffer->ref(); |
| 668 | geoSrc.fVertexLayout = vertexLayout; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | void GrDrawTarget::setIndexSourceToBuffer(const GrIndexBuffer* buffer) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 672 | this->releasePreviousIndexSource(); |
| 673 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 674 | geoSrc.fIndexSrc = kBuffer_GeometrySrcType; |
| 675 | geoSrc.fIndexBuffer = buffer; |
| 676 | buffer->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 677 | } |
| 678 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 679 | void GrDrawTarget::resetVertexSource() { |
| 680 | this->releasePreviousVertexSource(); |
| 681 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 682 | geoSrc.fVertexSrc = kNone_GeometrySrcType; |
| 683 | } |
| 684 | |
| 685 | void GrDrawTarget::resetIndexSource() { |
| 686 | this->releasePreviousIndexSource(); |
| 687 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 688 | geoSrc.fIndexSrc = kNone_GeometrySrcType; |
| 689 | } |
| 690 | |
| 691 | void GrDrawTarget::pushGeometrySource() { |
| 692 | this->geometrySourceWillPush(); |
| 693 | GeometrySrcState& newState = fGeoSrcStateStack.push_back(); |
| 694 | newState.fIndexSrc = kNone_GeometrySrcType; |
| 695 | newState.fVertexSrc = kNone_GeometrySrcType; |
| 696 | #if GR_DEBUG |
| 697 | newState.fVertexCount = ~0; |
| 698 | newState.fVertexBuffer = (GrVertexBuffer*)~0; |
| 699 | newState.fIndexCount = ~0; |
| 700 | newState.fIndexBuffer = (GrIndexBuffer*)~0; |
| 701 | #endif |
| 702 | } |
| 703 | |
| 704 | void GrDrawTarget::popGeometrySource() { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 705 | // if popping last element then pops are unbalanced with pushes |
| 706 | GrAssert(fGeoSrcStateStack.count() > 1); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 707 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 708 | this->geometrySourceWillPop(fGeoSrcStateStack.fromBack(1)); |
| 709 | this->releasePreviousVertexSource(); |
| 710 | this->releasePreviousIndexSource(); |
| 711 | fGeoSrcStateStack.pop_back(); |
| 712 | } |
| 713 | |
| 714 | //////////////////////////////////////////////////////////////////////////////// |
| 715 | |
bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 716 | bool GrDrawTarget::checkDraw(GrPrimitiveType type, int startVertex, |
| 717 | int startIndex, int vertexCount, |
| 718 | int indexCount) const { |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 719 | const GrDrawState& drawState = this->getDrawState(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 720 | #if GR_DEBUG |
bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 721 | const GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 722 | int maxVertex = startVertex + vertexCount; |
| 723 | int maxValidVertex; |
| 724 | switch (geoSrc.fVertexSrc) { |
| 725 | case kNone_GeometrySrcType: |
bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 726 | GrCrash("Attempting to draw without vertex src."); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 727 | case kReserved_GeometrySrcType: // fallthrough |
| 728 | case kArray_GeometrySrcType: |
| 729 | maxValidVertex = geoSrc.fVertexCount; |
| 730 | break; |
| 731 | case kBuffer_GeometrySrcType: |
bsalomon@google.com | f3ccda7 | 2012-07-11 14:56:33 +0000 | [diff] [blame] | 732 | maxValidVertex = geoSrc.fVertexBuffer->sizeInBytes() / VertexSize(geoSrc.fVertexLayout); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 733 | break; |
| 734 | } |
| 735 | if (maxVertex > maxValidVertex) { |
bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 736 | GrCrash("Drawing outside valid vertex range."); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 737 | } |
bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 738 | if (indexCount > 0) { |
| 739 | int maxIndex = startIndex + indexCount; |
| 740 | int maxValidIndex; |
| 741 | switch (geoSrc.fIndexSrc) { |
| 742 | case kNone_GeometrySrcType: |
| 743 | GrCrash("Attempting to draw indexed geom without index src."); |
| 744 | case kReserved_GeometrySrcType: // fallthrough |
| 745 | case kArray_GeometrySrcType: |
| 746 | maxValidIndex = geoSrc.fIndexCount; |
| 747 | break; |
| 748 | case kBuffer_GeometrySrcType: |
| 749 | maxValidIndex = geoSrc.fIndexBuffer->sizeInBytes() / sizeof(uint16_t); |
| 750 | break; |
| 751 | } |
| 752 | if (maxIndex > maxValidIndex) { |
| 753 | GrCrash("Index reads outside valid index range."); |
| 754 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 755 | } |
bsalomon@google.com | b4725b4 | 2012-03-30 17:24:17 +0000 | [diff] [blame] | 756 | |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 757 | GrAssert(NULL != drawState.getRenderTarget()); |
| 758 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 759 | if (drawState.isStageEnabled(s)) { |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 760 | const GrEffect* effect = drawState.getStage(s).getEffect(); |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 761 | int numTextures = effect->numTextures(); |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 762 | for (int t = 0; t < numTextures; ++t) { |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 763 | GrTexture* texture = effect->texture(t); |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 764 | GrAssert(texture->asRenderTarget() != drawState.getRenderTarget()); |
| 765 | } |
bsalomon@google.com | b4725b4 | 2012-03-30 17:24:17 +0000 | [diff] [blame] | 766 | } |
| 767 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 768 | #endif |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 769 | if (NULL == drawState.getRenderTarget()) { |
bsalomon@google.com | 0ba52fc | 2011-11-10 22:16:06 +0000 | [diff] [blame] | 770 | return false; |
| 771 | } |
bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 772 | return true; |
| 773 | } |
| 774 | |
| 775 | void GrDrawTarget::drawIndexed(GrPrimitiveType type, int startVertex, |
| 776 | int startIndex, int vertexCount, |
| 777 | int indexCount) { |
| 778 | if (indexCount > 0 && |
| 779 | this->checkDraw(type, startVertex, startIndex, |
| 780 | vertexCount, indexCount)) { |
bsalomon@google.com | 8214587 | 2011-08-23 14:32:40 +0000 | [diff] [blame] | 781 | this->onDrawIndexed(type, startVertex, startIndex, |
| 782 | vertexCount, indexCount); |
| 783 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 784 | } |
| 785 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 786 | void GrDrawTarget::drawNonIndexed(GrPrimitiveType type, |
| 787 | int startVertex, |
| 788 | int vertexCount) { |
bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 789 | if (vertexCount > 0 && |
| 790 | this->checkDraw(type, startVertex, -1, vertexCount, -1)) { |
bsalomon@google.com | 8214587 | 2011-08-23 14:32:40 +0000 | [diff] [blame] | 791 | this->onDrawNonIndexed(type, startVertex, vertexCount); |
| 792 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 793 | } |
| 794 | |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 795 | void GrDrawTarget::stencilPath(const GrPath* path, const SkStrokeRec& stroke, SkPath::FillType fill) { |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 796 | // TODO: extract portions of checkDraw that are relevant to path stenciling. |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 797 | GrAssert(NULL != path); |
bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 798 | GrAssert(fCaps.pathStencilingSupport()); |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 799 | GrAssert(!stroke.isHairlineStyle()); |
| 800 | GrAssert(!SkPath::IsInverseFillType(fill)); |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 801 | this->onStencilPath(path, stroke, fill); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 802 | } |
| 803 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 804 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 805 | |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 806 | // Some blend modes allow folding a partial coverage value into the color's |
| 807 | // alpha channel, while others will blend incorrectly. |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 808 | bool GrDrawTarget::canTweakAlphaForCoverage() const { |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 809 | /** |
| 810 | * The fractional coverage is f |
| 811 | * The src and dst coeffs are Cs and Cd |
| 812 | * The dst and src colors are S and D |
| 813 | * We want the blend to compute: f*Cs*S + (f*Cd + (1-f))D |
| 814 | * By tweaking the source color's alpha we're replacing S with S'=fS. It's |
| 815 | * obvious that that first term will always be ok. The second term can be |
bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 816 | * rearranged as [1-(1-Cd)f]D. By substituting in the various possibilities |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 817 | * for Cd we find that only 1, ISA, and ISC produce the correct depth |
bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 818 | * coefficient in terms of S' and D. |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 819 | */ |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 820 | GrBlendCoeff dstCoeff = this->getDrawState().getDstBlendCoeff(); |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 821 | return kOne_GrBlendCoeff == dstCoeff || |
| 822 | kISA_GrBlendCoeff == dstCoeff || |
bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 823 | kISC_GrBlendCoeff == dstCoeff || |
| 824 | this->getDrawState().isCoverageDrawing(); |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 825 | } |
| 826 | |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 827 | bool GrDrawTarget::srcAlphaWillBeOne(GrVertexLayout layout) const { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 828 | const GrDrawState& drawState = this->getDrawState(); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 829 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 830 | // Check if per-vertex or constant color may have partial alpha |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 831 | if ((layout & kColor_VertexLayoutBit) || |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 832 | 0xff != GrColorUnpackA(drawState.getColor())) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 833 | return false; |
| 834 | } |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 835 | // Check if color filter could introduce an alpha |
| 836 | // (TODO: Consider being more aggressive with regards to detecting 0xff |
| 837 | // final alpha from color filter). |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 838 | if (SkXfermode::kDst_Mode != drawState.getColorFilterMode()) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 839 | return false; |
| 840 | } |
bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 841 | int stageCnt; |
| 842 | // Check whether coverage is treated as color |
| 843 | if (drawState.isCoverageDrawing()) { |
| 844 | if (0xff != GrColorUnpackA(drawState.getCoverage())) { |
| 845 | return false; |
| 846 | } |
| 847 | stageCnt = GrDrawState::kNumStages; |
| 848 | } else { |
| 849 | stageCnt = drawState.getFirstCoverageStage(); |
| 850 | } |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 851 | // Check if a color stage could create a partial alpha |
bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 852 | for (int s = 0; s < stageCnt; ++s) { |
| 853 | const GrEffect* effect = drawState.getStage(s).getEffect(); |
| 854 | if (NULL != effect) { |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 855 | // FIXME: The param indicates whether the texture is opaque or not. However, the effect |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 856 | // already controls its textures. It really needs to know whether the incoming color |
| 857 | // (from a uni, per-vertex colors, or previous stage) is opaque or not. |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 858 | if (!effect->isOpaque(true)) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 859 | return false; |
| 860 | } |
| 861 | } |
| 862 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 863 | return true; |
| 864 | } |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 865 | |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 866 | namespace { |
| 867 | GrVertexLayout default_blend_opts_vertex_layout() { |
| 868 | GrVertexLayout layout = 0; |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 869 | return layout; |
| 870 | } |
| 871 | } |
| 872 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 873 | GrDrawTarget::BlendOptFlags |
| 874 | GrDrawTarget::getBlendOpts(bool forceCoverage, |
| 875 | GrBlendCoeff* srcCoeff, |
| 876 | GrBlendCoeff* dstCoeff) const { |
| 877 | |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 878 | GrVertexLayout layout; |
| 879 | if (kNone_GeometrySrcType == this->getGeomSrc().fVertexSrc) { |
| 880 | layout = default_blend_opts_vertex_layout(); |
| 881 | } else { |
| 882 | layout = this->getVertexLayout(); |
| 883 | } |
| 884 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 885 | const GrDrawState& drawState = this->getDrawState(); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 886 | |
| 887 | GrBlendCoeff bogusSrcCoeff, bogusDstCoeff; |
| 888 | if (NULL == srcCoeff) { |
| 889 | srcCoeff = &bogusSrcCoeff; |
| 890 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 891 | *srcCoeff = drawState.getSrcBlendCoeff(); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 892 | |
| 893 | if (NULL == dstCoeff) { |
| 894 | dstCoeff = &bogusDstCoeff; |
| 895 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 896 | *dstCoeff = drawState.getDstBlendCoeff(); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 897 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 898 | if (drawState.isColorWriteDisabled()) { |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 899 | *srcCoeff = kZero_GrBlendCoeff; |
| 900 | *dstCoeff = kOne_GrBlendCoeff; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 901 | } |
| 902 | |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 903 | bool srcAIsOne = this->srcAlphaWillBeOne(layout); |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 904 | bool dstCoeffIsOne = kOne_GrBlendCoeff == *dstCoeff || |
| 905 | (kSA_GrBlendCoeff == *dstCoeff && srcAIsOne); |
| 906 | bool dstCoeffIsZero = kZero_GrBlendCoeff == *dstCoeff || |
| 907 | (kISA_GrBlendCoeff == *dstCoeff && srcAIsOne); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 908 | |
bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 909 | bool covIsZero = !drawState.isCoverageDrawing() && |
| 910 | !(layout & kCoverage_VertexLayoutBit) && |
| 911 | 0 == drawState.getCoverage(); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 912 | // When coeffs are (0,1) there is no reason to draw at all, unless |
| 913 | // stenciling is enabled. Having color writes disabled is effectively |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 914 | // (0,1). The same applies when coverage is known to be 0. |
bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 915 | if ((kZero_GrBlendCoeff == *srcCoeff && dstCoeffIsOne) || covIsZero) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 916 | if (drawState.getStencil().doesWrite()) { |
bsalomon@google.com | dafde9e | 2012-01-11 18:45:39 +0000 | [diff] [blame] | 917 | return kDisableBlend_BlendOptFlag | |
| 918 | kEmitTransBlack_BlendOptFlag; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 919 | } else { |
| 920 | return kSkipDraw_BlendOptFlag; |
| 921 | } |
| 922 | } |
| 923 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 924 | // check for coverage due to constant coverage, per-vertex coverage, |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 925 | // edge aa or coverage stage |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 926 | bool hasCoverage = forceCoverage || |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 927 | 0xffffffff != drawState.getCoverage() || |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 928 | (layout & kCoverage_VertexLayoutBit) || |
| 929 | (layout & kEdge_VertexLayoutBit); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 930 | for (int s = drawState.getFirstCoverageStage(); |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 931 | !hasCoverage && s < GrDrawState::kNumStages; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 932 | ++s) { |
tomhudson@google.com | f13f588 | 2012-06-25 17:27:28 +0000 | [diff] [blame] | 933 | if (this->isStageEnabled(s)) { |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 934 | hasCoverage = true; |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | // if we don't have coverage we can check whether the dst |
| 939 | // has to read at all. If not, we'll disable blending. |
| 940 | if (!hasCoverage) { |
| 941 | if (dstCoeffIsZero) { |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 942 | if (kOne_GrBlendCoeff == *srcCoeff) { |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 943 | // if there is no coverage and coeffs are (1,0) then we |
| 944 | // won't need to read the dst at all, it gets replaced by src |
| 945 | return kDisableBlend_BlendOptFlag; |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 946 | } else if (kZero_GrBlendCoeff == *srcCoeff) { |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 947 | // if the op is "clear" then we don't need to emit a color |
| 948 | // or blend, just write transparent black into the dst. |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 949 | *srcCoeff = kOne_GrBlendCoeff; |
| 950 | *dstCoeff = kZero_GrBlendCoeff; |
bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 951 | return kDisableBlend_BlendOptFlag | kEmitTransBlack_BlendOptFlag; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 952 | } |
| 953 | } |
bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 954 | } else if (drawState.isCoverageDrawing()) { |
| 955 | // we have coverage but we aren't distinguishing it from alpha by request. |
| 956 | return kCoverageAsAlpha_BlendOptFlag; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 957 | } else { |
| 958 | // check whether coverage can be safely rolled into alpha |
| 959 | // of if we can skip color computation and just emit coverage |
| 960 | if (this->canTweakAlphaForCoverage()) { |
| 961 | return kCoverageAsAlpha_BlendOptFlag; |
| 962 | } |
bsalomon@google.com | dafde9e | 2012-01-11 18:45:39 +0000 | [diff] [blame] | 963 | if (dstCoeffIsZero) { |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 964 | if (kZero_GrBlendCoeff == *srcCoeff) { |
bsalomon@google.com | dafde9e | 2012-01-11 18:45:39 +0000 | [diff] [blame] | 965 | // the source color is not included in the blend |
| 966 | // the dst coeff is effectively zero so blend works out to: |
| 967 | // (c)(0)D + (1-c)D = (1-c)D. |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 968 | *dstCoeff = kISA_GrBlendCoeff; |
bsalomon@google.com | dafde9e | 2012-01-11 18:45:39 +0000 | [diff] [blame] | 969 | return kEmitCoverage_BlendOptFlag; |
| 970 | } else if (srcAIsOne) { |
| 971 | // the dst coeff is effectively zero so blend works out to: |
| 972 | // cS + (c)(0)D + (1-c)D = cS + (1-c)D. |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 973 | // If Sa is 1 then we can replace Sa with c |
bsalomon@google.com | dafde9e | 2012-01-11 18:45:39 +0000 | [diff] [blame] | 974 | // and set dst coeff to 1-Sa. |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 975 | *dstCoeff = kISA_GrBlendCoeff; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 976 | return kCoverageAsAlpha_BlendOptFlag; |
| 977 | } |
bsalomon@google.com | dafde9e | 2012-01-11 18:45:39 +0000 | [diff] [blame] | 978 | } else if (dstCoeffIsOne) { |
| 979 | // the dst coeff is effectively one so blend works out to: |
| 980 | // cS + (c)(1)D + (1-c)D = cS + D. |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 981 | *dstCoeff = kOne_GrBlendCoeff; |
bsalomon@google.com | dafde9e | 2012-01-11 18:45:39 +0000 | [diff] [blame] | 982 | return kCoverageAsAlpha_BlendOptFlag; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 983 | } |
| 984 | } |
| 985 | return kNone_BlendOpt; |
| 986 | } |
| 987 | |
| 988 | bool GrDrawTarget::willUseHWAALines() const { |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 989 | // there is a conflict between using smooth lines and our use of |
| 990 | // premultiplied alpha. Smooth lines tweak the incoming alpha value |
| 991 | // but not in a premul-alpha way. So we only use them when our alpha |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 992 | // is 0xff and tweaking the color for partial coverage is OK |
bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 993 | if (!fCaps.hwAALineSupport() || |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 994 | !this->getDrawState().isHWAntialiasState()) { |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 995 | return false; |
| 996 | } |
| 997 | BlendOptFlags opts = this->getBlendOpts(); |
| 998 | return (kDisableBlend_BlendOptFlag & opts) && |
| 999 | (kCoverageAsAlpha_BlendOptFlag & opts); |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | bool GrDrawTarget::canApplyCoverage() const { |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 1003 | // we can correctly apply coverage if a) we have dual source blending |
| 1004 | // or b) one of our blend optimizations applies. |
bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 1005 | return this->getCaps().dualSourceBlendingSupport() || |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 1006 | kNone_BlendOpt != this->getBlendOpts(true); |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 1009 | //////////////////////////////////////////////////////////////////////////////// |
| 1010 | |
| 1011 | void GrDrawTarget::drawIndexedInstances(GrPrimitiveType type, |
| 1012 | int instanceCount, |
| 1013 | int verticesPerInstance, |
| 1014 | int indicesPerInstance) { |
| 1015 | if (!verticesPerInstance || !indicesPerInstance) { |
| 1016 | return; |
| 1017 | } |
| 1018 | |
| 1019 | int instancesPerDraw = this->indexCountInCurrentSource() / |
| 1020 | indicesPerInstance; |
| 1021 | if (!instancesPerDraw) { |
| 1022 | return; |
| 1023 | } |
| 1024 | |
| 1025 | instancesPerDraw = GrMin(instanceCount, instancesPerDraw); |
| 1026 | int startVertex = 0; |
| 1027 | while (instanceCount) { |
| 1028 | this->drawIndexed(type, |
| 1029 | startVertex, |
| 1030 | 0, |
| 1031 | verticesPerInstance * instancesPerDraw, |
| 1032 | indicesPerInstance * instancesPerDraw); |
| 1033 | startVertex += verticesPerInstance; |
| 1034 | instanceCount -= instancesPerDraw; |
| 1035 | } |
| 1036 | } |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 1037 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1038 | //////////////////////////////////////////////////////////////////////////////// |
| 1039 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1040 | void GrDrawTarget::drawRect(const GrRect& rect, |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 1041 | const SkMatrix* matrix, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1042 | const GrRect* srcRects[], |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 1043 | const SkMatrix* srcMatrices[]) { |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 1044 | GrVertexLayout layout = GetRectVertexLayout(srcRects); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1045 | |
| 1046 | AutoReleaseGeometry geo(this, layout, 4, 0); |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 1047 | if (!geo.succeeded()) { |
| 1048 | GrPrintf("Failed to get space for vertices!\n"); |
| 1049 | return; |
| 1050 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1051 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1052 | SetRectVertices(rect, matrix, srcRects, |
robertphillips@google.com | 8b129aa | 2012-10-05 15:37:00 +0000 | [diff] [blame] | 1053 | srcMatrices, SK_ColorBLACK, layout, geo.vertices()); |
skia.committer@gmail.com | a18ed03 | 2012-10-06 02:05:26 +0000 | [diff] [blame] | 1054 | |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 1055 | drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 1058 | GrVertexLayout GrDrawTarget::GetRectVertexLayout(const GrRect* srcRects[]) { |
| 1059 | if (NULL == srcRects) { |
| 1060 | return 0; |
| 1061 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1062 | |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 1063 | GrVertexLayout layout = 0; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1064 | for (int i = 0; i < GrDrawState::kNumStages; ++i) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1065 | int numTC = 0; |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 1066 | if (NULL != srcRects[i]) { |
| 1067 | layout |= StageTexCoordVertexLayoutBit(i, numTC); |
| 1068 | ++numTC; |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | return layout; |
| 1072 | } |
bsalomon@google.com | dea2f8d | 2011-08-01 15:51:05 +0000 | [diff] [blame] | 1073 | |
skia.committer@gmail.com | a18ed03 | 2012-10-06 02:05:26 +0000 | [diff] [blame] | 1074 | // This method fills int the four vertices for drawing 'rect'. |
robertphillips@google.com | 8b129aa | 2012-10-05 15:37:00 +0000 | [diff] [blame] | 1075 | // matrix - is applied to each vertex |
| 1076 | // srcRects - provide the uvs for each vertex |
| 1077 | // srcMatrices - are applied to the corresponding 'srcRect' |
| 1078 | // color - vertex color (replicated in each vertex) |
| 1079 | // layout - specifies which uvs and/or color are present |
| 1080 | // vertices - storage for the resulting vertices |
| 1081 | // Note: the color parameter will only be used when kColor_VertexLayoutBit |
| 1082 | // is present in 'layout' |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1083 | void GrDrawTarget::SetRectVertices(const GrRect& rect, |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 1084 | const SkMatrix* matrix, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1085 | const GrRect* srcRects[], |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 1086 | const SkMatrix* srcMatrices[], |
robertphillips@google.com | 8b129aa | 2012-10-05 15:37:00 +0000 | [diff] [blame] | 1087 | GrColor color, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1088 | GrVertexLayout layout, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1089 | void* vertices) { |
| 1090 | #if GR_DEBUG |
| 1091 | // check that the layout and srcRects agree |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1092 | for (int i = 0; i < GrDrawState::kNumStages; ++i) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1093 | if (VertexTexCoordsForStage(i, layout) >= 0) { |
| 1094 | GR_DEBUGASSERT(NULL != srcRects && NULL != srcRects[i]); |
| 1095 | } else { |
| 1096 | GR_DEBUGASSERT(NULL == srcRects || NULL == srcRects[i]); |
| 1097 | } |
| 1098 | } |
| 1099 | #endif |
| 1100 | |
robertphillips@google.com | 8b129aa | 2012-10-05 15:37:00 +0000 | [diff] [blame] | 1101 | int stageOffsets[GrDrawState::kNumStages], colorOffset; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1102 | int vsize = VertexSizeAndOffsetsByStage(layout, stageOffsets, |
robertphillips@google.com | 8b129aa | 2012-10-05 15:37:00 +0000 | [diff] [blame] | 1103 | &colorOffset, NULL, NULL); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1104 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1105 | GrTCast<GrPoint*>(vertices)->setRectFan(rect.fLeft, rect.fTop, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1106 | rect.fRight, rect.fBottom, |
| 1107 | vsize); |
| 1108 | if (NULL != matrix) { |
| 1109 | matrix->mapPointsWithStride(GrTCast<GrPoint*>(vertices), vsize, 4); |
| 1110 | } |
| 1111 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1112 | for (int i = 0; i < GrDrawState::kNumStages; ++i) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1113 | if (stageOffsets[i] > 0) { |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1114 | GrPoint* coords = GrTCast<GrPoint*>(GrTCast<intptr_t>(vertices) + |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1115 | stageOffsets[i]); |
| 1116 | coords->setRectFan(srcRects[i]->fLeft, srcRects[i]->fTop, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1117 | srcRects[i]->fRight, srcRects[i]->fBottom, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1118 | vsize); |
| 1119 | if (NULL != srcMatrices && NULL != srcMatrices[i]) { |
| 1120 | srcMatrices[i]->mapPointsWithStride(coords, vsize, 4); |
| 1121 | } |
| 1122 | } |
| 1123 | } |
robertphillips@google.com | 8b129aa | 2012-10-05 15:37:00 +0000 | [diff] [blame] | 1124 | |
| 1125 | if (layout & kColor_VertexLayoutBit) { |
| 1126 | |
| 1127 | GrColor* vertCol = GrTCast<GrColor*>(GrTCast<intptr_t>(vertices) + colorOffset); |
| 1128 | |
| 1129 | for (int i = 0; i < 4; ++i) { |
| 1130 | *vertCol = color; |
| 1131 | vertCol = (GrColor*) ((intptr_t) vertCol + vsize); |
| 1132 | } |
| 1133 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1136 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 1137 | |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 1138 | GrDrawTarget::AutoStateRestore::AutoStateRestore() { |
| 1139 | fDrawTarget = NULL; |
| 1140 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1141 | |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 1142 | GrDrawTarget::AutoStateRestore::AutoStateRestore(GrDrawTarget* target, |
| 1143 | ASRInit init) { |
| 1144 | fDrawTarget = NULL; |
| 1145 | this->set(target, init); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | GrDrawTarget::AutoStateRestore::~AutoStateRestore() { |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 1149 | if (NULL != fDrawTarget) { |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 1150 | fDrawTarget->setDrawState(fSavedState); |
| 1151 | fSavedState->unref(); |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 1152 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1153 | } |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 1154 | |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 1155 | void GrDrawTarget::AutoStateRestore::set(GrDrawTarget* target, ASRInit init) { |
| 1156 | GrAssert(NULL == fDrawTarget); |
| 1157 | fDrawTarget = target; |
| 1158 | fSavedState = target->drawState(); |
| 1159 | GrAssert(fSavedState); |
| 1160 | fSavedState->ref(); |
| 1161 | if (kReset_ASRInit == init) { |
| 1162 | // calls the default cons |
| 1163 | fTempState.init(); |
| 1164 | } else { |
| 1165 | GrAssert(kPreserve_ASRInit == init); |
| 1166 | // calls the copy cons |
| 1167 | fTempState.set(*fSavedState); |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 1168 | } |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 1169 | target->setDrawState(fTempState.get()); |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 1170 | } |
bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 1171 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1172 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 1173 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1174 | GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry( |
| 1175 | GrDrawTarget* target, |
| 1176 | GrVertexLayout vertexLayout, |
| 1177 | int vertexCount, |
| 1178 | int indexCount) { |
| 1179 | fTarget = NULL; |
| 1180 | this->set(target, vertexLayout, vertexCount, indexCount); |
| 1181 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1182 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1183 | GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry() { |
| 1184 | fTarget = NULL; |
| 1185 | } |
| 1186 | |
| 1187 | GrDrawTarget::AutoReleaseGeometry::~AutoReleaseGeometry() { |
| 1188 | this->reset(); |
| 1189 | } |
| 1190 | |
| 1191 | bool GrDrawTarget::AutoReleaseGeometry::set(GrDrawTarget* target, |
| 1192 | GrVertexLayout vertexLayout, |
| 1193 | int vertexCount, |
| 1194 | int indexCount) { |
| 1195 | this->reset(); |
| 1196 | fTarget = target; |
| 1197 | bool success = true; |
| 1198 | if (NULL != fTarget) { |
| 1199 | fTarget = target; |
bsalomon@google.com | e3d7095 | 2012-03-13 12:40:53 +0000 | [diff] [blame] | 1200 | success = target->reserveVertexAndIndexSpace(vertexLayout, |
| 1201 | vertexCount, |
| 1202 | indexCount, |
| 1203 | &fVertices, |
| 1204 | &fIndices); |
| 1205 | if (!success) { |
| 1206 | fTarget = NULL; |
| 1207 | this->reset(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1208 | } |
| 1209 | } |
| 1210 | GrAssert(success == (NULL != fTarget)); |
| 1211 | return success; |
| 1212 | } |
| 1213 | |
| 1214 | void GrDrawTarget::AutoReleaseGeometry::reset() { |
| 1215 | if (NULL != fTarget) { |
| 1216 | if (NULL != fVertices) { |
| 1217 | fTarget->resetVertexSource(); |
| 1218 | } |
| 1219 | if (NULL != fIndices) { |
| 1220 | fTarget->resetIndexSource(); |
| 1221 | } |
| 1222 | fTarget = NULL; |
| 1223 | } |
bsalomon@google.com | cb0c5ab | 2011-06-29 17:48:17 +0000 | [diff] [blame] | 1224 | fVertices = NULL; |
| 1225 | fIndices = NULL; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
bsalomon@google.com | 8d67c07 | 2012-12-13 20:38:14 +0000 | [diff] [blame] | 1228 | GrDrawTarget::AutoClipRestore::AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip) { |
| 1229 | fTarget = target; |
| 1230 | fClip = fTarget->getClip(); |
| 1231 | fStack.init(); |
| 1232 | fStack.get()->clipDevRect(newClip, SkRegion::kReplace_Op); |
| 1233 | fReplacementClip.fClipStack = fStack.get(); |
| 1234 | target->setClip(&fReplacementClip); |
| 1235 | } |
| 1236 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 1237 | void GrDrawTarget::Caps::print() const { |
| 1238 | static const char* gNY[] = {"NO", "YES"}; |
bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 1239 | GrPrintf("8 Bit Palette Support : %s\n", gNY[fInternals.f8BitPaletteSupport]); |
| 1240 | GrPrintf("NPOT Texture Tile Support : %s\n", gNY[fInternals.fNPOTTextureTileSupport]); |
| 1241 | GrPrintf("Two Sided Stencil Support : %s\n", gNY[fInternals.fTwoSidedStencilSupport]); |
| 1242 | GrPrintf("Stencil Wrap Ops Support : %s\n", gNY[fInternals.fStencilWrapOpsSupport]); |
| 1243 | GrPrintf("HW AA Lines Support : %s\n", gNY[fInternals.fHWAALineSupport]); |
| 1244 | GrPrintf("Shader Derivative Support : %s\n", gNY[fInternals.fShaderDerivativeSupport]); |
| 1245 | GrPrintf("Geometry Shader Support : %s\n", gNY[fInternals.fGeometryShaderSupport]); |
| 1246 | GrPrintf("FSAA Support : %s\n", gNY[fInternals.fFSAASupport]); |
| 1247 | GrPrintf("Dual Source Blending Support: %s\n", gNY[fInternals.fDualSourceBlendingSupport]); |
| 1248 | GrPrintf("Buffer Lock Support : %s\n", gNY[fInternals.fBufferLockSupport]); |
| 1249 | GrPrintf("Path Stenciling Support : %s\n", gNY[fInternals.fPathStencilingSupport]); |
| 1250 | GrPrintf("Max Texture Size : %d\n", fInternals.fMaxTextureSize); |
| 1251 | GrPrintf("Max Render Target Size : %d\n", fInternals.fMaxRenderTargetSize); |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 1252 | } |