rsElement.cpp revision d01d970cf5973aa5186cc02c80fb2c143a69b0b1
1/*
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
19#include <GLES/gl.h>
20
21using namespace android;
22using namespace android::renderscript;
23
24
25Element::Element(Context *rsc) : ObjectBase(rsc)
26{
27    mBits = 0;
28    mAllocFile = __FILE__;
29    mAllocLine = __LINE__;
30    mFields = NULL;
31    mFieldCount = 0;
32}
33
34
35Element::~Element()
36{
37    clear();
38}
39
40void Element::clear()
41{
42    delete [] mFields;
43    mFields = NULL;
44    mFieldCount = 0;
45}
46/*
47void Element::setComponent(uint32_t idx, Component *c)
48{
49    rsAssert(!mComponents[idx].get());
50    rsAssert(idx < mComponentCount);
51    mComponents[idx].set(c);
52
53// Fixme: This should probably not be here
54    c->incUserRef();
55}
56*/
57
58size_t Element::getSizeBits() const
59{
60    if (!mFieldCount) {
61        return mBits;
62    }
63
64    size_t total = 0;
65    for (size_t ct=0; ct < mFieldCount; ct++) {
66        total += mFields[ct].e->mBits;
67    }
68    return total;
69}
70
71size_t Element::getFieldOffsetBits(uint32_t componentNumber) const
72{
73    size_t offset = 0;
74    for (uint32_t ct = 0; ct < componentNumber; ct++) {
75        offset += mFields[ct].e->mBits;
76    }
77    return offset;
78}
79
80void Element::dumpLOGV(const char *prefix) const
81{
82    ObjectBase::dumpLOGV(prefix);
83    LOGV("%s   Element: components %i,  size %i", prefix, mFieldCount, mBits);
84    for (uint32_t ct = 0; ct < mFieldCount; ct++) {
85        char buf[1024];
86        sprintf(buf, "%s component %i: ", prefix, ct);
87        //mComponents[ct]->dumpLOGV(buf);
88    }
89}
90
91
92Element * Element::create(Context *rsc, RsDataType dt, RsDataKind dk,
93                            bool isNorm, uint32_t vecSize)
94{
95    Element *e = new Element(rsc);
96    e->mComponent.set(dt, dk, isNorm, vecSize);
97    e->mBits = e->mComponent.getBits();
98    return e;
99}
100
101Element * Element::create(Context *rsc, size_t count, const Element **ein,
102                            const char **nin, const size_t * lengths)
103{
104    Element *e = new Element(rsc);
105    e->mFields = new ElementField_t [count];
106    e->mFieldCount = count;
107
108    for (size_t ct=0; ct < count; ct++) {
109        e->mFields[ct].e.set(ein[ct]);
110        e->mFields[ct].name.setTo(nin[ct], lengths[ct]);
111        LOGE("element %p %s", ein[ct], e->mFields[ct].name.string());
112    }
113
114    return e;
115}
116
117String8 Element::getCStructBody(uint32_t indent) const
118{
119    String8 si;
120    for (uint32_t ct=0; ct < indent; ct++) {
121        si.append(" ");
122    }
123
124    String8 s(si);
125    s.append("{\n");
126    for (uint32_t ct = 0; ct < mFieldCount; ct++) {
127        s.append(si);
128        s.append(mFields[ct].e->getCType(indent+4));
129        s.append(" ");
130        s.append(mFields[ct].name);
131        s.append(";\n");
132    }
133    s.append(si);
134    s.append("}");
135    return s;
136}
137
138String8 Element::getCType(uint32_t indent) const
139{
140    String8 s;
141    for (uint32_t ct=0; ct < indent; ct++) {
142        s.append(" ");
143    }
144
145    if (!mFieldCount) {
146        // Basic component.
147        s.append(mComponent.getCType());
148    } else {
149        s.append("struct ");
150        s.append(getCStructBody(indent));
151    }
152
153    return s;
154}
155
156
157
158
159ElementState::ElementState()
160{
161}
162
163ElementState::~ElementState()
164{
165}
166
167
168/////////////////////////////////////////
169//
170
171namespace android {
172namespace renderscript {
173
174RsElement rsi_ElementCreate(Context *rsc,
175                            RsDataType dt,
176                            RsDataKind dk,
177                            bool norm,
178                            uint32_t vecSize)
179{
180    //LOGE("rsi_ElementCreate %i %i %i %i", dt, dk, norm, vecSize);
181    Element *e = Element::create(rsc, dt, dk, norm, vecSize);
182    e->incUserRef();
183    return e;
184}
185
186RsElement rsi_ElementCreate2(Context *rsc,
187                             size_t count,
188                             const RsElement * ein,
189                             const char ** names,
190                             const size_t * nameLengths)
191{
192    //LOGE("rsi_ElementCreate2 %i", count);
193    Element *e = Element::create(rsc, count, (const Element **)ein, names, nameLengths);
194    e->incUserRef();
195    return e;
196}
197
198/*
199void rsi_ElementAdd(Context *rsc, RsDataKind dk, RsDataType dt, bool isNormalized, size_t bits, const char *name)
200{
201    ElementState * sec = &rsc->mStateElement;
202
203    rsAssert(bits > 0);
204
205    Element *c = Element::create(rsc, dk, dt, isNormalized, bits);
206    sec->mBuildList.add(c);
207    if (name)
208        sec->mNames.add(String8(name));
209    else
210        sec->mNames.add(String8(""));
211}
212
213RsElement rsi_ElementCreate(Context *rsc)
214{
215    ElementState * sec = &rsc->mStateElement;
216
217    size_t count = sec->mBuildList.size();
218    rsAssert(count > 0);
219
220    if (count == 1) {
221        Element *se = sec->mBuildList[0];
222        se->incUserRef();
223        sec->mBuildList.clear();
224        sec->mNames.clear();
225        return se;
226    }
227
228    Element ** tmpElements = (Element **)calloc(count, sizeof(Element *));
229    const char ** tmpNames = (const char **)calloc(count, sizeof(char *));
230    size_t * tmpLengths = (size_t *)calloc(count, sizeof(size_t));
231
232
233    for (size_t ct = 0; ct < count; ct++) {
234        tmpElements[ct] = sec->mBuildList[ct];
235        tmpNames[ct] = sec->mNames[ct].string();
236        tmpLengths[ct] = sec->mNames[ct].length();
237    }
238    Element *se = Element::create(rsc, tmpElements, tmpNames, tmpLengths, count);
239
240    sec->mBuildList.clear();
241    sec->mNames.clear();
242    se->incUserRef();
243    free(tmpElements);
244    free(tmpNames);
245    free(tmpLengths);
246    return se;
247}
248*/
249
250
251}
252}
253