blob: df16c8cb41c71e73ab748038df18a83f0bf20264 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com27847de2011-02-22 20:59:41 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 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.
bsalomon@google.com27847de2011-02-22 20:59:41 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.com27847de2011-02-22 20:59:41 +000010#ifndef GrContext_impl_DEFINED
11#define GrContext_impl_DEFINED
12
13template <typename POS_SRC, typename TEX_SRC,
14 typename COL_SRC, typename IDX_SRC>
15inline void GrContext::drawCustomVertices(const GrPaint& paint,
16 GrPrimitiveType primitiveType,
17 const POS_SRC& posSrc,
18 const TEX_SRC* texCoordSrc,
19 const COL_SRC* colorSrc,
20 const IDX_SRC* idxSrc) {
21
bsalomon@google.com27847de2011-02-22 20:59:41 +000022 GrDrawTarget::AutoReleaseGeometry geo;
23
24 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
25
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000026 bool hasTexCoords[GrPaint::kTotalStages] = {
27 NULL != texCoordSrc, // texCoordSrc provides explicit stage 0 coords
28 0 // remaining stages use positions
29 };
30 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, hasTexCoords);
bsalomon@google.com27847de2011-02-22 20:59:41 +000031
32 if (NULL != colorSrc) {
33 layout |= GrDrawTarget::kColor_VertexLayoutBit;
34 }
35
36 int vertexCount = posSrc.count();
37 int indexCount = (NULL != idxSrc) ? idxSrc->count() : 0;
38
39 if (!geo.set(target, layout, vertexCount, indexCount)) {
40 GrPrintf("Failed to get space for vertices!");
41 return;
42 }
43
44 int texOffsets[GrDrawTarget::kMaxTexCoords];
45 int colorOffset;
46 int vsize = GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
47 texOffsets,
48 &colorOffset);
49 void* curVertex = geo.vertices();
50
51 for (int i = 0; i < vertexCount; ++i) {
52 posSrc.writeValue(i, (GrPoint*)curVertex);
53
54 if (texOffsets[0] > 0) {
55 texCoordSrc->writeValue(i, (GrPoint*)((intptr_t)curVertex + texOffsets[0]));
56 }
57 if (colorOffset > 0) {
58 colorSrc->writeValue(i, (GrColor*)((intptr_t)curVertex + colorOffset));
59 }
60 curVertex = (void*)((intptr_t)curVertex + vsize);
61 }
62
63 uint16_t* indices = (uint16_t*) geo.indices();
64 for (int i = 0; i < indexCount; ++i) {
65 idxSrc->writeValue(i, indices + i);
66 }
67
bsalomon@google.com91958362011-06-13 17:58:13 +000068 // we don't currently apply offscreen AA to this path. Need improved
69 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.com8295dc12011-05-02 12:53:34 +000070
bsalomon@google.com27847de2011-02-22 20:59:41 +000071 if (NULL == idxSrc) {
72 target->drawNonIndexed(primitiveType, 0, vertexCount);
73 } else {
74 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
75 }
76}
77
78class GrNullTexCoordSource {
79public:
80 void writeValue(int i, GrPoint* dstCoord) const { GrAssert(false); }
81};
82
83class GrNullColorSource {
84public:
85 void writeValue(int i, GrColor* dstColor) const { GrAssert(false); }
86};
87
88class GrNullIndexSource {
89public:
90 void writeValue(int i, uint16_t* dstIndex) const { GrAssert(false); }
91 int count() const { GrAssert(false); return 0; }
92};
93
94template <typename POS_SRC>
95inline void GrContext::drawCustomVertices(const GrPaint& paint,
96 GrPrimitiveType primitiveType,
97 const POS_SRC& posSrc) {
98 this->drawCustomVertices<POS_SRC,
99 GrNullTexCoordSource,
100 GrNullColorSource,
101 GrNullIndexSource>(paint, primitiveType, posSrc,
102 NULL, NULL, NULL);
103}
104
105template <typename POS_SRC, typename TEX_SRC>
106inline void GrContext::drawCustomVertices(const GrPaint& paint,
107 GrPrimitiveType primitiveType,
108 const POS_SRC& posSrc,
109 const TEX_SRC* texCoordSrc) {
110 this->drawCustomVertices<POS_SRC, TEX_SRC,
111 GrNullColorSource,
112 GrNullIndexSource>(paint, primitiveType, posSrc,
113 texCoordSrc, NULL, NULL);
114}
115
116template <typename POS_SRC, typename TEX_SRC, typename COL_SRC>
117inline void GrContext::drawCustomVertices(const GrPaint& paint,
118 GrPrimitiveType primitiveType,
119 const POS_SRC& posSrc,
120 const TEX_SRC* texCoordSrc,
121 const COL_SRC* colorSrc) {
122 drawCustomVertices<POS_SRC, TEX_SRC, COL_SRC,
123 GrNullIndexSource>(paint, primitiveType, posSrc,
124 texCoordSrc, colorSrc, NULL);
125}
126
127#endif