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