blob: 67e4f1401066bdc2dedb84e66748a01ece5f9b70 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
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#include "rsContext.h"
18
Jason Sams565ac362009-06-03 16:04:54 -070019#include <GLES/gl.h>
20
Jason Sams326e0dd2009-05-22 14:03:28 -070021using namespace android;
22using namespace android::renderscript;
23
Jason Sams326e0dd2009-05-22 14:03:28 -070024
Jason Samse514b452009-09-25 14:51:22 -070025Element::Element(Context *rsc) : ObjectBase(rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -070026{
Jason Samsf2649a92009-09-25 16:37:33 -070027 mAllocFile = __FILE__;
28 mAllocLine = __LINE__;
Jason Sams326e0dd2009-05-22 14:03:28 -070029 mComponents = NULL;
30 mComponentCount = 0;
31}
32
Jason Samse514b452009-09-25 14:51:22 -070033Element::Element(Context *rsc, uint32_t count) : ObjectBase(rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -070034{
Jason Samsf2649a92009-09-25 16:37:33 -070035 mAllocFile = __FILE__;
36 mAllocLine = __LINE__;
Jason Sams326e0dd2009-05-22 14:03:28 -070037 mComponents = new ObjectBaseRef<Component> [count];
38 mComponentCount = count;
39}
40
41Element::~Element()
42{
43 clear();
44}
45
46void Element::clear()
47{
48 delete [] mComponents;
49 mComponents = NULL;
50 mComponentCount = 0;
51}
52
53void Element::setComponent(uint32_t idx, Component *c)
54{
55 rsAssert(!mComponents[idx].get());
56 rsAssert(idx < mComponentCount);
57 mComponents[idx].set(c);
Jason Samse12c1c52009-09-27 17:50:38 -070058
59// Fixme: This should probably not be here
Jason Sams9397e302009-08-27 20:23:34 -070060 c->incUserRef();
Jason Sams326e0dd2009-05-22 14:03:28 -070061}
62
63
64size_t Element::getSizeBits() const
65{
66 size_t total = 0;
67 for (size_t ct=0; ct < mComponentCount; ct++) {
68 total += mComponents[ct]->getBits();
69 }
70 return total;
71}
72
73size_t Element::getComponentOffsetBits(uint32_t componentNumber) const
74{
75 size_t offset = 0;
76 for (uint32_t ct = 0; ct < componentNumber; ct++) {
77 offset += mComponents[ct]->getBits();
78 }
79 return offset;
80}
81
Jason Sams565ac362009-06-03 16:04:54 -070082uint32_t Element::getGLType() const
83{
84 int bits[4];
85
86 if (mComponentCount > 4) {
87 return 0;
88 }
89
90 for (uint32_t ct=0; ct < mComponentCount; ct++) {
91 bits[ct] = mComponents[ct]->getBits();
92 if (mComponents[ct]->getType() != Component::UNSIGNED) {
93 return 0;
94 }
95 if (!mComponents[ct]->getIsNormalized()) {
96 return 0;
97 }
98 }
99
100 switch(mComponentCount) {
101 case 1:
102 if (bits[0] == 8) {
103 return GL_UNSIGNED_BYTE;
104 }
105 return 0;
106 case 2:
107 if ((bits[0] == 8) &&
108 (bits[1] == 8)) {
109 return GL_UNSIGNED_BYTE;
110 }
111 return 0;
112 case 3:
113 if ((bits[0] == 8) &&
114 (bits[1] == 8) &&
115 (bits[2] == 8)) {
116 return GL_UNSIGNED_BYTE;
117 }
118 if ((bits[0] == 5) &&
119 (bits[1] == 6) &&
120 (bits[2] == 5)) {
121 return GL_UNSIGNED_SHORT_5_6_5;
122 }
123 return 0;
124 case 4:
125 if ((bits[0] == 8) &&
126 (bits[1] == 8) &&
127 (bits[2] == 8) &&
128 (bits[3] == 8)) {
129 return GL_UNSIGNED_BYTE;
130 }
131 if ((bits[0] == 4) &&
132 (bits[1] == 4) &&
133 (bits[2] == 4) &&
134 (bits[3] == 4)) {
135 return GL_UNSIGNED_SHORT_4_4_4_4;
136 }
137 if ((bits[0] == 5) &&
138 (bits[1] == 5) &&
139 (bits[2] == 5) &&
140 (bits[3] == 1)) {
141 return GL_UNSIGNED_SHORT_5_5_5_1;
142 }
143 }
144 return 0;
145}
146
147uint32_t Element::getGLFormat() const
148{
149 switch(mComponentCount) {
150 case 1:
151 if (mComponents[0]->getKind() == Component::ALPHA) {
152 return GL_ALPHA;
153 }
154 if (mComponents[0]->getKind() == Component::LUMINANCE) {
155 return GL_LUMINANCE;
156 }
157 break;
158 case 2:
159 if ((mComponents[0]->getKind() == Component::LUMINANCE) &&
160 (mComponents[1]->getKind() == Component::ALPHA)) {
161 return GL_LUMINANCE_ALPHA;
162 }
163 break;
164 case 3:
165 if ((mComponents[0]->getKind() == Component::RED) &&
166 (mComponents[1]->getKind() == Component::GREEN) &&
167 (mComponents[2]->getKind() == Component::BLUE)) {
168 return GL_RGB;
169 }
170 break;
171 case 4:
172 if ((mComponents[0]->getKind() == Component::RED) &&
173 (mComponents[1]->getKind() == Component::GREEN) &&
174 (mComponents[2]->getKind() == Component::BLUE) &&
175 (mComponents[3]->getKind() == Component::ALPHA)) {
176 return GL_RGBA;
177 }
178 break;
179 }
180 return 0;
181}
182
183
Jason Samse12c1c52009-09-27 17:50:38 -0700184void Element::dumpLOGV(const char *prefix) const
185{
186 ObjectBase::dumpLOGV(prefix);
187 LOGV("%s Element: components %i, size %i", prefix, mComponentCount, getSizeBytes());
188 for (uint32_t ct = 0; ct < mComponentCount; ct++) {
189 char buf[1024];
190 sprintf(buf, "%s component %i: ", prefix, ct);
191 mComponents[ct]->dumpLOGV(buf);
192 }
193}
194
Jason Sams326e0dd2009-05-22 14:03:28 -0700195ElementState::ElementState()
196{
197}
198
199ElementState::~ElementState()
200{
201}
202
203/////////////////////////////////////////
Jason Samse5ffb872009-08-09 17:01:55 -0700204//
Jason Sams326e0dd2009-05-22 14:03:28 -0700205
206namespace android {
207namespace renderscript {
208
209void rsi_ElementBegin(Context *rsc)
210{
211 rsc->mStateElement.mComponentBuildList.clear();
212}
213
Jason Sams8b2c0652009-08-12 17:54:11 -0700214void rsi_ElementAdd(Context *rsc, RsDataKind dk, RsDataType dt, bool isNormalized, size_t bits, const char *name)
Jason Sams326e0dd2009-05-22 14:03:28 -0700215{
216 ElementState * sec = &rsc->mStateElement;
Jason Samse12c1c52009-09-27 17:50:38 -0700217
218 rsAssert(bits > 0);
219
Jason Samse514b452009-09-25 14:51:22 -0700220 Component *c = new Component(rsc,
221 static_cast<Component::DataKind>(dk),
Jason Samse5ffb872009-08-09 17:01:55 -0700222 static_cast<Component::DataType>(dt),
223 isNormalized,
Jason Sams8b2c0652009-08-12 17:54:11 -0700224 bits,
225 name);
Jason Samse5ffb872009-08-09 17:01:55 -0700226 sec->mComponentBuildList.add(c);
Jason Sams326e0dd2009-05-22 14:03:28 -0700227}
228
229RsElement rsi_ElementCreate(Context *rsc)
230{
231 ElementState * sec = &rsc->mStateElement;
Jason Samse514b452009-09-25 14:51:22 -0700232 Element *se = new Element(rsc, sec->mComponentBuildList.size());
Jason Sams326e0dd2009-05-22 14:03:28 -0700233
Jason Samse12c1c52009-09-27 17:50:38 -0700234 rsAssert(se->getComponentCount() > 0);
235
Jason Sams326e0dd2009-05-22 14:03:28 -0700236 for (size_t ct = 0; ct < se->getComponentCount(); ct++) {
237 se->setComponent(ct, sec->mComponentBuildList[ct]);
238 }
239
240 rsc->mStateElement.mComponentBuildList.clear();
Jason Sams9397e302009-08-27 20:23:34 -0700241 se->incUserRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700242 return se;
243}
244
Jason Sams326e0dd2009-05-22 14:03:28 -0700245
246}
247}