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// ---------------------------------------------------------------------------
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 *_unused;
51            uint32_t lodCount;
52            uint32_t dimYuv;
53            bool faces;
54        };
55        State state;
56    };
57    Hal mHal;
58
59    void operator delete(void* ptr);
60
61    Type * createTex2D(const Element *, size_t w, size_t h, bool mip);
62
63    size_t getCellCount() const {return mCellCount;}
64    size_t getElementSizeBytes() const {return mElement->getSizeBytes();}
65    size_t getPackedSizeBytes() const {return mCellCount * mElement->getSizeBytes();}
66    const Element * getElement() const {return mElement.get();}
67
68    uint32_t getDimX() const {return mHal.state.dimX;}
69    uint32_t getDimY() const {return mHal.state.dimY;}
70    uint32_t getDimZ() const {return mHal.state.dimZ;}
71    bool getDimLOD() const {return mDimLOD;}
72    bool getDimFaces() const {return mHal.state.faces;}
73    uint32_t getDimYuv() const {return mHal.state.dimYuv;}
74
75    uint32_t getLODDimX(uint32_t lod) const {
76        rsAssert(lod < mHal.state.lodCount);
77        return mHal.state.lodDimX[lod];
78    }
79    uint32_t getLODDimY(uint32_t lod) const {
80        rsAssert(lod < mHal.state.lodCount);
81        return mHal.state.lodDimY[lod];
82    }
83    uint32_t getLODDimZ(uint32_t lod) const {
84        rsAssert(lod < mHal.state.lodCount);
85        return mHal.state.lodDimZ[lod];
86    }
87
88    uint32_t getLODCount() const {return mHal.state.lodCount;}
89    bool getIsNp2() const;
90
91    void clear();
92    void compute();
93
94    void dumpLOGV(const char *prefix) const;
95    virtual void serialize(Context *rsc, OStream *stream) const;
96    virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_TYPE; }
97    static Type *createFromStream(Context *rsc, IStream *stream);
98
99    ObjectBaseRef<Type> cloneAndResize1D(Context *rsc, uint32_t dimX) const;
100    ObjectBaseRef<Type> cloneAndResize2D(Context *rsc, uint32_t dimX, uint32_t dimY) const;
101
102    static ObjectBaseRef<Type> getTypeRef(Context *rsc, const Element *e,
103                                          uint32_t dimX, uint32_t dimY, uint32_t dimZ,
104                                          bool dimLOD, bool dimFaces, uint32_t dimYuv);
105
106    static Type* getType(Context *rsc, const Element *e,
107                         uint32_t dimX, uint32_t dimY, uint32_t dimZ,
108                         bool dimLOD, bool dimFaces, uint32_t yuv) {
109        ObjectBaseRef<Type> type = getTypeRef(rsc, e, dimX, dimY, dimZ, dimLOD, dimFaces, yuv);
110        type->incUserRef();
111        return type.get();
112    }
113
114    void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
115    void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
116    virtual void callUpdateCacheObject(const Context *rsc, void *dstObj) const;
117
118protected:
119    void makeLODTable();
120    bool mDimLOD;
121
122    // Internal structure from most to least significant.
123    // * Array dimensions
124    // * Faces
125    // * Mipmaps
126    // * xyz
127
128    ObjectBaseRef<const Element> mElement;
129
130    // count of mipmap levels, 0 indicates no mipmapping
131
132    size_t mCellCount;
133protected:
134    virtual void preDestroy() const;
135    virtual ~Type();
136
137private:
138    Type(Context *);
139    Type(const Type &);
140};
141
142
143class TypeState {
144public:
145    TypeState();
146    ~TypeState();
147
148    // Cache of all existing types.
149    Vector<Type *> mTypes;
150};
151
152
153}
154}
155#endif //ANDROID_STRUCTURED_TYPE
156