reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2010 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #include "GrDrawTarget.h" |
| 19 | #include "GrGpuVertex.h" |
| 20 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 21 | // recursive helper for creating mask with all the tex coord bits set for |
| 22 | // one stage |
| 23 | template <int N> |
| 24 | static int stage_mask_recur(int stage) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 25 | return GrDrawTarget::StageTexCoordVertexLayoutBit(stage, N) | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 26 | stage_mask_recur<N+1>(stage); |
| 27 | } |
reed@google.com | d728f6e | 2011-01-13 20:02:47 +0000 | [diff] [blame] | 28 | template<> // linux build doesn't like static on specializations |
| 29 | int stage_mask_recur<GrDrawTarget::kNumStages>(int) { return 0; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 30 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 31 | // mask of all tex coord indices for one stage |
| 32 | static int stage_tex_coord_mask(int stage) { |
| 33 | return stage_mask_recur<0>(stage); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 34 | } |
| 35 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 36 | // mask of all bits relevant to one stage |
| 37 | static int stage_mask(int stage) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 38 | return stage_tex_coord_mask(stage) | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 39 | GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(stage); |
| 40 | } |
| 41 | |
| 42 | // recursive helper for creating mask of with all bits set relevant to one |
| 43 | // texture coordinate index |
| 44 | template <int N> |
| 45 | static int tex_coord_mask_recur(int texCoordIdx) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 46 | return GrDrawTarget::StageTexCoordVertexLayoutBit(N, texCoordIdx) | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 47 | tex_coord_mask_recur<N+1>(texCoordIdx); |
| 48 | } |
reed@google.com | d728f6e | 2011-01-13 20:02:47 +0000 | [diff] [blame] | 49 | template<> // linux build doesn't like static on specializations |
| 50 | int tex_coord_mask_recur<GrDrawTarget::kMaxTexCoords>(int) { return 0; } |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 51 | |
| 52 | // mask of all bits relevant to one texture coordinate index |
| 53 | static int tex_coord_idx_mask(int texCoordIdx) { |
| 54 | return tex_coord_mask_recur<0>(texCoordIdx); |
| 55 | } |
| 56 | |
| 57 | bool check_layout(GrVertexLayout layout) { |
| 58 | // can only have 1 or 0 bits set for each stage. |
| 59 | for (int s = 0; s < GrDrawTarget::kNumStages; ++s) { |
| 60 | int stageBits = layout & stage_mask(s); |
| 61 | if (stageBits && !GrIsPow2(stageBits)) { |
| 62 | return false; |
| 63 | } |
| 64 | } |
| 65 | return true; |
| 66 | } |
| 67 | |
| 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 | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 246 | GrVertexLayout posAsTex = tcMask | StagePosAsTexCoordVertexLayoutBit(s2); |
| 247 | GrAssert(0 == VertexStageCoordOffset(s2, posAsTex)); |
| 248 | GrAssert(VertexUsesStage(s2, posAsTex)); |
| 249 | GrAssert(2*sizeof(GrPoint) == VertexSize(posAsTex)); |
| 250 | GrAssert(-1 == VertexTexCoordsForStage(s2, posAsTex)); |
| 251 | } |
| 252 | GrVertexLayout withColor = tcMask | kColor_VertexLayoutBit; |
| 253 | GrAssert(2*sizeof(GrPoint) == VertexColorOffset(withColor)); |
| 254 | GrAssert(2*sizeof(GrPoint) + sizeof(GrColor) == VertexSize(withColor)); |
| 255 | } |
| 256 | GrAssert(tex_coord_idx_mask(t) == tcMask); |
| 257 | GrAssert(check_layout(tcMask)); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 258 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 259 | int stageOffsets[kNumStages]; |
| 260 | int colorOffset; |
| 261 | int size; |
| 262 | size = VertexSizeAndOffsetsByStage(tcMask, stageOffsets, &colorOffset); |
| 263 | GrAssert(2*sizeof(GrPoint) == size); |
| 264 | GrAssert(-1 == colorOffset); |
| 265 | for (int s = 0; s < kNumStages; ++s) { |
| 266 | GrAssert(VertexUsesStage(s, tcMask)); |
| 267 | GrAssert(sizeof(GrPoint) == stageOffsets[s]); |
| 268 | GrAssert(sizeof(GrPoint) == VertexStageCoordOffset(s, tcMask)); |
| 269 | } |
| 270 | } |
| 271 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | //////////////////////////////////////////////////////////////////////////////// |
| 275 | |
| 276 | GrDrawTarget::GrDrawTarget() { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 277 | #if GR_DEBUG |
| 278 | VertexLayoutUnitTest(); |
| 279 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 280 | fReservedGeometry.fLocked = false; |
| 281 | #if GR_DEBUG |
| 282 | fReservedGeometry.fVertexCount = ~0; |
| 283 | fReservedGeometry.fIndexCount = ~0; |
| 284 | #endif |
| 285 | fGeometrySrc.fVertexSrc = kReserved_GeometrySrcType; |
| 286 | fGeometrySrc.fIndexSrc = kReserved_GeometrySrcType; |
| 287 | } |
| 288 | |
| 289 | void GrDrawTarget::setClip(const GrClip& clip) { |
| 290 | clipWillChange(clip); |
| 291 | fClip = clip; |
| 292 | } |
| 293 | |
| 294 | const GrClip& GrDrawTarget::getClip() const { |
| 295 | return fClip; |
| 296 | } |
| 297 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 298 | void GrDrawTarget::setTexture(int stage, GrTexture* tex) { |
| 299 | GrAssert(stage >= 0 && stage < kNumStages); |
| 300 | fCurrDrawState.fTextures[stage] = tex; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 301 | } |
| 302 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 303 | const GrTexture* GrDrawTarget::getTexture(int stage) const { |
| 304 | GrAssert(stage >= 0 && stage < kNumStages); |
| 305 | return fCurrDrawState.fTextures[stage]; |
| 306 | } |
| 307 | |
| 308 | GrTexture* GrDrawTarget::getTexture(int stage) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 309 | GrAssert(stage >= 0 && stage < kNumStages); |
| 310 | return fCurrDrawState.fTextures[stage]; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | void GrDrawTarget::setRenderTarget(GrRenderTarget* target) { |
| 314 | fCurrDrawState.fRenderTarget = target; |
| 315 | } |
| 316 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 317 | const GrRenderTarget* GrDrawTarget::getRenderTarget() const { |
| 318 | return fCurrDrawState.fRenderTarget; |
| 319 | } |
| 320 | |
| 321 | GrRenderTarget* GrDrawTarget::getRenderTarget() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 322 | return fCurrDrawState.fRenderTarget; |
| 323 | } |
| 324 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 325 | void GrDrawTarget::setViewMatrix(const GrMatrix& m) { |
| 326 | fCurrDrawState.fViewMatrix = m; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 327 | } |
| 328 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 329 | void GrDrawTarget::concatViewMatrix(const GrMatrix& matrix) { |
| 330 | fCurrDrawState.fViewMatrix.preConcat(matrix); |
| 331 | } |
| 332 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 333 | const GrMatrix& GrDrawTarget::getViewMatrix() const { |
| 334 | return fCurrDrawState.fViewMatrix; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | bool GrDrawTarget::getViewInverse(GrMatrix* matrix) const { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 338 | // Mike: Can we cache this somewhere? |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 339 | // Brian: Sure, do we use it often? |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 340 | |
| 341 | GrMatrix inverse; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 342 | if (fCurrDrawState.fViewMatrix.invert(&inverse)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 343 | if (matrix) { |
| 344 | *matrix = inverse; |
| 345 | } |
| 346 | return true; |
| 347 | } |
| 348 | return false; |
| 349 | } |
| 350 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 351 | void GrDrawTarget::setSamplerState(int stage, const GrSamplerState& state) { |
| 352 | GrAssert(stage >= 0 && stage < kNumStages); |
| 353 | fCurrDrawState.fSamplerStates[stage] = state; |
| 354 | } |
| 355 | |
| 356 | void GrDrawTarget::setTextureMatrix(int stage, const GrMatrix& m) { |
| 357 | GrAssert(stage >= 0 && stage < kNumStages); |
| 358 | fCurrDrawState.fTextureMatrices[stage] = m; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | void GrDrawTarget::setStencilPass(StencilPass pass) { |
| 362 | fCurrDrawState.fStencilPass = pass; |
| 363 | } |
| 364 | |
| 365 | void GrDrawTarget::setReverseFill(bool reverse) { |
| 366 | fCurrDrawState.fReverseFill = reverse; |
| 367 | } |
| 368 | |
| 369 | void GrDrawTarget::enableState(uint32_t bits) { |
| 370 | fCurrDrawState.fFlagBits |= bits; |
| 371 | } |
| 372 | |
| 373 | void GrDrawTarget::disableState(uint32_t bits) { |
| 374 | fCurrDrawState.fFlagBits &= ~(bits); |
| 375 | } |
| 376 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 377 | void GrDrawTarget::setBlendFunc(BlendCoeff srcCoef, |
| 378 | BlendCoeff dstCoef) { |
| 379 | fCurrDrawState.fSrcBlend = srcCoef; |
| 380 | fCurrDrawState.fDstBlend = dstCoef; |
| 381 | } |
| 382 | |
| 383 | void GrDrawTarget::setColor(GrColor c) { |
| 384 | fCurrDrawState.fColor = c; |
| 385 | } |
| 386 | |
| 387 | void GrDrawTarget::setAlpha(uint8_t a) { |
| 388 | this->setColor((a << 24) | (a << 16) | (a << 8) | a); |
| 389 | } |
| 390 | |
| 391 | void GrDrawTarget::saveCurrentDrawState(SavedDrawState* state) const { |
| 392 | state->fState = fCurrDrawState; |
| 393 | } |
| 394 | |
| 395 | void GrDrawTarget::restoreDrawState(const SavedDrawState& state) { |
| 396 | fCurrDrawState = state.fState; |
| 397 | } |
| 398 | |
| 399 | void GrDrawTarget::copyDrawState(const GrDrawTarget& srcTarget) { |
| 400 | fCurrDrawState = srcTarget.fCurrDrawState; |
| 401 | } |
| 402 | |
| 403 | |
| 404 | bool GrDrawTarget::reserveAndLockGeometry(GrVertexLayout vertexLayout, |
| 405 | uint32_t vertexCount, |
| 406 | uint32_t indexCount, |
| 407 | void** vertices, |
| 408 | void** indices) { |
| 409 | GrAssert(!fReservedGeometry.fLocked); |
| 410 | fReservedGeometry.fVertexCount = vertexCount; |
| 411 | fReservedGeometry.fIndexCount = indexCount; |
| 412 | |
| 413 | fReservedGeometry.fLocked = acquireGeometryHelper(vertexLayout, |
| 414 | vertices, |
| 415 | indices); |
| 416 | if (fReservedGeometry.fLocked) { |
| 417 | if (vertexCount) { |
| 418 | fGeometrySrc.fVertexSrc = kReserved_GeometrySrcType; |
| 419 | fGeometrySrc.fVertexLayout = vertexLayout; |
| 420 | } |
| 421 | if (indexCount) { |
| 422 | fGeometrySrc.fIndexSrc = kReserved_GeometrySrcType; |
| 423 | } |
| 424 | } |
| 425 | return fReservedGeometry.fLocked; |
| 426 | } |
| 427 | |
| 428 | bool GrDrawTarget::geometryHints(GrVertexLayout vertexLayout, |
| 429 | int32_t* vertexCount, |
| 430 | int32_t* indexCount) const { |
| 431 | GrAssert(!fReservedGeometry.fLocked); |
| 432 | if (NULL != vertexCount) { |
| 433 | *vertexCount = -1; |
| 434 | } |
| 435 | if (NULL != indexCount) { |
| 436 | *indexCount = -1; |
| 437 | } |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | void GrDrawTarget::releaseReservedGeometry() { |
| 442 | GrAssert(fReservedGeometry.fLocked); |
| 443 | releaseGeometryHelper(); |
| 444 | fReservedGeometry.fLocked = false; |
| 445 | } |
| 446 | |
| 447 | void GrDrawTarget::setVertexSourceToArray(const void* array, |
| 448 | GrVertexLayout vertexLayout) { |
| 449 | fGeometrySrc.fVertexSrc = kArray_GeometrySrcType; |
| 450 | fGeometrySrc.fVertexArray = array; |
| 451 | fGeometrySrc.fVertexLayout = vertexLayout; |
| 452 | } |
| 453 | |
| 454 | void GrDrawTarget::setIndexSourceToArray(const void* array) { |
| 455 | fGeometrySrc.fIndexSrc = kArray_GeometrySrcType; |
| 456 | fGeometrySrc.fIndexArray = array; |
| 457 | } |
| 458 | |
| 459 | void GrDrawTarget::setVertexSourceToBuffer(const GrVertexBuffer* buffer, |
| 460 | GrVertexLayout vertexLayout) { |
| 461 | fGeometrySrc.fVertexSrc = kBuffer_GeometrySrcType; |
| 462 | fGeometrySrc.fVertexBuffer = buffer; |
| 463 | fGeometrySrc.fVertexLayout = vertexLayout; |
| 464 | } |
| 465 | |
| 466 | void GrDrawTarget::setIndexSourceToBuffer(const GrIndexBuffer* buffer) { |
| 467 | fGeometrySrc.fIndexSrc = kBuffer_GeometrySrcType; |
| 468 | fGeometrySrc.fIndexBuffer = buffer; |
| 469 | } |
| 470 | |
| 471 | //////////////////////////////////////////////////////////////////////////////// |
| 472 | |
| 473 | GrDrawTarget::AutoStateRestore::AutoStateRestore(GrDrawTarget* target) { |
| 474 | fDrawTarget = target; |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame^] | 475 | if (NULL != fDrawTarget) { |
| 476 | fDrawTarget->saveCurrentDrawState(&fDrawState); |
| 477 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | GrDrawTarget::AutoStateRestore::~AutoStateRestore() { |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame^] | 481 | if (NULL != fDrawTarget) { |
| 482 | fDrawTarget->restoreDrawState(fDrawState); |
| 483 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 484 | } |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame^] | 485 | |