[go: nahoru, domu]

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