blob: 6288bc425afac4b913d2cc9f3fbcc8dcbcf1a095 [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 Sams0011bcf2009-12-15 12:58:36 -080027 mBits = 0;
Jason Sams61f08d62009-09-25 16:37:33 -070028 mAllocFile = __FILE__;
29 mAllocLine = __LINE__;
Jason Sams0011bcf2009-12-15 12:58:36 -080030 mFields = NULL;
31 mFieldCount = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -070032}
33
Jason Samsd19f10d2009-05-22 14:03:28 -070034
35Element::~Element()
36{
Jason Samse4c487a2010-02-17 15:38:10 -080037 for (uint32_t ct = 0; ct < mRSC->mStateElement.mElements.size(); ct++) {
38 if (mRSC->mStateElement.mElements[ct] == this) {
39 mRSC->mStateElement.mElements.removeAt(ct);
40 break;
41 }
42 }
Jason Samsd19f10d2009-05-22 14:03:28 -070043 clear();
44}
45
46void Element::clear()
47{
Jason Sams0011bcf2009-12-15 12:58:36 -080048 delete [] mFields;
49 mFields = NULL;
50 mFieldCount = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -070051}
Jason Samsd19f10d2009-05-22 14:03:28 -070052
53size_t Element::getSizeBits() const
54{
Jason Sams0011bcf2009-12-15 12:58:36 -080055 if (!mFieldCount) {
56 return mBits;
57 }
58
Jason Samsd19f10d2009-05-22 14:03:28 -070059 size_t total = 0;
Jason Sams0011bcf2009-12-15 12:58:36 -080060 for (size_t ct=0; ct < mFieldCount; ct++) {
61 total += mFields[ct].e->mBits;
Jason Samsd19f10d2009-05-22 14:03:28 -070062 }
63 return total;
64}
65
Jason Sams0011bcf2009-12-15 12:58:36 -080066size_t Element::getFieldOffsetBits(uint32_t componentNumber) const
Jason Samsd19f10d2009-05-22 14:03:28 -070067{
68 size_t offset = 0;
69 for (uint32_t ct = 0; ct < componentNumber; ct++) {
Jason Sams0011bcf2009-12-15 12:58:36 -080070 offset += mFields[ct].e->mBits;
Jason Samsd19f10d2009-05-22 14:03:28 -070071 }
72 return offset;
73}
74
Jason Sams3c0dfba2009-09-27 17:50:38 -070075void Element::dumpLOGV(const char *prefix) const
76{
77 ObjectBase::dumpLOGV(prefix);
Jason Sams0011bcf2009-12-15 12:58:36 -080078 LOGV("%s Element: components %i, size %i", prefix, mFieldCount, mBits);
79 for (uint32_t ct = 0; ct < mFieldCount; ct++) {
Jason Sams3c0dfba2009-09-27 17:50:38 -070080 char buf[1024];
81 sprintf(buf, "%s component %i: ", prefix, ct);
Jason Sams0011bcf2009-12-15 12:58:36 -080082 //mComponents[ct]->dumpLOGV(buf);
Jason Sams3c0dfba2009-09-27 17:50:38 -070083 }
84}
85
Jason Sams718cd1f2009-12-23 14:35:29 -080086
Jason Samse4c487a2010-02-17 15:38:10 -080087const Element * Element::create(Context *rsc, RsDataType dt, RsDataKind dk,
Jason Sams718cd1f2009-12-23 14:35:29 -080088 bool isNorm, uint32_t vecSize)
Jason Sams0011bcf2009-12-15 12:58:36 -080089{
Jason Samse4c487a2010-02-17 15:38:10 -080090 // Look for an existing match.
91 for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) {
92 const Element *ee = rsc->mStateElement.mElements[ct];
93 if (!ee->getFieldCount() &&
94 (ee->getComponent().getType() == dt) &&
95 (ee->getComponent().getKind() == dk) &&
96 (ee->getComponent().getIsNormalized() == isNorm) &&
97 (ee->getComponent().getVectorSize() == vecSize)) {
98 // Match
99 ee->incUserRef();
100 return ee;
101 }
102 }
103
Jason Sams0011bcf2009-12-15 12:58:36 -0800104 Element *e = new Element(rsc);
Jason Sams718cd1f2009-12-23 14:35:29 -0800105 e->mComponent.set(dt, dk, isNorm, vecSize);
106 e->mBits = e->mComponent.getBits();
Jason Samse4c487a2010-02-17 15:38:10 -0800107 rsc->mStateElement.mElements.push(e);
Jason Sams0011bcf2009-12-15 12:58:36 -0800108 return e;
109}
110
Jason Samse4c487a2010-02-17 15:38:10 -0800111const Element * Element::create(Context *rsc, size_t count, const Element **ein,
Jason Sams718cd1f2009-12-23 14:35:29 -0800112 const char **nin, const size_t * lengths)
Jason Sams0011bcf2009-12-15 12:58:36 -0800113{
Jason Samse4c487a2010-02-17 15:38:10 -0800114 // Look for an existing match.
115 for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) {
116 const Element *ee = rsc->mStateElement.mElements[ct];
117 if (ee->getFieldCount() == count) {
118 bool match = true;
119 for (uint32_t i=0; i < count; i++) {
120 if ((ee->mFields[i].e.get() != ein[i]) ||
121 (ee->mFields[i].name.length() != lengths[i]) ||
122 (ee->mFields[i].name != nin[i])) {
123 match = false;
124 break;
125 }
126 }
127 if (match) {
128 ee->incUserRef();
129 return ee;
130 }
131 }
132 }
133
Jason Sams0011bcf2009-12-15 12:58:36 -0800134 Element *e = new Element(rsc);
135 e->mFields = new ElementField_t [count];
136 e->mFieldCount = count;
Jason Sams0011bcf2009-12-15 12:58:36 -0800137 for (size_t ct=0; ct < count; ct++) {
138 e->mFields[ct].e.set(ein[ct]);
139 e->mFields[ct].name.setTo(nin[ct], lengths[ct]);
140 }
141
Jason Samse4c487a2010-02-17 15:38:10 -0800142 rsc->mStateElement.mElements.push(e);
Jason Sams0011bcf2009-12-15 12:58:36 -0800143 return e;
144}
145
Jason Sams718cd1f2009-12-23 14:35:29 -0800146String8 Element::getCStructBody(uint32_t indent) const
147{
148 String8 si;
149 for (uint32_t ct=0; ct < indent; ct++) {
150 si.append(" ");
151 }
152
153 String8 s(si);
154 s.append("{\n");
155 for (uint32_t ct = 0; ct < mFieldCount; ct++) {
156 s.append(si);
157 s.append(mFields[ct].e->getCType(indent+4));
158 s.append(" ");
159 s.append(mFields[ct].name);
160 s.append(";\n");
161 }
162 s.append(si);
163 s.append("}");
164 return s;
165}
166
167String8 Element::getCType(uint32_t indent) const
168{
169 String8 s;
170 for (uint32_t ct=0; ct < indent; ct++) {
171 s.append(" ");
172 }
173
174 if (!mFieldCount) {
175 // Basic component.
176 s.append(mComponent.getCType());
177 } else {
178 s.append("struct ");
179 s.append(getCStructBody(indent));
180 }
181
182 return s;
183}
184
Jason Samse17964e2010-01-04 16:52:27 -0800185String8 Element::getGLSLType(uint32_t indent) const
186{
187 String8 s;
188 for (uint32_t ct=0; ct < indent; ct++) {
189 s.append(" ");
190 }
191
192 if (!mFieldCount) {
193 // Basic component.
194 s.append(mComponent.getGLSLType());
195 } else {
196 rsAssert(0);
197 //s.append("struct ");
198 //s.append(getCStructBody(indent));
199 }
200
201 return s;
202}
Jason Sams718cd1f2009-12-23 14:35:29 -0800203
204
Jason Sams0011bcf2009-12-15 12:58:36 -0800205
Jason Samsd19f10d2009-05-22 14:03:28 -0700206ElementState::ElementState()
207{
208}
209
210ElementState::~ElementState()
211{
Jason Samse4c487a2010-02-17 15:38:10 -0800212 rsAssert(!mElements.size());
Jason Samsd19f10d2009-05-22 14:03:28 -0700213}
214
Jason Sams0011bcf2009-12-15 12:58:36 -0800215
Jason Samsd19f10d2009-05-22 14:03:28 -0700216/////////////////////////////////////////
Jason Sams1bada8c2009-08-09 17:01:55 -0700217//
Jason Samsd19f10d2009-05-22 14:03:28 -0700218
219namespace android {
220namespace renderscript {
221
Jason Sams718cd1f2009-12-23 14:35:29 -0800222RsElement rsi_ElementCreate(Context *rsc,
223 RsDataType dt,
224 RsDataKind dk,
225 bool norm,
226 uint32_t vecSize)
Jason Samsd19f10d2009-05-22 14:03:28 -0700227{
Jason Sams718cd1f2009-12-23 14:35:29 -0800228 //LOGE("rsi_ElementCreate %i %i %i %i", dt, dk, norm, vecSize);
Jason Samse4c487a2010-02-17 15:38:10 -0800229 const Element *e = Element::create(rsc, dt, dk, norm, vecSize);
Jason Sams718cd1f2009-12-23 14:35:29 -0800230 e->incUserRef();
Jason Samse4c487a2010-02-17 15:38:10 -0800231 return (RsElement)e;
Jason Samsd19f10d2009-05-22 14:03:28 -0700232}
233
Jason Sams718cd1f2009-12-23 14:35:29 -0800234RsElement rsi_ElementCreate2(Context *rsc,
235 size_t count,
236 const RsElement * ein,
237 const char ** names,
238 const size_t * nameLengths)
239{
240 //LOGE("rsi_ElementCreate2 %i", count);
Jason Samse4c487a2010-02-17 15:38:10 -0800241 const Element *e = Element::create(rsc, count, (const Element **)ein, names, nameLengths);
Jason Sams718cd1f2009-12-23 14:35:29 -0800242 e->incUserRef();
Jason Samse4c487a2010-02-17 15:38:10 -0800243 return (RsElement)e;
Jason Sams718cd1f2009-12-23 14:35:29 -0800244}
245
Jason Samsd19f10d2009-05-22 14:03:28 -0700246
247}
248}