blob: a00fb52171285b5a60ccae7be7c9a935dd2c7d4d [file] [log] [blame]
Jason Samsd19f10d2009-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 Samse2ae85f2009-06-03 16:04:54 -070019#include <GLES/gl.h>
20
Jason Samsd19f10d2009-05-22 14:03:28 -070021using namespace android;
22using namespace android::renderscript;
23
Jason Samsd19f10d2009-05-22 14:03:28 -070024
Jason Samsa9e7a052009-09-25 14:51:22 -070025Element::Element(Context *rsc) : ObjectBase(rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -070026{
Jason Sams61f08d62009-09-25 16:37:33 -070027 mAllocFile = __FILE__;
28 mAllocLine = __LINE__;
Jason Samsd19f10d2009-05-22 14:03:28 -070029 mComponents = NULL;
30 mComponentCount = 0;
31}
32
Jason Samsa9e7a052009-09-25 14:51:22 -070033Element::Element(Context *rsc, uint32_t count) : ObjectBase(rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -070034{
Jason Sams61f08d62009-09-25 16:37:33 -070035 mAllocFile = __FILE__;
36 mAllocLine = __LINE__;
Jason Samsd19f10d2009-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 Sams07ae4062009-08-27 20:23:34 -070058 c->incUserRef();
Jason Samsd19f10d2009-05-22 14:03:28 -070059}
60
61
62size_t Element::getSizeBits() const
63{
64 size_t total = 0;
65 for (size_t ct=0; ct < mComponentCount; ct++) {
66 total += mComponents[ct]->getBits();
67 }
68 return total;
69}
70
71size_t Element::getComponentOffsetBits(uint32_t componentNumber) const
72{
73 size_t offset = 0;
74 for (uint32_t ct = 0; ct < componentNumber; ct++) {
75 offset += mComponents[ct]->getBits();
76 }
77 return offset;
78}
79
Jason Samse2ae85f2009-06-03 16:04:54 -070080uint32_t Element::getGLType() const
81{
82 int bits[4];
83
84 if (mComponentCount > 4) {
85 return 0;
86 }
87
88 for (uint32_t ct=0; ct < mComponentCount; ct++) {
89 bits[ct] = mComponents[ct]->getBits();
90 if (mComponents[ct]->getType() != Component::UNSIGNED) {
91 return 0;
92 }
93 if (!mComponents[ct]->getIsNormalized()) {
94 return 0;
95 }
96 }
97
98 switch(mComponentCount) {
99 case 1:
100 if (bits[0] == 8) {
101 return GL_UNSIGNED_BYTE;
102 }
103 return 0;
104 case 2:
105 if ((bits[0] == 8) &&
106 (bits[1] == 8)) {
107 return GL_UNSIGNED_BYTE;
108 }
109 return 0;
110 case 3:
111 if ((bits[0] == 8) &&
112 (bits[1] == 8) &&
113 (bits[2] == 8)) {
114 return GL_UNSIGNED_BYTE;
115 }
116 if ((bits[0] == 5) &&
117 (bits[1] == 6) &&
118 (bits[2] == 5)) {
119 return GL_UNSIGNED_SHORT_5_6_5;
120 }
121 return 0;
122 case 4:
123 if ((bits[0] == 8) &&
124 (bits[1] == 8) &&
125 (bits[2] == 8) &&
126 (bits[3] == 8)) {
127 return GL_UNSIGNED_BYTE;
128 }
129 if ((bits[0] == 4) &&
130 (bits[1] == 4) &&
131 (bits[2] == 4) &&
132 (bits[3] == 4)) {
133 return GL_UNSIGNED_SHORT_4_4_4_4;
134 }
135 if ((bits[0] == 5) &&
136 (bits[1] == 5) &&
137 (bits[2] == 5) &&
138 (bits[3] == 1)) {
139 return GL_UNSIGNED_SHORT_5_5_5_1;
140 }
141 }
142 return 0;
143}
144
145uint32_t Element::getGLFormat() const
146{
147 switch(mComponentCount) {
148 case 1:
149 if (mComponents[0]->getKind() == Component::ALPHA) {
150 return GL_ALPHA;
151 }
152 if (mComponents[0]->getKind() == Component::LUMINANCE) {
153 return GL_LUMINANCE;
154 }
155 break;
156 case 2:
157 if ((mComponents[0]->getKind() == Component::LUMINANCE) &&
158 (mComponents[1]->getKind() == Component::ALPHA)) {
159 return GL_LUMINANCE_ALPHA;
160 }
161 break;
162 case 3:
163 if ((mComponents[0]->getKind() == Component::RED) &&
164 (mComponents[1]->getKind() == Component::GREEN) &&
165 (mComponents[2]->getKind() == Component::BLUE)) {
166 return GL_RGB;
167 }
168 break;
169 case 4:
170 if ((mComponents[0]->getKind() == Component::RED) &&
171 (mComponents[1]->getKind() == Component::GREEN) &&
172 (mComponents[2]->getKind() == Component::BLUE) &&
173 (mComponents[3]->getKind() == Component::ALPHA)) {
174 return GL_RGBA;
175 }
176 break;
177 }
178 return 0;
179}
180
181
Jason Samsd19f10d2009-05-22 14:03:28 -0700182ElementState::ElementState()
183{
184}
185
186ElementState::~ElementState()
187{
188}
189
190/////////////////////////////////////////
Jason Sams1bada8c2009-08-09 17:01:55 -0700191//
Jason Samsd19f10d2009-05-22 14:03:28 -0700192
193namespace android {
194namespace renderscript {
195
196void rsi_ElementBegin(Context *rsc)
197{
198 rsc->mStateElement.mComponentBuildList.clear();
199}
200
Jason Sams43ee06852009-08-12 17:54:11 -0700201void rsi_ElementAdd(Context *rsc, RsDataKind dk, RsDataType dt, bool isNormalized, size_t bits, const char *name)
Jason Samsd19f10d2009-05-22 14:03:28 -0700202{
203 ElementState * sec = &rsc->mStateElement;
Jason Samsa9e7a052009-09-25 14:51:22 -0700204 Component *c = new Component(rsc,
205 static_cast<Component::DataKind>(dk),
Jason Sams1bada8c2009-08-09 17:01:55 -0700206 static_cast<Component::DataType>(dt),
207 isNormalized,
Jason Sams43ee06852009-08-12 17:54:11 -0700208 bits,
209 name);
Jason Sams1bada8c2009-08-09 17:01:55 -0700210 sec->mComponentBuildList.add(c);
Jason Samsd19f10d2009-05-22 14:03:28 -0700211}
212
213RsElement rsi_ElementCreate(Context *rsc)
214{
215 ElementState * sec = &rsc->mStateElement;
Jason Samsa9e7a052009-09-25 14:51:22 -0700216 Element *se = new Element(rsc, sec->mComponentBuildList.size());
Jason Samsd19f10d2009-05-22 14:03:28 -0700217
218 for (size_t ct = 0; ct < se->getComponentCount(); ct++) {
219 se->setComponent(ct, sec->mComponentBuildList[ct]);
220 }
221
222 rsc->mStateElement.mComponentBuildList.clear();
Jason Sams07ae4062009-08-27 20:23:34 -0700223 se->incUserRef();
Jason Samsd19f10d2009-05-22 14:03:28 -0700224 return se;
225}
226
Jason Samsd19f10d2009-05-22 14:03:28 -0700227
228}
229}