blob: bfdec53b99827fb37538217b9f179c96294d518b [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#ifndef ANDROID_STRUCTURED_ELEMENT_H
18#define ANDROID_STRUCTURED_ELEMENT_H
19
Jason Sams718cd1f2009-12-23 14:35:29 -080020#include "rsComponent.h"
Jason Sams0011bcf2009-12-15 12:58:36 -080021#include "rsUtils.h"
22#include "rsObjectBase.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070023
24// ---------------------------------------------------------------------------
25namespace android {
26namespace renderscript {
27
Jason Samsd19f10d2009-05-22 14:03:28 -070028// An element is a group of Components that occupies one cell in a structure.
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080029class Element : public ObjectBase {
Jason Samsd19f10d2009-05-22 14:03:28 -070030public:
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -070031 class Builder {
32 public:
Alex Sakhartchouka8bb9212011-08-19 09:43:18 -070033 Builder();
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -070034 void add(const Element *e, const char *nameStr, uint32_t arraySize);
35 ObjectBaseRef<const Element> create(Context *rsc);
36 private:
37 Vector<ObjectBaseRef<const Element> > mBuilderElementRefs;
38 Vector<const Element *> mBuilderElements;
39 Vector<const char*> mBuilderNameStrings;
40 Vector<size_t> mBuilderNameLengths;
41 Vector<uint32_t> mBuilderArrays;
42 };
Jason Samse2ae85f2009-06-03 16:04:54 -070043 uint32_t getGLType() const;
44 uint32_t getGLFormat() const;
Jason Samsd19f10d2009-05-22 14:03:28 -070045
Jason Samsd19f10d2009-05-22 14:03:28 -070046 size_t getSizeBits() const;
Jason Sams1bada8c2009-08-09 17:01:55 -070047 size_t getSizeBytes() const {
48 return (getSizeBits() + 7) >> 3;
Jason Samsd19f10d2009-05-22 14:03:28 -070049 }
50
Jason Samsb28ca96f2010-08-09 18:13:33 -070051 size_t getFieldOffsetBits(uint32_t componentNumber) const {
52 return mFields[componentNumber].offsetBits;
53 }
Jason Sams0011bcf2009-12-15 12:58:36 -080054 size_t getFieldOffsetBytes(uint32_t componentNumber) const {
Jason Samsb28ca96f2010-08-09 18:13:33 -070055 return mFields[componentNumber].offsetBits >> 3;
Jason Samsd19f10d2009-05-22 14:03:28 -070056 }
57
Jason Sams0011bcf2009-12-15 12:58:36 -080058 uint32_t getFieldCount() const {return mFieldCount;}
59 const Element * getField(uint32_t idx) const {return mFields[idx].e.get();}
60 const char * getFieldName(uint32_t idx) const {return mFields[idx].name.string();}
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -070061 uint32_t getFieldArraySize(uint32_t idx) const {return mFields[idx].arraySize;}
Jason Samsd19f10d2009-05-22 14:03:28 -070062
Jason Sams718cd1f2009-12-23 14:35:29 -080063 const Component & getComponent() const {return mComponent;}
64 RsDataType getType() const {return mComponent.getType();}
Jason Sams718cd1f2009-12-23 14:35:29 -080065 RsDataKind getKind() const {return mComponent.getKind();}
Jason Sams0011bcf2009-12-15 12:58:36 -080066 uint32_t getBits() const {return mBits;}
Jason Sams718cd1f2009-12-23 14:35:29 -080067
Jason Sams3c0dfba2009-09-27 17:50:38 -070068 void dumpLOGV(const char *prefix) const;
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070069 virtual void serialize(OStream *stream) const;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070070 virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ELEMENT; }
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070071 static Element *createFromStream(Context *rsc, IStream *stream);
Jason Sams3c0dfba2009-09-27 17:50:38 -070072
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -070073 static ObjectBaseRef<const Element> createRef(Context *rsc,
74 RsDataType dt,
75 RsDataKind dk,
76 bool isNorm,
77 uint32_t vecSize);
78 static ObjectBaseRef<const Element> createRef(Context *rsc, size_t count,
79 const Element **,
80 const char **,
81 const size_t * lengths,
82 const uint32_t *asin);
83
84 static const Element* create(Context *rsc,
85 RsDataType dt,
86 RsDataKind dk,
87 bool isNorm,
88 uint32_t vecSize) {
89 ObjectBaseRef<const Element> elem = createRef(rsc, dt, dk, isNorm, vecSize);
90 elem->incUserRef();
91 return elem.get();
92 }
93 static const Element* create(Context *rsc, size_t count,
94 const Element **ein,
95 const char **nin,
96 const size_t * lengths,
97 const uint32_t *asin) {
98 ObjectBaseRef<const Element> elem = createRef(rsc, count, ein, nin, lengths, asin);
99 elem->incUserRef();
100 return elem.get();
101 }
Jason Sams0011bcf2009-12-15 12:58:36 -0800102
Jason Samsb28ca96f2010-08-09 18:13:33 -0700103 void incRefs(const void *) const;
104 void decRefs(const void *) const;
105 bool getHasReferences() const {return mHasReference;}
106
Jason Samsd19f10d2009-05-22 14:03:28 -0700107protected:
108 // deallocate any components that are part of this element.
109 void clear();
110
Jason Sams0011bcf2009-12-15 12:58:36 -0800111 typedef struct {
112 String8 name;
Jason Sams718cd1f2009-12-23 14:35:29 -0800113 ObjectBaseRef<const Element> e;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700114 uint32_t offsetBits;
Jason Sams70d4e502010-09-02 17:35:23 -0700115 uint32_t arraySize;
Jason Sams0011bcf2009-12-15 12:58:36 -0800116 } ElementField_t;
117 ElementField_t *mFields;
118 size_t mFieldCount;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700119 bool mHasReference;
Jason Sams0011bcf2009-12-15 12:58:36 -0800120
Jason Samsd19f10d2009-05-22 14:03:28 -0700121
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700122 virtual ~Element();
Jason Samsa9e7a052009-09-25 14:51:22 -0700123 Element(Context *);
Jason Sams0011bcf2009-12-15 12:58:36 -0800124
Jason Sams718cd1f2009-12-23 14:35:29 -0800125 Component mComponent;
Jason Sams0011bcf2009-12-15 12:58:36 -0800126 uint32_t mBits;
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800127
128 void compute();
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700129
130 virtual void preDestroy() const;
Jason Samsd19f10d2009-05-22 14:03:28 -0700131};
132
133
134class ElementState {
135public:
136 ElementState();
137 ~ElementState();
138
Jason Samse4c487a2010-02-17 15:38:10 -0800139 // Cache of all existing elements.
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700140 Vector<Element *> mElements;
Jason Samsd19f10d2009-05-22 14:03:28 -0700141};
142
143
144}
145}
146#endif //ANDROID_STRUCTURED_ELEMENT_H