blob: 2d1563de7dc1c02b19fea874e3891dc4ff7b2475 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#include "GrDrawTarget.h"
12#include "GrGpuVertex.h"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000013#include "GrTexture.h"
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000014#include "GrVertexBuffer.h"
15#include "GrIndexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016
junov@google.com6acc9b32011-05-16 18:32:07 +000017namespace {
18
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000019// recursive helper for creating mask with all the tex coord bits set for
20// one stage
21template <int N>
reed@google.com34cec242011-04-19 15:53:12 +000022int stage_mask_recur(int stage) {
bsalomon@google.com5782d712011-01-21 21:03:59 +000023 return GrDrawTarget::StageTexCoordVertexLayoutBit(stage, N) |
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000024 stage_mask_recur<N+1>(stage);
25}
junov@google.com6acc9b32011-05-16 18:32:07 +000026template<>
tomhudson@google.com93813632011-10-27 20:21:16 +000027int stage_mask_recur<GrDrawState::kNumStages>(int) { return 0; }
reed@google.comac10a2d2010-12-22 21:39:39 +000028
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000029// mask of all tex coord indices for one stage
junov@google.com6acc9b32011-05-16 18:32:07 +000030int stage_tex_coord_mask(int stage) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000031 return stage_mask_recur<0>(stage);
reed@google.comac10a2d2010-12-22 21:39:39 +000032}
33
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000034// mask of all bits relevant to one stage
junov@google.com6acc9b32011-05-16 18:32:07 +000035int stage_mask(int stage) {
bsalomon@google.com5782d712011-01-21 21:03:59 +000036 return stage_tex_coord_mask(stage) |
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000037 GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(stage);
38}
39
40// recursive helper for creating mask of with all bits set relevant to one
41// texture coordinate index
42template <int N>
reed@google.com34cec242011-04-19 15:53:12 +000043int tex_coord_mask_recur(int texCoordIdx) {
bsalomon@google.com5782d712011-01-21 21:03:59 +000044 return GrDrawTarget::StageTexCoordVertexLayoutBit(N, texCoordIdx) |
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000045 tex_coord_mask_recur<N+1>(texCoordIdx);
46}
junov@google.com6acc9b32011-05-16 18:32:07 +000047template<>
tomhudson@google.com93813632011-10-27 20:21:16 +000048int tex_coord_mask_recur<GrDrawState::kMaxTexCoords>(int) { return 0; }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000049
50// mask of all bits relevant to one texture coordinate index
junov@google.com6acc9b32011-05-16 18:32:07 +000051int tex_coord_idx_mask(int texCoordIdx) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000052 return tex_coord_mask_recur<0>(texCoordIdx);
53}
54
55bool check_layout(GrVertexLayout layout) {
56 // can only have 1 or 0 bits set for each stage.
tomhudson@google.com93813632011-10-27 20:21:16 +000057 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000058 int stageBits = layout & stage_mask(s);
59 if (stageBits && !GrIsPow2(stageBits)) {
60 return false;
61 }
62 }
63 return true;
64}
65
bsalomon@google.comaeb21602011-08-30 18:13:44 +000066int num_tex_coords(GrVertexLayout layout) {
67 int cnt = 0;
68 // figure out how many tex coordinates are present
tomhudson@google.com93813632011-10-27 20:21:16 +000069 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +000070 if (tex_coord_idx_mask(t) & layout) {
71 ++cnt;
72 }
73 }
74 return cnt;
75}
76
junov@google.com6acc9b32011-05-16 18:32:07 +000077} //unnamed namespace
78
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000079size_t GrDrawTarget::VertexSize(GrVertexLayout vertexLayout) {
80 GrAssert(check_layout(vertexLayout));
bsalomon@google.com5782d712011-01-21 21:03:59 +000081
82 size_t vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ?
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000083 sizeof(GrGpuTextVertex) :
84 sizeof(GrPoint);
85
86 size_t size = vecSize; // position
bsalomon@google.comaeb21602011-08-30 18:13:44 +000087 size += num_tex_coords(vertexLayout) * vecSize;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000088 if (vertexLayout & kColor_VertexLayoutBit) {
89 size += sizeof(GrColor);
90 }
bsalomon@google.coma3108262011-10-10 14:08:47 +000091 if (vertexLayout & kCoverage_VertexLayoutBit) {
92 size += sizeof(GrColor);
93 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +000094 if (vertexLayout & kEdge_VertexLayoutBit) {
95 size += 4 * sizeof(GrScalar);
96 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000097 return size;
98}
99
bsalomon@google.coma3108262011-10-10 14:08:47 +0000100////////////////////////////////////////////////////////////////////////////////
101
102/**
103 * Functions for computing offsets of various components from the layout
104 * bitfield.
105 *
106 * Order of vertex components:
107 * Position
108 * Tex Coord 0
109 * ...
tomhudson@google.com93813632011-10-27 20:21:16 +0000110 * Tex Coord GrDrawState::kMaxTexCoords-1
bsalomon@google.coma3108262011-10-10 14:08:47 +0000111 * Color
112 * Coverage
113 */
114
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000115int GrDrawTarget::VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout) {
116 GrAssert(check_layout(vertexLayout));
117 if (StagePosAsTexCoordVertexLayoutBit(stage) & vertexLayout) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000118 return 0;
119 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000120 int tcIdx = VertexTexCoordsForStage(stage, vertexLayout);
121 if (tcIdx >= 0) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000122
123 int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ?
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000124 sizeof(GrGpuTextVertex) :
125 sizeof(GrPoint);
126 int offset = vecSize; // position
127 // figure out how many tex coordinates are present and precede this one.
128 for (int t = 0; t < tcIdx; ++t) {
129 if (tex_coord_idx_mask(t) & vertexLayout) {
130 offset += vecSize;
131 }
132 }
133 return offset;
134 }
135
reed@google.comac10a2d2010-12-22 21:39:39 +0000136 return -1;
137}
138
139int GrDrawTarget::VertexColorOffset(GrVertexLayout vertexLayout) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000140 GrAssert(check_layout(vertexLayout));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000141
reed@google.comac10a2d2010-12-22 21:39:39 +0000142 if (vertexLayout & kColor_VertexLayoutBit) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000143 int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ?
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000144 sizeof(GrGpuTextVertex) :
145 sizeof(GrPoint);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000146 return vecSize * (num_tex_coords(vertexLayout) + 1); //+1 for pos
147 }
148 return -1;
149}
150
bsalomon@google.coma3108262011-10-10 14:08:47 +0000151int GrDrawTarget::VertexCoverageOffset(GrVertexLayout vertexLayout) {
152 GrAssert(check_layout(vertexLayout));
153
154 if (vertexLayout & kCoverage_VertexLayoutBit) {
155 int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ?
156 sizeof(GrGpuTextVertex) :
157 sizeof(GrPoint);
158
159 int offset = vecSize * (num_tex_coords(vertexLayout) + 1);
160 if (vertexLayout & kColor_VertexLayoutBit) {
161 offset += sizeof(GrColor);
162 }
163 return offset;
164 }
165 return -1;
166}
167
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000168int GrDrawTarget::VertexEdgeOffset(GrVertexLayout vertexLayout) {
169 GrAssert(check_layout(vertexLayout));
170
171 // edge pts are after the pos, tex coords, and color
172 if (vertexLayout & kEdge_VertexLayoutBit) {
173 int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ?
174 sizeof(GrGpuTextVertex) :
175 sizeof(GrPoint);
176 int offset = vecSize * (num_tex_coords(vertexLayout) + 1); //+1 for pos
177 if (vertexLayout & kColor_VertexLayoutBit) {
178 offset += sizeof(GrColor);
reed@google.comac10a2d2010-12-22 21:39:39 +0000179 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000180 if (vertexLayout & kCoverage_VertexLayoutBit) {
181 offset += sizeof(GrColor);
182 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000183 return offset;
reed@google.comac10a2d2010-12-22 21:39:39 +0000184 }
185 return -1;
186}
187
tomhudson@google.com93813632011-10-27 20:21:16 +0000188int GrDrawTarget::VertexSizeAndOffsetsByIdx(
189 GrVertexLayout vertexLayout,
190 int texCoordOffsetsByIdx[GrDrawState::kMaxTexCoords],
191 int* colorOffset,
192 int* coverageOffset,
193 int* edgeOffset) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000194 GrAssert(check_layout(vertexLayout));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000195
bsalomon@google.com5782d712011-01-21 21:03:59 +0000196 int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ?
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000197 sizeof(GrGpuTextVertex) :
198 sizeof(GrPoint);
199 int size = vecSize; // position
bsalomon@google.com5782d712011-01-21 21:03:59 +0000200
tomhudson@google.com93813632011-10-27 20:21:16 +0000201 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000202 if (tex_coord_idx_mask(t) & vertexLayout) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000203 if (NULL != texCoordOffsetsByIdx) {
204 texCoordOffsetsByIdx[t] = size;
205 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000206 size += vecSize;
reed@google.comac10a2d2010-12-22 21:39:39 +0000207 } else {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000208 if (NULL != texCoordOffsetsByIdx) {
209 texCoordOffsetsByIdx[t] = -1;
210 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000211 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000212 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000213 if (kColor_VertexLayoutBit & vertexLayout) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000214 if (NULL != colorOffset) {
215 *colorOffset = size;
216 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000217 size += sizeof(GrColor);
218 } else {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000219 if (NULL != colorOffset) {
220 *colorOffset = -1;
221 }
222 }
223 if (kCoverage_VertexLayoutBit & vertexLayout) {
224 if (NULL != coverageOffset) {
225 *coverageOffset = size;
226 }
227 size += sizeof(GrColor);
228 } else {
229 if (NULL != coverageOffset) {
230 *coverageOffset = -1;
231 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000232 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000233 if (kEdge_VertexLayoutBit & vertexLayout) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000234 if (NULL != edgeOffset) {
235 *edgeOffset = size;
236 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000237 size += 4 * sizeof(GrScalar);
238 } else {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000239 if (NULL != edgeOffset) {
240 *edgeOffset = -1;
241 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000242 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000243 return size;
reed@google.comac10a2d2010-12-22 21:39:39 +0000244}
245
tomhudson@google.com93813632011-10-27 20:21:16 +0000246int GrDrawTarget::VertexSizeAndOffsetsByStage(
247 GrVertexLayout vertexLayout,
248 int texCoordOffsetsByStage[GrDrawState::kNumStages],
249 int* colorOffset,
250 int* coverageOffset,
251 int* edgeOffset) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000252 GrAssert(check_layout(vertexLayout));
253
tomhudson@google.com93813632011-10-27 20:21:16 +0000254 int texCoordOffsetsByIdx[GrDrawState::kMaxTexCoords];
bsalomon@google.com5782d712011-01-21 21:03:59 +0000255 int size = VertexSizeAndOffsetsByIdx(vertexLayout,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000256 (NULL == texCoordOffsetsByStage) ?
257 NULL :
258 texCoordOffsetsByIdx,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000259 colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000260 coverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000261 edgeOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000262 if (NULL != texCoordOffsetsByStage) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000263 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000264 int tcIdx;
265 if (StagePosAsTexCoordVertexLayoutBit(s) & vertexLayout) {
266 texCoordOffsetsByStage[s] = 0;
267 } else if ((tcIdx = VertexTexCoordsForStage(s, vertexLayout)) >= 0) {
268 texCoordOffsetsByStage[s] = texCoordOffsetsByIdx[tcIdx];
269 } else {
270 texCoordOffsetsByStage[s] = -1;
271 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000272 }
273 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000274 return size;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000275}
276
bsalomon@google.coma3108262011-10-10 14:08:47 +0000277////////////////////////////////////////////////////////////////////////////////
278
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000279bool GrDrawTarget::VertexUsesStage(int stage, GrVertexLayout vertexLayout) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000280 GrAssert(stage < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000281 GrAssert(check_layout(vertexLayout));
282 return !!(stage_mask(stage) & vertexLayout);
283}
284
bsalomon@google.com5782d712011-01-21 21:03:59 +0000285bool GrDrawTarget::VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000286 GrVertexLayout vertexLayout) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000287 GrAssert(coordIndex < GrDrawState::kMaxTexCoords);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000288 GrAssert(check_layout(vertexLayout));
289 return !!(tex_coord_idx_mask(coordIndex) & vertexLayout);
290}
291
tomhudson@google.com93813632011-10-27 20:21:16 +0000292int GrDrawTarget::VertexTexCoordsForStage(int stage,
293 GrVertexLayout vertexLayout) {
294 GrAssert(stage < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000295 GrAssert(check_layout(vertexLayout));
296 int bit = vertexLayout & stage_tex_coord_mask(stage);
297 if (bit) {
298 // figure out which set of texture coordates is used
299 // bits are ordered T0S0, T0S1, T0S2, ..., T1S0, T1S1, ...
300 // and start at bit 0.
301 GR_STATIC_ASSERT(sizeof(GrVertexLayout) <= sizeof(uint32_t));
tomhudson@google.com93813632011-10-27 20:21:16 +0000302 return (32 - Gr_clz(bit) - 1) / GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000303 }
304 return -1;
305}
306
bsalomon@google.coma3108262011-10-10 14:08:47 +0000307////////////////////////////////////////////////////////////////////////////////
308
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000309void GrDrawTarget::VertexLayoutUnitTest() {
310 // not necessarily exhaustive
311 static bool run;
312 if (!run) {
313 run = true;
tomhudson@google.com93813632011-10-27 20:21:16 +0000314 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000315
316 GrAssert(!VertexUsesStage(s, 0));
317 GrAssert(-1 == VertexStageCoordOffset(s, 0));
318 GrVertexLayout stageMask = 0;
tomhudson@google.com93813632011-10-27 20:21:16 +0000319 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000320 stageMask |= StageTexCoordVertexLayoutBit(s,t);
321 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000322 GrAssert(1 == GrDrawState::kMaxTexCoords ||
323 !check_layout(stageMask));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000324 GrAssert(stage_tex_coord_mask(s) == stageMask);
325 stageMask |= StagePosAsTexCoordVertexLayoutBit(s);
326 GrAssert(stage_mask(s) == stageMask);
327 GrAssert(!check_layout(stageMask));
328 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000329 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000330 GrVertexLayout tcMask = 0;
331 GrAssert(!VertexUsesTexCoordIdx(t, 0));
tomhudson@google.com93813632011-10-27 20:21:16 +0000332 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000333 tcMask |= StageTexCoordVertexLayoutBit(s,t);
334 GrAssert(VertexUsesStage(s, tcMask));
335 GrAssert(sizeof(GrPoint) == VertexStageCoordOffset(s, tcMask));
336 GrAssert(VertexUsesTexCoordIdx(t, tcMask));
337 GrAssert(2*sizeof(GrPoint) == VertexSize(tcMask));
338 GrAssert(t == VertexTexCoordsForStage(s, tcMask));
tomhudson@google.com93813632011-10-27 20:21:16 +0000339 for (int s2 = s + 1; s2 < GrDrawState::kNumStages; ++s2) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000340 GrAssert(-1 == VertexStageCoordOffset(s2, tcMask));
341 GrAssert(!VertexUsesStage(s2, tcMask));
342 GrAssert(-1 == VertexTexCoordsForStage(s2, tcMask));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000343
bsalomon@google.com19628322011-02-03 21:30:17 +0000344 #if GR_DEBUG
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000345 GrVertexLayout posAsTex = tcMask | StagePosAsTexCoordVertexLayoutBit(s2);
bsalomon@google.com19628322011-02-03 21:30:17 +0000346 #endif
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000347 GrAssert(0 == VertexStageCoordOffset(s2, posAsTex));
348 GrAssert(VertexUsesStage(s2, posAsTex));
349 GrAssert(2*sizeof(GrPoint) == VertexSize(posAsTex));
350 GrAssert(-1 == VertexTexCoordsForStage(s2, posAsTex));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000351 GrAssert(-1 == VertexEdgeOffset(posAsTex));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000352 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000353 GrAssert(-1 == VertexEdgeOffset(tcMask));
354 GrAssert(-1 == VertexColorOffset(tcMask));
bsalomon@google.coma3108262011-10-10 14:08:47 +0000355 GrAssert(-1 == VertexCoverageOffset(tcMask));
bsalomon@google.com19628322011-02-03 21:30:17 +0000356 #if GR_DEBUG
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000357 GrVertexLayout withColor = tcMask | kColor_VertexLayoutBit;
bsalomon@google.com19628322011-02-03 21:30:17 +0000358 #endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000359 GrAssert(-1 == VertexCoverageOffset(withColor));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000360 GrAssert(2*sizeof(GrPoint) == VertexColorOffset(withColor));
361 GrAssert(2*sizeof(GrPoint) + sizeof(GrColor) == VertexSize(withColor));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000362 #if GR_DEBUG
363 GrVertexLayout withEdge = tcMask | kEdge_VertexLayoutBit;
364 #endif
365 GrAssert(-1 == VertexColorOffset(withEdge));
366 GrAssert(2*sizeof(GrPoint) == VertexEdgeOffset(withEdge));
367 GrAssert(4*sizeof(GrPoint) == VertexSize(withEdge));
368 #if GR_DEBUG
369 GrVertexLayout withColorAndEdge = withColor | kEdge_VertexLayoutBit;
370 #endif
371 GrAssert(2*sizeof(GrPoint) == VertexColorOffset(withColorAndEdge));
372 GrAssert(2*sizeof(GrPoint) + sizeof(GrColor) == VertexEdgeOffset(withColorAndEdge));
373 GrAssert(4*sizeof(GrPoint) + sizeof(GrColor) == VertexSize(withColorAndEdge));
bsalomon@google.coma3108262011-10-10 14:08:47 +0000374 #if GR_DEBUG
375 GrVertexLayout withCoverage = tcMask | kCoverage_VertexLayoutBit;
376 #endif
377 GrAssert(-1 == VertexColorOffset(withCoverage));
378 GrAssert(2*sizeof(GrPoint) == VertexCoverageOffset(withCoverage));
379 GrAssert(2*sizeof(GrPoint) + sizeof(GrColor) == VertexSize(withCoverage));
380 #if GR_DEBUG
381 GrVertexLayout withCoverageAndColor = tcMask | kCoverage_VertexLayoutBit |
382 kColor_VertexLayoutBit;
383 #endif
384 GrAssert(2*sizeof(GrPoint) == VertexColorOffset(withCoverageAndColor));
385 GrAssert(2*sizeof(GrPoint) + sizeof(GrColor) == VertexCoverageOffset(withCoverageAndColor));
386 GrAssert(2*sizeof(GrPoint) + 2 * sizeof(GrColor) == VertexSize(withCoverageAndColor));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000387 }
388 GrAssert(tex_coord_idx_mask(t) == tcMask);
389 GrAssert(check_layout(tcMask));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000390
tomhudson@google.com93813632011-10-27 20:21:16 +0000391 int stageOffsets[GrDrawState::kNumStages];
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000392 int colorOffset;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000393 int edgeOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000394 int coverageOffset;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000395 int size;
tomhudson@google.com93813632011-10-27 20:21:16 +0000396 size = VertexSizeAndOffsetsByStage(tcMask,
397 stageOffsets, &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000398 &coverageOffset, &edgeOffset);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000399 GrAssert(2*sizeof(GrPoint) == size);
400 GrAssert(-1 == colorOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000401 GrAssert(-1 == coverageOffset);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000402 GrAssert(-1 == edgeOffset);
tomhudson@google.com93813632011-10-27 20:21:16 +0000403 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000404 GrAssert(VertexUsesStage(s, tcMask));
405 GrAssert(sizeof(GrPoint) == stageOffsets[s]);
406 GrAssert(sizeof(GrPoint) == VertexStageCoordOffset(s, tcMask));
407 }
408 }
409 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000410}
411
412////////////////////////////////////////////////////////////////////////////////
413
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000414#define DEBUG_INVAL_BUFFER 0xdeadcafe
415#define DEBUG_INVAL_START_IDX -1
416
bsalomon@google.com92669012011-09-27 19:10:05 +0000417GrDrawTarget::GrDrawTarget() {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000418#if GR_DEBUG
419 VertexLayoutUnitTest();
420#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000421 GeometrySrcState& geoSrc = fGeoSrcStateStack.push_back();
reed@google.comac10a2d2010-12-22 21:39:39 +0000422#if GR_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000423 geoSrc.fVertexCount = DEBUG_INVAL_START_IDX;
424 geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
425 geoSrc.fIndexCount = DEBUG_INVAL_START_IDX;
426 geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
reed@google.comac10a2d2010-12-22 21:39:39 +0000427#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000428 geoSrc.fVertexSrc = kNone_GeometrySrcType;
429 geoSrc.fIndexSrc = kNone_GeometrySrcType;
430}
431
432GrDrawTarget::~GrDrawTarget() {
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000433 GrAssert(1 == fGeoSrcStateStack.count());
434 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
435 GrAssert(kNone_GeometrySrcType == geoSrc.fIndexSrc);
436 GrAssert(kNone_GeometrySrcType == geoSrc.fVertexSrc);
437}
438
439void GrDrawTarget::releaseGeometry() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000440 int popCnt = fGeoSrcStateStack.count() - 1;
441 while (popCnt) {
442 this->popGeometrySource();
443 --popCnt;
444 }
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000445 this->resetVertexSource();
446 this->resetIndexSource();
reed@google.comac10a2d2010-12-22 21:39:39 +0000447}
448
449void GrDrawTarget::setClip(const GrClip& clip) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000450 clipWillBeSet(clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000451 fClip = clip;
452}
453
454const GrClip& GrDrawTarget::getClip() const {
455 return fClip;
456}
457
reed@google.comac10a2d2010-12-22 21:39:39 +0000458void GrDrawTarget::saveCurrentDrawState(SavedDrawState* state) const {
459 state->fState = fCurrDrawState;
460}
461
462void GrDrawTarget::restoreDrawState(const SavedDrawState& state) {
463 fCurrDrawState = state.fState;
464}
465
466void GrDrawTarget::copyDrawState(const GrDrawTarget& srcTarget) {
467 fCurrDrawState = srcTarget.fCurrDrawState;
468}
469
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000470bool GrDrawTarget::reserveVertexSpace(GrVertexLayout vertexLayout,
471 int vertexCount,
472 void** vertices) {
473 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
474 bool acquired = false;
475 if (vertexCount > 0) {
476 GrAssert(NULL != vertices);
477 this->releasePreviousVertexSource();
478 geoSrc.fVertexSrc = kNone_GeometrySrcType;
reed@google.comac10a2d2010-12-22 21:39:39 +0000479
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000480 acquired = this->onReserveVertexSpace(vertexLayout,
481 vertexCount,
482 vertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000483 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000484 if (acquired) {
485 geoSrc.fVertexSrc = kReserved_GeometrySrcType;
486 geoSrc.fVertexCount = vertexCount;
487 geoSrc.fVertexLayout = vertexLayout;
488 } else if (NULL != vertices) {
489 *vertices = NULL;
490 }
491 return acquired;
492}
493
494bool GrDrawTarget::reserveIndexSpace(int indexCount,
495 void** indices) {
496 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
497 bool acquired = false;
498 if (indexCount > 0) {
499 GrAssert(NULL != indices);
500 this->releasePreviousIndexSource();
501 geoSrc.fIndexSrc = kNone_GeometrySrcType;
502
503 acquired = this->onReserveIndexSpace(indexCount, indices);
504 }
505 if (acquired) {
506 geoSrc.fIndexSrc = kReserved_GeometrySrcType;
507 geoSrc.fIndexCount = indexCount;
508 } else if (NULL != indices) {
509 *indices = NULL;
510 }
511 return acquired;
512
reed@google.comac10a2d2010-12-22 21:39:39 +0000513}
514
515bool GrDrawTarget::geometryHints(GrVertexLayout vertexLayout,
516 int32_t* vertexCount,
517 int32_t* indexCount) const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000518 if (NULL != vertexCount) {
519 *vertexCount = -1;
520 }
521 if (NULL != indexCount) {
522 *indexCount = -1;
523 }
524 return false;
525}
526
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000527void GrDrawTarget::releasePreviousVertexSource() {
528 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
529 switch (geoSrc.fVertexSrc) {
530 case kNone_GeometrySrcType:
531 break;
532 case kArray_GeometrySrcType:
533 this->releaseVertexArray();
534 break;
535 case kReserved_GeometrySrcType:
536 this->releaseReservedVertexSpace();
537 break;
538 case kBuffer_GeometrySrcType:
539 geoSrc.fVertexBuffer->unref();
540#if GR_DEBUG
541 geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
542#endif
543 break;
544 default:
545 GrCrash("Unknown Vertex Source Type.");
546 break;
547 }
548}
549
550void GrDrawTarget::releasePreviousIndexSource() {
551 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
552 switch (geoSrc.fIndexSrc) {
553 case kNone_GeometrySrcType: // these two don't require
554 break;
555 case kArray_GeometrySrcType:
556 this->releaseIndexArray();
557 break;
558 case kReserved_GeometrySrcType:
559 this->releaseReservedIndexSpace();
560 break;
561 case kBuffer_GeometrySrcType:
562 geoSrc.fIndexBuffer->unref();
563#if GR_DEBUG
564 geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
565#endif
566 break;
567 default:
568 GrCrash("Unknown Index Source Type.");
569 break;
570 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000571}
572
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000573void GrDrawTarget::setVertexSourceToArray(GrVertexLayout vertexLayout,
574 const void* vertexArray,
575 int vertexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000576 this->releasePreviousVertexSource();
577 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
578 geoSrc.fVertexSrc = kArray_GeometrySrcType;
579 geoSrc.fVertexLayout = vertexLayout;
580 geoSrc.fVertexCount = vertexCount;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000581 this->onSetVertexSourceToArray(vertexArray, vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000582}
583
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000584void GrDrawTarget::setIndexSourceToArray(const void* indexArray,
585 int indexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000586 this->releasePreviousIndexSource();
587 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
588 geoSrc.fIndexSrc = kArray_GeometrySrcType;
589 geoSrc.fIndexCount = indexCount;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000590 this->onSetIndexSourceToArray(indexArray, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000591}
592
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000593void GrDrawTarget::setVertexSourceToBuffer(GrVertexLayout vertexLayout,
594 const GrVertexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000595 this->releasePreviousVertexSource();
596 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
597 geoSrc.fVertexSrc = kBuffer_GeometrySrcType;
598 geoSrc.fVertexBuffer = buffer;
599 buffer->ref();
600 geoSrc.fVertexLayout = vertexLayout;
reed@google.comac10a2d2010-12-22 21:39:39 +0000601}
602
603void GrDrawTarget::setIndexSourceToBuffer(const GrIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000604 this->releasePreviousIndexSource();
605 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
606 geoSrc.fIndexSrc = kBuffer_GeometrySrcType;
607 geoSrc.fIndexBuffer = buffer;
608 buffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000609}
610
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000611void GrDrawTarget::resetVertexSource() {
612 this->releasePreviousVertexSource();
613 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
614 geoSrc.fVertexSrc = kNone_GeometrySrcType;
615}
616
617void GrDrawTarget::resetIndexSource() {
618 this->releasePreviousIndexSource();
619 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
620 geoSrc.fIndexSrc = kNone_GeometrySrcType;
621}
622
623void GrDrawTarget::pushGeometrySource() {
624 this->geometrySourceWillPush();
625 GeometrySrcState& newState = fGeoSrcStateStack.push_back();
626 newState.fIndexSrc = kNone_GeometrySrcType;
627 newState.fVertexSrc = kNone_GeometrySrcType;
628#if GR_DEBUG
629 newState.fVertexCount = ~0;
630 newState.fVertexBuffer = (GrVertexBuffer*)~0;
631 newState.fIndexCount = ~0;
632 newState.fIndexBuffer = (GrIndexBuffer*)~0;
633#endif
634}
635
636void GrDrawTarget::popGeometrySource() {
637 const GeometrySrcState& geoSrc = this->getGeomSrc();
638 // if popping last element then pops are unbalanced with pushes
639 GrAssert(fGeoSrcStateStack.count() > 1);
640
641 this->geometrySourceWillPop(fGeoSrcStateStack.fromBack(1));
642 this->releasePreviousVertexSource();
643 this->releasePreviousIndexSource();
644 fGeoSrcStateStack.pop_back();
645}
646
647////////////////////////////////////////////////////////////////////////////////
648
bsalomon@google.come8262622011-11-07 02:30:51 +0000649bool GrDrawTarget::checkDraw(GrPrimitiveType type, int startVertex,
650 int startIndex, int vertexCount,
651 int indexCount) const {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000652#if GR_DEBUG
bsalomon@google.come8262622011-11-07 02:30:51 +0000653 const GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000654 int maxVertex = startVertex + vertexCount;
655 int maxValidVertex;
656 switch (geoSrc.fVertexSrc) {
657 case kNone_GeometrySrcType:
bsalomon@google.come8262622011-11-07 02:30:51 +0000658 GrCrash("Attempting to draw without vertex src.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000659 case kReserved_GeometrySrcType: // fallthrough
660 case kArray_GeometrySrcType:
661 maxValidVertex = geoSrc.fVertexCount;
662 break;
663 case kBuffer_GeometrySrcType:
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000664 maxValidVertex = geoSrc.fVertexBuffer->sizeInBytes() /
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000665 VertexSize(geoSrc.fVertexLayout);
666 break;
667 }
668 if (maxVertex > maxValidVertex) {
bsalomon@google.come8262622011-11-07 02:30:51 +0000669 GrCrash("Drawing outside valid vertex range.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000670 }
bsalomon@google.come8262622011-11-07 02:30:51 +0000671 if (indexCount > 0) {
672 int maxIndex = startIndex + indexCount;
673 int maxValidIndex;
674 switch (geoSrc.fIndexSrc) {
675 case kNone_GeometrySrcType:
676 GrCrash("Attempting to draw indexed geom without index src.");
677 case kReserved_GeometrySrcType: // fallthrough
678 case kArray_GeometrySrcType:
679 maxValidIndex = geoSrc.fIndexCount;
680 break;
681 case kBuffer_GeometrySrcType:
682 maxValidIndex = geoSrc.fIndexBuffer->sizeInBytes() / sizeof(uint16_t);
683 break;
684 }
685 if (maxIndex > maxValidIndex) {
686 GrCrash("Index reads outside valid index range.");
687 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000688 }
689#endif
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000690 if (NULL == this->getRenderTarget()) {
691 return false;
692 }
693 if (GrPixelConfigIsUnpremultiplied(this->getRenderTarget()->config())) {
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000694 if (kOne_BlendCoeff != getDrawState().getSrcBlendCoeff() ||
695 kZero_BlendCoeff != getDrawState().getDstBlendCoeff()) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000696 return false;
697 }
698 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000699 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000700 // We don't support using unpremultiplied textures with filters (other
701 // than nearest). Alpha-premulling is not distributive WRT to filtering.
702 // We'd have to filter each texel before filtering. We could do this for
703 // our custom filters but we would also have to disable bilerp and do
704 // a custom bilerp in the shader. Until Skia itself supports unpremul
705 // configs there is no pressure to implement this.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000706 if (this->isStageEnabled(s) &&
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000707 GrPixelConfigIsUnpremultiplied(this->getTexture(s)->config()) &&
708 GrSamplerState::kNearest_Filter != this->getSampler(s).getFilter()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000709 return false;
710 }
711 }
bsalomon@google.come8262622011-11-07 02:30:51 +0000712 return true;
713}
714
715void GrDrawTarget::drawIndexed(GrPrimitiveType type, int startVertex,
716 int startIndex, int vertexCount,
717 int indexCount) {
718 if (indexCount > 0 &&
719 this->checkDraw(type, startVertex, startIndex,
720 vertexCount, indexCount)) {
bsalomon@google.com82145872011-08-23 14:32:40 +0000721 this->onDrawIndexed(type, startVertex, startIndex,
722 vertexCount, indexCount);
723 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000724}
725
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000726void GrDrawTarget::drawNonIndexed(GrPrimitiveType type,
727 int startVertex,
728 int vertexCount) {
bsalomon@google.come8262622011-11-07 02:30:51 +0000729 if (vertexCount > 0 &&
730 this->checkDraw(type, startVertex, -1, vertexCount, -1)) {
bsalomon@google.com82145872011-08-23 14:32:40 +0000731 this->onDrawNonIndexed(type, startVertex, vertexCount);
732 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000733}
734
735////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000736
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000737// Some blend modes allow folding a partial coverage value into the color's
738// alpha channel, while others will blend incorrectly.
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000739bool GrDrawTarget::canTweakAlphaForCoverage() const {
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000740 /**
741 * The fractional coverage is f
742 * The src and dst coeffs are Cs and Cd
743 * The dst and src colors are S and D
744 * We want the blend to compute: f*Cs*S + (f*Cd + (1-f))D
745 * By tweaking the source color's alpha we're replacing S with S'=fS. It's
746 * obvious that that first term will always be ok. The second term can be
747 * rearranged as [1-(1-Cd)f]D. By substituing in the various possbilities
748 * for Cd we find that only 1, ISA, and ISC produce the correct depth
749 * coeffecient in terms of S' and D.
750 */
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000751 GrBlendCoeff dstCoeff = this->getDrawState().getDstBlendCoeff();
752 return kOne_BlendCoeff == dstCoeff ||
753 kISA_BlendCoeff == dstCoeff ||
754 kISC_BlendCoeff == dstCoeff;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000755}
756
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000757
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000758bool GrDrawTarget::srcAlphaWillBeOne() const {
759 const GrVertexLayout& layout = this->getGeomSrc().fVertexLayout;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000760
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000761 // Check if per-vertex or constant color may have partial alpha
bsalomon@google.com471d4712011-08-23 15:45:25 +0000762 if ((layout & kColor_VertexLayoutBit) ||
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000763 0xff != GrColorUnpackA(this->getColor())) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000764 return false;
765 }
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000766 // Check if color filter could introduce an alpha
767 // (TODO: Consider being more aggressive with regards to detecting 0xff
768 // final alpha from color filter).
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000769 if (SkXfermode::kDst_Mode != this->getDrawState().getColorFilterMode()) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000770 return false;
771 }
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000772 // Check if a color stage could create a partial alpha
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000773 int firstCoverageStage = this->getFirstCoverageStage();
774 for (int s = 0; s < firstCoverageStage; ++s) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000775 if (StageWillBeUsed(s, layout, fCurrDrawState)) {
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000776 GrAssert(NULL != this->getTexture(s));
777 GrPixelConfig config = this->getTexture(s)->config();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000778 if (!GrPixelConfigIsOpaque(config)) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000779 return false;
780 }
781 }
782 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000783 return true;
784}
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000785
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000786GrDrawTarget::BlendOptFlags
787GrDrawTarget::getBlendOpts(bool forceCoverage,
788 GrBlendCoeff* srcCoeff,
789 GrBlendCoeff* dstCoeff) const {
790
791 const GrVertexLayout& layout = this->getGeomSrc().fVertexLayout;
792
793 GrBlendCoeff bogusSrcCoeff, bogusDstCoeff;
794 if (NULL == srcCoeff) {
795 srcCoeff = &bogusSrcCoeff;
796 }
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000797 *srcCoeff = this->getDrawState().getSrcBlendCoeff();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000798
799 if (NULL == dstCoeff) {
800 dstCoeff = &bogusDstCoeff;
801 }
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000802 *dstCoeff = this->getDrawState().getDstBlendCoeff();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000803
804 // We don't ever expect source coeffecients to reference the source
805 GrAssert(kSA_BlendCoeff != *srcCoeff &&
806 kISA_BlendCoeff != *srcCoeff &&
807 kSC_BlendCoeff != *srcCoeff &&
808 kISC_BlendCoeff != *srcCoeff);
809 // same for dst
810 GrAssert(kDA_BlendCoeff != *dstCoeff &&
811 kIDA_BlendCoeff != *dstCoeff &&
812 kDC_BlendCoeff != *dstCoeff &&
813 kIDC_BlendCoeff != *dstCoeff);
814
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000815 if (this->getDrawState().isColorWriteDisabled()) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000816 *srcCoeff = kZero_BlendCoeff;
817 *dstCoeff = kOne_BlendCoeff;
818 }
819
820 bool srcAIsOne = this->srcAlphaWillBeOne();
821 bool dstCoeffIsOne = kOne_BlendCoeff == *dstCoeff ||
822 (kSA_BlendCoeff == *dstCoeff && srcAIsOne);
823 bool dstCoeffIsZero = kZero_BlendCoeff == *dstCoeff ||
824 (kISA_BlendCoeff == *dstCoeff && srcAIsOne);
825
826
827 // When coeffs are (0,1) there is no reason to draw at all, unless
828 // stenciling is enabled. Having color writes disabled is effectively
829 // (0,1).
830 if ((kZero_BlendCoeff == *srcCoeff && dstCoeffIsOne)) {
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000831 if (this->getDrawState().getStencil().doesWrite()) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000832 if (fCaps.fShaderSupport) {
833 return kDisableBlend_BlendOptFlag |
834 kEmitTransBlack_BlendOptFlag;
835 } else {
836 return kDisableBlend_BlendOptFlag;
837 }
838 } else {
839 return kSkipDraw_BlendOptFlag;
840 }
841 }
842
843 // check for coverage due to edge aa or coverage texture stage
844 bool hasCoverage = forceCoverage ||
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000845 this->getDrawState().getNumAAEdges() > 0 ||
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000846 (layout & kCoverage_VertexLayoutBit) ||
847 (layout & kEdge_VertexLayoutBit);
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000848 for (int s = this->getFirstCoverageStage();
tomhudson@google.com93813632011-10-27 20:21:16 +0000849 !hasCoverage && s < GrDrawState::kNumStages;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000850 ++s) {
851 if (StageWillBeUsed(s, layout, fCurrDrawState)) {
852 hasCoverage = true;
853 }
854 }
855
856 // if we don't have coverage we can check whether the dst
857 // has to read at all. If not, we'll disable blending.
858 if (!hasCoverage) {
859 if (dstCoeffIsZero) {
860 if (kOne_BlendCoeff == *srcCoeff) {
861 // if there is no coverage and coeffs are (1,0) then we
862 // won't need to read the dst at all, it gets replaced by src
863 return kDisableBlend_BlendOptFlag;
864 } else if (kZero_BlendCoeff == *srcCoeff &&
865 fCaps.fShaderSupport) {
866 // if the op is "clear" then we don't need to emit a color
867 // or blend, just write transparent black into the dst.
868 *srcCoeff = kOne_BlendCoeff;
869 *dstCoeff = kZero_BlendCoeff;
870 return kDisableBlend_BlendOptFlag |
871 kEmitTransBlack_BlendOptFlag;
872 }
873 }
874 } else {
875 // check whether coverage can be safely rolled into alpha
876 // of if we can skip color computation and just emit coverage
877 if (this->canTweakAlphaForCoverage()) {
878 return kCoverageAsAlpha_BlendOptFlag;
879 }
880 // We haven't implemented support for these optimizations in the
881 // fixed pipe (which is on its deathbed)
882 if (fCaps.fShaderSupport) {
883 if (dstCoeffIsZero) {
884 if (kZero_BlendCoeff == *srcCoeff) {
885 // the source color is not included in the blend
886 // the dst coeff is effectively zero so blend works out to:
887 // (c)(0)D + (1-c)D = (1-c)D.
888 *dstCoeff = kISA_BlendCoeff;
889 return kEmitCoverage_BlendOptFlag;
890 } else if (srcAIsOne) {
891 // the dst coeff is effectively zero so blend works out to:
892 // cS + (c)(0)D + (1-c)D = cS + (1-c)D.
893 // If Sa is 1 then we can replace Sa with c
894 // and set dst coeff to 1-Sa.
895 *dstCoeff = kISA_BlendCoeff;
896 return kCoverageAsAlpha_BlendOptFlag;
897 }
898 } else if (dstCoeffIsOne) {
899 // the dst coeff is effectively one so blend works out to:
900 // cS + (c)(1)D + (1-c)D = cS + D.
901 *dstCoeff = kOne_BlendCoeff;
902 return kCoverageAsAlpha_BlendOptFlag;
903 }
904 }
905 }
906 return kNone_BlendOpt;
907}
908
909bool GrDrawTarget::willUseHWAALines() const {
bsalomon@google.com471d4712011-08-23 15:45:25 +0000910 // there is a conflict between using smooth lines and our use of
911 // premultiplied alpha. Smooth lines tweak the incoming alpha value
912 // but not in a premul-alpha way. So we only use them when our alpha
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000913 // is 0xff and tweaking the color for partial coverage is OK
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000914 if (!fCaps.fHWAALineSupport ||
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000915 !(this->getDrawState().isHWAntialiasState())) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000916 return false;
917 }
918 BlendOptFlags opts = this->getBlendOpts();
919 return (kDisableBlend_BlendOptFlag & opts) &&
920 (kCoverageAsAlpha_BlendOptFlag & opts);
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000921}
922
923bool GrDrawTarget::canApplyCoverage() const {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000924 // we can correctly apply coverage if a) we have dual source blending
925 // or b) one of our blend optimizations applies.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000926 return this->getCaps().fDualSourceBlendingSupport ||
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000927 kNone_BlendOpt != this->getBlendOpts(true);
bsalomon@google.com471d4712011-08-23 15:45:25 +0000928}
929
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000930bool GrDrawTarget::drawWillReadDst() const {
931 return SkToBool((kDisableBlend_BlendOptFlag | kSkipDraw_BlendOptFlag) &
932 this->getBlendOpts());
bsalomon@google.com471d4712011-08-23 15:45:25 +0000933}
934
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000935////////////////////////////////////////////////////////////////////////////////
936
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000937void GrDrawTarget::drawRect(const GrRect& rect,
938 const GrMatrix* matrix,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000939 StageMask stageMask,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000940 const GrRect* srcRects[],
941 const GrMatrix* srcMatrices[]) {
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000942 GrVertexLayout layout = GetRectVertexLayout(stageMask, srcRects);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000943
944 AutoReleaseGeometry geo(this, layout, 4, 0);
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000945 if (!geo.succeeded()) {
946 GrPrintf("Failed to get space for vertices!\n");
947 return;
948 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000949
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000950 SetRectVertices(rect, matrix, srcRects,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000951 srcMatrices, layout, geo.vertices());
952
953 drawNonIndexed(kTriangleFan_PrimitiveType, 0, 4);
954}
955
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000956GrVertexLayout GrDrawTarget::GetRectVertexLayout(StageMask stageMask,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000957 const GrRect* srcRects[]) {
958 GrVertexLayout layout = 0;
959
tomhudson@google.com93813632011-10-27 20:21:16 +0000960 for (int i = 0; i < GrDrawState::kNumStages; ++i) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000961 int numTC = 0;
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000962 if (stageMask & (1 << i)) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000963 if (NULL != srcRects && NULL != srcRects[i]) {
964 layout |= StageTexCoordVertexLayoutBit(i, numTC);
965 ++numTC;
966 } else {
967 layout |= StagePosAsTexCoordVertexLayoutBit(i);
968 }
969 }
970 }
971 return layout;
972}
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000973
974void GrDrawTarget::clipWillBeSet(const GrClip& clip) {
975}
976
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000977void GrDrawTarget::SetRectVertices(const GrRect& rect,
978 const GrMatrix* matrix,
979 const GrRect* srcRects[],
980 const GrMatrix* srcMatrices[],
981 GrVertexLayout layout,
982 void* vertices) {
983#if GR_DEBUG
984 // check that the layout and srcRects agree
tomhudson@google.com93813632011-10-27 20:21:16 +0000985 for (int i = 0; i < GrDrawState::kNumStages; ++i) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000986 if (VertexTexCoordsForStage(i, layout) >= 0) {
987 GR_DEBUGASSERT(NULL != srcRects && NULL != srcRects[i]);
988 } else {
989 GR_DEBUGASSERT(NULL == srcRects || NULL == srcRects[i]);
990 }
991 }
992#endif
993
tomhudson@google.com93813632011-10-27 20:21:16 +0000994 int stageOffsets[GrDrawState::kNumStages];
bsalomon@google.coma3108262011-10-10 14:08:47 +0000995 int vsize = VertexSizeAndOffsetsByStage(layout, stageOffsets,
996 NULL, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000997
bsalomon@google.coma3108262011-10-10 14:08:47 +0000998 GrTCast<GrPoint*>(vertices)->setRectFan(rect.fLeft, rect.fTop,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000999 rect.fRight, rect.fBottom,
1000 vsize);
1001 if (NULL != matrix) {
1002 matrix->mapPointsWithStride(GrTCast<GrPoint*>(vertices), vsize, 4);
1003 }
1004
tomhudson@google.com93813632011-10-27 20:21:16 +00001005 for (int i = 0; i < GrDrawState::kNumStages; ++i) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001006 if (stageOffsets[i] > 0) {
bsalomon@google.coma3108262011-10-10 14:08:47 +00001007 GrPoint* coords = GrTCast<GrPoint*>(GrTCast<intptr_t>(vertices) +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001008 stageOffsets[i]);
1009 coords->setRectFan(srcRects[i]->fLeft, srcRects[i]->fTop,
bsalomon@google.coma3108262011-10-10 14:08:47 +00001010 srcRects[i]->fRight, srcRects[i]->fBottom,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001011 vsize);
1012 if (NULL != srcMatrices && NULL != srcMatrices[i]) {
1013 srcMatrices[i]->mapPointsWithStride(coords, vsize, 4);
1014 }
1015 }
1016 }
1017}
1018
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001019////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001020
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001021GrDrawTarget::AutoStateRestore::AutoStateRestore() {
1022 fDrawTarget = NULL;
1023}
reed@google.comac10a2d2010-12-22 21:39:39 +00001024
1025GrDrawTarget::AutoStateRestore::AutoStateRestore(GrDrawTarget* target) {
1026 fDrawTarget = target;
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001027 if (NULL != fDrawTarget) {
1028 fDrawTarget->saveCurrentDrawState(&fDrawState);
1029 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001030}
1031
1032GrDrawTarget::AutoStateRestore::~AutoStateRestore() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001033 if (NULL != fDrawTarget) {
1034 fDrawTarget->restoreDrawState(fDrawState);
1035 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001036}
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001037
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001038void GrDrawTarget::AutoStateRestore::set(GrDrawTarget* target) {
1039 if (target != fDrawTarget) {
1040 if (NULL != fDrawTarget) {
1041 fDrawTarget->restoreDrawState(fDrawState);
1042 }
1043 if (NULL != target) {
bsalomon@google.comd19aa272011-06-22 01:28:17 +00001044 target->saveCurrentDrawState(&fDrawState);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001045 }
1046 fDrawTarget = target;
1047 }
1048}
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001049
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001050////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001051
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001052GrDrawTarget::AutoDeviceCoordDraw::AutoDeviceCoordDraw(
1053 GrDrawTarget* target,
1054 GrDrawState::StageMask stageMask) {
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001055 GrAssert(NULL != target);
1056
1057 fDrawTarget = target;
1058 fViewMatrix = target->getViewMatrix();
1059 fStageMask = stageMask;
1060 if (fStageMask) {
1061 GrMatrix invVM;
1062 if (fViewMatrix.invert(&invVM)) {
tomhudson@google.com93813632011-10-27 20:21:16 +00001063 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001064 if (fStageMask & (1 << s)) {
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001065 fSamplerMatrices[s] = target->getSampler(s).getMatrix();
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001066 }
1067 }
1068 target->preConcatSamplerMatrices(fStageMask, invVM);
1069 } else {
1070 // sad trombone sound
1071 fStageMask = 0;
1072 }
1073 }
1074 target->setViewMatrix(GrMatrix::I());
1075}
1076
1077GrDrawTarget::AutoDeviceCoordDraw::~AutoDeviceCoordDraw() {
1078 fDrawTarget->setViewMatrix(fViewMatrix);
tomhudson@google.com93813632011-10-27 20:21:16 +00001079 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001080 if (fStageMask & (1 << s)) {
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001081 GrSamplerState* sampler = fDrawTarget->drawState()->sampler(s);
1082 sampler->setMatrix(fSamplerMatrices[s]);
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001083 }
1084 }
1085}
1086
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001087////////////////////////////////////////////////////////////////////////////////
1088
1089GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry(
1090 GrDrawTarget* target,
1091 GrVertexLayout vertexLayout,
1092 int vertexCount,
1093 int indexCount) {
1094 fTarget = NULL;
1095 this->set(target, vertexLayout, vertexCount, indexCount);
1096}
1097
1098GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry() {
1099 fTarget = NULL;
1100}
1101
1102GrDrawTarget::AutoReleaseGeometry::~AutoReleaseGeometry() {
1103 this->reset();
1104}
1105
1106bool GrDrawTarget::AutoReleaseGeometry::set(GrDrawTarget* target,
1107 GrVertexLayout vertexLayout,
1108 int vertexCount,
1109 int indexCount) {
1110 this->reset();
1111 fTarget = target;
1112 bool success = true;
1113 if (NULL != fTarget) {
1114 fTarget = target;
1115 if (vertexCount > 0) {
1116 success = target->reserveVertexSpace(vertexLayout,
1117 vertexCount,
1118 &fVertices);
1119 if (!success) {
1120 this->reset();
1121 }
1122 }
1123 if (success && indexCount > 0) {
1124 success = target->reserveIndexSpace(indexCount, &fIndices);
1125 if (!success) {
1126 this->reset();
1127 }
1128 }
1129 }
1130 GrAssert(success == (NULL != fTarget));
1131 return success;
1132}
1133
1134void GrDrawTarget::AutoReleaseGeometry::reset() {
1135 if (NULL != fTarget) {
1136 if (NULL != fVertices) {
1137 fTarget->resetVertexSource();
1138 }
1139 if (NULL != fIndices) {
1140 fTarget->resetIndexSource();
1141 }
1142 fTarget = NULL;
1143 }
bsalomon@google.comcb0c5ab2011-06-29 17:48:17 +00001144 fVertices = NULL;
1145 fIndices = NULL;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001146}
1147
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001148void GrDrawTarget::Caps::print() const {
1149 static const char* gNY[] = {"NO", "YES"};
1150 GrPrintf("8 Bit Palette Support : %s\n", gNY[f8BitPaletteSupport]);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001151 GrPrintf("NPOT Texture Tile Support : %s\n", gNY[fNPOTTextureTileSupport]);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001152 GrPrintf("Two Sided Stencil Support : %s\n", gNY[fTwoSidedStencilSupport]);
1153 GrPrintf("Stencil Wrap Ops Support : %s\n", gNY[fStencilWrapOpsSupport]);
1154 GrPrintf("HW AA Lines Support : %s\n", gNY[fHWAALineSupport]);
1155 GrPrintf("Shader Support : %s\n", gNY[fShaderSupport]);
1156 GrPrintf("Shader Derivative Support : %s\n", gNY[fShaderDerivativeSupport]);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001157 GrPrintf("Geometry Shader Support : %s\n", gNY[fGeometryShaderSupport]);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001158 GrPrintf("FSAA Support : %s\n", gNY[fFSAASupport]);
1159 GrPrintf("Dual Source Blending Support: %s\n", gNY[fDualSourceBlendingSupport]);
1160 GrPrintf("Buffer Lock Support : %s\n", gNY[fBufferLockSupport]);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001161 GrPrintf("Max Texture Size : %d\n", fMaxTextureSize);
1162 GrPrintf("Max Render Target Size : %d\n", fMaxRenderTargetSize);
1163}
1164