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