blob: b33d0b4d4fecc91dae07057b21f02a981fb1d075 [file] [log] [blame]
bsalomon@google.com27847de2011-02-22 20:59:41 +00001/*
2 Copyright 2011 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17#ifndef GrContext_impl_DEFINED
18#define GrContext_impl_DEFINED
19
20template <typename POS_SRC, typename TEX_SRC,
21 typename COL_SRC, typename IDX_SRC>
22inline void GrContext::drawCustomVertices(const GrPaint& paint,
23 GrPrimitiveType primitiveType,
24 const POS_SRC& posSrc,
25 const TEX_SRC* texCoordSrc,
26 const COL_SRC* colorSrc,
27 const IDX_SRC* idxSrc) {
28
bsalomon@google.com27847de2011-02-22 20:59:41 +000029 GrDrawTarget::AutoReleaseGeometry geo;
30
31 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
32
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000033 bool hasTexCoords[GrPaint::kTotalStages] = {
34 NULL != texCoordSrc, // texCoordSrc provides explicit stage 0 coords
35 0 // remaining stages use positions
36 };
37 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, hasTexCoords);
bsalomon@google.com27847de2011-02-22 20:59:41 +000038
39 if (NULL != colorSrc) {
40 layout |= GrDrawTarget::kColor_VertexLayoutBit;
41 }
42
43 int vertexCount = posSrc.count();
44 int indexCount = (NULL != idxSrc) ? idxSrc->count() : 0;
45
46 if (!geo.set(target, layout, vertexCount, indexCount)) {
47 GrPrintf("Failed to get space for vertices!");
48 return;
49 }
50
51 int texOffsets[GrDrawTarget::kMaxTexCoords];
52 int colorOffset;
53 int vsize = GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
54 texOffsets,
55 &colorOffset);
56 void* curVertex = geo.vertices();
57
58 for (int i = 0; i < vertexCount; ++i) {
59 posSrc.writeValue(i, (GrPoint*)curVertex);
60
61 if (texOffsets[0] > 0) {
62 texCoordSrc->writeValue(i, (GrPoint*)((intptr_t)curVertex + texOffsets[0]));
63 }
64 if (colorOffset > 0) {
65 colorSrc->writeValue(i, (GrColor*)((intptr_t)curVertex + colorOffset));
66 }
67 curVertex = (void*)((intptr_t)curVertex + vsize);
68 }
69
70 uint16_t* indices = (uint16_t*) geo.indices();
71 for (int i = 0; i < indexCount; ++i) {
72 idxSrc->writeValue(i, indices + i);
73 }
74
bsalomon@google.com91958362011-06-13 17:58:13 +000075 // we don't currently apply offscreen AA to this path. Need improved
76 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.com8295dc12011-05-02 12:53:34 +000077
bsalomon@google.com27847de2011-02-22 20:59:41 +000078 if (NULL == idxSrc) {
79 target->drawNonIndexed(primitiveType, 0, vertexCount);
80 } else {
81 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
82 }
83}
84
85class GrNullTexCoordSource {
86public:
87 void writeValue(int i, GrPoint* dstCoord) const { GrAssert(false); }
88};
89
90class GrNullColorSource {
91public:
92 void writeValue(int i, GrColor* dstColor) const { GrAssert(false); }
93};
94
95class GrNullIndexSource {
96public:
97 void writeValue(int i, uint16_t* dstIndex) const { GrAssert(false); }
98 int count() const { GrAssert(false); return 0; }
99};
100
101template <typename POS_SRC>
102inline void GrContext::drawCustomVertices(const GrPaint& paint,
103 GrPrimitiveType primitiveType,
104 const POS_SRC& posSrc) {
105 this->drawCustomVertices<POS_SRC,
106 GrNullTexCoordSource,
107 GrNullColorSource,
108 GrNullIndexSource>(paint, primitiveType, posSrc,
109 NULL, NULL, NULL);
110}
111
112template <typename POS_SRC, typename TEX_SRC>
113inline void GrContext::drawCustomVertices(const GrPaint& paint,
114 GrPrimitiveType primitiveType,
115 const POS_SRC& posSrc,
116 const TEX_SRC* texCoordSrc) {
117 this->drawCustomVertices<POS_SRC, TEX_SRC,
118 GrNullColorSource,
119 GrNullIndexSource>(paint, primitiveType, posSrc,
120 texCoordSrc, NULL, NULL);
121}
122
123template <typename POS_SRC, typename TEX_SRC, typename COL_SRC>
124inline void GrContext::drawCustomVertices(const GrPaint& paint,
125 GrPrimitiveType primitiveType,
126 const POS_SRC& posSrc,
127 const TEX_SRC* texCoordSrc,
128 const COL_SRC* colorSrc) {
129 drawCustomVertices<POS_SRC, TEX_SRC, COL_SRC,
130 GrNullIndexSource>(paint, primitiveType, posSrc,
131 texCoordSrc, colorSrc, NULL);
132}
133
134#endif