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> |
reed@google.com | 34cec24 | 2011-04-19 15:53:12 +0000 | [diff] [blame] | 25 | 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> |
reed@google.com | 34cec24 | 2011-04-19 15:53:12 +0000 | [diff] [blame] | 46 | 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 | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 334 | void GrDrawTarget::preConcatViewMatrix(const GrMatrix& matrix) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 335 | fCurrDrawState.fViewMatrix.preConcat(matrix); |
| 336 | } |
| 337 | |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 338 | void GrDrawTarget::postConcatViewMatrix(const GrMatrix& matrix) { |
| 339 | fCurrDrawState.fViewMatrix.postConcat(matrix); |
| 340 | } |
| 341 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 342 | const GrMatrix& GrDrawTarget::getViewMatrix() const { |
| 343 | return fCurrDrawState.fViewMatrix; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | bool GrDrawTarget::getViewInverse(GrMatrix* matrix) const { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 347 | // Mike: Can we cache this somewhere? |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 348 | // Brian: Sure, do we use it often? |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 349 | |
| 350 | GrMatrix inverse; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 351 | if (fCurrDrawState.fViewMatrix.invert(&inverse)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 352 | if (matrix) { |
| 353 | *matrix = inverse; |
| 354 | } |
| 355 | return true; |
| 356 | } |
| 357 | return false; |
| 358 | } |
| 359 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 360 | void GrDrawTarget::setSamplerState(int stage, const GrSamplerState& state) { |
| 361 | GrAssert(stage >= 0 && stage < kNumStages); |
| 362 | fCurrDrawState.fSamplerStates[stage] = state; |
| 363 | } |
| 364 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 365 | void GrDrawTarget::enableState(uint32_t bits) { |
| 366 | fCurrDrawState.fFlagBits |= bits; |
| 367 | } |
| 368 | |
| 369 | void GrDrawTarget::disableState(uint32_t bits) { |
| 370 | fCurrDrawState.fFlagBits &= ~(bits); |
| 371 | } |
| 372 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 373 | void GrDrawTarget::setBlendFunc(GrBlendCoeff srcCoef, |
| 374 | GrBlendCoeff dstCoef) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 375 | fCurrDrawState.fSrcBlend = srcCoef; |
| 376 | fCurrDrawState.fDstBlend = dstCoef; |
| 377 | } |
| 378 | |
| 379 | void GrDrawTarget::setColor(GrColor c) { |
| 380 | fCurrDrawState.fColor = c; |
| 381 | } |
| 382 | |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 383 | void GrDrawTarget::setColorFilter(GrColor c, SkXfermode::Mode mode) { |
| 384 | fCurrDrawState.fColorFilterColor = c; |
| 385 | fCurrDrawState.fColorFilterXfermode = mode; |
| 386 | } |
| 387 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 388 | void GrDrawTarget::setAlpha(uint8_t a) { |
| 389 | this->setColor((a << 24) | (a << 16) | (a << 8) | a); |
| 390 | } |
| 391 | |
| 392 | void GrDrawTarget::saveCurrentDrawState(SavedDrawState* state) const { |
| 393 | state->fState = fCurrDrawState; |
| 394 | } |
| 395 | |
| 396 | void GrDrawTarget::restoreDrawState(const SavedDrawState& state) { |
| 397 | fCurrDrawState = state.fState; |
| 398 | } |
| 399 | |
| 400 | void GrDrawTarget::copyDrawState(const GrDrawTarget& srcTarget) { |
| 401 | fCurrDrawState = srcTarget.fCurrDrawState; |
| 402 | } |
| 403 | |
| 404 | |
| 405 | bool GrDrawTarget::reserveAndLockGeometry(GrVertexLayout vertexLayout, |
| 406 | uint32_t vertexCount, |
| 407 | uint32_t indexCount, |
| 408 | void** vertices, |
| 409 | void** indices) { |
| 410 | GrAssert(!fReservedGeometry.fLocked); |
| 411 | fReservedGeometry.fVertexCount = vertexCount; |
| 412 | fReservedGeometry.fIndexCount = indexCount; |
| 413 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 414 | fReservedGeometry.fLocked = this->onAcquireGeometry(vertexLayout, |
| 415 | vertices, |
| 416 | indices); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 417 | if (fReservedGeometry.fLocked) { |
| 418 | if (vertexCount) { |
| 419 | fGeometrySrc.fVertexSrc = kReserved_GeometrySrcType; |
| 420 | fGeometrySrc.fVertexLayout = vertexLayout; |
bsalomon@google.com | eaf6a5d | 2011-04-28 19:22:57 +0000 | [diff] [blame] | 421 | } else if (NULL != vertices) { |
| 422 | *vertices = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 423 | } |
| 424 | if (indexCount) { |
| 425 | fGeometrySrc.fIndexSrc = kReserved_GeometrySrcType; |
bsalomon@google.com | eaf6a5d | 2011-04-28 19:22:57 +0000 | [diff] [blame] | 426 | } else if (NULL != indices) { |
| 427 | *indices = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | return fReservedGeometry.fLocked; |
| 431 | } |
| 432 | |
| 433 | bool GrDrawTarget::geometryHints(GrVertexLayout vertexLayout, |
| 434 | int32_t* vertexCount, |
| 435 | int32_t* indexCount) const { |
| 436 | GrAssert(!fReservedGeometry.fLocked); |
| 437 | if (NULL != vertexCount) { |
| 438 | *vertexCount = -1; |
| 439 | } |
| 440 | if (NULL != indexCount) { |
| 441 | *indexCount = -1; |
| 442 | } |
| 443 | return false; |
| 444 | } |
| 445 | |
| 446 | void GrDrawTarget::releaseReservedGeometry() { |
| 447 | GrAssert(fReservedGeometry.fLocked); |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 448 | this->onReleaseGeometry(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 449 | fReservedGeometry.fLocked = false; |
| 450 | } |
| 451 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 452 | void GrDrawTarget::setVertexSourceToArray(GrVertexLayout vertexLayout, |
| 453 | const void* vertexArray, |
| 454 | int vertexCount) { |
| 455 | fGeometrySrc.fVertexSrc = kArray_GeometrySrcType; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 456 | fGeometrySrc.fVertexLayout = vertexLayout; |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 457 | this->onSetVertexSourceToArray(vertexArray, vertexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 458 | } |
| 459 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 460 | void GrDrawTarget::setIndexSourceToArray(const void* indexArray, |
| 461 | int indexCount) { |
| 462 | fGeometrySrc.fIndexSrc = kArray_GeometrySrcType; |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 463 | this->onSetIndexSourceToArray(indexArray, indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 464 | } |
| 465 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 466 | void GrDrawTarget::setVertexSourceToBuffer(GrVertexLayout vertexLayout, |
| 467 | const GrVertexBuffer* buffer) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 468 | fGeometrySrc.fVertexSrc = kBuffer_GeometrySrcType; |
| 469 | fGeometrySrc.fVertexBuffer = buffer; |
| 470 | fGeometrySrc.fVertexLayout = vertexLayout; |
| 471 | } |
| 472 | |
| 473 | void GrDrawTarget::setIndexSourceToBuffer(const GrIndexBuffer* buffer) { |
| 474 | fGeometrySrc.fIndexSrc = kBuffer_GeometrySrcType; |
| 475 | fGeometrySrc.fIndexBuffer = buffer; |
| 476 | } |
| 477 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 478 | /////////////////////////////////////////////////////////////////////////////// |
| 479 | |
| 480 | bool GrDrawTarget::canDisableBlend() const { |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame^] | 481 | // If we're using edge antialiasing, we can't force blend off. |
| 482 | if (fCurrDrawState.fFlagBits & kAntialias_StateBit) { |
| 483 | return false; |
| 484 | } |
| 485 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 486 | if ((kOne_BlendCoeff == fCurrDrawState.fSrcBlend) && |
| 487 | (kZero_BlendCoeff == fCurrDrawState.fDstBlend)) { |
| 488 | return true; |
| 489 | } |
| 490 | |
| 491 | // If we have vertex color without alpha then we can't force blend off |
| 492 | if ((fGeometrySrc.fVertexLayout & kColor_VertexLayoutBit) || |
| 493 | 0xff != GrColorUnpackA(fCurrDrawState.fColor)) { |
| 494 | return false; |
| 495 | } |
| 496 | |
| 497 | // If the src coef will always be 1... |
| 498 | if (kSA_BlendCoeff != fCurrDrawState.fSrcBlend && |
| 499 | kOne_BlendCoeff != fCurrDrawState.fSrcBlend) { |
| 500 | return false; |
| 501 | } |
| 502 | |
| 503 | // ...and the dst coef is always 0... |
| 504 | if (kISA_BlendCoeff != fCurrDrawState.fDstBlend && |
| 505 | kZero_BlendCoeff != fCurrDrawState.fDstBlend) { |
| 506 | return false; |
| 507 | } |
| 508 | |
| 509 | // ...and there isn't a texture with an alpha channel... |
| 510 | for (int s = 0; s < kNumStages; ++s) { |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 511 | if (this->isStageEnabled(s)) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 512 | GrAssert(NULL != fCurrDrawState.fTextures[s]); |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 513 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 514 | GrPixelConfig config = fCurrDrawState.fTextures[s]->config(); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 515 | |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 516 | if (!GrPixelConfigIsOpaque(config)) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 517 | return false; |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
Scroggo | 0bad673 | 2011-05-11 20:25:01 +0000 | [diff] [blame] | 522 | // ...and there isn't an interesting color filter... |
| 523 | // TODO: Consider being more aggressive with regards to disabling |
| 524 | // blending when a color filter is used. |
| 525 | if (SkXfermode::kDst_Mode != fCurrDrawState.fColorFilterXfermode) { |
| 526 | return false; |
| 527 | } |
| 528 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 529 | // ...then we disable blend. |
| 530 | return true; |
| 531 | } |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame^] | 532 | |
| 533 | /////////////////////////////////////////////////////////////////////////////// |
| 534 | void GrDrawTarget::setEdgeAAData(const float edges[18]) { |
| 535 | memcpy(fCurrDrawState.fEdgeAAEdges, edges, sizeof(fCurrDrawState.fEdgeAAEdges)); |
| 536 | } |
| 537 | |
| 538 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 539 | /////////////////////////////////////////////////////////////////////////////// |
| 540 | void GrDrawTarget::drawRect(const GrRect& rect, |
| 541 | const GrMatrix* matrix, |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 542 | StageBitfield stageEnableBitfield, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 543 | const GrRect* srcRects[], |
| 544 | const GrMatrix* srcMatrices[]) { |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 545 | GrVertexLayout layout = GetRectVertexLayout(stageEnableBitfield, srcRects); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 546 | |
| 547 | AutoReleaseGeometry geo(this, layout, 4, 0); |
| 548 | |
| 549 | SetRectVertices(rect, matrix, srcRects, |
| 550 | srcMatrices, layout, geo.vertices()); |
| 551 | |
| 552 | drawNonIndexed(kTriangleFan_PrimitiveType, 0, 4); |
| 553 | } |
| 554 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 555 | GrVertexLayout GrDrawTarget::GetRectVertexLayout(StageBitfield stageEnableBitfield, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 556 | const GrRect* srcRects[]) { |
| 557 | GrVertexLayout layout = 0; |
| 558 | |
| 559 | for (int i = 0; i < kNumStages; ++i) { |
| 560 | int numTC = 0; |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 561 | if (stageEnableBitfield & (1 << i)) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 562 | if (NULL != srcRects && NULL != srcRects[i]) { |
| 563 | layout |= StageTexCoordVertexLayoutBit(i, numTC); |
| 564 | ++numTC; |
| 565 | } else { |
| 566 | layout |= StagePosAsTexCoordVertexLayoutBit(i); |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | return layout; |
| 571 | } |
| 572 | void GrDrawTarget::SetRectVertices(const GrRect& rect, |
| 573 | const GrMatrix* matrix, |
| 574 | const GrRect* srcRects[], |
| 575 | const GrMatrix* srcMatrices[], |
| 576 | GrVertexLayout layout, |
| 577 | void* vertices) { |
| 578 | #if GR_DEBUG |
| 579 | // check that the layout and srcRects agree |
| 580 | for (int i = 0; i < kNumStages; ++i) { |
| 581 | if (VertexTexCoordsForStage(i, layout) >= 0) { |
| 582 | GR_DEBUGASSERT(NULL != srcRects && NULL != srcRects[i]); |
| 583 | } else { |
| 584 | GR_DEBUGASSERT(NULL == srcRects || NULL == srcRects[i]); |
| 585 | } |
| 586 | } |
| 587 | #endif |
| 588 | |
| 589 | int stageOffsets[kNumStages]; |
| 590 | int colorOffset; |
| 591 | int vsize = VertexSizeAndOffsetsByStage(layout, stageOffsets, &colorOffset); |
| 592 | GrAssert(-1 == colorOffset); |
| 593 | |
| 594 | GrTCast<GrPoint*>(vertices)->setRectFan(rect.fLeft, rect.fTop, |
| 595 | rect.fRight, rect.fBottom, |
| 596 | vsize); |
| 597 | if (NULL != matrix) { |
| 598 | matrix->mapPointsWithStride(GrTCast<GrPoint*>(vertices), vsize, 4); |
| 599 | } |
| 600 | |
| 601 | for (int i = 0; i < kNumStages; ++i) { |
| 602 | if (stageOffsets[i] > 0) { |
| 603 | GrPoint* coords = GrTCast<GrPoint*>(GrTCast<intptr_t>(vertices) + |
| 604 | stageOffsets[i]); |
| 605 | coords->setRectFan(srcRects[i]->fLeft, srcRects[i]->fTop, |
| 606 | srcRects[i]->fRight, srcRects[i]->fBottom, |
| 607 | vsize); |
| 608 | if (NULL != srcMatrices && NULL != srcMatrices[i]) { |
| 609 | srcMatrices[i]->mapPointsWithStride(coords, vsize, 4); |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 616 | GrDrawTarget::AutoStateRestore::AutoStateRestore() { |
| 617 | fDrawTarget = NULL; |
| 618 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 619 | |
| 620 | GrDrawTarget::AutoStateRestore::AutoStateRestore(GrDrawTarget* target) { |
| 621 | fDrawTarget = target; |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 622 | if (NULL != fDrawTarget) { |
| 623 | fDrawTarget->saveCurrentDrawState(&fDrawState); |
| 624 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | GrDrawTarget::AutoStateRestore::~AutoStateRestore() { |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 628 | if (NULL != fDrawTarget) { |
| 629 | fDrawTarget->restoreDrawState(fDrawState); |
| 630 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 631 | } |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 632 | |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 633 | void GrDrawTarget::AutoStateRestore::set(GrDrawTarget* target) { |
| 634 | if (target != fDrawTarget) { |
| 635 | if (NULL != fDrawTarget) { |
| 636 | fDrawTarget->restoreDrawState(fDrawState); |
| 637 | } |
| 638 | if (NULL != target) { |
| 639 | fDrawTarget->saveCurrentDrawState(&fDrawState); |
| 640 | } |
| 641 | fDrawTarget = target; |
| 642 | } |
| 643 | } |