blob: fa266d1cefbde3de3913c8f54be2d4d795cdce67 [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.com8f9cbd62011-12-09 15:55:34 +000013#include "GrIndexBuffer.h"
14#include "GrRenderTarget.h"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000015#include "GrTexture.h"
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000016#include "GrVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
junov@google.com6acc9b32011-05-16 18:32:07 +000018namespace {
19
bsalomon@google.com35ff3842011-12-15 16:58:19 +000020/**
21 * This function generates some masks that we like to have known at compile
22 * time. When the number of stages or tex coords is bumped or the way bits
23 * are defined in GrDrawTarget.h changes this funcion should be rerun to
24 * generate the new masks. (We attempted to force the compiler to generate the
25 * masks using recursive templates but always wound up with static initializers
26 * under gcc, even if they were just a series of immediate->memory moves.)
27 *
28 */
29void gen_mask_arrays(GrVertexLayout* stageTexCoordMasks,
30 GrVertexLayout* stageMasks,
31 GrVertexLayout* texCoordMasks) {
32 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
33 stageTexCoordMasks[s] = 0;
34 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
35 stageTexCoordMasks[s] |= GrDrawTarget::StageTexCoordVertexLayoutBit(s, t);
36 }
37 stageMasks[s] = stageTexCoordMasks[s] | GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s);
38 }
39 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
40 texCoordMasks[t] = 0;
41 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
42 texCoordMasks[t] |= GrDrawTarget::StageTexCoordVertexLayoutBit(s, t);
43 }
44 }
reed@google.comac10a2d2010-12-22 21:39:39 +000045}
46
bsalomon@google.com35ff3842011-12-15 16:58:19 +000047/**
48 * Run this function to generate the code that declares the global masks.
49 */
50void gen_globals() {
51 GrVertexLayout stageTexCoordMasks[GrDrawState::kNumStages];
52 GrVertexLayout stageMasks[GrDrawState::kNumStages];
53 GrVertexLayout texCoordMasks[GrDrawState::kMaxTexCoords];
54 gen_mask_arrays(stageTexCoordMasks, stageMasks, texCoordMasks);
55
56 GrPrintf("const GrVertexLayout gStageTexCoordMasks[] = {\n");
57 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
58 GrPrintf(" 0x%x,\n", stageTexCoordMasks[s]);
59 }
60 GrPrintf("};\n");
61 GrPrintf("GR_STATIC_ASSERT(GrDrawState::kNumStages == GR_ARRAY_COUNT(gStageTexCoordMasks));\n\n");
62 GrPrintf("const GrVertexLayout gStageMasks[] = {\n");
63 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
64 GrPrintf(" 0x%x,\n", stageMasks[s]);
65 }
66 GrPrintf("};\n");
67 GrPrintf("GR_STATIC_ASSERT(GrDrawState::kNumStages == GR_ARRAY_COUNT(gStageMasks));\n\n");
68 GrPrintf("const GrVertexLayout gTexCoordMasks[] = {\n");
69 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
70 GrPrintf(" 0x%x,\n", texCoordMasks[t]);
71 }
72 GrPrintf("};\n");
73 GrPrintf("GR_STATIC_ASSERT(GrDrawState::kMaxTexCoords == GR_ARRAY_COUNT(gTexCoordMasks));\n");
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000074}
75
bsalomon@google.com35ff3842011-12-15 16:58:19 +000076/* These values were generated by the above function */
77const GrVertexLayout gStageTexCoordMasks[] = {
78 0x49,
79 0x92,
80 0x124
81};
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000082
bsalomon@google.com35ff3842011-12-15 16:58:19 +000083GR_STATIC_ASSERT(GrDrawState::kNumStages == GR_ARRAY_COUNT(gStageTexCoordMasks));
84const GrVertexLayout gStageMasks[] = {
85 0x249,
86 0x492,
87 0x924
88};
89
90GR_STATIC_ASSERT(GrDrawState::kNumStages == GR_ARRAY_COUNT(gStageMasks));
91const GrVertexLayout gTexCoordMasks[] = {
92 0x7,
93 0x38,
94 0x1c0,
95};
96GR_STATIC_ASSERT(GrDrawState::kMaxTexCoords == GR_ARRAY_COUNT(gTexCoordMasks));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000097
98bool check_layout(GrVertexLayout layout) {
99 // can only have 1 or 0 bits set for each stage.
tomhudson@google.com93813632011-10-27 20:21:16 +0000100 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000101 int stageBits = layout & gStageMasks[s];
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000102 if (stageBits && !GrIsPow2(stageBits)) {
103 return false;
104 }
105 }
106 return true;
107}
108
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000109int num_tex_coords(GrVertexLayout layout) {
110 int cnt = 0;
111 // figure out how many tex coordinates are present
tomhudson@google.com93813632011-10-27 20:21:16 +0000112 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000113 if (gTexCoordMasks[t] & layout) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000114 ++cnt;
115 }
116 }
117 return cnt;
118}
119
junov@google.com6acc9b32011-05-16 18:32:07 +0000120} //unnamed namespace
121
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000122size_t GrDrawTarget::VertexSize(GrVertexLayout vertexLayout) {
123 GrAssert(check_layout(vertexLayout));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000124
125 size_t vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ?
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000126 sizeof(GrGpuTextVertex) :
127 sizeof(GrPoint);
128
129 size_t size = vecSize; // position
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000130 size += num_tex_coords(vertexLayout) * vecSize;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000131 if (vertexLayout & kColor_VertexLayoutBit) {
132 size += sizeof(GrColor);
133 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000134 if (vertexLayout & kCoverage_VertexLayoutBit) {
135 size += sizeof(GrColor);
136 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000137 if (vertexLayout & kEdge_VertexLayoutBit) {
138 size += 4 * sizeof(GrScalar);
139 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000140 return size;
141}
142
bsalomon@google.coma3108262011-10-10 14:08:47 +0000143////////////////////////////////////////////////////////////////////////////////
144
145/**
146 * Functions for computing offsets of various components from the layout
147 * bitfield.
148 *
149 * Order of vertex components:
150 * Position
151 * Tex Coord 0
152 * ...
tomhudson@google.com93813632011-10-27 20:21:16 +0000153 * Tex Coord GrDrawState::kMaxTexCoords-1
bsalomon@google.coma3108262011-10-10 14:08:47 +0000154 * Color
155 * Coverage
156 */
157
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000158int GrDrawTarget::VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout) {
159 GrAssert(check_layout(vertexLayout));
160 if (StagePosAsTexCoordVertexLayoutBit(stage) & vertexLayout) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000161 return 0;
162 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000163 int tcIdx = VertexTexCoordsForStage(stage, vertexLayout);
164 if (tcIdx >= 0) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000165
166 int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ?
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000167 sizeof(GrGpuTextVertex) :
168 sizeof(GrPoint);
169 int offset = vecSize; // position
170 // figure out how many tex coordinates are present and precede this one.
171 for (int t = 0; t < tcIdx; ++t) {
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000172 if (gTexCoordMasks[t] & vertexLayout) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000173 offset += vecSize;
174 }
175 }
176 return offset;
177 }
178
reed@google.comac10a2d2010-12-22 21:39:39 +0000179 return -1;
180}
181
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000182int GrDrawTarget::VertexColorOffset(GrVertexLayout vertexLayout) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000183 GrAssert(check_layout(vertexLayout));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000184
reed@google.comac10a2d2010-12-22 21:39:39 +0000185 if (vertexLayout & kColor_VertexLayoutBit) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000186 int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ?
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000187 sizeof(GrGpuTextVertex) :
188 sizeof(GrPoint);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000189 return vecSize * (num_tex_coords(vertexLayout) + 1); //+1 for pos
190 }
191 return -1;
192}
193
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000194int GrDrawTarget::VertexCoverageOffset(GrVertexLayout vertexLayout) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000195 GrAssert(check_layout(vertexLayout));
196
197 if (vertexLayout & kCoverage_VertexLayoutBit) {
198 int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ?
199 sizeof(GrGpuTextVertex) :
200 sizeof(GrPoint);
201
202 int offset = vecSize * (num_tex_coords(vertexLayout) + 1);
203 if (vertexLayout & kColor_VertexLayoutBit) {
204 offset += sizeof(GrColor);
205 }
206 return offset;
207 }
208 return -1;
209}
210
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000211int GrDrawTarget::VertexEdgeOffset(GrVertexLayout vertexLayout) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000212 GrAssert(check_layout(vertexLayout));
213
214 // edge pts are after the pos, tex coords, and color
215 if (vertexLayout & kEdge_VertexLayoutBit) {
216 int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ?
217 sizeof(GrGpuTextVertex) :
218 sizeof(GrPoint);
219 int offset = vecSize * (num_tex_coords(vertexLayout) + 1); //+1 for pos
220 if (vertexLayout & kColor_VertexLayoutBit) {
221 offset += sizeof(GrColor);
reed@google.comac10a2d2010-12-22 21:39:39 +0000222 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000223 if (vertexLayout & kCoverage_VertexLayoutBit) {
224 offset += sizeof(GrColor);
225 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000226 return offset;
reed@google.comac10a2d2010-12-22 21:39:39 +0000227 }
228 return -1;
229}
230
tomhudson@google.com93813632011-10-27 20:21:16 +0000231int GrDrawTarget::VertexSizeAndOffsetsByIdx(
232 GrVertexLayout vertexLayout,
233 int texCoordOffsetsByIdx[GrDrawState::kMaxTexCoords],
234 int* colorOffset,
235 int* coverageOffset,
236 int* edgeOffset) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000237 GrAssert(check_layout(vertexLayout));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000238
bsalomon@google.com5782d712011-01-21 21:03:59 +0000239 int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ?
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000240 sizeof(GrGpuTextVertex) :
241 sizeof(GrPoint);
242 int size = vecSize; // position
bsalomon@google.com5782d712011-01-21 21:03:59 +0000243
tomhudson@google.com93813632011-10-27 20:21:16 +0000244 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000245 if (gTexCoordMasks[t] & vertexLayout) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000246 if (NULL != texCoordOffsetsByIdx) {
247 texCoordOffsetsByIdx[t] = size;
248 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000249 size += vecSize;
reed@google.comac10a2d2010-12-22 21:39:39 +0000250 } else {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000251 if (NULL != texCoordOffsetsByIdx) {
252 texCoordOffsetsByIdx[t] = -1;
253 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000254 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000255 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000256 if (kColor_VertexLayoutBit & vertexLayout) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000257 if (NULL != colorOffset) {
258 *colorOffset = size;
259 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000260 size += sizeof(GrColor);
261 } else {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000262 if (NULL != colorOffset) {
263 *colorOffset = -1;
264 }
265 }
266 if (kCoverage_VertexLayoutBit & vertexLayout) {
267 if (NULL != coverageOffset) {
268 *coverageOffset = size;
269 }
270 size += sizeof(GrColor);
271 } else {
272 if (NULL != coverageOffset) {
273 *coverageOffset = -1;
274 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000275 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000276 if (kEdge_VertexLayoutBit & vertexLayout) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000277 if (NULL != edgeOffset) {
278 *edgeOffset = size;
279 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000280 size += 4 * sizeof(GrScalar);
281 } else {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000282 if (NULL != edgeOffset) {
283 *edgeOffset = -1;
284 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000285 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000286 return size;
reed@google.comac10a2d2010-12-22 21:39:39 +0000287}
288
tomhudson@google.com93813632011-10-27 20:21:16 +0000289int GrDrawTarget::VertexSizeAndOffsetsByStage(
290 GrVertexLayout vertexLayout,
291 int texCoordOffsetsByStage[GrDrawState::kNumStages],
292 int* colorOffset,
293 int* coverageOffset,
294 int* edgeOffset) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000295 GrAssert(check_layout(vertexLayout));
296
tomhudson@google.com93813632011-10-27 20:21:16 +0000297 int texCoordOffsetsByIdx[GrDrawState::kMaxTexCoords];
bsalomon@google.com5782d712011-01-21 21:03:59 +0000298 int size = VertexSizeAndOffsetsByIdx(vertexLayout,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000299 (NULL == texCoordOffsetsByStage) ?
300 NULL :
301 texCoordOffsetsByIdx,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000302 colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000303 coverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000304 edgeOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000305 if (NULL != texCoordOffsetsByStage) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000306 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000307 int tcIdx;
308 if (StagePosAsTexCoordVertexLayoutBit(s) & vertexLayout) {
309 texCoordOffsetsByStage[s] = 0;
310 } else if ((tcIdx = VertexTexCoordsForStage(s, vertexLayout)) >= 0) {
311 texCoordOffsetsByStage[s] = texCoordOffsetsByIdx[tcIdx];
312 } else {
313 texCoordOffsetsByStage[s] = -1;
314 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000315 }
316 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000317 return size;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000318}
319
bsalomon@google.coma3108262011-10-10 14:08:47 +0000320////////////////////////////////////////////////////////////////////////////////
321
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000322bool GrDrawTarget::VertexUsesStage(int stage, GrVertexLayout vertexLayout) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000323 GrAssert(stage < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000324 GrAssert(check_layout(vertexLayout));
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000325 return !!(gStageMasks[stage] & vertexLayout);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000326}
327
bsalomon@google.com5782d712011-01-21 21:03:59 +0000328bool GrDrawTarget::VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000329 GrVertexLayout vertexLayout) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000330 GrAssert(coordIndex < GrDrawState::kMaxTexCoords);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000331 GrAssert(check_layout(vertexLayout));
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000332 return !!(gTexCoordMasks[coordIndex] & vertexLayout);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000333}
334
tomhudson@google.com93813632011-10-27 20:21:16 +0000335int GrDrawTarget::VertexTexCoordsForStage(int stage,
336 GrVertexLayout vertexLayout) {
337 GrAssert(stage < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000338 GrAssert(check_layout(vertexLayout));
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000339 int bit = vertexLayout & gStageTexCoordMasks[stage];
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000340 if (bit) {
341 // figure out which set of texture coordates is used
342 // bits are ordered T0S0, T0S1, T0S2, ..., T1S0, T1S1, ...
343 // and start at bit 0.
344 GR_STATIC_ASSERT(sizeof(GrVertexLayout) <= sizeof(uint32_t));
tomhudson@google.com93813632011-10-27 20:21:16 +0000345 return (32 - Gr_clz(bit) - 1) / GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000346 }
347 return -1;
348}
349
bsalomon@google.coma3108262011-10-10 14:08:47 +0000350////////////////////////////////////////////////////////////////////////////////
351
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000352void GrDrawTarget::VertexLayoutUnitTest() {
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000353 // Ensure that our globals mask arrays are correct
354 GrVertexLayout stageTexCoordMasks[GrDrawState::kNumStages];
355 GrVertexLayout stageMasks[GrDrawState::kNumStages];
356 GrVertexLayout texCoordMasks[GrDrawState::kMaxTexCoords];
357 gen_mask_arrays(stageTexCoordMasks, stageMasks, texCoordMasks);
358 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
359 GrAssert(stageTexCoordMasks[s] == gStageTexCoordMasks[s]);
360 GrAssert(stageMasks[s] == gStageMasks[s]);
361 }
362 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
363 GrAssert(texCoordMasks[t] == gTexCoordMasks[t]);
364 }
365
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000366 // not necessarily exhaustive
367 static bool run;
368 if (!run) {
369 run = true;
tomhudson@google.com93813632011-10-27 20:21:16 +0000370 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000371
372 GrAssert(!VertexUsesStage(s, 0));
373 GrAssert(-1 == VertexStageCoordOffset(s, 0));
374 GrVertexLayout stageMask = 0;
tomhudson@google.com93813632011-10-27 20:21:16 +0000375 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000376 stageMask |= StageTexCoordVertexLayoutBit(s,t);
377 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000378 GrAssert(1 == GrDrawState::kMaxTexCoords ||
379 !check_layout(stageMask));
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000380 GrAssert(gStageTexCoordMasks[s] == stageMask);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000381 stageMask |= StagePosAsTexCoordVertexLayoutBit(s);
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000382 GrAssert(gStageMasks[s] == stageMask);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000383 GrAssert(!check_layout(stageMask));
384 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000385 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000386 GrVertexLayout tcMask = 0;
387 GrAssert(!VertexUsesTexCoordIdx(t, 0));
tomhudson@google.com93813632011-10-27 20:21:16 +0000388 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000389 tcMask |= StageTexCoordVertexLayoutBit(s,t);
390 GrAssert(VertexUsesStage(s, tcMask));
391 GrAssert(sizeof(GrPoint) == VertexStageCoordOffset(s, tcMask));
392 GrAssert(VertexUsesTexCoordIdx(t, tcMask));
393 GrAssert(2*sizeof(GrPoint) == VertexSize(tcMask));
394 GrAssert(t == VertexTexCoordsForStage(s, tcMask));
tomhudson@google.com93813632011-10-27 20:21:16 +0000395 for (int s2 = s + 1; s2 < GrDrawState::kNumStages; ++s2) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000396 GrAssert(-1 == VertexStageCoordOffset(s2, tcMask));
397 GrAssert(!VertexUsesStage(s2, tcMask));
398 GrAssert(-1 == VertexTexCoordsForStage(s2, tcMask));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000399
bsalomon@google.com19628322011-02-03 21:30:17 +0000400 #if GR_DEBUG
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000401 GrVertexLayout posAsTex = tcMask | StagePosAsTexCoordVertexLayoutBit(s2);
bsalomon@google.com19628322011-02-03 21:30:17 +0000402 #endif
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000403 GrAssert(0 == VertexStageCoordOffset(s2, posAsTex));
404 GrAssert(VertexUsesStage(s2, posAsTex));
405 GrAssert(2*sizeof(GrPoint) == VertexSize(posAsTex));
406 GrAssert(-1 == VertexTexCoordsForStage(s2, posAsTex));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000407 GrAssert(-1 == VertexEdgeOffset(posAsTex));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000408 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000409 GrAssert(-1 == VertexEdgeOffset(tcMask));
410 GrAssert(-1 == VertexColorOffset(tcMask));
bsalomon@google.coma3108262011-10-10 14:08:47 +0000411 GrAssert(-1 == VertexCoverageOffset(tcMask));
bsalomon@google.com19628322011-02-03 21:30:17 +0000412 #if GR_DEBUG
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000413 GrVertexLayout withColor = tcMask | kColor_VertexLayoutBit;
bsalomon@google.com19628322011-02-03 21:30:17 +0000414 #endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000415 GrAssert(-1 == VertexCoverageOffset(withColor));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000416 GrAssert(2*sizeof(GrPoint) == VertexColorOffset(withColor));
417 GrAssert(2*sizeof(GrPoint) + sizeof(GrColor) == VertexSize(withColor));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000418 #if GR_DEBUG
419 GrVertexLayout withEdge = tcMask | kEdge_VertexLayoutBit;
420 #endif
421 GrAssert(-1 == VertexColorOffset(withEdge));
422 GrAssert(2*sizeof(GrPoint) == VertexEdgeOffset(withEdge));
423 GrAssert(4*sizeof(GrPoint) == VertexSize(withEdge));
424 #if GR_DEBUG
425 GrVertexLayout withColorAndEdge = withColor | kEdge_VertexLayoutBit;
426 #endif
427 GrAssert(2*sizeof(GrPoint) == VertexColorOffset(withColorAndEdge));
428 GrAssert(2*sizeof(GrPoint) + sizeof(GrColor) == VertexEdgeOffset(withColorAndEdge));
429 GrAssert(4*sizeof(GrPoint) + sizeof(GrColor) == VertexSize(withColorAndEdge));
bsalomon@google.coma3108262011-10-10 14:08:47 +0000430 #if GR_DEBUG
431 GrVertexLayout withCoverage = tcMask | kCoverage_VertexLayoutBit;
432 #endif
433 GrAssert(-1 == VertexColorOffset(withCoverage));
434 GrAssert(2*sizeof(GrPoint) == VertexCoverageOffset(withCoverage));
435 GrAssert(2*sizeof(GrPoint) + sizeof(GrColor) == VertexSize(withCoverage));
436 #if GR_DEBUG
437 GrVertexLayout withCoverageAndColor = tcMask | kCoverage_VertexLayoutBit |
438 kColor_VertexLayoutBit;
439 #endif
440 GrAssert(2*sizeof(GrPoint) == VertexColorOffset(withCoverageAndColor));
441 GrAssert(2*sizeof(GrPoint) + sizeof(GrColor) == VertexCoverageOffset(withCoverageAndColor));
442 GrAssert(2*sizeof(GrPoint) + 2 * sizeof(GrColor) == VertexSize(withCoverageAndColor));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000443 }
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000444 GrAssert(gTexCoordMasks[t] == tcMask);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000445 GrAssert(check_layout(tcMask));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000446
tomhudson@google.com93813632011-10-27 20:21:16 +0000447 int stageOffsets[GrDrawState::kNumStages];
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000448 int colorOffset;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000449 int edgeOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000450 int coverageOffset;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000451 int size;
tomhudson@google.com93813632011-10-27 20:21:16 +0000452 size = VertexSizeAndOffsetsByStage(tcMask,
453 stageOffsets, &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000454 &coverageOffset, &edgeOffset);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000455 GrAssert(2*sizeof(GrPoint) == size);
456 GrAssert(-1 == colorOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000457 GrAssert(-1 == coverageOffset);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000458 GrAssert(-1 == edgeOffset);
tomhudson@google.com93813632011-10-27 20:21:16 +0000459 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000460 GrAssert(VertexUsesStage(s, tcMask));
461 GrAssert(sizeof(GrPoint) == stageOffsets[s]);
462 GrAssert(sizeof(GrPoint) == VertexStageCoordOffset(s, tcMask));
463 }
464 }
465 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000466}
467
468////////////////////////////////////////////////////////////////////////////////
469
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000470#define DEBUG_INVAL_BUFFER 0xdeadcafe
471#define DEBUG_INVAL_START_IDX -1
472
bsalomon@google.com92669012011-09-27 19:10:05 +0000473GrDrawTarget::GrDrawTarget() {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000474#if GR_DEBUG
475 VertexLayoutUnitTest();
476#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000477 GeometrySrcState& geoSrc = fGeoSrcStateStack.push_back();
reed@google.comac10a2d2010-12-22 21:39:39 +0000478#if GR_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000479 geoSrc.fVertexCount = DEBUG_INVAL_START_IDX;
480 geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
481 geoSrc.fIndexCount = DEBUG_INVAL_START_IDX;
482 geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
reed@google.comac10a2d2010-12-22 21:39:39 +0000483#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000484 geoSrc.fVertexSrc = kNone_GeometrySrcType;
485 geoSrc.fIndexSrc = kNone_GeometrySrcType;
486}
487
488GrDrawTarget::~GrDrawTarget() {
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000489 GrAssert(1 == fGeoSrcStateStack.count());
490 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
491 GrAssert(kNone_GeometrySrcType == geoSrc.fIndexSrc);
492 GrAssert(kNone_GeometrySrcType == geoSrc.fVertexSrc);
493}
494
495void GrDrawTarget::releaseGeometry() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000496 int popCnt = fGeoSrcStateStack.count() - 1;
497 while (popCnt) {
498 this->popGeometrySource();
499 --popCnt;
500 }
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000501 this->resetVertexSource();
502 this->resetIndexSource();
reed@google.comac10a2d2010-12-22 21:39:39 +0000503}
504
505void GrDrawTarget::setClip(const GrClip& clip) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000506 clipWillBeSet(clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000507 fClip = clip;
508}
509
510const GrClip& GrDrawTarget::getClip() const {
511 return fClip;
512}
513
reed@google.comac10a2d2010-12-22 21:39:39 +0000514void GrDrawTarget::saveCurrentDrawState(SavedDrawState* state) const {
515 state->fState = fCurrDrawState;
516}
517
518void GrDrawTarget::restoreDrawState(const SavedDrawState& state) {
519 fCurrDrawState = state.fState;
520}
521
522void GrDrawTarget::copyDrawState(const GrDrawTarget& srcTarget) {
523 fCurrDrawState = srcTarget.fCurrDrawState;
524}
525
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000526bool GrDrawTarget::reserveVertexSpace(GrVertexLayout vertexLayout,
527 int vertexCount,
528 void** vertices) {
529 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
530 bool acquired = false;
531 if (vertexCount > 0) {
532 GrAssert(NULL != vertices);
533 this->releasePreviousVertexSource();
534 geoSrc.fVertexSrc = kNone_GeometrySrcType;
reed@google.comac10a2d2010-12-22 21:39:39 +0000535
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000536 acquired = this->onReserveVertexSpace(vertexLayout,
537 vertexCount,
538 vertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000539 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000540 if (acquired) {
541 geoSrc.fVertexSrc = kReserved_GeometrySrcType;
542 geoSrc.fVertexCount = vertexCount;
543 geoSrc.fVertexLayout = vertexLayout;
544 } else if (NULL != vertices) {
545 *vertices = NULL;
546 }
547 return acquired;
548}
549
550bool GrDrawTarget::reserveIndexSpace(int indexCount,
551 void** indices) {
552 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
553 bool acquired = false;
554 if (indexCount > 0) {
555 GrAssert(NULL != indices);
556 this->releasePreviousIndexSource();
557 geoSrc.fIndexSrc = kNone_GeometrySrcType;
558
559 acquired = this->onReserveIndexSpace(indexCount, indices);
560 }
561 if (acquired) {
562 geoSrc.fIndexSrc = kReserved_GeometrySrcType;
563 geoSrc.fIndexCount = indexCount;
564 } else if (NULL != indices) {
565 *indices = NULL;
566 }
567 return acquired;
568
reed@google.comac10a2d2010-12-22 21:39:39 +0000569}
570
571bool GrDrawTarget::geometryHints(GrVertexLayout vertexLayout,
572 int32_t* vertexCount,
573 int32_t* indexCount) const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000574 if (NULL != vertexCount) {
575 *vertexCount = -1;
576 }
577 if (NULL != indexCount) {
578 *indexCount = -1;
579 }
580 return false;
581}
582
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000583void GrDrawTarget::releasePreviousVertexSource() {
584 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
585 switch (geoSrc.fVertexSrc) {
586 case kNone_GeometrySrcType:
587 break;
588 case kArray_GeometrySrcType:
589 this->releaseVertexArray();
590 break;
591 case kReserved_GeometrySrcType:
592 this->releaseReservedVertexSpace();
593 break;
594 case kBuffer_GeometrySrcType:
595 geoSrc.fVertexBuffer->unref();
596#if GR_DEBUG
597 geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
598#endif
599 break;
600 default:
601 GrCrash("Unknown Vertex Source Type.");
602 break;
603 }
604}
605
606void GrDrawTarget::releasePreviousIndexSource() {
607 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
608 switch (geoSrc.fIndexSrc) {
609 case kNone_GeometrySrcType: // these two don't require
610 break;
611 case kArray_GeometrySrcType:
612 this->releaseIndexArray();
613 break;
614 case kReserved_GeometrySrcType:
615 this->releaseReservedIndexSpace();
616 break;
617 case kBuffer_GeometrySrcType:
618 geoSrc.fIndexBuffer->unref();
619#if GR_DEBUG
620 geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
621#endif
622 break;
623 default:
624 GrCrash("Unknown Index Source Type.");
625 break;
626 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000627}
628
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000629void GrDrawTarget::setVertexSourceToArray(GrVertexLayout vertexLayout,
630 const void* vertexArray,
631 int vertexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000632 this->releasePreviousVertexSource();
633 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
634 geoSrc.fVertexSrc = kArray_GeometrySrcType;
635 geoSrc.fVertexLayout = vertexLayout;
636 geoSrc.fVertexCount = vertexCount;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000637 this->onSetVertexSourceToArray(vertexArray, vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000638}
639
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000640void GrDrawTarget::setIndexSourceToArray(const void* indexArray,
641 int indexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000642 this->releasePreviousIndexSource();
643 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
644 geoSrc.fIndexSrc = kArray_GeometrySrcType;
645 geoSrc.fIndexCount = indexCount;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000646 this->onSetIndexSourceToArray(indexArray, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000647}
648
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000649void GrDrawTarget::setVertexSourceToBuffer(GrVertexLayout vertexLayout,
650 const GrVertexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000651 this->releasePreviousVertexSource();
652 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
653 geoSrc.fVertexSrc = kBuffer_GeometrySrcType;
654 geoSrc.fVertexBuffer = buffer;
655 buffer->ref();
656 geoSrc.fVertexLayout = vertexLayout;
reed@google.comac10a2d2010-12-22 21:39:39 +0000657}
658
659void GrDrawTarget::setIndexSourceToBuffer(const GrIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000660 this->releasePreviousIndexSource();
661 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
662 geoSrc.fIndexSrc = kBuffer_GeometrySrcType;
663 geoSrc.fIndexBuffer = buffer;
664 buffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000665}
666
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000667void GrDrawTarget::resetVertexSource() {
668 this->releasePreviousVertexSource();
669 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
670 geoSrc.fVertexSrc = kNone_GeometrySrcType;
671}
672
673void GrDrawTarget::resetIndexSource() {
674 this->releasePreviousIndexSource();
675 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
676 geoSrc.fIndexSrc = kNone_GeometrySrcType;
677}
678
679void GrDrawTarget::pushGeometrySource() {
680 this->geometrySourceWillPush();
681 GeometrySrcState& newState = fGeoSrcStateStack.push_back();
682 newState.fIndexSrc = kNone_GeometrySrcType;
683 newState.fVertexSrc = kNone_GeometrySrcType;
684#if GR_DEBUG
685 newState.fVertexCount = ~0;
686 newState.fVertexBuffer = (GrVertexBuffer*)~0;
687 newState.fIndexCount = ~0;
688 newState.fIndexBuffer = (GrIndexBuffer*)~0;
689#endif
690}
691
692void GrDrawTarget::popGeometrySource() {
693 const GeometrySrcState& geoSrc = this->getGeomSrc();
694 // if popping last element then pops are unbalanced with pushes
695 GrAssert(fGeoSrcStateStack.count() > 1);
696
697 this->geometrySourceWillPop(fGeoSrcStateStack.fromBack(1));
698 this->releasePreviousVertexSource();
699 this->releasePreviousIndexSource();
700 fGeoSrcStateStack.pop_back();
701}
702
703////////////////////////////////////////////////////////////////////////////////
704
bsalomon@google.come8262622011-11-07 02:30:51 +0000705bool GrDrawTarget::checkDraw(GrPrimitiveType type, int startVertex,
706 int startIndex, int vertexCount,
707 int indexCount) const {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000708#if GR_DEBUG
bsalomon@google.come8262622011-11-07 02:30:51 +0000709 const GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000710 int maxVertex = startVertex + vertexCount;
711 int maxValidVertex;
712 switch (geoSrc.fVertexSrc) {
713 case kNone_GeometrySrcType:
bsalomon@google.come8262622011-11-07 02:30:51 +0000714 GrCrash("Attempting to draw without vertex src.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000715 case kReserved_GeometrySrcType: // fallthrough
716 case kArray_GeometrySrcType:
717 maxValidVertex = geoSrc.fVertexCount;
718 break;
719 case kBuffer_GeometrySrcType:
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000720 maxValidVertex = geoSrc.fVertexBuffer->sizeInBytes() /
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000721 VertexSize(geoSrc.fVertexLayout);
722 break;
723 }
724 if (maxVertex > maxValidVertex) {
bsalomon@google.come8262622011-11-07 02:30:51 +0000725 GrCrash("Drawing outside valid vertex range.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000726 }
bsalomon@google.come8262622011-11-07 02:30:51 +0000727 if (indexCount > 0) {
728 int maxIndex = startIndex + indexCount;
729 int maxValidIndex;
730 switch (geoSrc.fIndexSrc) {
731 case kNone_GeometrySrcType:
732 GrCrash("Attempting to draw indexed geom without index src.");
733 case kReserved_GeometrySrcType: // fallthrough
734 case kArray_GeometrySrcType:
735 maxValidIndex = geoSrc.fIndexCount;
736 break;
737 case kBuffer_GeometrySrcType:
738 maxValidIndex = geoSrc.fIndexBuffer->sizeInBytes() / sizeof(uint16_t);
739 break;
740 }
741 if (maxIndex > maxValidIndex) {
742 GrCrash("Index reads outside valid index range.");
743 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000744 }
745#endif
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000746 const GrDrawState& drawState = this->getDrawState();
747 if (NULL == drawState.getRenderTarget()) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000748 return false;
749 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000750 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
751 if (kOne_BlendCoeff != drawState.getSrcBlendCoeff() ||
752 kZero_BlendCoeff != drawState.getDstBlendCoeff()) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000753 return false;
754 }
755 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000756 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000757 // We don't support using unpremultiplied textures with filters (other
758 // than nearest). Alpha-premulling is not distributive WRT to filtering.
759 // We'd have to filter each texel before filtering. We could do this for
760 // our custom filters but we would also have to disable bilerp and do
761 // a custom bilerp in the shader. Until Skia itself supports unpremul
762 // configs there is no pressure to implement this.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000763 if (this->isStageEnabled(s) &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000764 GrPixelConfigIsUnpremultiplied(drawState.getTexture(s)->config()) &&
765 GrSamplerState::kNearest_Filter !=
766 drawState.getSampler(s).getFilter()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000767 return false;
768 }
769 }
bsalomon@google.come8262622011-11-07 02:30:51 +0000770 return true;
771}
772
773void GrDrawTarget::drawIndexed(GrPrimitiveType type, int startVertex,
774 int startIndex, int vertexCount,
775 int indexCount) {
776 if (indexCount > 0 &&
777 this->checkDraw(type, startVertex, startIndex,
778 vertexCount, indexCount)) {
bsalomon@google.com82145872011-08-23 14:32:40 +0000779 this->onDrawIndexed(type, startVertex, startIndex,
780 vertexCount, indexCount);
781 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000782}
783
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000784void GrDrawTarget::drawNonIndexed(GrPrimitiveType type,
785 int startVertex,
786 int vertexCount) {
bsalomon@google.come8262622011-11-07 02:30:51 +0000787 if (vertexCount > 0 &&
788 this->checkDraw(type, startVertex, -1, vertexCount, -1)) {
bsalomon@google.com82145872011-08-23 14:32:40 +0000789 this->onDrawNonIndexed(type, startVertex, vertexCount);
790 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000791}
792
793////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000794
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000795// Some blend modes allow folding a partial coverage value into the color's
796// alpha channel, while others will blend incorrectly.
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000797bool GrDrawTarget::canTweakAlphaForCoverage() const {
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000798 /**
799 * The fractional coverage is f
800 * The src and dst coeffs are Cs and Cd
801 * The dst and src colors are S and D
802 * We want the blend to compute: f*Cs*S + (f*Cd + (1-f))D
803 * By tweaking the source color's alpha we're replacing S with S'=fS. It's
804 * obvious that that first term will always be ok. The second term can be
805 * rearranged as [1-(1-Cd)f]D. By substituing in the various possbilities
806 * for Cd we find that only 1, ISA, and ISC produce the correct depth
807 * coeffecient in terms of S' and D.
808 */
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000809 GrBlendCoeff dstCoeff = this->getDrawState().getDstBlendCoeff();
810 return kOne_BlendCoeff == dstCoeff ||
811 kISA_BlendCoeff == dstCoeff ||
812 kISC_BlendCoeff == dstCoeff;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000813}
814
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000815
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000816bool GrDrawTarget::srcAlphaWillBeOne() const {
817 const GrVertexLayout& layout = this->getGeomSrc().fVertexLayout;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000818 const GrDrawState& drawState = this->getDrawState();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000819
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000820 // Check if per-vertex or constant color may have partial alpha
bsalomon@google.com471d4712011-08-23 15:45:25 +0000821 if ((layout & kColor_VertexLayoutBit) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000822 0xff != GrColorUnpackA(drawState.getColor())) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000823 return false;
824 }
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000825 // Check if color filter could introduce an alpha
826 // (TODO: Consider being more aggressive with regards to detecting 0xff
827 // final alpha from color filter).
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000828 if (SkXfermode::kDst_Mode != drawState.getColorFilterMode()) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000829 return false;
830 }
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000831 // Check if a color stage could create a partial alpha
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000832 for (int s = 0; s < drawState.getFirstCoverageStage(); ++s) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000833 if (StageWillBeUsed(s, layout, fCurrDrawState)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000834 GrAssert(NULL != drawState.getTexture(s));
835 GrPixelConfig config = drawState.getTexture(s)->config();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000836 if (!GrPixelConfigIsOpaque(config)) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000837 return false;
838 }
839 }
840 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000841 return true;
842}
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000843
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000844GrDrawTarget::BlendOptFlags
845GrDrawTarget::getBlendOpts(bool forceCoverage,
846 GrBlendCoeff* srcCoeff,
847 GrBlendCoeff* dstCoeff) const {
848
849 const GrVertexLayout& layout = this->getGeomSrc().fVertexLayout;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000850 const GrDrawState& drawState = this->getDrawState();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000851
852 GrBlendCoeff bogusSrcCoeff, bogusDstCoeff;
853 if (NULL == srcCoeff) {
854 srcCoeff = &bogusSrcCoeff;
855 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000856 *srcCoeff = drawState.getSrcBlendCoeff();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000857
858 if (NULL == dstCoeff) {
859 dstCoeff = &bogusDstCoeff;
860 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000861 *dstCoeff = drawState.getDstBlendCoeff();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000862
863 // We don't ever expect source coeffecients to reference the source
864 GrAssert(kSA_BlendCoeff != *srcCoeff &&
865 kISA_BlendCoeff != *srcCoeff &&
866 kSC_BlendCoeff != *srcCoeff &&
867 kISC_BlendCoeff != *srcCoeff);
868 // same for dst
869 GrAssert(kDA_BlendCoeff != *dstCoeff &&
870 kIDA_BlendCoeff != *dstCoeff &&
871 kDC_BlendCoeff != *dstCoeff &&
872 kIDC_BlendCoeff != *dstCoeff);
873
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000874 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000875 *srcCoeff = kZero_BlendCoeff;
876 *dstCoeff = kOne_BlendCoeff;
877 }
878
879 bool srcAIsOne = this->srcAlphaWillBeOne();
880 bool dstCoeffIsOne = kOne_BlendCoeff == *dstCoeff ||
881 (kSA_BlendCoeff == *dstCoeff && srcAIsOne);
882 bool dstCoeffIsZero = kZero_BlendCoeff == *dstCoeff ||
883 (kISA_BlendCoeff == *dstCoeff && srcAIsOne);
884
885
886 // When coeffs are (0,1) there is no reason to draw at all, unless
887 // stenciling is enabled. Having color writes disabled is effectively
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000888 // (0,1). The same applies when coverage is known to be 0.
889 if ((kZero_BlendCoeff == *srcCoeff && dstCoeffIsOne) ||
890 (!(layout & kCoverage_VertexLayoutBit) &&
891 0 == drawState.getCoverage())) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000892 if (drawState.getStencil().doesWrite()) {
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000893 return kDisableBlend_BlendOptFlag |
894 kEmitTransBlack_BlendOptFlag;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000895 } else {
896 return kSkipDraw_BlendOptFlag;
897 }
898 }
899
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000900 // check for coverage due to constant coverage, per-vertex coverage,
901 // edge aa or coverage texture stage
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000902 bool hasCoverage = forceCoverage ||
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000903 0xffffffff != drawState.getCoverage() ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000904 drawState.getNumAAEdges() > 0 ||
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000905 (layout & kCoverage_VertexLayoutBit) ||
906 (layout & kEdge_VertexLayoutBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000907 for (int s = drawState.getFirstCoverageStage();
tomhudson@google.com93813632011-10-27 20:21:16 +0000908 !hasCoverage && s < GrDrawState::kNumStages;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000909 ++s) {
910 if (StageWillBeUsed(s, layout, fCurrDrawState)) {
911 hasCoverage = true;
912 }
913 }
914
915 // if we don't have coverage we can check whether the dst
916 // has to read at all. If not, we'll disable blending.
917 if (!hasCoverage) {
918 if (dstCoeffIsZero) {
919 if (kOne_BlendCoeff == *srcCoeff) {
920 // if there is no coverage and coeffs are (1,0) then we
921 // won't need to read the dst at all, it gets replaced by src
922 return kDisableBlend_BlendOptFlag;
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000923 } else if (kZero_BlendCoeff == *srcCoeff) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000924 // if the op is "clear" then we don't need to emit a color
925 // or blend, just write transparent black into the dst.
926 *srcCoeff = kOne_BlendCoeff;
927 *dstCoeff = kZero_BlendCoeff;
928 return kDisableBlend_BlendOptFlag |
929 kEmitTransBlack_BlendOptFlag;
930 }
931 }
932 } else {
933 // check whether coverage can be safely rolled into alpha
934 // of if we can skip color computation and just emit coverage
935 if (this->canTweakAlphaForCoverage()) {
936 return kCoverageAsAlpha_BlendOptFlag;
937 }
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000938 if (dstCoeffIsZero) {
939 if (kZero_BlendCoeff == *srcCoeff) {
940 // the source color is not included in the blend
941 // the dst coeff is effectively zero so blend works out to:
942 // (c)(0)D + (1-c)D = (1-c)D.
943 *dstCoeff = kISA_BlendCoeff;
944 return kEmitCoverage_BlendOptFlag;
945 } else if (srcAIsOne) {
946 // the dst coeff is effectively zero so blend works out to:
947 // cS + (c)(0)D + (1-c)D = cS + (1-c)D.
948 // If Sa is 1 then we can replace Sa with c
949 // and set dst coeff to 1-Sa.
950 *dstCoeff = kISA_BlendCoeff;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000951 return kCoverageAsAlpha_BlendOptFlag;
952 }
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000953 } else if (dstCoeffIsOne) {
954 // the dst coeff is effectively one so blend works out to:
955 // cS + (c)(1)D + (1-c)D = cS + D.
956 *dstCoeff = kOne_BlendCoeff;
957 return kCoverageAsAlpha_BlendOptFlag;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000958 }
959 }
960 return kNone_BlendOpt;
961}
962
963bool GrDrawTarget::willUseHWAALines() const {
bsalomon@google.com471d4712011-08-23 15:45:25 +0000964 // there is a conflict between using smooth lines and our use of
965 // premultiplied alpha. Smooth lines tweak the incoming alpha value
966 // but not in a premul-alpha way. So we only use them when our alpha
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000967 // is 0xff and tweaking the color for partial coverage is OK
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000968 if (!fCaps.fHWAALineSupport ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000969 !this->getDrawState().isHWAntialiasState()) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000970 return false;
971 }
972 BlendOptFlags opts = this->getBlendOpts();
973 return (kDisableBlend_BlendOptFlag & opts) &&
974 (kCoverageAsAlpha_BlendOptFlag & opts);
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000975}
976
977bool GrDrawTarget::canApplyCoverage() const {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000978 // we can correctly apply coverage if a) we have dual source blending
979 // or b) one of our blend optimizations applies.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000980 return this->getCaps().fDualSourceBlendingSupport ||
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000981 kNone_BlendOpt != this->getBlendOpts(true);
bsalomon@google.com471d4712011-08-23 15:45:25 +0000982}
983
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000984bool GrDrawTarget::drawWillReadDst() const {
985 return SkToBool((kDisableBlend_BlendOptFlag | kSkipDraw_BlendOptFlag) &
986 this->getBlendOpts());
bsalomon@google.com471d4712011-08-23 15:45:25 +0000987}
988
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000989
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000990////////////////////////////////////////////////////////////////////////////////
991
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000992void GrDrawTarget::drawRect(const GrRect& rect,
993 const GrMatrix* matrix,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000994 StageMask stageMask,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000995 const GrRect* srcRects[],
996 const GrMatrix* srcMatrices[]) {
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000997 GrVertexLayout layout = GetRectVertexLayout(stageMask, srcRects);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000998
999 AutoReleaseGeometry geo(this, layout, 4, 0);
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001000 if (!geo.succeeded()) {
1001 GrPrintf("Failed to get space for vertices!\n");
1002 return;
1003 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001004
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001005 SetRectVertices(rect, matrix, srcRects,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001006 srcMatrices, layout, geo.vertices());
1007
1008 drawNonIndexed(kTriangleFan_PrimitiveType, 0, 4);
1009}
1010
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001011GrVertexLayout GrDrawTarget::GetRectVertexLayout(StageMask stageMask,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001012 const GrRect* srcRects[]) {
1013 GrVertexLayout layout = 0;
1014
tomhudson@google.com93813632011-10-27 20:21:16 +00001015 for (int i = 0; i < GrDrawState::kNumStages; ++i) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001016 int numTC = 0;
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001017 if (stageMask & (1 << i)) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001018 if (NULL != srcRects && NULL != srcRects[i]) {
1019 layout |= StageTexCoordVertexLayoutBit(i, numTC);
1020 ++numTC;
1021 } else {
1022 layout |= StagePosAsTexCoordVertexLayoutBit(i);
1023 }
1024 }
1025 }
1026 return layout;
1027}
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001028
1029void GrDrawTarget::clipWillBeSet(const GrClip& clip) {
1030}
1031
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001032void GrDrawTarget::SetRectVertices(const GrRect& rect,
1033 const GrMatrix* matrix,
1034 const GrRect* srcRects[],
1035 const GrMatrix* srcMatrices[],
1036 GrVertexLayout layout,
1037 void* vertices) {
1038#if GR_DEBUG
1039 // check that the layout and srcRects agree
tomhudson@google.com93813632011-10-27 20:21:16 +00001040 for (int i = 0; i < GrDrawState::kNumStages; ++i) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001041 if (VertexTexCoordsForStage(i, layout) >= 0) {
1042 GR_DEBUGASSERT(NULL != srcRects && NULL != srcRects[i]);
1043 } else {
1044 GR_DEBUGASSERT(NULL == srcRects || NULL == srcRects[i]);
1045 }
1046 }
1047#endif
1048
tomhudson@google.com93813632011-10-27 20:21:16 +00001049 int stageOffsets[GrDrawState::kNumStages];
bsalomon@google.coma3108262011-10-10 14:08:47 +00001050 int vsize = VertexSizeAndOffsetsByStage(layout, stageOffsets,
1051 NULL, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001052
bsalomon@google.coma3108262011-10-10 14:08:47 +00001053 GrTCast<GrPoint*>(vertices)->setRectFan(rect.fLeft, rect.fTop,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001054 rect.fRight, rect.fBottom,
1055 vsize);
1056 if (NULL != matrix) {
1057 matrix->mapPointsWithStride(GrTCast<GrPoint*>(vertices), vsize, 4);
1058 }
1059
tomhudson@google.com93813632011-10-27 20:21:16 +00001060 for (int i = 0; i < GrDrawState::kNumStages; ++i) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001061 if (stageOffsets[i] > 0) {
bsalomon@google.coma3108262011-10-10 14:08:47 +00001062 GrPoint* coords = GrTCast<GrPoint*>(GrTCast<intptr_t>(vertices) +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001063 stageOffsets[i]);
1064 coords->setRectFan(srcRects[i]->fLeft, srcRects[i]->fTop,
bsalomon@google.coma3108262011-10-10 14:08:47 +00001065 srcRects[i]->fRight, srcRects[i]->fBottom,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001066 vsize);
1067 if (NULL != srcMatrices && NULL != srcMatrices[i]) {
1068 srcMatrices[i]->mapPointsWithStride(coords, vsize, 4);
1069 }
1070 }
1071 }
1072}
1073
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001074////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001075
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001076GrDrawTarget::AutoStateRestore::AutoStateRestore() {
1077 fDrawTarget = NULL;
1078}
reed@google.comac10a2d2010-12-22 21:39:39 +00001079
1080GrDrawTarget::AutoStateRestore::AutoStateRestore(GrDrawTarget* target) {
1081 fDrawTarget = target;
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001082 if (NULL != fDrawTarget) {
1083 fDrawTarget->saveCurrentDrawState(&fDrawState);
1084 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001085}
1086
1087GrDrawTarget::AutoStateRestore::~AutoStateRestore() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001088 if (NULL != fDrawTarget) {
1089 fDrawTarget->restoreDrawState(fDrawState);
1090 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001091}
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001092
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001093void GrDrawTarget::AutoStateRestore::set(GrDrawTarget* target) {
1094 if (target != fDrawTarget) {
1095 if (NULL != fDrawTarget) {
1096 fDrawTarget->restoreDrawState(fDrawState);
1097 }
1098 if (NULL != target) {
bsalomon@google.comd19aa272011-06-22 01:28:17 +00001099 target->saveCurrentDrawState(&fDrawState);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001100 }
1101 fDrawTarget = target;
1102 }
1103}
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001104
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001105////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001106
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001107GrDrawTarget::AutoDeviceCoordDraw::AutoDeviceCoordDraw(
1108 GrDrawTarget* target,
1109 GrDrawState::StageMask stageMask) {
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001110 GrAssert(NULL != target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001111 GrDrawState* drawState = target->drawState();
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001112
1113 fDrawTarget = target;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001114 fViewMatrix = drawState->getViewMatrix();
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001115 fStageMask = stageMask;
1116 if (fStageMask) {
1117 GrMatrix invVM;
1118 if (fViewMatrix.invert(&invVM)) {
tomhudson@google.com93813632011-10-27 20:21:16 +00001119 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001120 if (fStageMask & (1 << s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001121 fSamplerMatrices[s] = drawState->getSampler(s).getMatrix();
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001122 }
1123 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001124 drawState->preConcatSamplerMatrices(fStageMask, invVM);
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001125 } else {
1126 // sad trombone sound
1127 fStageMask = 0;
1128 }
1129 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001130 drawState->setViewMatrix(GrMatrix::I());
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001131}
1132
1133GrDrawTarget::AutoDeviceCoordDraw::~AutoDeviceCoordDraw() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001134 GrDrawState* drawState = fDrawTarget->drawState();
1135 drawState->setViewMatrix(fViewMatrix);
tomhudson@google.com93813632011-10-27 20:21:16 +00001136 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001137 if (fStageMask & (1 << s)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001138 *drawState->sampler(s)->matrix() = fSamplerMatrices[s];
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001139 }
1140 }
1141}
1142
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001143////////////////////////////////////////////////////////////////////////////////
1144
1145GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry(
1146 GrDrawTarget* target,
1147 GrVertexLayout vertexLayout,
1148 int vertexCount,
1149 int indexCount) {
1150 fTarget = NULL;
1151 this->set(target, vertexLayout, vertexCount, indexCount);
1152}
1153
1154GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry() {
1155 fTarget = NULL;
1156}
1157
1158GrDrawTarget::AutoReleaseGeometry::~AutoReleaseGeometry() {
1159 this->reset();
1160}
1161
1162bool GrDrawTarget::AutoReleaseGeometry::set(GrDrawTarget* target,
1163 GrVertexLayout vertexLayout,
1164 int vertexCount,
1165 int indexCount) {
1166 this->reset();
1167 fTarget = target;
1168 bool success = true;
1169 if (NULL != fTarget) {
1170 fTarget = target;
1171 if (vertexCount > 0) {
1172 success = target->reserveVertexSpace(vertexLayout,
1173 vertexCount,
1174 &fVertices);
1175 if (!success) {
1176 this->reset();
1177 }
1178 }
1179 if (success && indexCount > 0) {
1180 success = target->reserveIndexSpace(indexCount, &fIndices);
1181 if (!success) {
1182 this->reset();
1183 }
1184 }
1185 }
1186 GrAssert(success == (NULL != fTarget));
1187 return success;
1188}
1189
1190void GrDrawTarget::AutoReleaseGeometry::reset() {
1191 if (NULL != fTarget) {
1192 if (NULL != fVertices) {
1193 fTarget->resetVertexSource();
1194 }
1195 if (NULL != fIndices) {
1196 fTarget->resetIndexSource();
1197 }
1198 fTarget = NULL;
1199 }
bsalomon@google.comcb0c5ab2011-06-29 17:48:17 +00001200 fVertices = NULL;
1201 fIndices = NULL;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001202}
1203
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001204void GrDrawTarget::Caps::print() const {
1205 static const char* gNY[] = {"NO", "YES"};
1206 GrPrintf("8 Bit Palette Support : %s\n", gNY[f8BitPaletteSupport]);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001207 GrPrintf("NPOT Texture Tile Support : %s\n", gNY[fNPOTTextureTileSupport]);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001208 GrPrintf("Two Sided Stencil Support : %s\n", gNY[fTwoSidedStencilSupport]);
1209 GrPrintf("Stencil Wrap Ops Support : %s\n", gNY[fStencilWrapOpsSupport]);
1210 GrPrintf("HW AA Lines Support : %s\n", gNY[fHWAALineSupport]);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001211 GrPrintf("Shader Derivative Support : %s\n", gNY[fShaderDerivativeSupport]);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001212 GrPrintf("Geometry Shader Support : %s\n", gNY[fGeometryShaderSupport]);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001213 GrPrintf("FSAA Support : %s\n", gNY[fFSAASupport]);
1214 GrPrintf("Dual Source Blending Support: %s\n", gNY[fDualSourceBlendingSupport]);
1215 GrPrintf("Buffer Lock Support : %s\n", gNY[fBufferLockSupport]);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001216 GrPrintf("Max Texture Size : %d\n", fMaxTextureSize);
1217 GrPrintf("Max Render Target Size : %d\n", fMaxRenderTargetSize);
1218}
1219