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