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 | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 13 | #include "GrTexture.h" |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 14 | #include "GrVertexBuffer.h" |
| 15 | #include "GrIndexBuffer.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 16 | |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 17 | namespace { |
| 18 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 19 | // recursive helper for creating mask with all the tex coord bits set for |
| 20 | // one stage |
| 21 | template <int N> |
reed@google.com | 34cec24 | 2011-04-19 15:53:12 +0000 | [diff] [blame] | 22 | int stage_mask_recur(int stage) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 23 | return GrDrawTarget::StageTexCoordVertexLayoutBit(stage, N) | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 24 | stage_mask_recur<N+1>(stage); |
| 25 | } |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 26 | template<> |
reed@google.com | d728f6e | 2011-01-13 20:02:47 +0000 | [diff] [blame] | 27 | int stage_mask_recur<GrDrawTarget::kNumStages>(int) { return 0; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 28 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 29 | // mask of all tex coord indices for one stage |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 30 | int stage_tex_coord_mask(int stage) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 31 | return stage_mask_recur<0>(stage); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 32 | } |
| 33 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 34 | // mask of all bits relevant to one stage |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 35 | int stage_mask(int stage) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 36 | return stage_tex_coord_mask(stage) | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 37 | GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(stage); |
| 38 | } |
| 39 | |
| 40 | // recursive helper for creating mask of with all bits set relevant to one |
| 41 | // texture coordinate index |
| 42 | template <int N> |
reed@google.com | 34cec24 | 2011-04-19 15:53:12 +0000 | [diff] [blame] | 43 | int tex_coord_mask_recur(int texCoordIdx) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 44 | return GrDrawTarget::StageTexCoordVertexLayoutBit(N, texCoordIdx) | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 45 | tex_coord_mask_recur<N+1>(texCoordIdx); |
| 46 | } |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 47 | template<> |
reed@google.com | d728f6e | 2011-01-13 20:02:47 +0000 | [diff] [blame] | 48 | int tex_coord_mask_recur<GrDrawTarget::kMaxTexCoords>(int) { return 0; } |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 49 | |
| 50 | // mask of all bits relevant to one texture coordinate index |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 51 | int tex_coord_idx_mask(int texCoordIdx) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 52 | return tex_coord_mask_recur<0>(texCoordIdx); |
| 53 | } |
| 54 | |
| 55 | bool check_layout(GrVertexLayout layout) { |
| 56 | // can only have 1 or 0 bits set for each stage. |
| 57 | for (int s = 0; s < GrDrawTarget::kNumStages; ++s) { |
| 58 | int stageBits = layout & stage_mask(s); |
| 59 | if (stageBits && !GrIsPow2(stageBits)) { |
| 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | return true; |
| 64 | } |
| 65 | |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 66 | } //unnamed namespace |
| 67 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 68 | size_t GrDrawTarget::VertexSize(GrVertexLayout vertexLayout) { |
| 69 | GrAssert(check_layout(vertexLayout)); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 70 | |
| 71 | size_t vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ? |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 72 | sizeof(GrGpuTextVertex) : |
| 73 | sizeof(GrPoint); |
| 74 | |
| 75 | size_t size = vecSize; // position |
| 76 | for (int t = 0; t < kMaxTexCoords; ++t) { |
| 77 | if (tex_coord_idx_mask(t) & vertexLayout) { |
| 78 | size += vecSize; |
| 79 | } |
| 80 | } |
| 81 | if (vertexLayout & kColor_VertexLayoutBit) { |
| 82 | size += sizeof(GrColor); |
| 83 | } |
| 84 | return size; |
| 85 | } |
| 86 | |
| 87 | int GrDrawTarget::VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout) { |
| 88 | GrAssert(check_layout(vertexLayout)); |
| 89 | if (StagePosAsTexCoordVertexLayoutBit(stage) & vertexLayout) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 90 | return 0; |
| 91 | } |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 92 | int tcIdx = VertexTexCoordsForStage(stage, vertexLayout); |
| 93 | if (tcIdx >= 0) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 94 | |
| 95 | int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ? |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 96 | sizeof(GrGpuTextVertex) : |
| 97 | sizeof(GrPoint); |
| 98 | int offset = vecSize; // position |
| 99 | // figure out how many tex coordinates are present and precede this one. |
| 100 | for (int t = 0; t < tcIdx; ++t) { |
| 101 | if (tex_coord_idx_mask(t) & vertexLayout) { |
| 102 | offset += vecSize; |
| 103 | } |
| 104 | } |
| 105 | return offset; |
| 106 | } |
| 107 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 108 | return -1; |
| 109 | } |
| 110 | |
| 111 | int GrDrawTarget::VertexColorOffset(GrVertexLayout vertexLayout) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 112 | GrAssert(check_layout(vertexLayout)); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 113 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 114 | if (vertexLayout & kColor_VertexLayoutBit) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 115 | int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ? |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 116 | sizeof(GrGpuTextVertex) : |
| 117 | sizeof(GrPoint); |
| 118 | int offset = vecSize; // position |
| 119 | // figure out how many tex coordinates are present and precede this one. |
| 120 | for (int t = 0; t < kMaxTexCoords; ++t) { |
| 121 | if (tex_coord_idx_mask(t) & vertexLayout) { |
| 122 | offset += vecSize; |
| 123 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 124 | } |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 125 | return offset; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 126 | } |
| 127 | return -1; |
| 128 | } |
| 129 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 130 | int GrDrawTarget::VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout, |
| 131 | int texCoordOffsetsByIdx[kMaxTexCoords], |
| 132 | int* colorOffset) { |
| 133 | GrAssert(check_layout(vertexLayout)); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 134 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 135 | GrAssert(NULL != texCoordOffsetsByIdx); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 136 | GrAssert(NULL != colorOffset); |
| 137 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 138 | int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ? |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 139 | sizeof(GrGpuTextVertex) : |
| 140 | sizeof(GrPoint); |
| 141 | int size = vecSize; // position |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 142 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 143 | for (int t = 0; t < kMaxTexCoords; ++t) { |
| 144 | if (tex_coord_idx_mask(t) & vertexLayout) { |
| 145 | texCoordOffsetsByIdx[t] = size; |
| 146 | size += vecSize; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 147 | } else { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 148 | texCoordOffsetsByIdx[t] = -1; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 149 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 150 | } |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 151 | if (kColor_VertexLayoutBit & vertexLayout) { |
| 152 | *colorOffset = size; |
| 153 | size += sizeof(GrColor); |
| 154 | } else { |
| 155 | *colorOffset = -1; |
| 156 | } |
| 157 | return size; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 158 | } |
| 159 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 160 | int GrDrawTarget::VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout, |
| 161 | int texCoordOffsetsByStage[kNumStages], |
| 162 | int* colorOffset) { |
| 163 | GrAssert(check_layout(vertexLayout)); |
| 164 | |
| 165 | GrAssert(NULL != texCoordOffsetsByStage); |
| 166 | GrAssert(NULL != colorOffset); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 167 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 168 | int texCoordOffsetsByIdx[kMaxTexCoords]; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 169 | int size = VertexSizeAndOffsetsByIdx(vertexLayout, |
| 170 | texCoordOffsetsByIdx, |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 171 | colorOffset); |
| 172 | for (int s = 0; s < kNumStages; ++s) { |
| 173 | int tcIdx; |
| 174 | if (StagePosAsTexCoordVertexLayoutBit(s) & vertexLayout) { |
| 175 | texCoordOffsetsByStage[s] = 0; |
| 176 | } else if ((tcIdx = VertexTexCoordsForStage(s, vertexLayout)) >= 0) { |
| 177 | texCoordOffsetsByStage[s] = texCoordOffsetsByIdx[tcIdx]; |
| 178 | } else { |
| 179 | texCoordOffsetsByStage[s] = -1; |
| 180 | } |
| 181 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 182 | return size; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | bool GrDrawTarget::VertexUsesStage(int stage, GrVertexLayout vertexLayout) { |
| 186 | GrAssert(stage < kNumStages); |
| 187 | GrAssert(check_layout(vertexLayout)); |
| 188 | return !!(stage_mask(stage) & vertexLayout); |
| 189 | } |
| 190 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 191 | bool GrDrawTarget::VertexUsesTexCoordIdx(int coordIndex, |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 192 | GrVertexLayout vertexLayout) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 193 | GrAssert(coordIndex < kMaxTexCoords); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 194 | GrAssert(check_layout(vertexLayout)); |
| 195 | return !!(tex_coord_idx_mask(coordIndex) & vertexLayout); |
| 196 | } |
| 197 | |
| 198 | int GrDrawTarget::VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout) { |
| 199 | GrAssert(stage < kNumStages); |
| 200 | GrAssert(check_layout(vertexLayout)); |
| 201 | int bit = vertexLayout & stage_tex_coord_mask(stage); |
| 202 | if (bit) { |
| 203 | // figure out which set of texture coordates is used |
| 204 | // bits are ordered T0S0, T0S1, T0S2, ..., T1S0, T1S1, ... |
| 205 | // and start at bit 0. |
| 206 | GR_STATIC_ASSERT(sizeof(GrVertexLayout) <= sizeof(uint32_t)); |
| 207 | return (32 - Gr_clz(bit) - 1) / kNumStages; |
| 208 | } |
| 209 | return -1; |
| 210 | } |
| 211 | |
| 212 | void GrDrawTarget::VertexLayoutUnitTest() { |
| 213 | // not necessarily exhaustive |
| 214 | static bool run; |
| 215 | if (!run) { |
| 216 | run = true; |
| 217 | for (int s = 0; s < kNumStages; ++s) { |
| 218 | |
| 219 | GrAssert(!VertexUsesStage(s, 0)); |
| 220 | GrAssert(-1 == VertexStageCoordOffset(s, 0)); |
| 221 | GrVertexLayout stageMask = 0; |
| 222 | for (int t = 0; t < kMaxTexCoords; ++t) { |
| 223 | stageMask |= StageTexCoordVertexLayoutBit(s,t); |
| 224 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 225 | GrAssert(1 == kMaxTexCoords || !check_layout(stageMask)); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 226 | GrAssert(stage_tex_coord_mask(s) == stageMask); |
| 227 | stageMask |= StagePosAsTexCoordVertexLayoutBit(s); |
| 228 | GrAssert(stage_mask(s) == stageMask); |
| 229 | GrAssert(!check_layout(stageMask)); |
| 230 | } |
| 231 | for (int t = 0; t < kMaxTexCoords; ++t) { |
| 232 | GrVertexLayout tcMask = 0; |
| 233 | GrAssert(!VertexUsesTexCoordIdx(t, 0)); |
| 234 | for (int s = 0; s < kNumStages; ++s) { |
| 235 | tcMask |= StageTexCoordVertexLayoutBit(s,t); |
| 236 | GrAssert(VertexUsesStage(s, tcMask)); |
| 237 | GrAssert(sizeof(GrPoint) == VertexStageCoordOffset(s, tcMask)); |
| 238 | GrAssert(VertexUsesTexCoordIdx(t, tcMask)); |
| 239 | GrAssert(2*sizeof(GrPoint) == VertexSize(tcMask)); |
| 240 | GrAssert(t == VertexTexCoordsForStage(s, tcMask)); |
| 241 | for (int s2 = s + 1; s2 < kNumStages; ++s2) { |
| 242 | GrAssert(-1 == VertexStageCoordOffset(s2, tcMask)); |
| 243 | GrAssert(!VertexUsesStage(s2, tcMask)); |
| 244 | GrAssert(-1 == VertexTexCoordsForStage(s2, tcMask)); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 245 | |
bsalomon@google.com | 1962832 | 2011-02-03 21:30:17 +0000 | [diff] [blame] | 246 | #if GR_DEBUG |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 247 | GrVertexLayout posAsTex = tcMask | StagePosAsTexCoordVertexLayoutBit(s2); |
bsalomon@google.com | 1962832 | 2011-02-03 21:30:17 +0000 | [diff] [blame] | 248 | #endif |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 249 | GrAssert(0 == VertexStageCoordOffset(s2, posAsTex)); |
| 250 | GrAssert(VertexUsesStage(s2, posAsTex)); |
| 251 | GrAssert(2*sizeof(GrPoint) == VertexSize(posAsTex)); |
| 252 | GrAssert(-1 == VertexTexCoordsForStage(s2, posAsTex)); |
| 253 | } |
bsalomon@google.com | 1962832 | 2011-02-03 21:30:17 +0000 | [diff] [blame] | 254 | #if GR_DEBUG |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 255 | GrVertexLayout withColor = tcMask | kColor_VertexLayoutBit; |
bsalomon@google.com | 1962832 | 2011-02-03 21:30:17 +0000 | [diff] [blame] | 256 | #endif |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 257 | GrAssert(2*sizeof(GrPoint) == VertexColorOffset(withColor)); |
| 258 | GrAssert(2*sizeof(GrPoint) + sizeof(GrColor) == VertexSize(withColor)); |
| 259 | } |
| 260 | GrAssert(tex_coord_idx_mask(t) == tcMask); |
| 261 | GrAssert(check_layout(tcMask)); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 262 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 263 | int stageOffsets[kNumStages]; |
| 264 | int colorOffset; |
| 265 | int size; |
| 266 | size = VertexSizeAndOffsetsByStage(tcMask, stageOffsets, &colorOffset); |
| 267 | GrAssert(2*sizeof(GrPoint) == size); |
| 268 | GrAssert(-1 == colorOffset); |
| 269 | for (int s = 0; s < kNumStages; ++s) { |
| 270 | GrAssert(VertexUsesStage(s, tcMask)); |
| 271 | GrAssert(sizeof(GrPoint) == stageOffsets[s]); |
| 272 | GrAssert(sizeof(GrPoint) == VertexStageCoordOffset(s, tcMask)); |
| 273 | } |
| 274 | } |
| 275 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | //////////////////////////////////////////////////////////////////////////////// |
| 279 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 280 | #define DEBUG_INVAL_BUFFER 0xdeadcafe |
| 281 | #define DEBUG_INVAL_START_IDX -1 |
| 282 | |
| 283 | GrDrawTarget::GrDrawTarget() |
| 284 | : fGeoSrcStateStack(&fGeoSrcStateStackStorage) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 285 | #if GR_DEBUG |
| 286 | VertexLayoutUnitTest(); |
| 287 | #endif |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 288 | GeometrySrcState& geoSrc = fGeoSrcStateStack.push_back(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 289 | #if GR_DEBUG |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 290 | geoSrc.fVertexCount = DEBUG_INVAL_START_IDX; |
| 291 | geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER; |
| 292 | geoSrc.fIndexCount = DEBUG_INVAL_START_IDX; |
| 293 | geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 294 | #endif |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 295 | geoSrc.fVertexSrc = kNone_GeometrySrcType; |
| 296 | geoSrc.fIndexSrc = kNone_GeometrySrcType; |
| 297 | } |
| 298 | |
| 299 | GrDrawTarget::~GrDrawTarget() { |
| 300 | int popCnt = fGeoSrcStateStack.count() - 1; |
| 301 | while (popCnt) { |
| 302 | this->popGeometrySource(); |
| 303 | --popCnt; |
| 304 | } |
| 305 | this->releasePreviousVertexSource(); |
| 306 | this->releasePreviousIndexSource(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | void GrDrawTarget::setClip(const GrClip& clip) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 310 | clipWillBeSet(clip); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 311 | fClip = clip; |
| 312 | } |
| 313 | |
| 314 | const GrClip& GrDrawTarget::getClip() const { |
| 315 | return fClip; |
| 316 | } |
| 317 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 318 | void GrDrawTarget::setTexture(int stage, GrTexture* tex) { |
| 319 | GrAssert(stage >= 0 && stage < kNumStages); |
| 320 | fCurrDrawState.fTextures[stage] = tex; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 321 | } |
| 322 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 323 | const GrTexture* GrDrawTarget::getTexture(int stage) const { |
| 324 | GrAssert(stage >= 0 && stage < kNumStages); |
| 325 | return fCurrDrawState.fTextures[stage]; |
| 326 | } |
| 327 | |
| 328 | GrTexture* GrDrawTarget::getTexture(int stage) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 329 | GrAssert(stage >= 0 && stage < kNumStages); |
| 330 | return fCurrDrawState.fTextures[stage]; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | void GrDrawTarget::setRenderTarget(GrRenderTarget* target) { |
| 334 | fCurrDrawState.fRenderTarget = target; |
| 335 | } |
| 336 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 337 | const GrRenderTarget* GrDrawTarget::getRenderTarget() const { |
| 338 | return fCurrDrawState.fRenderTarget; |
| 339 | } |
| 340 | |
| 341 | GrRenderTarget* GrDrawTarget::getRenderTarget() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 342 | return fCurrDrawState.fRenderTarget; |
| 343 | } |
| 344 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 345 | void GrDrawTarget::setViewMatrix(const GrMatrix& m) { |
| 346 | fCurrDrawState.fViewMatrix = m; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 347 | } |
| 348 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 349 | void GrDrawTarget::preConcatViewMatrix(const GrMatrix& matrix) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 350 | fCurrDrawState.fViewMatrix.preConcat(matrix); |
| 351 | } |
| 352 | |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 353 | void GrDrawTarget::postConcatViewMatrix(const GrMatrix& matrix) { |
| 354 | fCurrDrawState.fViewMatrix.postConcat(matrix); |
| 355 | } |
| 356 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 357 | const GrMatrix& GrDrawTarget::getViewMatrix() const { |
| 358 | return fCurrDrawState.fViewMatrix; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | bool GrDrawTarget::getViewInverse(GrMatrix* matrix) const { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 362 | // Mike: Can we cache this somewhere? |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 363 | // Brian: Sure, do we use it often? |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 364 | |
| 365 | GrMatrix inverse; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 366 | if (fCurrDrawState.fViewMatrix.invert(&inverse)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 367 | if (matrix) { |
| 368 | *matrix = inverse; |
| 369 | } |
| 370 | return true; |
| 371 | } |
| 372 | return false; |
| 373 | } |
| 374 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 375 | void GrDrawTarget::setSamplerState(int stage, const GrSamplerState& state) { |
| 376 | GrAssert(stage >= 0 && stage < kNumStages); |
| 377 | fCurrDrawState.fSamplerStates[stage] = state; |
| 378 | } |
| 379 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 380 | void GrDrawTarget::enableState(uint32_t bits) { |
| 381 | fCurrDrawState.fFlagBits |= bits; |
| 382 | } |
| 383 | |
| 384 | void GrDrawTarget::disableState(uint32_t bits) { |
| 385 | fCurrDrawState.fFlagBits &= ~(bits); |
| 386 | } |
| 387 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 388 | void GrDrawTarget::setBlendFunc(GrBlendCoeff srcCoeff, |
| 389 | GrBlendCoeff dstCoeff) { |
| 390 | fCurrDrawState.fSrcBlend = srcCoeff; |
| 391 | fCurrDrawState.fDstBlend = dstCoeff; |
| 392 | #if GR_DEBUG |
| 393 | switch (dstCoeff) { |
| 394 | case kDC_BlendCoeff: |
| 395 | case kIDC_BlendCoeff: |
| 396 | case kDA_BlendCoeff: |
| 397 | case kIDA_BlendCoeff: |
| 398 | GrPrintf("Unexpected dst blend coeff. Won't work correctly with" |
| 399 | "coverage stages.\n"); |
| 400 | break; |
| 401 | default: |
| 402 | break; |
| 403 | } |
| 404 | switch (srcCoeff) { |
| 405 | case kSC_BlendCoeff: |
| 406 | case kISC_BlendCoeff: |
| 407 | case kSA_BlendCoeff: |
| 408 | case kISA_BlendCoeff: |
| 409 | GrPrintf("Unexpected src blend coeff. Won't work correctly with" |
| 410 | "coverage stages.\n"); |
| 411 | break; |
| 412 | default: |
| 413 | break; |
| 414 | } |
| 415 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | void GrDrawTarget::setColor(GrColor c) { |
| 419 | fCurrDrawState.fColor = c; |
| 420 | } |
| 421 | |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 422 | void GrDrawTarget::setColorFilter(GrColor c, SkXfermode::Mode mode) { |
| 423 | fCurrDrawState.fColorFilterColor = c; |
| 424 | fCurrDrawState.fColorFilterXfermode = mode; |
| 425 | } |
| 426 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 427 | void GrDrawTarget::setAlpha(uint8_t a) { |
| 428 | this->setColor((a << 24) | (a << 16) | (a << 8) | a); |
| 429 | } |
| 430 | |
| 431 | void GrDrawTarget::saveCurrentDrawState(SavedDrawState* state) const { |
| 432 | state->fState = fCurrDrawState; |
| 433 | } |
| 434 | |
| 435 | void GrDrawTarget::restoreDrawState(const SavedDrawState& state) { |
| 436 | fCurrDrawState = state.fState; |
| 437 | } |
| 438 | |
| 439 | void GrDrawTarget::copyDrawState(const GrDrawTarget& srcTarget) { |
| 440 | fCurrDrawState = srcTarget.fCurrDrawState; |
| 441 | } |
| 442 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 443 | bool GrDrawTarget::reserveVertexSpace(GrVertexLayout vertexLayout, |
| 444 | int vertexCount, |
| 445 | void** vertices) { |
| 446 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 447 | bool acquired = false; |
| 448 | if (vertexCount > 0) { |
| 449 | GrAssert(NULL != vertices); |
| 450 | this->releasePreviousVertexSource(); |
| 451 | geoSrc.fVertexSrc = kNone_GeometrySrcType; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 452 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 453 | acquired = this->onReserveVertexSpace(vertexLayout, |
| 454 | vertexCount, |
| 455 | vertices); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 456 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 457 | if (acquired) { |
| 458 | geoSrc.fVertexSrc = kReserved_GeometrySrcType; |
| 459 | geoSrc.fVertexCount = vertexCount; |
| 460 | geoSrc.fVertexLayout = vertexLayout; |
| 461 | } else if (NULL != vertices) { |
| 462 | *vertices = NULL; |
| 463 | } |
| 464 | return acquired; |
| 465 | } |
| 466 | |
| 467 | bool GrDrawTarget::reserveIndexSpace(int indexCount, |
| 468 | void** indices) { |
| 469 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 470 | bool acquired = false; |
| 471 | if (indexCount > 0) { |
| 472 | GrAssert(NULL != indices); |
| 473 | this->releasePreviousIndexSource(); |
| 474 | geoSrc.fIndexSrc = kNone_GeometrySrcType; |
| 475 | |
| 476 | acquired = this->onReserveIndexSpace(indexCount, indices); |
| 477 | } |
| 478 | if (acquired) { |
| 479 | geoSrc.fIndexSrc = kReserved_GeometrySrcType; |
| 480 | geoSrc.fIndexCount = indexCount; |
| 481 | } else if (NULL != indices) { |
| 482 | *indices = NULL; |
| 483 | } |
| 484 | return acquired; |
| 485 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | bool GrDrawTarget::geometryHints(GrVertexLayout vertexLayout, |
| 489 | int32_t* vertexCount, |
| 490 | int32_t* indexCount) const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 491 | if (NULL != vertexCount) { |
| 492 | *vertexCount = -1; |
| 493 | } |
| 494 | if (NULL != indexCount) { |
| 495 | *indexCount = -1; |
| 496 | } |
| 497 | return false; |
| 498 | } |
| 499 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 500 | void GrDrawTarget::releasePreviousVertexSource() { |
| 501 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 502 | switch (geoSrc.fVertexSrc) { |
| 503 | case kNone_GeometrySrcType: |
| 504 | break; |
| 505 | case kArray_GeometrySrcType: |
| 506 | this->releaseVertexArray(); |
| 507 | break; |
| 508 | case kReserved_GeometrySrcType: |
| 509 | this->releaseReservedVertexSpace(); |
| 510 | break; |
| 511 | case kBuffer_GeometrySrcType: |
| 512 | geoSrc.fVertexBuffer->unref(); |
| 513 | #if GR_DEBUG |
| 514 | geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER; |
| 515 | #endif |
| 516 | break; |
| 517 | default: |
| 518 | GrCrash("Unknown Vertex Source Type."); |
| 519 | break; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | void GrDrawTarget::releasePreviousIndexSource() { |
| 524 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 525 | switch (geoSrc.fIndexSrc) { |
| 526 | case kNone_GeometrySrcType: // these two don't require |
| 527 | break; |
| 528 | case kArray_GeometrySrcType: |
| 529 | this->releaseIndexArray(); |
| 530 | break; |
| 531 | case kReserved_GeometrySrcType: |
| 532 | this->releaseReservedIndexSpace(); |
| 533 | break; |
| 534 | case kBuffer_GeometrySrcType: |
| 535 | geoSrc.fIndexBuffer->unref(); |
| 536 | #if GR_DEBUG |
| 537 | geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER; |
| 538 | #endif |
| 539 | break; |
| 540 | default: |
| 541 | GrCrash("Unknown Index Source Type."); |
| 542 | break; |
| 543 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 544 | } |
| 545 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 546 | void GrDrawTarget::setVertexSourceToArray(GrVertexLayout vertexLayout, |
| 547 | const void* vertexArray, |
| 548 | int vertexCount) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 549 | this->releasePreviousVertexSource(); |
| 550 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 551 | geoSrc.fVertexSrc = kArray_GeometrySrcType; |
| 552 | geoSrc.fVertexLayout = vertexLayout; |
| 553 | geoSrc.fVertexCount = vertexCount; |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 554 | this->onSetVertexSourceToArray(vertexArray, vertexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 555 | } |
| 556 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 557 | void GrDrawTarget::setIndexSourceToArray(const void* indexArray, |
| 558 | int indexCount) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 559 | this->releasePreviousIndexSource(); |
| 560 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 561 | geoSrc.fIndexSrc = kArray_GeometrySrcType; |
| 562 | geoSrc.fIndexCount = indexCount; |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 563 | this->onSetIndexSourceToArray(indexArray, indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 564 | } |
| 565 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 566 | void GrDrawTarget::setVertexSourceToBuffer(GrVertexLayout vertexLayout, |
| 567 | const GrVertexBuffer* buffer) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 568 | this->releasePreviousVertexSource(); |
| 569 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 570 | geoSrc.fVertexSrc = kBuffer_GeometrySrcType; |
| 571 | geoSrc.fVertexBuffer = buffer; |
| 572 | buffer->ref(); |
| 573 | geoSrc.fVertexLayout = vertexLayout; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | void GrDrawTarget::setIndexSourceToBuffer(const GrIndexBuffer* buffer) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 577 | this->releasePreviousIndexSource(); |
| 578 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 579 | geoSrc.fIndexSrc = kBuffer_GeometrySrcType; |
| 580 | geoSrc.fIndexBuffer = buffer; |
| 581 | buffer->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 582 | } |
| 583 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 584 | void GrDrawTarget::resetVertexSource() { |
| 585 | this->releasePreviousVertexSource(); |
| 586 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 587 | geoSrc.fVertexSrc = kNone_GeometrySrcType; |
| 588 | } |
| 589 | |
| 590 | void GrDrawTarget::resetIndexSource() { |
| 591 | this->releasePreviousIndexSource(); |
| 592 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 593 | geoSrc.fIndexSrc = kNone_GeometrySrcType; |
| 594 | } |
| 595 | |
| 596 | void GrDrawTarget::pushGeometrySource() { |
| 597 | this->geometrySourceWillPush(); |
| 598 | GeometrySrcState& newState = fGeoSrcStateStack.push_back(); |
| 599 | newState.fIndexSrc = kNone_GeometrySrcType; |
| 600 | newState.fVertexSrc = kNone_GeometrySrcType; |
| 601 | #if GR_DEBUG |
| 602 | newState.fVertexCount = ~0; |
| 603 | newState.fVertexBuffer = (GrVertexBuffer*)~0; |
| 604 | newState.fIndexCount = ~0; |
| 605 | newState.fIndexBuffer = (GrIndexBuffer*)~0; |
| 606 | #endif |
| 607 | } |
| 608 | |
| 609 | void GrDrawTarget::popGeometrySource() { |
| 610 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 611 | // if popping last element then pops are unbalanced with pushes |
| 612 | GrAssert(fGeoSrcStateStack.count() > 1); |
| 613 | |
| 614 | this->geometrySourceWillPop(fGeoSrcStateStack.fromBack(1)); |
| 615 | this->releasePreviousVertexSource(); |
| 616 | this->releasePreviousIndexSource(); |
| 617 | fGeoSrcStateStack.pop_back(); |
| 618 | } |
| 619 | |
| 620 | //////////////////////////////////////////////////////////////////////////////// |
| 621 | |
| 622 | void GrDrawTarget::drawIndexed(GrPrimitiveType type, int startVertex, |
| 623 | int startIndex, int vertexCount, |
| 624 | int indexCount) { |
| 625 | #if GR_DEBUG |
| 626 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 627 | int maxVertex = startVertex + vertexCount; |
| 628 | int maxValidVertex; |
| 629 | switch (geoSrc.fVertexSrc) { |
| 630 | case kNone_GeometrySrcType: |
| 631 | GrCrash("Attempting to draw indexed geom without vertex src."); |
| 632 | case kReserved_GeometrySrcType: // fallthrough |
| 633 | case kArray_GeometrySrcType: |
| 634 | maxValidVertex = geoSrc.fVertexCount; |
| 635 | break; |
| 636 | case kBuffer_GeometrySrcType: |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 637 | maxValidVertex = geoSrc.fVertexBuffer->sizeInBytes() / |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 638 | VertexSize(geoSrc.fVertexLayout); |
| 639 | break; |
| 640 | } |
| 641 | if (maxVertex > maxValidVertex) { |
| 642 | GrCrash("Indexed drawing outside valid vertex range."); |
| 643 | } |
| 644 | int maxIndex = startIndex + indexCount; |
| 645 | int maxValidIndex; |
| 646 | switch (geoSrc.fIndexSrc) { |
| 647 | case kNone_GeometrySrcType: |
| 648 | GrCrash("Attempting to draw indexed geom without index src."); |
| 649 | case kReserved_GeometrySrcType: // fallthrough |
| 650 | case kArray_GeometrySrcType: |
| 651 | maxValidIndex = geoSrc.fIndexCount; |
| 652 | break; |
| 653 | case kBuffer_GeometrySrcType: |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 654 | maxValidIndex = geoSrc.fIndexBuffer->sizeInBytes() / sizeof(uint16_t); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 655 | break; |
| 656 | } |
| 657 | if (maxIndex > maxValidIndex) { |
| 658 | GrCrash("Indexed drawing outside valid index range."); |
| 659 | } |
| 660 | #endif |
bsalomon@google.com | 8214587 | 2011-08-23 14:32:40 +0000 | [diff] [blame] | 661 | if (indexCount > 0) { |
| 662 | this->onDrawIndexed(type, startVertex, startIndex, |
| 663 | vertexCount, indexCount); |
| 664 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | |
| 668 | void GrDrawTarget::drawNonIndexed(GrPrimitiveType type, |
| 669 | int startVertex, |
| 670 | int vertexCount) { |
| 671 | #if GR_DEBUG |
| 672 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 673 | int maxVertex = startVertex + vertexCount; |
| 674 | int maxValidVertex; |
| 675 | switch (geoSrc.fVertexSrc) { |
| 676 | case kNone_GeometrySrcType: |
| 677 | GrCrash("Attempting to draw non-indexed geom without vertex src."); |
| 678 | case kReserved_GeometrySrcType: // fallthrough |
| 679 | case kArray_GeometrySrcType: |
| 680 | maxValidVertex = geoSrc.fVertexCount; |
| 681 | break; |
| 682 | case kBuffer_GeometrySrcType: |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 683 | maxValidVertex = geoSrc.fVertexBuffer->sizeInBytes() / |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 684 | VertexSize(geoSrc.fVertexLayout); |
| 685 | break; |
| 686 | } |
| 687 | if (maxVertex > maxValidVertex) { |
| 688 | GrCrash("Non-indexed drawing outside valid vertex range."); |
| 689 | } |
| 690 | #endif |
bsalomon@google.com | 8214587 | 2011-08-23 14:32:40 +0000 | [diff] [blame] | 691 | if (vertexCount > 0) { |
| 692 | this->onDrawNonIndexed(type, startVertex, vertexCount); |
| 693 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 697 | |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame^] | 698 | bool GrDrawTarget::CanDisableBlend(GrVertexLayout layout, const DrState& state) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 699 | // If we compute a coverage value (using edge AA or a coverage stage) then |
| 700 | // we can't force blending off. |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame^] | 701 | if (state.fEdgeAANumEdges > 0) { |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 702 | return false; |
| 703 | } |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame^] | 704 | for (int s = state.fFirstCoverageStage; s < kNumStages; ++s) { |
| 705 | if (StageWillBeUsed(s, layout, state)) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 706 | return false; |
| 707 | } |
| 708 | } |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 709 | |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame^] | 710 | if ((kOne_BlendCoeff == state.fSrcBlend) && |
| 711 | (kZero_BlendCoeff == state.fDstBlend)) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 712 | return true; |
| 713 | } |
| 714 | |
| 715 | // If we have vertex color without alpha then we can't force blend off |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame^] | 716 | if ((layout & kColor_VertexLayoutBit) || |
| 717 | 0xff != GrColorUnpackA(state.fColor)) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 718 | return false; |
| 719 | } |
| 720 | |
| 721 | // If the src coef will always be 1... |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame^] | 722 | if (kSA_BlendCoeff != state.fSrcBlend && |
| 723 | kOne_BlendCoeff != state.fSrcBlend) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 724 | return false; |
| 725 | } |
| 726 | |
| 727 | // ...and the dst coef is always 0... |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame^] | 728 | if (kISA_BlendCoeff != state.fDstBlend && |
| 729 | kZero_BlendCoeff != state.fDstBlend) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 730 | return false; |
| 731 | } |
| 732 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 733 | // ...and there isn't a texture stage with an alpha channel... |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame^] | 734 | for (int s = 0; s < state.fFirstCoverageStage; ++s) { |
| 735 | if (StageWillBeUsed(s, layout, state)) { |
| 736 | GrAssert(NULL != state.fTextures[s]); |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 737 | |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame^] | 738 | GrPixelConfig config = state.fTextures[s]->config(); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 739 | |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 740 | if (!GrPixelConfigIsOpaque(config)) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 741 | return false; |
| 742 | } |
| 743 | } |
| 744 | } |
| 745 | |
Scroggo | 0bad673 | 2011-05-11 20:25:01 +0000 | [diff] [blame] | 746 | // ...and there isn't an interesting color filter... |
| 747 | // TODO: Consider being more aggressive with regards to disabling |
| 748 | // blending when a color filter is used. |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame^] | 749 | if (SkXfermode::kDst_Mode != state.fColorFilterXfermode) { |
Scroggo | 0bad673 | 2011-05-11 20:25:01 +0000 | [diff] [blame] | 750 | return false; |
| 751 | } |
| 752 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 753 | // ...then we disable blend. |
| 754 | return true; |
| 755 | } |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 756 | |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame^] | 757 | bool GrDrawTarget::CanUseHWAALines(GrVertexLayout layout, const DrState& state) { |
| 758 | // there is a conflict between using smooth lines and our use of |
| 759 | // premultiplied alpha. Smooth lines tweak the incoming alpha value |
| 760 | // but not in a premul-alpha way. So we only use them when our alpha |
| 761 | // is 0xff. |
| 762 | return (kAntialias_StateBit & state.fFlagBits) && |
| 763 | CanDisableBlend(layout, state); |
| 764 | } |
| 765 | |
| 766 | bool GrDrawTarget::canDisableBlend() const { |
| 767 | return CanDisableBlend(this->getGeomSrc().fVertexLayout, fCurrDrawState); |
| 768 | } |
| 769 | |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 770 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame^] | 771 | |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 772 | void GrDrawTarget::setEdgeAAData(const Edge* edges, int numEdges) { |
| 773 | GrAssert(numEdges <= kMaxEdges); |
| 774 | memcpy(fCurrDrawState.fEdgeAAEdges, edges, numEdges * sizeof(Edge)); |
| 775 | fCurrDrawState.fEdgeAANumEdges = numEdges; |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 779 | //////////////////////////////////////////////////////////////////////////////// |
| 780 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 781 | void GrDrawTarget::drawRect(const GrRect& rect, |
| 782 | const GrMatrix* matrix, |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 783 | StageBitfield stageEnableBitfield, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 784 | const GrRect* srcRects[], |
| 785 | const GrMatrix* srcMatrices[]) { |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 786 | GrVertexLayout layout = GetRectVertexLayout(stageEnableBitfield, srcRects); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 787 | |
| 788 | AutoReleaseGeometry geo(this, layout, 4, 0); |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 789 | if (!geo.succeeded()) { |
| 790 | GrPrintf("Failed to get space for vertices!\n"); |
| 791 | return; |
| 792 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 793 | |
| 794 | SetRectVertices(rect, matrix, srcRects, |
| 795 | srcMatrices, layout, geo.vertices()); |
| 796 | |
| 797 | drawNonIndexed(kTriangleFan_PrimitiveType, 0, 4); |
| 798 | } |
| 799 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 800 | GrVertexLayout GrDrawTarget::GetRectVertexLayout(StageBitfield stageEnableBitfield, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 801 | const GrRect* srcRects[]) { |
| 802 | GrVertexLayout layout = 0; |
| 803 | |
| 804 | for (int i = 0; i < kNumStages; ++i) { |
| 805 | int numTC = 0; |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 806 | if (stageEnableBitfield & (1 << i)) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 807 | if (NULL != srcRects && NULL != srcRects[i]) { |
| 808 | layout |= StageTexCoordVertexLayoutBit(i, numTC); |
| 809 | ++numTC; |
| 810 | } else { |
| 811 | layout |= StagePosAsTexCoordVertexLayoutBit(i); |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | return layout; |
| 816 | } |
bsalomon@google.com | dea2f8d | 2011-08-01 15:51:05 +0000 | [diff] [blame] | 817 | |
| 818 | void GrDrawTarget::clipWillBeSet(const GrClip& clip) { |
| 819 | } |
| 820 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 821 | void GrDrawTarget::SetRectVertices(const GrRect& rect, |
| 822 | const GrMatrix* matrix, |
| 823 | const GrRect* srcRects[], |
| 824 | const GrMatrix* srcMatrices[], |
| 825 | GrVertexLayout layout, |
| 826 | void* vertices) { |
| 827 | #if GR_DEBUG |
| 828 | // check that the layout and srcRects agree |
| 829 | for (int i = 0; i < kNumStages; ++i) { |
| 830 | if (VertexTexCoordsForStage(i, layout) >= 0) { |
| 831 | GR_DEBUGASSERT(NULL != srcRects && NULL != srcRects[i]); |
| 832 | } else { |
| 833 | GR_DEBUGASSERT(NULL == srcRects || NULL == srcRects[i]); |
| 834 | } |
| 835 | } |
| 836 | #endif |
| 837 | |
| 838 | int stageOffsets[kNumStages]; |
| 839 | int colorOffset; |
| 840 | int vsize = VertexSizeAndOffsetsByStage(layout, stageOffsets, &colorOffset); |
| 841 | GrAssert(-1 == colorOffset); |
| 842 | |
| 843 | GrTCast<GrPoint*>(vertices)->setRectFan(rect.fLeft, rect.fTop, |
| 844 | rect.fRight, rect.fBottom, |
| 845 | vsize); |
| 846 | if (NULL != matrix) { |
| 847 | matrix->mapPointsWithStride(GrTCast<GrPoint*>(vertices), vsize, 4); |
| 848 | } |
| 849 | |
| 850 | for (int i = 0; i < kNumStages; ++i) { |
| 851 | if (stageOffsets[i] > 0) { |
| 852 | GrPoint* coords = GrTCast<GrPoint*>(GrTCast<intptr_t>(vertices) + |
| 853 | stageOffsets[i]); |
| 854 | coords->setRectFan(srcRects[i]->fLeft, srcRects[i]->fTop, |
| 855 | srcRects[i]->fRight, srcRects[i]->fBottom, |
| 856 | vsize); |
| 857 | if (NULL != srcMatrices && NULL != srcMatrices[i]) { |
| 858 | srcMatrices[i]->mapPointsWithStride(coords, vsize, 4); |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 864 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 865 | |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 866 | GrDrawTarget::AutoStateRestore::AutoStateRestore() { |
| 867 | fDrawTarget = NULL; |
| 868 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 869 | |
| 870 | GrDrawTarget::AutoStateRestore::AutoStateRestore(GrDrawTarget* target) { |
| 871 | fDrawTarget = target; |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 872 | if (NULL != fDrawTarget) { |
| 873 | fDrawTarget->saveCurrentDrawState(&fDrawState); |
| 874 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | GrDrawTarget::AutoStateRestore::~AutoStateRestore() { |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 878 | if (NULL != fDrawTarget) { |
| 879 | fDrawTarget->restoreDrawState(fDrawState); |
| 880 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 881 | } |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 882 | |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 883 | void GrDrawTarget::AutoStateRestore::set(GrDrawTarget* target) { |
| 884 | if (target != fDrawTarget) { |
| 885 | if (NULL != fDrawTarget) { |
| 886 | fDrawTarget->restoreDrawState(fDrawState); |
| 887 | } |
| 888 | if (NULL != target) { |
bsalomon@google.com | d19aa27 | 2011-06-22 01:28:17 +0000 | [diff] [blame] | 889 | target->saveCurrentDrawState(&fDrawState); |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 890 | } |
| 891 | fDrawTarget = target; |
| 892 | } |
| 893 | } |
bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 894 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 895 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 896 | |
| 897 | GrDrawTarget::AutoDeviceCoordDraw::AutoDeviceCoordDraw(GrDrawTarget* target, |
| 898 | int stageMask) { |
| 899 | GrAssert(NULL != target); |
| 900 | |
| 901 | fDrawTarget = target; |
| 902 | fViewMatrix = target->getViewMatrix(); |
| 903 | fStageMask = stageMask; |
| 904 | if (fStageMask) { |
| 905 | GrMatrix invVM; |
| 906 | if (fViewMatrix.invert(&invVM)) { |
| 907 | for (int s = 0; s < kNumStages; ++s) { |
| 908 | if (fStageMask & (1 << s)) { |
| 909 | fSamplerMatrices[s] = target->getSamplerMatrix(s); |
| 910 | } |
| 911 | } |
| 912 | target->preConcatSamplerMatrices(fStageMask, invVM); |
| 913 | } else { |
| 914 | // sad trombone sound |
| 915 | fStageMask = 0; |
| 916 | } |
| 917 | } |
| 918 | target->setViewMatrix(GrMatrix::I()); |
| 919 | } |
| 920 | |
| 921 | GrDrawTarget::AutoDeviceCoordDraw::~AutoDeviceCoordDraw() { |
| 922 | fDrawTarget->setViewMatrix(fViewMatrix); |
| 923 | for (int s = 0; s < kNumStages; ++s) { |
| 924 | if (fStageMask & (1 << s)) { |
| 925 | fDrawTarget->setSamplerMatrix(s, fSamplerMatrices[s]); |
| 926 | } |
| 927 | } |
| 928 | } |
| 929 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 930 | //////////////////////////////////////////////////////////////////////////////// |
| 931 | |
| 932 | GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry( |
| 933 | GrDrawTarget* target, |
| 934 | GrVertexLayout vertexLayout, |
| 935 | int vertexCount, |
| 936 | int indexCount) { |
| 937 | fTarget = NULL; |
| 938 | this->set(target, vertexLayout, vertexCount, indexCount); |
| 939 | } |
| 940 | |
| 941 | GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry() { |
| 942 | fTarget = NULL; |
| 943 | } |
| 944 | |
| 945 | GrDrawTarget::AutoReleaseGeometry::~AutoReleaseGeometry() { |
| 946 | this->reset(); |
| 947 | } |
| 948 | |
| 949 | bool GrDrawTarget::AutoReleaseGeometry::set(GrDrawTarget* target, |
| 950 | GrVertexLayout vertexLayout, |
| 951 | int vertexCount, |
| 952 | int indexCount) { |
| 953 | this->reset(); |
| 954 | fTarget = target; |
| 955 | bool success = true; |
| 956 | if (NULL != fTarget) { |
| 957 | fTarget = target; |
| 958 | if (vertexCount > 0) { |
| 959 | success = target->reserveVertexSpace(vertexLayout, |
| 960 | vertexCount, |
| 961 | &fVertices); |
| 962 | if (!success) { |
| 963 | this->reset(); |
| 964 | } |
| 965 | } |
| 966 | if (success && indexCount > 0) { |
| 967 | success = target->reserveIndexSpace(indexCount, &fIndices); |
| 968 | if (!success) { |
| 969 | this->reset(); |
| 970 | } |
| 971 | } |
| 972 | } |
| 973 | GrAssert(success == (NULL != fTarget)); |
| 974 | return success; |
| 975 | } |
| 976 | |
| 977 | void GrDrawTarget::AutoReleaseGeometry::reset() { |
| 978 | if (NULL != fTarget) { |
| 979 | if (NULL != fVertices) { |
| 980 | fTarget->resetVertexSource(); |
| 981 | } |
| 982 | if (NULL != fIndices) { |
| 983 | fTarget->resetIndexSource(); |
| 984 | } |
| 985 | fTarget = NULL; |
| 986 | } |
bsalomon@google.com | cb0c5ab | 2011-06-29 17:48:17 +0000 | [diff] [blame] | 987 | fVertices = NULL; |
| 988 | fIndices = NULL; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 989 | } |
| 990 | |