1 /*
2  * Copyright (C) 2013 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_TYPE_H
18 #define ANDROID_STRUCTURED_TYPE_H
19 
20 #include "rsElement.h"
21 
22 #include <vector>
23 
24 // ---------------------------------------------------------------------------
25 namespace android {
26 namespace renderscript {
27 /*****************************************************************************
28  * CAUTION
29  *
30  * Any layout changes for this class may require a corresponding change to be
31  * made to frameworks/rs/driver/runtime/rs_structs.h, which contains
32  * a partial copy of the information below.
33  *
34  *****************************************************************************/
35 
36 class Type : public ObjectBase {
37 public:
38     const static uint32_t mMaxArrays = 4;
39 
40     struct Hal {
41         mutable void *drv;
42 
43         struct State {
44             const Element * element;
45 
46             // Size of the structure in the various dimensions.  A missing Dimension is
47             // specified as a 0 and not a 1.
48             uint32_t dimX;
49             uint32_t dimY;
50             uint32_t dimZ;
51             uint32_t *lodDimX;
52             uint32_t *lodDimY;
53             uint32_t *lodDimZ;
54             uint32_t *arrays;
55             uint32_t lodCount;
56             uint32_t dimYuv;
57             uint32_t arrayCount;
58             bool faces;
59         };
60         State state;
61     };
62     Hal mHal;
63 
64     void operator delete(void* ptr);
65 
66     Type * createTex2D(const Element *, size_t w, size_t h, bool mip);
67 
getCellCount()68     size_t getCellCount() const {return mCellCount;}
getElementSizeBytes()69     size_t getElementSizeBytes() const {return mElement->getSizeBytes();}
getPackedSizeBytes()70     size_t getPackedSizeBytes() const {return mCellCount * mElement->getSizeBytes();}
getElement()71     const Element * getElement() const {return mElement.get();}
72 
getDimX()73     uint32_t getDimX() const {return mHal.state.dimX;}
getDimY()74     uint32_t getDimY() const {return mHal.state.dimY;}
getDimZ()75     uint32_t getDimZ() const {return mHal.state.dimZ;}
getDimLOD()76     bool getDimLOD() const {return mDimLOD;}
getDimFaces()77     bool getDimFaces() const {return mHal.state.faces;}
getDimYuv()78     uint32_t getDimYuv() const {return mHal.state.dimYuv;}
getArray(uint32_t idx)79     uint32_t getArray(uint32_t idx) const {
80         if (idx < mHal.state.arrayCount) {
81             return mHal.state.arrays[idx];
82         }
83         return 0;
84     }
85 
getLODDimX(uint32_t lod)86     uint32_t getLODDimX(uint32_t lod) const {
87         rsAssert(lod < mHal.state.lodCount);
88         return mHal.state.lodDimX[lod];
89     }
getLODDimY(uint32_t lod)90     uint32_t getLODDimY(uint32_t lod) const {
91         rsAssert(lod < mHal.state.lodCount);
92         return mHal.state.lodDimY[lod];
93     }
getLODDimZ(uint32_t lod)94     uint32_t getLODDimZ(uint32_t lod) const {
95         rsAssert(lod < mHal.state.lodCount);
96         return mHal.state.lodDimZ[lod];
97     }
98 
getLODCount()99     uint32_t getLODCount() const {return mHal.state.lodCount;}
100     bool getIsNp2() const;
101 
102     void clear();
103     void compute();
104 
105     void dumpLOGV(const char *prefix) const;
106     virtual void serialize(Context *rsc, OStream *stream) const;
getClassId()107     virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_TYPE; }
108     static Type *createFromStream(Context *rsc, IStream *stream);
109 
110     ObjectBaseRef<Type> cloneAndResize1D(Context *rsc, uint32_t dimX) const;
111     ObjectBaseRef<Type> cloneAndResize2D(Context *rsc, uint32_t dimX, uint32_t dimY) const;
112 
113     static ObjectBaseRef<Type> getTypeRef(Context *rsc, const Element *e,
114                                           const RsTypeCreateParams *params, size_t len);
115 
getType(Context * rsc,const Element * e,const RsTypeCreateParams * params,size_t len)116     static Type* getType(Context *rsc, const Element *e,
117                          const RsTypeCreateParams *params, size_t len) {
118         ObjectBaseRef<Type> type = getTypeRef(rsc, e, params, len);
119         type->incUserRef();
120         return type.get();
121     }
122 
123     static ObjectBaseRef<Type> getTypeRef(Context *rsc, const Element *e, uint32_t dimX, uint32_t dimY = 0) {
124         RsTypeCreateParams p;
125         memset(&p, 0, sizeof(p));
126         p.dimX = dimX;
127         p.dimY = dimY;
128         return getTypeRef(rsc, e, &p, sizeof(p));
129     }
130 
131     void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
132     void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
133     virtual void callUpdateCacheObject(const Context *rsc, void *dstObj) const;
134 
135 protected:
136     void makeLODTable();
137     bool mDimLOD;
138 
139     // Internal structure from most to least significant.
140     // * Array dimensions
141     // * Faces
142     // * Mipmaps
143     // * xyz
144 
145     ObjectBaseRef<const Element> mElement;
146 
147     // count of mipmap levels, 0 indicates no mipmapping
148 
149     size_t mCellCount;
150 protected:
151     virtual void preDestroy() const;
152     virtual ~Type();
153 
154 private:
155     explicit Type(Context *);
156     Type(const Type &);
157 };
158 
159 
160 class TypeState {
161 public:
162     TypeState();
163     ~TypeState();
164 
165     // Cache of all existing types.
166     std::vector<Type *> mTypes;
167 };
168 
169 
170 } // namespace renderscript
171 } // namespace android
172 #endif //ANDROID_STRUCTURED_TYPE
173