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