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 | 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 | |
| 280 | GrDrawTarget::GrDrawTarget() { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 281 | #if GR_DEBUG |
| 282 | VertexLayoutUnitTest(); |
| 283 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 284 | fReservedGeometry.fLocked = false; |
| 285 | #if GR_DEBUG |
| 286 | fReservedGeometry.fVertexCount = ~0; |
| 287 | fReservedGeometry.fIndexCount = ~0; |
| 288 | #endif |
| 289 | fGeometrySrc.fVertexSrc = kReserved_GeometrySrcType; |
| 290 | fGeometrySrc.fIndexSrc = kReserved_GeometrySrcType; |
| 291 | } |
| 292 | |
| 293 | void GrDrawTarget::setClip(const GrClip& clip) { |
| 294 | clipWillChange(clip); |
| 295 | fClip = clip; |
| 296 | } |
| 297 | |
| 298 | const GrClip& GrDrawTarget::getClip() const { |
| 299 | return fClip; |
| 300 | } |
| 301 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 302 | void GrDrawTarget::setTexture(int stage, GrTexture* tex) { |
| 303 | GrAssert(stage >= 0 && stage < kNumStages); |
| 304 | fCurrDrawState.fTextures[stage] = tex; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 305 | } |
| 306 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 307 | const GrTexture* GrDrawTarget::getTexture(int stage) const { |
| 308 | GrAssert(stage >= 0 && stage < kNumStages); |
| 309 | return fCurrDrawState.fTextures[stage]; |
| 310 | } |
| 311 | |
| 312 | GrTexture* GrDrawTarget::getTexture(int stage) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 313 | GrAssert(stage >= 0 && stage < kNumStages); |
| 314 | return fCurrDrawState.fTextures[stage]; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | void GrDrawTarget::setRenderTarget(GrRenderTarget* target) { |
| 318 | fCurrDrawState.fRenderTarget = target; |
| 319 | } |
| 320 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 321 | const GrRenderTarget* GrDrawTarget::getRenderTarget() const { |
| 322 | return fCurrDrawState.fRenderTarget; |
| 323 | } |
| 324 | |
| 325 | GrRenderTarget* GrDrawTarget::getRenderTarget() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 326 | return fCurrDrawState.fRenderTarget; |
| 327 | } |
| 328 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 329 | void GrDrawTarget::setViewMatrix(const GrMatrix& m) { |
| 330 | fCurrDrawState.fViewMatrix = m; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 331 | } |
| 332 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 333 | void GrDrawTarget::concatViewMatrix(const GrMatrix& matrix) { |
| 334 | fCurrDrawState.fViewMatrix.preConcat(matrix); |
| 335 | } |
| 336 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 337 | const GrMatrix& GrDrawTarget::getViewMatrix() const { |
| 338 | return fCurrDrawState.fViewMatrix; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | bool GrDrawTarget::getViewInverse(GrMatrix* matrix) const { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 342 | // Mike: Can we cache this somewhere? |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 343 | // Brian: Sure, do we use it often? |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 344 | |
| 345 | GrMatrix inverse; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 346 | if (fCurrDrawState.fViewMatrix.invert(&inverse)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 347 | if (matrix) { |
| 348 | *matrix = inverse; |
| 349 | } |
| 350 | return true; |
| 351 | } |
| 352 | return false; |
| 353 | } |
| 354 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 355 | void GrDrawTarget::setSamplerState(int stage, const GrSamplerState& state) { |
| 356 | GrAssert(stage >= 0 && stage < kNumStages); |
| 357 | fCurrDrawState.fSamplerStates[stage] = state; |
| 358 | } |
| 359 | |
| 360 | void GrDrawTarget::setTextureMatrix(int stage, const GrMatrix& m) { |
| 361 | GrAssert(stage >= 0 && stage < kNumStages); |
| 362 | fCurrDrawState.fTextureMatrices[stage] = m; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 363 | } |
| 364 | |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 365 | void GrDrawTarget::concatTextureMatrix(int stage, const GrMatrix& m) { |
| 366 | GrAssert(stage >= 0 && stage < kNumStages); |
| 367 | fCurrDrawState.fTextureMatrices[stage].preConcat(m); |
| 368 | } |
| 369 | |
| 370 | const GrMatrix& GrDrawTarget::getTextureMatrix(int stage) const { |
| 371 | GrAssert(stage >= 0 && stage < kNumStages); |
| 372 | return fCurrDrawState.fTextureMatrices[stage]; |
| 373 | } |
| 374 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 375 | void GrDrawTarget::setStencilPass(StencilPass pass) { |
| 376 | fCurrDrawState.fStencilPass = pass; |
| 377 | } |
| 378 | |
| 379 | void GrDrawTarget::setReverseFill(bool reverse) { |
| 380 | fCurrDrawState.fReverseFill = reverse; |
| 381 | } |
| 382 | |
| 383 | void GrDrawTarget::enableState(uint32_t bits) { |
| 384 | fCurrDrawState.fFlagBits |= bits; |
| 385 | } |
| 386 | |
| 387 | void GrDrawTarget::disableState(uint32_t bits) { |
| 388 | fCurrDrawState.fFlagBits &= ~(bits); |
| 389 | } |
| 390 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 391 | void GrDrawTarget::setBlendFunc(BlendCoeff srcCoef, |
| 392 | BlendCoeff dstCoef) { |
| 393 | fCurrDrawState.fSrcBlend = srcCoef; |
| 394 | fCurrDrawState.fDstBlend = dstCoef; |
| 395 | } |
| 396 | |
| 397 | void GrDrawTarget::setColor(GrColor c) { |
| 398 | fCurrDrawState.fColor = c; |
| 399 | } |
| 400 | |
| 401 | void GrDrawTarget::setAlpha(uint8_t a) { |
| 402 | this->setColor((a << 24) | (a << 16) | (a << 8) | a); |
| 403 | } |
| 404 | |
| 405 | void GrDrawTarget::saveCurrentDrawState(SavedDrawState* state) const { |
| 406 | state->fState = fCurrDrawState; |
| 407 | } |
| 408 | |
| 409 | void GrDrawTarget::restoreDrawState(const SavedDrawState& state) { |
| 410 | fCurrDrawState = state.fState; |
| 411 | } |
| 412 | |
| 413 | void GrDrawTarget::copyDrawState(const GrDrawTarget& srcTarget) { |
| 414 | fCurrDrawState = srcTarget.fCurrDrawState; |
| 415 | } |
| 416 | |
| 417 | |
| 418 | bool GrDrawTarget::reserveAndLockGeometry(GrVertexLayout vertexLayout, |
| 419 | uint32_t vertexCount, |
| 420 | uint32_t indexCount, |
| 421 | void** vertices, |
| 422 | void** indices) { |
| 423 | GrAssert(!fReservedGeometry.fLocked); |
| 424 | fReservedGeometry.fVertexCount = vertexCount; |
| 425 | fReservedGeometry.fIndexCount = indexCount; |
| 426 | |
| 427 | fReservedGeometry.fLocked = acquireGeometryHelper(vertexLayout, |
| 428 | vertices, |
| 429 | indices); |
| 430 | if (fReservedGeometry.fLocked) { |
| 431 | if (vertexCount) { |
| 432 | fGeometrySrc.fVertexSrc = kReserved_GeometrySrcType; |
| 433 | fGeometrySrc.fVertexLayout = vertexLayout; |
| 434 | } |
| 435 | if (indexCount) { |
| 436 | fGeometrySrc.fIndexSrc = kReserved_GeometrySrcType; |
| 437 | } |
| 438 | } |
| 439 | return fReservedGeometry.fLocked; |
| 440 | } |
| 441 | |
| 442 | bool GrDrawTarget::geometryHints(GrVertexLayout vertexLayout, |
| 443 | int32_t* vertexCount, |
| 444 | int32_t* indexCount) const { |
| 445 | GrAssert(!fReservedGeometry.fLocked); |
| 446 | if (NULL != vertexCount) { |
| 447 | *vertexCount = -1; |
| 448 | } |
| 449 | if (NULL != indexCount) { |
| 450 | *indexCount = -1; |
| 451 | } |
| 452 | return false; |
| 453 | } |
| 454 | |
| 455 | void GrDrawTarget::releaseReservedGeometry() { |
| 456 | GrAssert(fReservedGeometry.fLocked); |
| 457 | releaseGeometryHelper(); |
| 458 | fReservedGeometry.fLocked = false; |
| 459 | } |
| 460 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 461 | void GrDrawTarget::setVertexSourceToArray(GrVertexLayout vertexLayout, |
| 462 | const void* vertexArray, |
| 463 | int vertexCount) { |
| 464 | fGeometrySrc.fVertexSrc = kArray_GeometrySrcType; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 465 | fGeometrySrc.fVertexLayout = vertexLayout; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 466 | setVertexSourceToArrayHelper(vertexArray, vertexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 467 | } |
| 468 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 469 | void GrDrawTarget::setIndexSourceToArray(const void* indexArray, |
| 470 | int indexCount) { |
| 471 | fGeometrySrc.fIndexSrc = kArray_GeometrySrcType; |
| 472 | setIndexSourceToArrayHelper(indexArray, indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 473 | } |
| 474 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 475 | void GrDrawTarget::setVertexSourceToBuffer(GrVertexLayout vertexLayout, |
| 476 | const GrVertexBuffer* buffer) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 477 | fGeometrySrc.fVertexSrc = kBuffer_GeometrySrcType; |
| 478 | fGeometrySrc.fVertexBuffer = buffer; |
| 479 | fGeometrySrc.fVertexLayout = vertexLayout; |
| 480 | } |
| 481 | |
| 482 | void GrDrawTarget::setIndexSourceToBuffer(const GrIndexBuffer* buffer) { |
| 483 | fGeometrySrc.fIndexSrc = kBuffer_GeometrySrcType; |
| 484 | fGeometrySrc.fIndexBuffer = buffer; |
| 485 | } |
| 486 | |
| 487 | //////////////////////////////////////////////////////////////////////////////// |
| 488 | |
| 489 | GrDrawTarget::AutoStateRestore::AutoStateRestore(GrDrawTarget* target) { |
| 490 | fDrawTarget = target; |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 491 | if (NULL != fDrawTarget) { |
| 492 | fDrawTarget->saveCurrentDrawState(&fDrawState); |
| 493 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | GrDrawTarget::AutoStateRestore::~AutoStateRestore() { |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 497 | if (NULL != fDrawTarget) { |
| 498 | fDrawTarget->restoreDrawState(fDrawState); |
| 499 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 500 | } |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 501 | |