blob: 692789537cb0626a9f8b7503ecdab3cde2b5dfb7 [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
sugoi@google.com12b4e272012-12-06 20:13:11 +000018#include "SkStroke.h"
19
reed@google.comfa35e3d2012-06-26 20:16:17 +000020SK_DEFINE_INST_COUNT(GrDrawTarget)
21
junov@google.com6acc9b32011-05-16 18:32:07 +000022namespace {
23
bsalomon@google.com35ff3842011-12-15 16:58:19 +000024/**
25 * This function generates some masks that we like to have known at compile
26 * time. When the number of stages or tex coords is bumped or the way bits
robertphillips@google.coma72eef32012-05-01 17:22:59 +000027 * are defined in GrDrawTarget.h changes this function should be rerun to
bsalomon@google.com35ff3842011-12-15 16:58:19 +000028 * generate the new masks. (We attempted to force the compiler to generate the
29 * masks using recursive templates but always wound up with static initializers
30 * under gcc, even if they were just a series of immediate->memory moves.)
rmistry@google.comd6176b02012-08-23 18:14:13 +000031 *
bsalomon@google.com35ff3842011-12-15 16:58:19 +000032 */
33void gen_mask_arrays(GrVertexLayout* stageTexCoordMasks,
bsalomon@google.com35ff3842011-12-15 16:58:19 +000034 GrVertexLayout* texCoordMasks) {
35 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
36 stageTexCoordMasks[s] = 0;
37 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
38 stageTexCoordMasks[s] |= GrDrawTarget::StageTexCoordVertexLayoutBit(s, t);
39 }
bsalomon@google.com35ff3842011-12-15 16:58:19 +000040 }
41 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
42 texCoordMasks[t] = 0;
43 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
44 texCoordMasks[t] |= GrDrawTarget::StageTexCoordVertexLayoutBit(s, t);
45 }
46 }
reed@google.comac10a2d2010-12-22 21:39:39 +000047}
48
rmistry@google.comd6176b02012-08-23 18:14:13 +000049/**
bsalomon@google.com35ff3842011-12-15 16:58:19 +000050 * Run this function to generate the code that declares the global masks.
51 */
52void gen_globals() {
53 GrVertexLayout stageTexCoordMasks[GrDrawState::kNumStages];
bsalomon@google.com35ff3842011-12-15 16:58:19 +000054 GrVertexLayout texCoordMasks[GrDrawState::kMaxTexCoords];
bsalomon@google.com52544c72012-07-10 15:06:59 +000055 gen_mask_arrays(stageTexCoordMasks, texCoordMasks);
rmistry@google.comd6176b02012-08-23 18:14:13 +000056
bsalomon@google.com35ff3842011-12-15 16:58:19 +000057 GrPrintf("const GrVertexLayout gStageTexCoordMasks[] = {\n");
58 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
59 GrPrintf(" 0x%x,\n", stageTexCoordMasks[s]);
60 }
61 GrPrintf("};\n");
62 GrPrintf("GR_STATIC_ASSERT(GrDrawState::kNumStages == GR_ARRAY_COUNT(gStageTexCoordMasks));\n\n");
bsalomon@google.com35ff3842011-12-15 16:58:19 +000063 GrPrintf("const GrVertexLayout gTexCoordMasks[] = {\n");
64 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
65 GrPrintf(" 0x%x,\n", texCoordMasks[t]);
66 }
67 GrPrintf("};\n");
68 GrPrintf("GR_STATIC_ASSERT(GrDrawState::kMaxTexCoords == GR_ARRAY_COUNT(gTexCoordMasks));\n");
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000069}
70
bsalomon@google.com35ff3842011-12-15 16:58:19 +000071/* These values were generated by the above function */
twiz@google.com58071162012-07-18 21:41:50 +000072
bsalomon@google.com35ff3842011-12-15 16:58:19 +000073const GrVertexLayout gStageTexCoordMasks[] = {
twiz@google.com58071162012-07-18 21:41:50 +000074 0x108421,
75 0x210842,
76 0x421084,
77 0x842108,
78 0x1084210,
bsalomon@google.com35ff3842011-12-15 16:58:19 +000079};
bsalomon@google.com35ff3842011-12-15 16:58:19 +000080GR_STATIC_ASSERT(GrDrawState::kNumStages == GR_ARRAY_COUNT(gStageTexCoordMasks));
bsalomon@google.com35ff3842011-12-15 16:58:19 +000081
bsalomon@google.com35ff3842011-12-15 16:58:19 +000082const GrVertexLayout gTexCoordMasks[] = {
twiz@google.com58071162012-07-18 21:41:50 +000083 0x1f,
84 0x3e0,
85 0x7c00,
86 0xf8000,
87 0x1f00000,
bsalomon@google.com35ff3842011-12-15 16:58:19 +000088};
89GR_STATIC_ASSERT(GrDrawState::kMaxTexCoords == GR_ARRAY_COUNT(gTexCoordMasks));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000090
91bool check_layout(GrVertexLayout layout) {
92 // can only have 1 or 0 bits set for each stage.
tomhudson@google.com93813632011-10-27 20:21:16 +000093 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com52544c72012-07-10 15:06:59 +000094 int stageBits = layout & gStageTexCoordMasks[s];
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000095 if (stageBits && !GrIsPow2(stageBits)) {
96 return false;
97 }
98 }
99 return true;
100}
101
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000102int num_tex_coords(GrVertexLayout layout) {
103 int cnt = 0;
104 // figure out how many tex coordinates are present
tomhudson@google.com93813632011-10-27 20:21:16 +0000105 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000106 if (gTexCoordMasks[t] & layout) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000107 ++cnt;
108 }
109 }
110 return cnt;
111}
112
junov@google.com6acc9b32011-05-16 18:32:07 +0000113} //unnamed namespace
114
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000115size_t GrDrawTarget::VertexSize(GrVertexLayout vertexLayout) {
116 GrAssert(check_layout(vertexLayout));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000117
118 size_t vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ?
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000119 sizeof(GrGpuTextVertex) :
120 sizeof(GrPoint);
121
122 size_t size = vecSize; // position
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000123 size += num_tex_coords(vertexLayout) * vecSize;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000124 if (vertexLayout & kColor_VertexLayoutBit) {
125 size += sizeof(GrColor);
126 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000127 if (vertexLayout & kCoverage_VertexLayoutBit) {
128 size += sizeof(GrColor);
129 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000130 if (vertexLayout & kEdge_VertexLayoutBit) {
bsalomon@google.com81712882012-11-01 17:12:34 +0000131 size += 4 * sizeof(SkScalar);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000132 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000133 return size;
134}
135
bsalomon@google.coma3108262011-10-10 14:08:47 +0000136////////////////////////////////////////////////////////////////////////////////
137
138/**
139 * Functions for computing offsets of various components from the layout
140 * bitfield.
rmistry@google.comd6176b02012-08-23 18:14:13 +0000141 *
bsalomon@google.coma3108262011-10-10 14:08:47 +0000142 * Order of vertex components:
143 * Position
144 * Tex Coord 0
145 * ...
tomhudson@google.com93813632011-10-27 20:21:16 +0000146 * Tex Coord GrDrawState::kMaxTexCoords-1
bsalomon@google.coma3108262011-10-10 14:08:47 +0000147 * Color
148 * Coverage
149 */
150
bsalomon@google.com08283af2012-10-26 13:01:20 +0000151int GrDrawTarget::VertexStageCoordOffset(int stageIdx, GrVertexLayout vertexLayout) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000152 GrAssert(check_layout(vertexLayout));
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000153
bsalomon@google.com08283af2012-10-26 13:01:20 +0000154 if (!StageUsesTexCoords(vertexLayout, stageIdx)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000155 return 0;
156 }
bsalomon@google.com08283af2012-10-26 13:01:20 +0000157 int tcIdx = VertexTexCoordsForStage(stageIdx, vertexLayout);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000158 if (tcIdx >= 0) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000159
160 int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ?
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000161 sizeof(GrGpuTextVertex) :
162 sizeof(GrPoint);
163 int offset = vecSize; // position
164 // figure out how many tex coordinates are present and precede this one.
165 for (int t = 0; t < tcIdx; ++t) {
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000166 if (gTexCoordMasks[t] & vertexLayout) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000167 offset += vecSize;
168 }
169 }
170 return offset;
171 }
172
reed@google.comac10a2d2010-12-22 21:39:39 +0000173 return -1;
174}
175
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000176int GrDrawTarget::VertexColorOffset(GrVertexLayout vertexLayout) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000177 GrAssert(check_layout(vertexLayout));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000178
reed@google.comac10a2d2010-12-22 21:39:39 +0000179 if (vertexLayout & kColor_VertexLayoutBit) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000180 int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ?
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000181 sizeof(GrGpuTextVertex) :
182 sizeof(GrPoint);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000183 return vecSize * (num_tex_coords(vertexLayout) + 1); //+1 for pos
184 }
185 return -1;
186}
187
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000188int GrDrawTarget::VertexCoverageOffset(GrVertexLayout vertexLayout) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000189 GrAssert(check_layout(vertexLayout));
190
191 if (vertexLayout & kCoverage_VertexLayoutBit) {
192 int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ?
193 sizeof(GrGpuTextVertex) :
194 sizeof(GrPoint);
195
196 int offset = vecSize * (num_tex_coords(vertexLayout) + 1);
197 if (vertexLayout & kColor_VertexLayoutBit) {
198 offset += sizeof(GrColor);
199 }
200 return offset;
201 }
202 return -1;
203}
204
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000205int GrDrawTarget::VertexEdgeOffset(GrVertexLayout vertexLayout) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000206 GrAssert(check_layout(vertexLayout));
207
208 // edge pts are after the pos, tex coords, and color
209 if (vertexLayout & kEdge_VertexLayoutBit) {
210 int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ?
211 sizeof(GrGpuTextVertex) :
212 sizeof(GrPoint);
213 int offset = vecSize * (num_tex_coords(vertexLayout) + 1); //+1 for pos
214 if (vertexLayout & kColor_VertexLayoutBit) {
215 offset += sizeof(GrColor);
reed@google.comac10a2d2010-12-22 21:39:39 +0000216 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000217 if (vertexLayout & kCoverage_VertexLayoutBit) {
218 offset += sizeof(GrColor);
219 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000220 return offset;
reed@google.comac10a2d2010-12-22 21:39:39 +0000221 }
222 return -1;
223}
224
tomhudson@google.com93813632011-10-27 20:21:16 +0000225int GrDrawTarget::VertexSizeAndOffsetsByIdx(
226 GrVertexLayout vertexLayout,
227 int texCoordOffsetsByIdx[GrDrawState::kMaxTexCoords],
228 int* colorOffset,
229 int* coverageOffset,
230 int* edgeOffset) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000231 GrAssert(check_layout(vertexLayout));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000232
bsalomon@google.com5782d712011-01-21 21:03:59 +0000233 int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ?
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000234 sizeof(GrGpuTextVertex) :
235 sizeof(GrPoint);
236 int size = vecSize; // position
bsalomon@google.com5782d712011-01-21 21:03:59 +0000237
tomhudson@google.com93813632011-10-27 20:21:16 +0000238 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000239 if (gTexCoordMasks[t] & vertexLayout) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000240 if (NULL != texCoordOffsetsByIdx) {
241 texCoordOffsetsByIdx[t] = size;
242 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000243 size += vecSize;
reed@google.comac10a2d2010-12-22 21:39:39 +0000244 } else {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000245 if (NULL != texCoordOffsetsByIdx) {
246 texCoordOffsetsByIdx[t] = -1;
247 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000248 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000249 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000250 if (kColor_VertexLayoutBit & vertexLayout) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000251 if (NULL != colorOffset) {
252 *colorOffset = size;
253 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000254 size += sizeof(GrColor);
255 } else {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000256 if (NULL != colorOffset) {
257 *colorOffset = -1;
258 }
259 }
260 if (kCoverage_VertexLayoutBit & vertexLayout) {
261 if (NULL != coverageOffset) {
262 *coverageOffset = size;
263 }
264 size += sizeof(GrColor);
265 } else {
266 if (NULL != coverageOffset) {
267 *coverageOffset = -1;
268 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000269 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000270 if (kEdge_VertexLayoutBit & vertexLayout) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000271 if (NULL != edgeOffset) {
272 *edgeOffset = size;
273 }
bsalomon@google.com81712882012-11-01 17:12:34 +0000274 size += 4 * sizeof(SkScalar);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000275 } else {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000276 if (NULL != edgeOffset) {
277 *edgeOffset = -1;
278 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000279 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000280 return size;
reed@google.comac10a2d2010-12-22 21:39:39 +0000281}
282
tomhudson@google.com93813632011-10-27 20:21:16 +0000283int GrDrawTarget::VertexSizeAndOffsetsByStage(
284 GrVertexLayout vertexLayout,
285 int texCoordOffsetsByStage[GrDrawState::kNumStages],
286 int* colorOffset,
287 int* coverageOffset,
288 int* edgeOffset) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000289 GrAssert(check_layout(vertexLayout));
290
tomhudson@google.com93813632011-10-27 20:21:16 +0000291 int texCoordOffsetsByIdx[GrDrawState::kMaxTexCoords];
bsalomon@google.com5782d712011-01-21 21:03:59 +0000292 int size = VertexSizeAndOffsetsByIdx(vertexLayout,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000293 (NULL == texCoordOffsetsByStage) ?
294 NULL :
295 texCoordOffsetsByIdx,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000296 colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000297 coverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000298 edgeOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000299 if (NULL != texCoordOffsetsByStage) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000300 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000301 int tcIdx = VertexTexCoordsForStage(s, vertexLayout);
302 texCoordOffsetsByStage[s] =
303 tcIdx < 0 ? 0 : texCoordOffsetsByIdx[tcIdx];
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000304 }
305 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000306 return size;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000307}
308
bsalomon@google.coma3108262011-10-10 14:08:47 +0000309////////////////////////////////////////////////////////////////////////////////
310
bsalomon@google.com5782d712011-01-21 21:03:59 +0000311bool GrDrawTarget::VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000312 GrVertexLayout vertexLayout) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000313 GrAssert(coordIndex < GrDrawState::kMaxTexCoords);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000314 GrAssert(check_layout(vertexLayout));
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000315 return !!(gTexCoordMasks[coordIndex] & vertexLayout);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000316}
317
bsalomon@google.com08283af2012-10-26 13:01:20 +0000318int GrDrawTarget::VertexTexCoordsForStage(int stageIdx,
tomhudson@google.com93813632011-10-27 20:21:16 +0000319 GrVertexLayout vertexLayout) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000320 GrAssert(stageIdx < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000321 GrAssert(check_layout(vertexLayout));
bsalomon@google.com08283af2012-10-26 13:01:20 +0000322 int bit = vertexLayout & gStageTexCoordMasks[stageIdx];
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000323 if (bit) {
324 // figure out which set of texture coordates is used
325 // bits are ordered T0S0, T0S1, T0S2, ..., T1S0, T1S1, ...
326 // and start at bit 0.
327 GR_STATIC_ASSERT(sizeof(GrVertexLayout) <= sizeof(uint32_t));
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000328 return (32 - SkCLZ(bit) - 1) / GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000329 }
330 return -1;
331}
332
bsalomon@google.coma3108262011-10-10 14:08:47 +0000333////////////////////////////////////////////////////////////////////////////////
334
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000335void GrDrawTarget::VertexLayoutUnitTest() {
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000336 // Ensure that our globals mask arrays are correct
337 GrVertexLayout stageTexCoordMasks[GrDrawState::kNumStages];
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000338 GrVertexLayout texCoordMasks[GrDrawState::kMaxTexCoords];
bsalomon@google.com52544c72012-07-10 15:06:59 +0000339 gen_mask_arrays(stageTexCoordMasks, texCoordMasks);
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000340 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
341 GrAssert(stageTexCoordMasks[s] == gStageTexCoordMasks[s]);
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000342 }
343 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
344 GrAssert(texCoordMasks[t] == gTexCoordMasks[t]);
345 }
346
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000347 // not necessarily exhaustive
348 static bool run;
349 if (!run) {
350 run = true;
tomhudson@google.com93813632011-10-27 20:21:16 +0000351 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000352
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000353 GrVertexLayout stageMask = 0;
tomhudson@google.com93813632011-10-27 20:21:16 +0000354 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000355 stageMask |= StageTexCoordVertexLayoutBit(s,t);
356 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000357 GrAssert(1 == GrDrawState::kMaxTexCoords ||
358 !check_layout(stageMask));
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000359 GrAssert(gStageTexCoordMasks[s] == stageMask);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000360 GrAssert(!check_layout(stageMask));
361 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000362 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000363 GrVertexLayout tcMask = 0;
364 GrAssert(!VertexUsesTexCoordIdx(t, 0));
tomhudson@google.com93813632011-10-27 20:21:16 +0000365 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000366 tcMask |= StageTexCoordVertexLayoutBit(s,t);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000367 GrAssert(sizeof(GrPoint) == VertexStageCoordOffset(s, tcMask));
368 GrAssert(VertexUsesTexCoordIdx(t, tcMask));
369 GrAssert(2*sizeof(GrPoint) == VertexSize(tcMask));
370 GrAssert(t == VertexTexCoordsForStage(s, tcMask));
tomhudson@google.com93813632011-10-27 20:21:16 +0000371 for (int s2 = s + 1; s2 < GrDrawState::kNumStages; ++s2) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000372 GrAssert(-1 == VertexTexCoordsForStage(s2, tcMask));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000373
bsalomon@google.com19628322011-02-03 21:30:17 +0000374 #if GR_DEBUG
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000375 GrVertexLayout posAsTex = tcMask;
bsalomon@google.com19628322011-02-03 21:30:17 +0000376 #endif
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000377 GrAssert(0 == VertexStageCoordOffset(s2, posAsTex));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000378 GrAssert(2*sizeof(GrPoint) == VertexSize(posAsTex));
379 GrAssert(-1 == VertexTexCoordsForStage(s2, posAsTex));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000380 GrAssert(-1 == VertexEdgeOffset(posAsTex));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000381 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000382 GrAssert(-1 == VertexEdgeOffset(tcMask));
383 GrAssert(-1 == VertexColorOffset(tcMask));
bsalomon@google.coma3108262011-10-10 14:08:47 +0000384 GrAssert(-1 == VertexCoverageOffset(tcMask));
bsalomon@google.com19628322011-02-03 21:30:17 +0000385 #if GR_DEBUG
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000386 GrVertexLayout withColor = tcMask | kColor_VertexLayoutBit;
bsalomon@google.com19628322011-02-03 21:30:17 +0000387 #endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000388 GrAssert(-1 == VertexCoverageOffset(withColor));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000389 GrAssert(2*sizeof(GrPoint) == VertexColorOffset(withColor));
390 GrAssert(2*sizeof(GrPoint) + sizeof(GrColor) == VertexSize(withColor));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000391 #if GR_DEBUG
392 GrVertexLayout withEdge = tcMask | kEdge_VertexLayoutBit;
393 #endif
394 GrAssert(-1 == VertexColorOffset(withEdge));
395 GrAssert(2*sizeof(GrPoint) == VertexEdgeOffset(withEdge));
396 GrAssert(4*sizeof(GrPoint) == VertexSize(withEdge));
397 #if GR_DEBUG
398 GrVertexLayout withColorAndEdge = withColor | kEdge_VertexLayoutBit;
399 #endif
400 GrAssert(2*sizeof(GrPoint) == VertexColorOffset(withColorAndEdge));
401 GrAssert(2*sizeof(GrPoint) + sizeof(GrColor) == VertexEdgeOffset(withColorAndEdge));
402 GrAssert(4*sizeof(GrPoint) + sizeof(GrColor) == VertexSize(withColorAndEdge));
bsalomon@google.coma3108262011-10-10 14:08:47 +0000403 #if GR_DEBUG
404 GrVertexLayout withCoverage = tcMask | kCoverage_VertexLayoutBit;
405 #endif
406 GrAssert(-1 == VertexColorOffset(withCoverage));
407 GrAssert(2*sizeof(GrPoint) == VertexCoverageOffset(withCoverage));
408 GrAssert(2*sizeof(GrPoint) + sizeof(GrColor) == VertexSize(withCoverage));
409 #if GR_DEBUG
410 GrVertexLayout withCoverageAndColor = tcMask | kCoverage_VertexLayoutBit |
411 kColor_VertexLayoutBit;
412 #endif
413 GrAssert(2*sizeof(GrPoint) == VertexColorOffset(withCoverageAndColor));
414 GrAssert(2*sizeof(GrPoint) + sizeof(GrColor) == VertexCoverageOffset(withCoverageAndColor));
415 GrAssert(2*sizeof(GrPoint) + 2 * sizeof(GrColor) == VertexSize(withCoverageAndColor));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000416 }
bsalomon@google.com35ff3842011-12-15 16:58:19 +0000417 GrAssert(gTexCoordMasks[t] == tcMask);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000418 GrAssert(check_layout(tcMask));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000419
tomhudson@google.com93813632011-10-27 20:21:16 +0000420 int stageOffsets[GrDrawState::kNumStages];
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000421 int colorOffset;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000422 int edgeOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000423 int coverageOffset;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000424 int size;
tomhudson@google.com93813632011-10-27 20:21:16 +0000425 size = VertexSizeAndOffsetsByStage(tcMask,
426 stageOffsets, &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000427 &coverageOffset, &edgeOffset);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000428 GrAssert(2*sizeof(GrPoint) == size);
429 GrAssert(-1 == colorOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000430 GrAssert(-1 == coverageOffset);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000431 GrAssert(-1 == edgeOffset);
tomhudson@google.com93813632011-10-27 20:21:16 +0000432 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000433 GrAssert(sizeof(GrPoint) == stageOffsets[s]);
434 GrAssert(sizeof(GrPoint) == VertexStageCoordOffset(s, tcMask));
435 }
436 }
437 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000438}
439
440////////////////////////////////////////////////////////////////////////////////
441
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000442#define DEBUG_INVAL_BUFFER 0xdeadcafe
443#define DEBUG_INVAL_START_IDX -1
444
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000445GrDrawTarget::GrDrawTarget() : fClip(NULL) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000446#if GR_DEBUG
447 VertexLayoutUnitTest();
448#endif
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000449 fDrawState = &fDefaultDrawState;
450 // We assume that fDrawState always owns a ref to the object it points at.
451 fDefaultDrawState.ref();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000452 GeometrySrcState& geoSrc = fGeoSrcStateStack.push_back();
reed@google.comac10a2d2010-12-22 21:39:39 +0000453#if GR_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000454 geoSrc.fVertexCount = DEBUG_INVAL_START_IDX;
455 geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
456 geoSrc.fIndexCount = DEBUG_INVAL_START_IDX;
457 geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
reed@google.comac10a2d2010-12-22 21:39:39 +0000458#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000459 geoSrc.fVertexSrc = kNone_GeometrySrcType;
460 geoSrc.fIndexSrc = kNone_GeometrySrcType;
461}
462
463GrDrawTarget::~GrDrawTarget() {
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000464 GrAssert(1 == fGeoSrcStateStack.count());
465 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
466 GrAssert(kNone_GeometrySrcType == geoSrc.fIndexSrc);
467 GrAssert(kNone_GeometrySrcType == geoSrc.fVertexSrc);
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000468 fDrawState->unref();
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000469}
470
471void GrDrawTarget::releaseGeometry() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000472 int popCnt = fGeoSrcStateStack.count() - 1;
473 while (popCnt) {
474 this->popGeometrySource();
475 --popCnt;
476 }
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000477 this->resetVertexSource();
478 this->resetIndexSource();
reed@google.comac10a2d2010-12-22 21:39:39 +0000479}
480
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000481void GrDrawTarget::setClip(const GrClipData* clip) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000482 clipWillBeSet(clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000483 fClip = clip;
484}
485
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000486const GrClipData* GrDrawTarget::getClip() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000487 return fClip;
488}
489
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000490void GrDrawTarget::setDrawState(GrDrawState* drawState) {
491 GrAssert(NULL != fDrawState);
492 if (NULL == drawState) {
493 drawState = &fDefaultDrawState;
494 }
495 if (fDrawState != drawState) {
496 fDrawState->unref();
497 drawState->ref();
498 fDrawState = drawState;
499 }
500}
501
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000502bool GrDrawTarget::reserveVertexSpace(GrVertexLayout vertexLayout,
503 int vertexCount,
504 void** vertices) {
505 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
506 bool acquired = false;
507 if (vertexCount > 0) {
508 GrAssert(NULL != vertices);
509 this->releasePreviousVertexSource();
510 geoSrc.fVertexSrc = kNone_GeometrySrcType;
reed@google.comac10a2d2010-12-22 21:39:39 +0000511
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000512 acquired = this->onReserveVertexSpace(vertexLayout,
513 vertexCount,
514 vertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000515 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000516 if (acquired) {
517 geoSrc.fVertexSrc = kReserved_GeometrySrcType;
518 geoSrc.fVertexCount = vertexCount;
519 geoSrc.fVertexLayout = vertexLayout;
520 } else if (NULL != vertices) {
521 *vertices = NULL;
522 }
523 return acquired;
524}
525
526bool GrDrawTarget::reserveIndexSpace(int indexCount,
527 void** indices) {
528 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
529 bool acquired = false;
530 if (indexCount > 0) {
531 GrAssert(NULL != indices);
532 this->releasePreviousIndexSource();
533 geoSrc.fIndexSrc = kNone_GeometrySrcType;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000534
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000535 acquired = this->onReserveIndexSpace(indexCount, indices);
536 }
537 if (acquired) {
538 geoSrc.fIndexSrc = kReserved_GeometrySrcType;
539 geoSrc.fIndexCount = indexCount;
540 } else if (NULL != indices) {
541 *indices = NULL;
542 }
543 return acquired;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000544
reed@google.comac10a2d2010-12-22 21:39:39 +0000545}
546
bsalomon@google.com08283af2012-10-26 13:01:20 +0000547bool GrDrawTarget::StageUsesTexCoords(GrVertexLayout layout, int stageIdx) {
548 return SkToBool(layout & gStageTexCoordMasks[stageIdx]);
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000549}
550
bsalomon@google.come3d70952012-03-13 12:40:53 +0000551bool GrDrawTarget::reserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
552 int vertexCount,
553 int indexCount,
554 void** vertices,
555 void** indices) {
bsalomon@google.com97805382012-03-13 14:32:07 +0000556 this->willReserveVertexAndIndexSpace(vertexLayout, vertexCount, indexCount);
bsalomon@google.come3d70952012-03-13 12:40:53 +0000557 if (vertexCount) {
558 if (!this->reserveVertexSpace(vertexLayout, vertexCount, vertices)) {
559 if (indexCount) {
560 this->resetIndexSource();
561 }
562 return false;
563 }
564 }
565 if (indexCount) {
566 if (!this->reserveIndexSpace(indexCount, indices)) {
567 if (vertexCount) {
568 this->resetVertexSource();
569 }
570 return false;
571 }
572 }
573 return true;
574}
575
reed@google.comac10a2d2010-12-22 21:39:39 +0000576bool GrDrawTarget::geometryHints(GrVertexLayout vertexLayout,
577 int32_t* vertexCount,
578 int32_t* indexCount) const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000579 if (NULL != vertexCount) {
580 *vertexCount = -1;
581 }
582 if (NULL != indexCount) {
583 *indexCount = -1;
584 }
585 return false;
586}
587
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000588void GrDrawTarget::releasePreviousVertexSource() {
589 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
590 switch (geoSrc.fVertexSrc) {
591 case kNone_GeometrySrcType:
592 break;
593 case kArray_GeometrySrcType:
594 this->releaseVertexArray();
595 break;
596 case kReserved_GeometrySrcType:
597 this->releaseReservedVertexSpace();
598 break;
599 case kBuffer_GeometrySrcType:
600 geoSrc.fVertexBuffer->unref();
601#if GR_DEBUG
602 geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
603#endif
604 break;
605 default:
606 GrCrash("Unknown Vertex Source Type.");
607 break;
608 }
609}
610
611void GrDrawTarget::releasePreviousIndexSource() {
612 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
613 switch (geoSrc.fIndexSrc) {
614 case kNone_GeometrySrcType: // these two don't require
615 break;
616 case kArray_GeometrySrcType:
617 this->releaseIndexArray();
618 break;
619 case kReserved_GeometrySrcType:
620 this->releaseReservedIndexSpace();
621 break;
622 case kBuffer_GeometrySrcType:
623 geoSrc.fIndexBuffer->unref();
624#if GR_DEBUG
625 geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
626#endif
627 break;
628 default:
629 GrCrash("Unknown Index Source Type.");
630 break;
631 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000632}
633
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000634void GrDrawTarget::setVertexSourceToArray(GrVertexLayout vertexLayout,
635 const void* vertexArray,
636 int vertexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000637 this->releasePreviousVertexSource();
638 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
639 geoSrc.fVertexSrc = kArray_GeometrySrcType;
640 geoSrc.fVertexLayout = vertexLayout;
641 geoSrc.fVertexCount = vertexCount;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000642 this->onSetVertexSourceToArray(vertexArray, vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000643}
644
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000645void GrDrawTarget::setIndexSourceToArray(const void* indexArray,
646 int indexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000647 this->releasePreviousIndexSource();
648 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
649 geoSrc.fIndexSrc = kArray_GeometrySrcType;
650 geoSrc.fIndexCount = indexCount;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000651 this->onSetIndexSourceToArray(indexArray, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000652}
653
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000654void GrDrawTarget::setVertexSourceToBuffer(GrVertexLayout vertexLayout,
655 const GrVertexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000656 this->releasePreviousVertexSource();
657 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
658 geoSrc.fVertexSrc = kBuffer_GeometrySrcType;
659 geoSrc.fVertexBuffer = buffer;
660 buffer->ref();
661 geoSrc.fVertexLayout = vertexLayout;
reed@google.comac10a2d2010-12-22 21:39:39 +0000662}
663
664void GrDrawTarget::setIndexSourceToBuffer(const GrIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000665 this->releasePreviousIndexSource();
666 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
667 geoSrc.fIndexSrc = kBuffer_GeometrySrcType;
668 geoSrc.fIndexBuffer = buffer;
669 buffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000670}
671
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000672void GrDrawTarget::resetVertexSource() {
673 this->releasePreviousVertexSource();
674 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
675 geoSrc.fVertexSrc = kNone_GeometrySrcType;
676}
677
678void GrDrawTarget::resetIndexSource() {
679 this->releasePreviousIndexSource();
680 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
681 geoSrc.fIndexSrc = kNone_GeometrySrcType;
682}
683
684void GrDrawTarget::pushGeometrySource() {
685 this->geometrySourceWillPush();
686 GeometrySrcState& newState = fGeoSrcStateStack.push_back();
687 newState.fIndexSrc = kNone_GeometrySrcType;
688 newState.fVertexSrc = kNone_GeometrySrcType;
689#if GR_DEBUG
690 newState.fVertexCount = ~0;
691 newState.fVertexBuffer = (GrVertexBuffer*)~0;
692 newState.fIndexCount = ~0;
693 newState.fIndexBuffer = (GrIndexBuffer*)~0;
694#endif
695}
696
697void GrDrawTarget::popGeometrySource() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000698 // if popping last element then pops are unbalanced with pushes
699 GrAssert(fGeoSrcStateStack.count() > 1);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000700
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000701 this->geometrySourceWillPop(fGeoSrcStateStack.fromBack(1));
702 this->releasePreviousVertexSource();
703 this->releasePreviousIndexSource();
704 fGeoSrcStateStack.pop_back();
705}
706
707////////////////////////////////////////////////////////////////////////////////
708
bsalomon@google.come8262622011-11-07 02:30:51 +0000709bool GrDrawTarget::checkDraw(GrPrimitiveType type, int startVertex,
710 int startIndex, int vertexCount,
711 int indexCount) const {
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000712 const GrDrawState& drawState = this->getDrawState();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000713#if GR_DEBUG
bsalomon@google.come8262622011-11-07 02:30:51 +0000714 const GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000715 int maxVertex = startVertex + vertexCount;
716 int maxValidVertex;
717 switch (geoSrc.fVertexSrc) {
718 case kNone_GeometrySrcType:
bsalomon@google.come8262622011-11-07 02:30:51 +0000719 GrCrash("Attempting to draw without vertex src.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000720 case kReserved_GeometrySrcType: // fallthrough
721 case kArray_GeometrySrcType:
722 maxValidVertex = geoSrc.fVertexCount;
723 break;
724 case kBuffer_GeometrySrcType:
bsalomon@google.comf3ccda72012-07-11 14:56:33 +0000725 maxValidVertex = geoSrc.fVertexBuffer->sizeInBytes() / VertexSize(geoSrc.fVertexLayout);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000726 break;
727 }
728 if (maxVertex > maxValidVertex) {
bsalomon@google.come8262622011-11-07 02:30:51 +0000729 GrCrash("Drawing outside valid vertex range.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000730 }
bsalomon@google.come8262622011-11-07 02:30:51 +0000731 if (indexCount > 0) {
732 int maxIndex = startIndex + indexCount;
733 int maxValidIndex;
734 switch (geoSrc.fIndexSrc) {
735 case kNone_GeometrySrcType:
736 GrCrash("Attempting to draw indexed geom without index src.");
737 case kReserved_GeometrySrcType: // fallthrough
738 case kArray_GeometrySrcType:
739 maxValidIndex = geoSrc.fIndexCount;
740 break;
741 case kBuffer_GeometrySrcType:
742 maxValidIndex = geoSrc.fIndexBuffer->sizeInBytes() / sizeof(uint16_t);
743 break;
744 }
745 if (maxIndex > maxValidIndex) {
746 GrCrash("Index reads outside valid index range.");
747 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000748 }
bsalomon@google.comb4725b42012-03-30 17:24:17 +0000749
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000750 GrAssert(NULL != drawState.getRenderTarget());
751 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
752 if (drawState.isStageEnabled(s)) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000753 const GrEffect* effect = drawState.getStage(s).getEffect();
bsalomon@google.com021fc732012-10-25 12:47:42 +0000754 int numTextures = effect->numTextures();
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000755 for (int t = 0; t < numTextures; ++t) {
bsalomon@google.com021fc732012-10-25 12:47:42 +0000756 GrTexture* texture = effect->texture(t);
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000757 GrAssert(texture->asRenderTarget() != drawState.getRenderTarget());
758 }
bsalomon@google.comb4725b42012-03-30 17:24:17 +0000759 }
760 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000761#endif
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000762 if (NULL == drawState.getRenderTarget()) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000763 return false;
764 }
bsalomon@google.come8262622011-11-07 02:30:51 +0000765 return true;
766}
767
768void GrDrawTarget::drawIndexed(GrPrimitiveType type, int startVertex,
769 int startIndex, int vertexCount,
770 int indexCount) {
771 if (indexCount > 0 &&
772 this->checkDraw(type, startVertex, startIndex,
773 vertexCount, indexCount)) {
bsalomon@google.com82145872011-08-23 14:32:40 +0000774 this->onDrawIndexed(type, startVertex, startIndex,
775 vertexCount, indexCount);
776 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000777}
778
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000779void GrDrawTarget::drawNonIndexed(GrPrimitiveType type,
780 int startVertex,
781 int vertexCount) {
bsalomon@google.come8262622011-11-07 02:30:51 +0000782 if (vertexCount > 0 &&
783 this->checkDraw(type, startVertex, -1, vertexCount, -1)) {
bsalomon@google.com82145872011-08-23 14:32:40 +0000784 this->onDrawNonIndexed(type, startVertex, vertexCount);
785 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000786}
787
sugoi@google.com12b4e272012-12-06 20:13:11 +0000788void GrDrawTarget::stencilPath(const GrPath* path, const SkStroke& stroke, SkPath::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000789 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000790 GrAssert(NULL != path);
bsalomon@google.comf6601872012-08-28 21:11:35 +0000791 GrAssert(fCaps.pathStencilingSupport());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000792 GrAssert(0 != stroke.getWidthIfStroked());
793 GrAssert(!SkPath::IsInverseFill(fill));
794 this->onStencilPath(path, stroke, fill);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000795}
796
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000797////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000798
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000799// Some blend modes allow folding a partial coverage value into the color's
800// alpha channel, while others will blend incorrectly.
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000801bool GrDrawTarget::canTweakAlphaForCoverage() const {
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000802 /**
803 * The fractional coverage is f
804 * The src and dst coeffs are Cs and Cd
805 * The dst and src colors are S and D
806 * We want the blend to compute: f*Cs*S + (f*Cd + (1-f))D
807 * By tweaking the source color's alpha we're replacing S with S'=fS. It's
808 * obvious that that first term will always be ok. The second term can be
809 * rearranged as [1-(1-Cd)f]D. By substituing in the various possbilities
810 * for Cd we find that only 1, ISA, and ISC produce the correct depth
811 * coeffecient in terms of S' and D.
812 */
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000813 GrBlendCoeff dstCoeff = this->getDrawState().getDstBlendCoeff();
bsalomon@google.com47059542012-06-06 20:51:20 +0000814 return kOne_GrBlendCoeff == dstCoeff ||
815 kISA_GrBlendCoeff == dstCoeff ||
816 kISC_GrBlendCoeff == dstCoeff;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000817}
818
bsalomon@google.come79c8152012-03-29 19:07:12 +0000819bool GrDrawTarget::srcAlphaWillBeOne(GrVertexLayout layout) const {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000820 const GrDrawState& drawState = this->getDrawState();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000821
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000822 // Check if per-vertex or constant color may have partial alpha
bsalomon@google.com471d4712011-08-23 15:45:25 +0000823 if ((layout & kColor_VertexLayoutBit) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000824 0xff != GrColorUnpackA(drawState.getColor())) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000825 return false;
826 }
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000827 // Check if color filter could introduce an alpha
828 // (TODO: Consider being more aggressive with regards to detecting 0xff
829 // final alpha from color filter).
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000830 if (SkXfermode::kDst_Mode != drawState.getColorFilterMode()) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000831 return false;
832 }
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000833 // Check if a color stage could create a partial alpha
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000834 for (int s = 0; s < drawState.getFirstCoverageStage(); ++s) {
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000835 if (this->isStageEnabled(s)) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000836 const GrEffect* effect = drawState.getStage(s).getEffect();
bsalomon@google.com021fc732012-10-25 12:47:42 +0000837 // FIXME: The param indicates whether the texture is opaque or not. However, the effect
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000838 // already controls its textures. It really needs to know whether the incoming color
839 // (from a uni, per-vertex colors, or previous stage) is opaque or not.
bsalomon@google.com021fc732012-10-25 12:47:42 +0000840 if (!effect->isOpaque(true)) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000841 return false;
842 }
843 }
844 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000845 return true;
846}
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000847
bsalomon@google.come79c8152012-03-29 19:07:12 +0000848namespace {
849GrVertexLayout default_blend_opts_vertex_layout() {
850 GrVertexLayout layout = 0;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000851 return layout;
852}
853}
854
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000855GrDrawTarget::BlendOptFlags
856GrDrawTarget::getBlendOpts(bool forceCoverage,
857 GrBlendCoeff* srcCoeff,
858 GrBlendCoeff* dstCoeff) const {
859
bsalomon@google.come79c8152012-03-29 19:07:12 +0000860 GrVertexLayout layout;
861 if (kNone_GeometrySrcType == this->getGeomSrc().fVertexSrc) {
862 layout = default_blend_opts_vertex_layout();
863 } else {
864 layout = this->getVertexLayout();
865 }
866
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000867 const GrDrawState& drawState = this->getDrawState();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000868
869 GrBlendCoeff bogusSrcCoeff, bogusDstCoeff;
870 if (NULL == srcCoeff) {
871 srcCoeff = &bogusSrcCoeff;
872 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000873 *srcCoeff = drawState.getSrcBlendCoeff();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000874
875 if (NULL == dstCoeff) {
876 dstCoeff = &bogusDstCoeff;
877 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000878 *dstCoeff = drawState.getDstBlendCoeff();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000879
880 // We don't ever expect source coeffecients to reference the source
bsalomon@google.com47059542012-06-06 20:51:20 +0000881 GrAssert(kSA_GrBlendCoeff != *srcCoeff &&
882 kISA_GrBlendCoeff != *srcCoeff &&
883 kSC_GrBlendCoeff != *srcCoeff &&
884 kISC_GrBlendCoeff != *srcCoeff);
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000885 // same for dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000886 GrAssert(kDA_GrBlendCoeff != *dstCoeff &&
887 kIDA_GrBlendCoeff != *dstCoeff &&
888 kDC_GrBlendCoeff != *dstCoeff &&
889 kIDC_GrBlendCoeff != *dstCoeff);
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000890
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000891 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000892 *srcCoeff = kZero_GrBlendCoeff;
893 *dstCoeff = kOne_GrBlendCoeff;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000894 }
895
bsalomon@google.come79c8152012-03-29 19:07:12 +0000896 bool srcAIsOne = this->srcAlphaWillBeOne(layout);
bsalomon@google.com47059542012-06-06 20:51:20 +0000897 bool dstCoeffIsOne = kOne_GrBlendCoeff == *dstCoeff ||
898 (kSA_GrBlendCoeff == *dstCoeff && srcAIsOne);
899 bool dstCoeffIsZero = kZero_GrBlendCoeff == *dstCoeff ||
900 (kISA_GrBlendCoeff == *dstCoeff && srcAIsOne);
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000901
902
903 // When coeffs are (0,1) there is no reason to draw at all, unless
904 // stenciling is enabled. Having color writes disabled is effectively
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000905 // (0,1). The same applies when coverage is known to be 0.
bsalomon@google.com47059542012-06-06 20:51:20 +0000906 if ((kZero_GrBlendCoeff == *srcCoeff && dstCoeffIsOne) ||
rmistry@google.comd6176b02012-08-23 18:14:13 +0000907 (!(layout & kCoverage_VertexLayoutBit) &&
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000908 0 == drawState.getCoverage())) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000909 if (drawState.getStencil().doesWrite()) {
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000910 return kDisableBlend_BlendOptFlag |
911 kEmitTransBlack_BlendOptFlag;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000912 } else {
913 return kSkipDraw_BlendOptFlag;
914 }
915 }
916
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000917 // check for coverage due to constant coverage, per-vertex coverage,
bsalomon@google.com021fc732012-10-25 12:47:42 +0000918 // edge aa or coverage stage
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000919 bool hasCoverage = forceCoverage ||
rmistry@google.comd6176b02012-08-23 18:14:13 +0000920 0xffffffff != drawState.getCoverage() ||
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000921 (layout & kCoverage_VertexLayoutBit) ||
922 (layout & kEdge_VertexLayoutBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000923 for (int s = drawState.getFirstCoverageStage();
tomhudson@google.com93813632011-10-27 20:21:16 +0000924 !hasCoverage && s < GrDrawState::kNumStages;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000925 ++s) {
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000926 if (this->isStageEnabled(s)) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000927 hasCoverage = true;
928 }
929 }
930
931 // if we don't have coverage we can check whether the dst
932 // has to read at all. If not, we'll disable blending.
933 if (!hasCoverage) {
934 if (dstCoeffIsZero) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000935 if (kOne_GrBlendCoeff == *srcCoeff) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000936 // if there is no coverage and coeffs are (1,0) then we
937 // won't need to read the dst at all, it gets replaced by src
938 return kDisableBlend_BlendOptFlag;
bsalomon@google.com47059542012-06-06 20:51:20 +0000939 } else if (kZero_GrBlendCoeff == *srcCoeff) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000940 // if the op is "clear" then we don't need to emit a color
941 // or blend, just write transparent black into the dst.
bsalomon@google.com47059542012-06-06 20:51:20 +0000942 *srcCoeff = kOne_GrBlendCoeff;
943 *dstCoeff = kZero_GrBlendCoeff;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000944 return kDisableBlend_BlendOptFlag |
945 kEmitTransBlack_BlendOptFlag;
946 }
947 }
948 } else {
949 // check whether coverage can be safely rolled into alpha
950 // of if we can skip color computation and just emit coverage
951 if (this->canTweakAlphaForCoverage()) {
952 return kCoverageAsAlpha_BlendOptFlag;
953 }
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000954 if (dstCoeffIsZero) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000955 if (kZero_GrBlendCoeff == *srcCoeff) {
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000956 // the source color is not included in the blend
957 // the dst coeff is effectively zero so blend works out to:
958 // (c)(0)D + (1-c)D = (1-c)D.
bsalomon@google.com47059542012-06-06 20:51:20 +0000959 *dstCoeff = kISA_GrBlendCoeff;
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000960 return kEmitCoverage_BlendOptFlag;
961 } else if (srcAIsOne) {
962 // the dst coeff is effectively zero so blend works out to:
963 // cS + (c)(0)D + (1-c)D = cS + (1-c)D.
rmistry@google.comd6176b02012-08-23 18:14:13 +0000964 // If Sa is 1 then we can replace Sa with c
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000965 // and set dst coeff to 1-Sa.
bsalomon@google.com47059542012-06-06 20:51:20 +0000966 *dstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000967 return kCoverageAsAlpha_BlendOptFlag;
968 }
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000969 } else if (dstCoeffIsOne) {
970 // the dst coeff is effectively one so blend works out to:
971 // cS + (c)(1)D + (1-c)D = cS + D.
bsalomon@google.com47059542012-06-06 20:51:20 +0000972 *dstCoeff = kOne_GrBlendCoeff;
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000973 return kCoverageAsAlpha_BlendOptFlag;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000974 }
975 }
976 return kNone_BlendOpt;
977}
978
979bool GrDrawTarget::willUseHWAALines() const {
bsalomon@google.com471d4712011-08-23 15:45:25 +0000980 // there is a conflict between using smooth lines and our use of
981 // premultiplied alpha. Smooth lines tweak the incoming alpha value
982 // but not in a premul-alpha way. So we only use them when our alpha
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000983 // is 0xff and tweaking the color for partial coverage is OK
bsalomon@google.comf6601872012-08-28 21:11:35 +0000984 if (!fCaps.hwAALineSupport() ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000985 !this->getDrawState().isHWAntialiasState()) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000986 return false;
987 }
988 BlendOptFlags opts = this->getBlendOpts();
989 return (kDisableBlend_BlendOptFlag & opts) &&
990 (kCoverageAsAlpha_BlendOptFlag & opts);
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000991}
992
993bool GrDrawTarget::canApplyCoverage() const {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000994 // we can correctly apply coverage if a) we have dual source blending
995 // or b) one of our blend optimizations applies.
bsalomon@google.comf6601872012-08-28 21:11:35 +0000996 return this->getCaps().dualSourceBlendingSupport() ||
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000997 kNone_BlendOpt != this->getBlendOpts(true);
bsalomon@google.com471d4712011-08-23 15:45:25 +0000998}
999
bsalomon@google.com934c5702012-03-20 21:17:58 +00001000////////////////////////////////////////////////////////////////////////////////
1001
1002void GrDrawTarget::drawIndexedInstances(GrPrimitiveType type,
1003 int instanceCount,
1004 int verticesPerInstance,
1005 int indicesPerInstance) {
1006 if (!verticesPerInstance || !indicesPerInstance) {
1007 return;
1008 }
1009
1010 int instancesPerDraw = this->indexCountInCurrentSource() /
1011 indicesPerInstance;
1012 if (!instancesPerDraw) {
1013 return;
1014 }
1015
1016 instancesPerDraw = GrMin(instanceCount, instancesPerDraw);
1017 int startVertex = 0;
1018 while (instanceCount) {
1019 this->drawIndexed(type,
1020 startVertex,
1021 0,
1022 verticesPerInstance * instancesPerDraw,
1023 indicesPerInstance * instancesPerDraw);
1024 startVertex += verticesPerInstance;
1025 instanceCount -= instancesPerDraw;
1026 }
1027}
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001028
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001029////////////////////////////////////////////////////////////////////////////////
1030
rmistry@google.comd6176b02012-08-23 18:14:13 +00001031void GrDrawTarget::drawRect(const GrRect& rect,
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001032 const SkMatrix* matrix,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001033 const GrRect* srcRects[],
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001034 const SkMatrix* srcMatrices[]) {
bsalomon@google.come3d32162012-07-20 13:37:06 +00001035 GrVertexLayout layout = GetRectVertexLayout(srcRects);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001036
1037 AutoReleaseGeometry geo(this, layout, 4, 0);
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001038 if (!geo.succeeded()) {
1039 GrPrintf("Failed to get space for vertices!\n");
1040 return;
1041 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001042
rmistry@google.comd6176b02012-08-23 18:14:13 +00001043 SetRectVertices(rect, matrix, srcRects,
robertphillips@google.com8b129aa2012-10-05 15:37:00 +00001044 srcMatrices, SK_ColorBLACK, layout, geo.vertices());
skia.committer@gmail.coma18ed032012-10-06 02:05:26 +00001045
bsalomon@google.com47059542012-06-06 20:51:20 +00001046 drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001047}
1048
bsalomon@google.come3d32162012-07-20 13:37:06 +00001049GrVertexLayout GrDrawTarget::GetRectVertexLayout(const GrRect* srcRects[]) {
1050 if (NULL == srcRects) {
1051 return 0;
1052 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001053
bsalomon@google.come3d32162012-07-20 13:37:06 +00001054 GrVertexLayout layout = 0;
tomhudson@google.com93813632011-10-27 20:21:16 +00001055 for (int i = 0; i < GrDrawState::kNumStages; ++i) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001056 int numTC = 0;
bsalomon@google.come3d32162012-07-20 13:37:06 +00001057 if (NULL != srcRects[i]) {
1058 layout |= StageTexCoordVertexLayoutBit(i, numTC);
1059 ++numTC;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001060 }
1061 }
1062 return layout;
1063}
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001064
skia.committer@gmail.coma18ed032012-10-06 02:05:26 +00001065// This method fills int the four vertices for drawing 'rect'.
robertphillips@google.com8b129aa2012-10-05 15:37:00 +00001066// matrix - is applied to each vertex
1067// srcRects - provide the uvs for each vertex
1068// srcMatrices - are applied to the corresponding 'srcRect'
1069// color - vertex color (replicated in each vertex)
1070// layout - specifies which uvs and/or color are present
1071// vertices - storage for the resulting vertices
1072// Note: the color parameter will only be used when kColor_VertexLayoutBit
1073// is present in 'layout'
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001074void GrDrawTarget::SetRectVertices(const GrRect& rect,
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001075 const SkMatrix* matrix,
rmistry@google.comd6176b02012-08-23 18:14:13 +00001076 const GrRect* srcRects[],
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001077 const SkMatrix* srcMatrices[],
robertphillips@google.com8b129aa2012-10-05 15:37:00 +00001078 GrColor color,
rmistry@google.comd6176b02012-08-23 18:14:13 +00001079 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001080 void* vertices) {
1081#if GR_DEBUG
1082 // check that the layout and srcRects agree
tomhudson@google.com93813632011-10-27 20:21:16 +00001083 for (int i = 0; i < GrDrawState::kNumStages; ++i) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001084 if (VertexTexCoordsForStage(i, layout) >= 0) {
1085 GR_DEBUGASSERT(NULL != srcRects && NULL != srcRects[i]);
1086 } else {
1087 GR_DEBUGASSERT(NULL == srcRects || NULL == srcRects[i]);
1088 }
1089 }
1090#endif
1091
robertphillips@google.com8b129aa2012-10-05 15:37:00 +00001092 int stageOffsets[GrDrawState::kNumStages], colorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001093 int vsize = VertexSizeAndOffsetsByStage(layout, stageOffsets,
robertphillips@google.com8b129aa2012-10-05 15:37:00 +00001094 &colorOffset, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001095
bsalomon@google.coma3108262011-10-10 14:08:47 +00001096 GrTCast<GrPoint*>(vertices)->setRectFan(rect.fLeft, rect.fTop,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001097 rect.fRight, rect.fBottom,
1098 vsize);
1099 if (NULL != matrix) {
1100 matrix->mapPointsWithStride(GrTCast<GrPoint*>(vertices), vsize, 4);
1101 }
1102
tomhudson@google.com93813632011-10-27 20:21:16 +00001103 for (int i = 0; i < GrDrawState::kNumStages; ++i) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001104 if (stageOffsets[i] > 0) {
bsalomon@google.coma3108262011-10-10 14:08:47 +00001105 GrPoint* coords = GrTCast<GrPoint*>(GrTCast<intptr_t>(vertices) +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001106 stageOffsets[i]);
1107 coords->setRectFan(srcRects[i]->fLeft, srcRects[i]->fTop,
bsalomon@google.coma3108262011-10-10 14:08:47 +00001108 srcRects[i]->fRight, srcRects[i]->fBottom,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001109 vsize);
1110 if (NULL != srcMatrices && NULL != srcMatrices[i]) {
1111 srcMatrices[i]->mapPointsWithStride(coords, vsize, 4);
1112 }
1113 }
1114 }
robertphillips@google.com8b129aa2012-10-05 15:37:00 +00001115
1116 if (layout & kColor_VertexLayoutBit) {
1117
1118 GrColor* vertCol = GrTCast<GrColor*>(GrTCast<intptr_t>(vertices) + colorOffset);
1119
1120 for (int i = 0; i < 4; ++i) {
1121 *vertCol = color;
1122 vertCol = (GrColor*) ((intptr_t) vertCol + vsize);
1123 }
1124 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001125}
1126
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001127////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001128
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001129GrDrawTarget::AutoStateRestore::AutoStateRestore() {
1130 fDrawTarget = NULL;
1131}
reed@google.comac10a2d2010-12-22 21:39:39 +00001132
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001133GrDrawTarget::AutoStateRestore::AutoStateRestore(GrDrawTarget* target,
1134 ASRInit init) {
1135 fDrawTarget = NULL;
1136 this->set(target, init);
reed@google.comac10a2d2010-12-22 21:39:39 +00001137}
1138
1139GrDrawTarget::AutoStateRestore::~AutoStateRestore() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001140 if (NULL != fDrawTarget) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001141 fDrawTarget->setDrawState(fSavedState);
1142 fSavedState->unref();
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001143 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001144}
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001145
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001146void GrDrawTarget::AutoStateRestore::set(GrDrawTarget* target, ASRInit init) {
1147 GrAssert(NULL == fDrawTarget);
1148 fDrawTarget = target;
1149 fSavedState = target->drawState();
1150 GrAssert(fSavedState);
1151 fSavedState->ref();
1152 if (kReset_ASRInit == init) {
1153 // calls the default cons
1154 fTempState.init();
1155 } else {
1156 GrAssert(kPreserve_ASRInit == init);
1157 // calls the copy cons
1158 fTempState.set(*fSavedState);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001159 }
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001160 target->setDrawState(fTempState.get());
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001161}
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001162
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001163////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001164
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001165GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry(
1166 GrDrawTarget* target,
1167 GrVertexLayout vertexLayout,
1168 int vertexCount,
1169 int indexCount) {
1170 fTarget = NULL;
1171 this->set(target, vertexLayout, vertexCount, indexCount);
1172}
rmistry@google.comd6176b02012-08-23 18:14:13 +00001173
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001174GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry() {
1175 fTarget = NULL;
1176}
1177
1178GrDrawTarget::AutoReleaseGeometry::~AutoReleaseGeometry() {
1179 this->reset();
1180}
1181
1182bool GrDrawTarget::AutoReleaseGeometry::set(GrDrawTarget* target,
1183 GrVertexLayout vertexLayout,
1184 int vertexCount,
1185 int indexCount) {
1186 this->reset();
1187 fTarget = target;
1188 bool success = true;
1189 if (NULL != fTarget) {
1190 fTarget = target;
bsalomon@google.come3d70952012-03-13 12:40:53 +00001191 success = target->reserveVertexAndIndexSpace(vertexLayout,
1192 vertexCount,
1193 indexCount,
1194 &fVertices,
1195 &fIndices);
1196 if (!success) {
1197 fTarget = NULL;
1198 this->reset();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001199 }
1200 }
1201 GrAssert(success == (NULL != fTarget));
1202 return success;
1203}
1204
1205void GrDrawTarget::AutoReleaseGeometry::reset() {
1206 if (NULL != fTarget) {
1207 if (NULL != fVertices) {
1208 fTarget->resetVertexSource();
1209 }
1210 if (NULL != fIndices) {
1211 fTarget->resetIndexSource();
1212 }
1213 fTarget = NULL;
1214 }
bsalomon@google.comcb0c5ab2011-06-29 17:48:17 +00001215 fVertices = NULL;
1216 fIndices = NULL;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001217}
1218
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001219void GrDrawTarget::Caps::print() const {
1220 static const char* gNY[] = {"NO", "YES"};
bsalomon@google.comf6601872012-08-28 21:11:35 +00001221 GrPrintf("8 Bit Palette Support : %s\n", gNY[fInternals.f8BitPaletteSupport]);
1222 GrPrintf("NPOT Texture Tile Support : %s\n", gNY[fInternals.fNPOTTextureTileSupport]);
1223 GrPrintf("Two Sided Stencil Support : %s\n", gNY[fInternals.fTwoSidedStencilSupport]);
1224 GrPrintf("Stencil Wrap Ops Support : %s\n", gNY[fInternals.fStencilWrapOpsSupport]);
1225 GrPrintf("HW AA Lines Support : %s\n", gNY[fInternals.fHWAALineSupport]);
1226 GrPrintf("Shader Derivative Support : %s\n", gNY[fInternals.fShaderDerivativeSupport]);
1227 GrPrintf("Geometry Shader Support : %s\n", gNY[fInternals.fGeometryShaderSupport]);
1228 GrPrintf("FSAA Support : %s\n", gNY[fInternals.fFSAASupport]);
1229 GrPrintf("Dual Source Blending Support: %s\n", gNY[fInternals.fDualSourceBlendingSupport]);
1230 GrPrintf("Buffer Lock Support : %s\n", gNY[fInternals.fBufferLockSupport]);
1231 GrPrintf("Path Stenciling Support : %s\n", gNY[fInternals.fPathStencilingSupport]);
1232 GrPrintf("Max Texture Size : %d\n", fInternals.fMaxTextureSize);
1233 GrPrintf("Max Render Target Size : %d\n", fInternals.fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001234}