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" |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 20 | #include "GrTexture.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 21 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 22 | // recursive helper for creating mask with all the tex coord bits set for |
| 23 | // one stage |
| 24 | template <int N> |
| 25 | static int stage_mask_recur(int stage) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 26 | return GrDrawTarget::StageTexCoordVertexLayoutBit(stage, N) | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 27 | stage_mask_recur<N+1>(stage); |
| 28 | } |
reed@google.com | d728f6e | 2011-01-13 20:02:47 +0000 | [diff] [blame] | 29 | template<> // linux build doesn't like static on specializations |
| 30 | int stage_mask_recur<GrDrawTarget::kNumStages>(int) { return 0; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 31 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 32 | // mask of all tex coord indices for one stage |
| 33 | static int stage_tex_coord_mask(int stage) { |
| 34 | return stage_mask_recur<0>(stage); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 35 | } |
| 36 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 37 | // mask of all bits relevant to one stage |
| 38 | static int stage_mask(int stage) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 39 | return stage_tex_coord_mask(stage) | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 40 | GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(stage); |
| 41 | } |
| 42 | |
| 43 | // recursive helper for creating mask of with all bits set relevant to one |
| 44 | // texture coordinate index |
| 45 | template <int N> |
| 46 | static int tex_coord_mask_recur(int texCoordIdx) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 47 | return GrDrawTarget::StageTexCoordVertexLayoutBit(N, texCoordIdx) | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 48 | tex_coord_mask_recur<N+1>(texCoordIdx); |
| 49 | } |
reed@google.com | d728f6e | 2011-01-13 20:02:47 +0000 | [diff] [blame] | 50 | template<> // linux build doesn't like static on specializations |
| 51 | int tex_coord_mask_recur<GrDrawTarget::kMaxTexCoords>(int) { return 0; } |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 52 | |
| 53 | // mask of all bits relevant to one texture coordinate index |
| 54 | static int tex_coord_idx_mask(int texCoordIdx) { |
| 55 | return tex_coord_mask_recur<0>(texCoordIdx); |
| 56 | } |
| 57 | |
| 58 | bool check_layout(GrVertexLayout layout) { |
| 59 | // can only have 1 or 0 bits set for each stage. |
| 60 | for (int s = 0; s < GrDrawTarget::kNumStages; ++s) { |
| 61 | int stageBits = layout & stage_mask(s); |
| 62 | if (stageBits && !GrIsPow2(stageBits)) { |
| 63 | return false; |
| 64 | } |
| 65 | } |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | size_t GrDrawTarget::VertexSize(GrVertexLayout vertexLayout) { |
| 70 | GrAssert(check_layout(vertexLayout)); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 71 | |
| 72 | size_t vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ? |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 73 | sizeof(GrGpuTextVertex) : |
| 74 | sizeof(GrPoint); |
| 75 | |
| 76 | size_t size = vecSize; // position |
| 77 | for (int t = 0; t < kMaxTexCoords; ++t) { |
| 78 | if (tex_coord_idx_mask(t) & vertexLayout) { |
| 79 | size += vecSize; |
| 80 | } |
| 81 | } |
| 82 | if (vertexLayout & kColor_VertexLayoutBit) { |
| 83 | size += sizeof(GrColor); |
| 84 | } |
| 85 | return size; |
| 86 | } |
| 87 | |
| 88 | int GrDrawTarget::VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout) { |
| 89 | GrAssert(check_layout(vertexLayout)); |
| 90 | if (StagePosAsTexCoordVertexLayoutBit(stage) & vertexLayout) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 91 | return 0; |
| 92 | } |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 93 | int tcIdx = VertexTexCoordsForStage(stage, vertexLayout); |
| 94 | if (tcIdx >= 0) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 95 | |
| 96 | int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ? |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 97 | sizeof(GrGpuTextVertex) : |
| 98 | sizeof(GrPoint); |
| 99 | int offset = vecSize; // position |
| 100 | // figure out how many tex coordinates are present and precede this one. |
| 101 | for (int t = 0; t < tcIdx; ++t) { |
| 102 | if (tex_coord_idx_mask(t) & vertexLayout) { |
| 103 | offset += vecSize; |
| 104 | } |
| 105 | } |
| 106 | return offset; |
| 107 | } |
| 108 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | int GrDrawTarget::VertexColorOffset(GrVertexLayout vertexLayout) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 113 | GrAssert(check_layout(vertexLayout)); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 114 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 115 | if (vertexLayout & kColor_VertexLayoutBit) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 116 | int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ? |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 117 | sizeof(GrGpuTextVertex) : |
| 118 | sizeof(GrPoint); |
| 119 | int offset = vecSize; // position |
| 120 | // figure out how many tex coordinates are present and precede this one. |
| 121 | for (int t = 0; t < kMaxTexCoords; ++t) { |
| 122 | if (tex_coord_idx_mask(t) & vertexLayout) { |
| 123 | offset += vecSize; |
| 124 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 125 | } |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 126 | return offset; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 127 | } |
| 128 | return -1; |
| 129 | } |
| 130 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 131 | int GrDrawTarget::VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout, |
| 132 | int texCoordOffsetsByIdx[kMaxTexCoords], |
| 133 | int* colorOffset) { |
| 134 | GrAssert(check_layout(vertexLayout)); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 135 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 136 | GrAssert(NULL != texCoordOffsetsByIdx); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 137 | GrAssert(NULL != colorOffset); |
| 138 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 139 | int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ? |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 140 | sizeof(GrGpuTextVertex) : |
| 141 | sizeof(GrPoint); |
| 142 | int size = vecSize; // position |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 143 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 144 | for (int t = 0; t < kMaxTexCoords; ++t) { |
| 145 | if (tex_coord_idx_mask(t) & vertexLayout) { |
| 146 | texCoordOffsetsByIdx[t] = size; |
| 147 | size += vecSize; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 148 | } else { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 149 | texCoordOffsetsByIdx[t] = -1; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 150 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 151 | } |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 152 | if (kColor_VertexLayoutBit & vertexLayout) { |
| 153 | *colorOffset = size; |
| 154 | size += sizeof(GrColor); |
| 155 | } else { |
| 156 | *colorOffset = -1; |
| 157 | } |
| 158 | return size; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 159 | } |
| 160 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 161 | int GrDrawTarget::VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout, |
| 162 | int texCoordOffsetsByStage[kNumStages], |
| 163 | int* colorOffset) { |
| 164 | GrAssert(check_layout(vertexLayout)); |
| 165 | |
| 166 | GrAssert(NULL != texCoordOffsetsByStage); |
| 167 | GrAssert(NULL != colorOffset); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 168 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 169 | int texCoordOffsetsByIdx[kMaxTexCoords]; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 170 | int size = VertexSizeAndOffsetsByIdx(vertexLayout, |
| 171 | texCoordOffsetsByIdx, |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 172 | colorOffset); |
| 173 | for (int s = 0; s < kNumStages; ++s) { |
| 174 | int tcIdx; |
| 175 | if (StagePosAsTexCoordVertexLayoutBit(s) & vertexLayout) { |
| 176 | texCoordOffsetsByStage[s] = 0; |
| 177 | } else if ((tcIdx = VertexTexCoordsForStage(s, vertexLayout)) >= 0) { |
| 178 | texCoordOffsetsByStage[s] = texCoordOffsetsByIdx[tcIdx]; |
| 179 | } else { |
| 180 | texCoordOffsetsByStage[s] = -1; |
| 181 | } |
| 182 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 183 | return size; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | bool GrDrawTarget::VertexUsesStage(int stage, GrVertexLayout vertexLayout) { |
| 187 | GrAssert(stage < kNumStages); |
| 188 | GrAssert(check_layout(vertexLayout)); |
| 189 | return !!(stage_mask(stage) & vertexLayout); |
| 190 | } |
| 191 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 192 | bool GrDrawTarget::VertexUsesTexCoordIdx(int coordIndex, |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 193 | GrVertexLayout vertexLayout) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 194 | GrAssert(coordIndex < kMaxTexCoords); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 195 | GrAssert(check_layout(vertexLayout)); |
| 196 | return !!(tex_coord_idx_mask(coordIndex) & vertexLayout); |
| 197 | } |
| 198 | |
| 199 | int GrDrawTarget::VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout) { |
| 200 | GrAssert(stage < kNumStages); |
| 201 | GrAssert(check_layout(vertexLayout)); |
| 202 | int bit = vertexLayout & stage_tex_coord_mask(stage); |
| 203 | if (bit) { |
| 204 | // figure out which set of texture coordates is used |
| 205 | // bits are ordered T0S0, T0S1, T0S2, ..., T1S0, T1S1, ... |
| 206 | // and start at bit 0. |
| 207 | GR_STATIC_ASSERT(sizeof(GrVertexLayout) <= sizeof(uint32_t)); |
| 208 | return (32 - Gr_clz(bit) - 1) / kNumStages; |
| 209 | } |
| 210 | return -1; |
| 211 | } |
| 212 | |
| 213 | void GrDrawTarget::VertexLayoutUnitTest() { |
| 214 | // not necessarily exhaustive |
| 215 | static bool run; |
| 216 | if (!run) { |
| 217 | run = true; |
| 218 | for (int s = 0; s < kNumStages; ++s) { |
| 219 | |
| 220 | GrAssert(!VertexUsesStage(s, 0)); |
| 221 | GrAssert(-1 == VertexStageCoordOffset(s, 0)); |
| 222 | GrVertexLayout stageMask = 0; |
| 223 | for (int t = 0; t < kMaxTexCoords; ++t) { |
| 224 | stageMask |= StageTexCoordVertexLayoutBit(s,t); |
| 225 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 226 | GrAssert(1 == kMaxTexCoords || !check_layout(stageMask)); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 227 | GrAssert(stage_tex_coord_mask(s) == stageMask); |
| 228 | stageMask |= StagePosAsTexCoordVertexLayoutBit(s); |
| 229 | GrAssert(stage_mask(s) == stageMask); |
| 230 | GrAssert(!check_layout(stageMask)); |
| 231 | } |
| 232 | for (int t = 0; t < kMaxTexCoords; ++t) { |
| 233 | GrVertexLayout tcMask = 0; |
| 234 | GrAssert(!VertexUsesTexCoordIdx(t, 0)); |
| 235 | for (int s = 0; s < kNumStages; ++s) { |
| 236 | tcMask |= StageTexCoordVertexLayoutBit(s,t); |
| 237 | GrAssert(VertexUsesStage(s, tcMask)); |
| 238 | GrAssert(sizeof(GrPoint) == VertexStageCoordOffset(s, tcMask)); |
| 239 | GrAssert(VertexUsesTexCoordIdx(t, tcMask)); |
| 240 | GrAssert(2*sizeof(GrPoint) == VertexSize(tcMask)); |
| 241 | GrAssert(t == VertexTexCoordsForStage(s, tcMask)); |
| 242 | for (int s2 = s + 1; s2 < kNumStages; ++s2) { |
| 243 | GrAssert(-1 == VertexStageCoordOffset(s2, tcMask)); |
| 244 | GrAssert(!VertexUsesStage(s2, tcMask)); |
| 245 | GrAssert(-1 == VertexTexCoordsForStage(s2, tcMask)); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 246 | |
bsalomon@google.com | 1962832 | 2011-02-03 21:30:17 +0000 | [diff] [blame] | 247 | #if GR_DEBUG |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 248 | GrVertexLayout posAsTex = tcMask | StagePosAsTexCoordVertexLayoutBit(s2); |
bsalomon@google.com | 1962832 | 2011-02-03 21:30:17 +0000 | [diff] [blame] | 249 | #endif |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 250 | GrAssert(0 == VertexStageCoordOffset(s2, posAsTex)); |
| 251 | GrAssert(VertexUsesStage(s2, posAsTex)); |
| 252 | GrAssert(2*sizeof(GrPoint) == VertexSize(posAsTex)); |
| 253 | GrAssert(-1 == VertexTexCoordsForStage(s2, posAsTex)); |
| 254 | } |
bsalomon@google.com | 1962832 | 2011-02-03 21:30:17 +0000 | [diff] [blame] | 255 | #if GR_DEBUG |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 256 | GrVertexLayout withColor = tcMask | kColor_VertexLayoutBit; |
bsalomon@google.com | 1962832 | 2011-02-03 21:30:17 +0000 | [diff] [blame] | 257 | #endif |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 258 | GrAssert(2*sizeof(GrPoint) == VertexColorOffset(withColor)); |
| 259 | GrAssert(2*sizeof(GrPoint) + sizeof(GrColor) == VertexSize(withColor)); |
| 260 | } |
| 261 | GrAssert(tex_coord_idx_mask(t) == tcMask); |
| 262 | GrAssert(check_layout(tcMask)); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 263 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 264 | int stageOffsets[kNumStages]; |
| 265 | int colorOffset; |
| 266 | int size; |
| 267 | size = VertexSizeAndOffsetsByStage(tcMask, stageOffsets, &colorOffset); |
| 268 | GrAssert(2*sizeof(GrPoint) == size); |
| 269 | GrAssert(-1 == colorOffset); |
| 270 | for (int s = 0; s < kNumStages; ++s) { |
| 271 | GrAssert(VertexUsesStage(s, tcMask)); |
| 272 | GrAssert(sizeof(GrPoint) == stageOffsets[s]); |
| 273 | GrAssert(sizeof(GrPoint) == VertexStageCoordOffset(s, tcMask)); |
| 274 | } |
| 275 | } |
| 276 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | //////////////////////////////////////////////////////////////////////////////// |
| 280 | |
| 281 | GrDrawTarget::GrDrawTarget() { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 282 | #if GR_DEBUG |
| 283 | VertexLayoutUnitTest(); |
| 284 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 285 | fReservedGeometry.fLocked = false; |
| 286 | #if GR_DEBUG |
| 287 | fReservedGeometry.fVertexCount = ~0; |
| 288 | fReservedGeometry.fIndexCount = ~0; |
| 289 | #endif |
| 290 | fGeometrySrc.fVertexSrc = kReserved_GeometrySrcType; |
| 291 | fGeometrySrc.fIndexSrc = kReserved_GeometrySrcType; |
| 292 | } |
| 293 | |
| 294 | void GrDrawTarget::setClip(const GrClip& clip) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 295 | clipWillBeSet(clip); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 296 | fClip = clip; |
| 297 | } |
| 298 | |
| 299 | const GrClip& GrDrawTarget::getClip() const { |
| 300 | return fClip; |
| 301 | } |
| 302 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 303 | void GrDrawTarget::setTexture(int stage, GrTexture* tex) { |
| 304 | GrAssert(stage >= 0 && stage < kNumStages); |
| 305 | fCurrDrawState.fTextures[stage] = tex; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 306 | } |
| 307 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 308 | const GrTexture* GrDrawTarget::getTexture(int stage) const { |
| 309 | GrAssert(stage >= 0 && stage < kNumStages); |
| 310 | return fCurrDrawState.fTextures[stage]; |
| 311 | } |
| 312 | |
| 313 | GrTexture* GrDrawTarget::getTexture(int stage) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 314 | GrAssert(stage >= 0 && stage < kNumStages); |
| 315 | return fCurrDrawState.fTextures[stage]; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | void GrDrawTarget::setRenderTarget(GrRenderTarget* target) { |
| 319 | fCurrDrawState.fRenderTarget = target; |
| 320 | } |
| 321 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 322 | const GrRenderTarget* GrDrawTarget::getRenderTarget() const { |
| 323 | return fCurrDrawState.fRenderTarget; |
| 324 | } |
| 325 | |
| 326 | GrRenderTarget* GrDrawTarget::getRenderTarget() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 327 | return fCurrDrawState.fRenderTarget; |
| 328 | } |
| 329 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 330 | void GrDrawTarget::setViewMatrix(const GrMatrix& m) { |
| 331 | fCurrDrawState.fViewMatrix = m; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 332 | } |
| 333 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 334 | void GrDrawTarget::concatViewMatrix(const GrMatrix& matrix) { |
| 335 | fCurrDrawState.fViewMatrix.preConcat(matrix); |
| 336 | } |
| 337 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 338 | const GrMatrix& GrDrawTarget::getViewMatrix() const { |
| 339 | return fCurrDrawState.fViewMatrix; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | bool GrDrawTarget::getViewInverse(GrMatrix* matrix) const { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 343 | // Mike: Can we cache this somewhere? |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 344 | // Brian: Sure, do we use it often? |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 345 | |
| 346 | GrMatrix inverse; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 347 | if (fCurrDrawState.fViewMatrix.invert(&inverse)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 348 | if (matrix) { |
| 349 | *matrix = inverse; |
| 350 | } |
| 351 | return true; |
| 352 | } |
| 353 | return false; |
| 354 | } |
| 355 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 356 | void GrDrawTarget::setSamplerState(int stage, const GrSamplerState& state) { |
| 357 | GrAssert(stage >= 0 && stage < kNumStages); |
| 358 | fCurrDrawState.fSamplerStates[stage] = state; |
| 359 | } |
| 360 | |
| 361 | void GrDrawTarget::setTextureMatrix(int stage, const GrMatrix& m) { |
| 362 | GrAssert(stage >= 0 && stage < kNumStages); |
| 363 | fCurrDrawState.fTextureMatrices[stage] = m; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 364 | } |
| 365 | |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 366 | void GrDrawTarget::concatTextureMatrix(int stage, const GrMatrix& m) { |
| 367 | GrAssert(stage >= 0 && stage < kNumStages); |
| 368 | fCurrDrawState.fTextureMatrices[stage].preConcat(m); |
| 369 | } |
| 370 | |
| 371 | const GrMatrix& GrDrawTarget::getTextureMatrix(int stage) const { |
| 372 | GrAssert(stage >= 0 && stage < kNumStages); |
| 373 | return fCurrDrawState.fTextureMatrices[stage]; |
| 374 | } |
| 375 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 376 | void GrDrawTarget::setStencilPass(StencilPass pass) { |
| 377 | fCurrDrawState.fStencilPass = pass; |
| 378 | } |
| 379 | |
| 380 | void GrDrawTarget::setReverseFill(bool reverse) { |
| 381 | fCurrDrawState.fReverseFill = reverse; |
| 382 | } |
| 383 | |
| 384 | void GrDrawTarget::enableState(uint32_t bits) { |
| 385 | fCurrDrawState.fFlagBits |= bits; |
| 386 | } |
| 387 | |
| 388 | void GrDrawTarget::disableState(uint32_t bits) { |
| 389 | fCurrDrawState.fFlagBits &= ~(bits); |
| 390 | } |
| 391 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 392 | void GrDrawTarget::setBlendFunc(BlendCoeff srcCoef, |
| 393 | BlendCoeff dstCoef) { |
| 394 | fCurrDrawState.fSrcBlend = srcCoef; |
| 395 | fCurrDrawState.fDstBlend = dstCoef; |
| 396 | } |
| 397 | |
| 398 | void GrDrawTarget::setColor(GrColor c) { |
| 399 | fCurrDrawState.fColor = c; |
| 400 | } |
| 401 | |
| 402 | void GrDrawTarget::setAlpha(uint8_t a) { |
| 403 | this->setColor((a << 24) | (a << 16) | (a << 8) | a); |
| 404 | } |
| 405 | |
| 406 | void GrDrawTarget::saveCurrentDrawState(SavedDrawState* state) const { |
| 407 | state->fState = fCurrDrawState; |
| 408 | } |
| 409 | |
| 410 | void GrDrawTarget::restoreDrawState(const SavedDrawState& state) { |
| 411 | fCurrDrawState = state.fState; |
| 412 | } |
| 413 | |
| 414 | void GrDrawTarget::copyDrawState(const GrDrawTarget& srcTarget) { |
| 415 | fCurrDrawState = srcTarget.fCurrDrawState; |
| 416 | } |
| 417 | |
| 418 | |
| 419 | bool GrDrawTarget::reserveAndLockGeometry(GrVertexLayout vertexLayout, |
| 420 | uint32_t vertexCount, |
| 421 | uint32_t indexCount, |
| 422 | void** vertices, |
| 423 | void** indices) { |
| 424 | GrAssert(!fReservedGeometry.fLocked); |
| 425 | fReservedGeometry.fVertexCount = vertexCount; |
| 426 | fReservedGeometry.fIndexCount = indexCount; |
| 427 | |
| 428 | fReservedGeometry.fLocked = acquireGeometryHelper(vertexLayout, |
| 429 | vertices, |
| 430 | indices); |
| 431 | if (fReservedGeometry.fLocked) { |
| 432 | if (vertexCount) { |
| 433 | fGeometrySrc.fVertexSrc = kReserved_GeometrySrcType; |
| 434 | fGeometrySrc.fVertexLayout = vertexLayout; |
| 435 | } |
| 436 | if (indexCount) { |
| 437 | fGeometrySrc.fIndexSrc = kReserved_GeometrySrcType; |
| 438 | } |
| 439 | } |
| 440 | return fReservedGeometry.fLocked; |
| 441 | } |
| 442 | |
| 443 | bool GrDrawTarget::geometryHints(GrVertexLayout vertexLayout, |
| 444 | int32_t* vertexCount, |
| 445 | int32_t* indexCount) const { |
| 446 | GrAssert(!fReservedGeometry.fLocked); |
| 447 | if (NULL != vertexCount) { |
| 448 | *vertexCount = -1; |
| 449 | } |
| 450 | if (NULL != indexCount) { |
| 451 | *indexCount = -1; |
| 452 | } |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | void GrDrawTarget::releaseReservedGeometry() { |
| 457 | GrAssert(fReservedGeometry.fLocked); |
| 458 | releaseGeometryHelper(); |
| 459 | fReservedGeometry.fLocked = false; |
| 460 | } |
| 461 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 462 | void GrDrawTarget::setVertexSourceToArray(GrVertexLayout vertexLayout, |
| 463 | const void* vertexArray, |
| 464 | int vertexCount) { |
| 465 | fGeometrySrc.fVertexSrc = kArray_GeometrySrcType; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 466 | fGeometrySrc.fVertexLayout = vertexLayout; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 467 | setVertexSourceToArrayHelper(vertexArray, vertexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 468 | } |
| 469 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 470 | void GrDrawTarget::setIndexSourceToArray(const void* indexArray, |
| 471 | int indexCount) { |
| 472 | fGeometrySrc.fIndexSrc = kArray_GeometrySrcType; |
| 473 | setIndexSourceToArrayHelper(indexArray, indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 474 | } |
| 475 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 476 | void GrDrawTarget::setVertexSourceToBuffer(GrVertexLayout vertexLayout, |
| 477 | const GrVertexBuffer* buffer) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 478 | fGeometrySrc.fVertexSrc = kBuffer_GeometrySrcType; |
| 479 | fGeometrySrc.fVertexBuffer = buffer; |
| 480 | fGeometrySrc.fVertexLayout = vertexLayout; |
| 481 | } |
| 482 | |
| 483 | void GrDrawTarget::setIndexSourceToBuffer(const GrIndexBuffer* buffer) { |
| 484 | fGeometrySrc.fIndexSrc = kBuffer_GeometrySrcType; |
| 485 | fGeometrySrc.fIndexBuffer = buffer; |
| 486 | } |
| 487 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 488 | /////////////////////////////////////////////////////////////////////////////// |
| 489 | |
| 490 | bool GrDrawTarget::canDisableBlend() const { |
| 491 | if ((kOne_BlendCoeff == fCurrDrawState.fSrcBlend) && |
| 492 | (kZero_BlendCoeff == fCurrDrawState.fDstBlend)) { |
| 493 | return true; |
| 494 | } |
| 495 | |
| 496 | // If we have vertex color without alpha then we can't force blend off |
| 497 | if ((fGeometrySrc.fVertexLayout & kColor_VertexLayoutBit) || |
| 498 | 0xff != GrColorUnpackA(fCurrDrawState.fColor)) { |
| 499 | return false; |
| 500 | } |
| 501 | |
| 502 | // If the src coef will always be 1... |
| 503 | if (kSA_BlendCoeff != fCurrDrawState.fSrcBlend && |
| 504 | kOne_BlendCoeff != fCurrDrawState.fSrcBlend) { |
| 505 | return false; |
| 506 | } |
| 507 | |
| 508 | // ...and the dst coef is always 0... |
| 509 | if (kISA_BlendCoeff != fCurrDrawState.fDstBlend && |
| 510 | kZero_BlendCoeff != fCurrDrawState.fDstBlend) { |
| 511 | return false; |
| 512 | } |
| 513 | |
| 514 | // ...and there isn't a texture with an alpha channel... |
| 515 | for (int s = 0; s < kNumStages; ++s) { |
| 516 | if (VertexUsesStage(s, fGeometrySrc.fVertexLayout)) { |
| 517 | GrAssert(NULL != fCurrDrawState.fTextures[s]); |
| 518 | GrTexture::PixelConfig config = fCurrDrawState.fTextures[s]->config(); |
| 519 | |
| 520 | if (GrTexture::kRGB_565_PixelConfig != config && |
| 521 | GrTexture::kRGBX_8888_PixelConfig != config) { |
| 522 | return false; |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | // ...then we disable blend. |
| 528 | return true; |
| 529 | } |
| 530 | /////////////////////////////////////////////////////////////////////////////// |
| 531 | void GrDrawTarget::drawRect(const GrRect& rect, |
| 532 | const GrMatrix* matrix, |
| 533 | int stageEnableMask, |
| 534 | const GrRect* srcRects[], |
| 535 | const GrMatrix* srcMatrices[]) { |
| 536 | GR_STATIC_ASSERT(8*sizeof(int) >= kNumStages); |
| 537 | |
| 538 | GrVertexLayout layout = GetRectVertexLayout(stageEnableMask, srcRects); |
| 539 | |
| 540 | AutoReleaseGeometry geo(this, layout, 4, 0); |
| 541 | |
| 542 | SetRectVertices(rect, matrix, srcRects, |
| 543 | srcMatrices, layout, geo.vertices()); |
| 544 | |
| 545 | drawNonIndexed(kTriangleFan_PrimitiveType, 0, 4); |
| 546 | } |
| 547 | |
| 548 | GrVertexLayout GrDrawTarget::GetRectVertexLayout(int stageEnableMask, |
| 549 | const GrRect* srcRects[]) { |
| 550 | GrVertexLayout layout = 0; |
| 551 | |
| 552 | for (int i = 0; i < kNumStages; ++i) { |
| 553 | int numTC = 0; |
| 554 | if (stageEnableMask & (1 << i)) { |
| 555 | if (NULL != srcRects && NULL != srcRects[i]) { |
| 556 | layout |= StageTexCoordVertexLayoutBit(i, numTC); |
| 557 | ++numTC; |
| 558 | } else { |
| 559 | layout |= StagePosAsTexCoordVertexLayoutBit(i); |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | return layout; |
| 564 | } |
| 565 | void GrDrawTarget::SetRectVertices(const GrRect& rect, |
| 566 | const GrMatrix* matrix, |
| 567 | const GrRect* srcRects[], |
| 568 | const GrMatrix* srcMatrices[], |
| 569 | GrVertexLayout layout, |
| 570 | void* vertices) { |
| 571 | #if GR_DEBUG |
| 572 | // check that the layout and srcRects agree |
| 573 | for (int i = 0; i < kNumStages; ++i) { |
| 574 | if (VertexTexCoordsForStage(i, layout) >= 0) { |
| 575 | GR_DEBUGASSERT(NULL != srcRects && NULL != srcRects[i]); |
| 576 | } else { |
| 577 | GR_DEBUGASSERT(NULL == srcRects || NULL == srcRects[i]); |
| 578 | } |
| 579 | } |
| 580 | #endif |
| 581 | |
| 582 | int stageOffsets[kNumStages]; |
| 583 | int colorOffset; |
| 584 | int vsize = VertexSizeAndOffsetsByStage(layout, stageOffsets, &colorOffset); |
| 585 | GrAssert(-1 == colorOffset); |
| 586 | |
| 587 | GrTCast<GrPoint*>(vertices)->setRectFan(rect.fLeft, rect.fTop, |
| 588 | rect.fRight, rect.fBottom, |
| 589 | vsize); |
| 590 | if (NULL != matrix) { |
| 591 | matrix->mapPointsWithStride(GrTCast<GrPoint*>(vertices), vsize, 4); |
| 592 | } |
| 593 | |
| 594 | for (int i = 0; i < kNumStages; ++i) { |
| 595 | if (stageOffsets[i] > 0) { |
| 596 | GrPoint* coords = GrTCast<GrPoint*>(GrTCast<intptr_t>(vertices) + |
| 597 | stageOffsets[i]); |
| 598 | coords->setRectFan(srcRects[i]->fLeft, srcRects[i]->fTop, |
| 599 | srcRects[i]->fRight, srcRects[i]->fBottom, |
| 600 | vsize); |
| 601 | if (NULL != srcMatrices && NULL != srcMatrices[i]) { |
| 602 | srcMatrices[i]->mapPointsWithStride(coords, vsize, 4); |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | /////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 609 | |
| 610 | GrDrawTarget::AutoStateRestore::AutoStateRestore(GrDrawTarget* target) { |
| 611 | fDrawTarget = target; |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 612 | if (NULL != fDrawTarget) { |
| 613 | fDrawTarget->saveCurrentDrawState(&fDrawState); |
| 614 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | GrDrawTarget::AutoStateRestore::~AutoStateRestore() { |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 618 | if (NULL != fDrawTarget) { |
| 619 | fDrawTarget->restoreDrawState(fDrawState); |
| 620 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 621 | } |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 622 | |