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