blob: 0fa3b8d2983758883de6135e79e50fba9aea59d2 [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
bsalomon@google.coma47a48d2011-04-26 20:22:11 +000020struct GrContext::OffscreenRecord {
bsalomon@google.com8295dc12011-05-02 12:53:34 +000021 OffscreenRecord() { fEntry0 = NULL; fEntry1 = NULL; }
22 ~OffscreenRecord() { GrAssert(NULL == fEntry0 && NULL == fEntry1); }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +000023
bsalomon@google.com8295dc12011-05-02 12:53:34 +000024 GrTextureEntry* fEntry0;
25 GrTextureEntry* fEntry1;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +000026 GrDrawTarget::SavedDrawState fSavedState;
27};
28
bsalomon@google.com27847de2011-02-22 20:59:41 +000029template <typename POS_SRC, typename TEX_SRC,
30 typename COL_SRC, typename IDX_SRC>
31inline void GrContext::drawCustomVertices(const GrPaint& paint,
32 GrPrimitiveType primitiveType,
33 const POS_SRC& posSrc,
34 const TEX_SRC* texCoordSrc,
35 const COL_SRC* colorSrc,
36 const IDX_SRC* idxSrc) {
37
38 GrVertexLayout layout = 0;
39
40 GrDrawTarget::AutoReleaseGeometry geo;
41
42 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
43
44 if (NULL != paint.getTexture()) {
45 if (NULL != texCoordSrc) {
46 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
47 } else {
48 layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(0);
49 }
50 }
51
52 if (NULL != colorSrc) {
53 layout |= GrDrawTarget::kColor_VertexLayoutBit;
54 }
55
56 int vertexCount = posSrc.count();
57 int indexCount = (NULL != idxSrc) ? idxSrc->count() : 0;
58
59 if (!geo.set(target, layout, vertexCount, indexCount)) {
60 GrPrintf("Failed to get space for vertices!");
61 return;
62 }
63
64 int texOffsets[GrDrawTarget::kMaxTexCoords];
65 int colorOffset;
66 int vsize = GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
67 texOffsets,
68 &colorOffset);
69 void* curVertex = geo.vertices();
70
71 for (int i = 0; i < vertexCount; ++i) {
72 posSrc.writeValue(i, (GrPoint*)curVertex);
73
74 if (texOffsets[0] > 0) {
75 texCoordSrc->writeValue(i, (GrPoint*)((intptr_t)curVertex + texOffsets[0]));
76 }
77 if (colorOffset > 0) {
78 colorSrc->writeValue(i, (GrColor*)((intptr_t)curVertex + colorOffset));
79 }
80 curVertex = (void*)((intptr_t)curVertex + vsize);
81 }
82
83 uint16_t* indices = (uint16_t*) geo.indices();
84 for (int i = 0; i < indexCount; ++i) {
85 idxSrc->writeValue(i, indices + i);
86 }
87
bsalomon@google.com8295dc12011-05-02 12:53:34 +000088 bool doAA = false;
89 OffscreenRecord record;
90 GrIRect bounds;
91
92 if (-1 == texOffsets[0] && -1 == colorOffset &&
93 this->doOffscreenAA(target, paint, GrIsPrimTypeLines(primitiveType))) {
94 GrRect b;
95 b.setBounds(geo.positions(), vertexCount);
96 target->getViewMatrix().mapRect(&b);
97 b.roundOut(&bounds);
98 if (this->setupOffscreenAAPass1(target, false, bounds, &record)) {
99 doAA = true;
100 }
101 }
102
bsalomon@google.com27847de2011-02-22 20:59:41 +0000103 if (NULL == idxSrc) {
104 target->drawNonIndexed(primitiveType, 0, vertexCount);
105 } else {
106 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
107 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000108
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000109 if (doAA) {
110 geo.set(NULL, 0, 0, 0); // have to release geom before can draw again
111 this->offscreenAAPass2(target, paint, bounds, &record);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000112 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000113}
114
115class GrNullTexCoordSource {
116public:
117 void writeValue(int i, GrPoint* dstCoord) const { GrAssert(false); }
118};
119
120class GrNullColorSource {
121public:
122 void writeValue(int i, GrColor* dstColor) const { GrAssert(false); }
123};
124
125class GrNullIndexSource {
126public:
127 void writeValue(int i, uint16_t* dstIndex) const { GrAssert(false); }
128 int count() const { GrAssert(false); return 0; }
129};
130
131template <typename POS_SRC>
132inline void GrContext::drawCustomVertices(const GrPaint& paint,
133 GrPrimitiveType primitiveType,
134 const POS_SRC& posSrc) {
135 this->drawCustomVertices<POS_SRC,
136 GrNullTexCoordSource,
137 GrNullColorSource,
138 GrNullIndexSource>(paint, primitiveType, posSrc,
139 NULL, NULL, NULL);
140}
141
142template <typename POS_SRC, typename TEX_SRC>
143inline void GrContext::drawCustomVertices(const GrPaint& paint,
144 GrPrimitiveType primitiveType,
145 const POS_SRC& posSrc,
146 const TEX_SRC* texCoordSrc) {
147 this->drawCustomVertices<POS_SRC, TEX_SRC,
148 GrNullColorSource,
149 GrNullIndexSource>(paint, primitiveType, posSrc,
150 texCoordSrc, NULL, NULL);
151}
152
153template <typename POS_SRC, typename TEX_SRC, typename COL_SRC>
154inline void GrContext::drawCustomVertices(const GrPaint& paint,
155 GrPrimitiveType primitiveType,
156 const POS_SRC& posSrc,
157 const TEX_SRC* texCoordSrc,
158 const COL_SRC* colorSrc) {
159 drawCustomVertices<POS_SRC, TEX_SRC, COL_SRC,
160 GrNullIndexSource>(paint, primitiveType, posSrc,
161 texCoordSrc, colorSrc, NULL);
162}
163
164#endif