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