1326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams/*
2bc0ca6ba4e31239bf77060578d0bdf1a10e04168Jason Sams * Copyright (C) 2013 The Android Open Source Project
3326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *
4326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * Licensed under the Apache License, Version 2.0 (the "License");
5326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * you may not use this file except in compliance with the License.
6326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * You may obtain a copy of the License at
7326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *
8326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *      http://www.apache.org/licenses/LICENSE-2.0
9326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *
10326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * Unless required by applicable law or agreed to in writing, software
11326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * distributed under the License is distributed on an "AS IS" BASIS,
12326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * See the License for the specific language governing permissions and
14326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * limitations under the License.
15326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams */
16326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
17326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams#ifndef ANDROID_STRUCTURED_TYPE_H
18326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams#define ANDROID_STRUCTURED_TYPE_H
19326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
20326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams#include "rsElement.h"
21326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
22326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams// ---------------------------------------------------------------------------
23326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsnamespace android {
24326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsnamespace renderscript {
254a43e3ea8a0328c56ace0af1869b4c8c781a7b26Alex Sakhartchouk/*****************************************************************************
264a43e3ea8a0328c56ace0af1869b4c8c781a7b26Alex Sakhartchouk * CAUTION
274a43e3ea8a0328c56ace0af1869b4c8c781a7b26Alex Sakhartchouk *
284a43e3ea8a0328c56ace0af1869b4c8c781a7b26Alex Sakhartchouk * Any layout changes for this class may require a corresponding change to be
294a43e3ea8a0328c56ace0af1869b4c8c781a7b26Alex Sakhartchouk * made to frameworks/compile/libbcc/lib/ScriptCRT/rs_core.c, which contains
304a43e3ea8a0328c56ace0af1869b4c8c781a7b26Alex Sakhartchouk * a partial copy of the information below.
314a43e3ea8a0328c56ace0af1869b4c8c781a7b26Alex Sakhartchouk *
324a43e3ea8a0328c56ace0af1869b4c8c781a7b26Alex Sakhartchouk *****************************************************************************/
33e5ffb879ae535a899a486285a23bea05e912480fJason Sams
34afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukclass Type : public ObjectBase {
35326e0ddf89e8df2837752fbfd7a014814b32082cJason Samspublic:
3650bfc354e61e174a465893fd0dafe913f1954478Alex Sakhartchouk    struct Hal {
3750bfc354e61e174a465893fd0dafe913f1954478Alex Sakhartchouk        mutable void *drv;
3850bfc354e61e174a465893fd0dafe913f1954478Alex Sakhartchouk
3950bfc354e61e174a465893fd0dafe913f1954478Alex Sakhartchouk        struct State {
4050bfc354e61e174a465893fd0dafe913f1954478Alex Sakhartchouk            const Element * element;
4150bfc354e61e174a465893fd0dafe913f1954478Alex Sakhartchouk
4250bfc354e61e174a465893fd0dafe913f1954478Alex Sakhartchouk            // Size of the structure in the various dimensions.  A missing Dimension is
4350bfc354e61e174a465893fd0dafe913f1954478Alex Sakhartchouk            // specified as a 0 and not a 1.
4450bfc354e61e174a465893fd0dafe913f1954478Alex Sakhartchouk            uint32_t dimX;
4550bfc354e61e174a465893fd0dafe913f1954478Alex Sakhartchouk            uint32_t dimY;
4650bfc354e61e174a465893fd0dafe913f1954478Alex Sakhartchouk            uint32_t dimZ;
47246fbee66ccb17cb1f08a5420e104a709183bce1Alex Sakhartchouk            uint32_t *lodDimX;
48246fbee66ccb17cb1f08a5420e104a709183bce1Alex Sakhartchouk            uint32_t *lodDimY;
49246fbee66ccb17cb1f08a5420e104a709183bce1Alex Sakhartchouk            uint32_t *lodDimZ;
5061656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams            uint32_t *_unused;
51246fbee66ccb17cb1f08a5420e104a709183bce1Alex Sakhartchouk            uint32_t lodCount;
52a572aca4eb4ddb32c10baa1f529431cfefd756b8Jason Sams            uint32_t dimYuv;
5350bfc354e61e174a465893fd0dafe913f1954478Alex Sakhartchouk            bool faces;
5450bfc354e61e174a465893fd0dafe913f1954478Alex Sakhartchouk        };
5550bfc354e61e174a465893fd0dafe913f1954478Alex Sakhartchouk        State state;
5650bfc354e61e174a465893fd0dafe913f1954478Alex Sakhartchouk    };
5750bfc354e61e174a465893fd0dafe913f1954478Alex Sakhartchouk    Hal mHal;
5850bfc354e61e174a465893fd0dafe913f1954478Alex Sakhartchouk
5949a87772b9abe620d28cbe110260424b8950055dTim Murray    void operator delete(void* ptr);
6049a87772b9abe620d28cbe110260424b8950055dTim Murray
61326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    Type * createTex2D(const Element *, size_t w, size_t h, bool mip);
62326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
6361656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams    size_t getCellCount() const {return mCellCount;}
64326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    size_t getElementSizeBytes() const {return mElement->getSizeBytes();}
6561656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams    size_t getPackedSizeBytes() const {return mCellCount * mElement->getSizeBytes();}
66326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    const Element * getElement() const {return mElement.get();}
67326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
6850bfc354e61e174a465893fd0dafe913f1954478Alex Sakhartchouk    uint32_t getDimX() const {return mHal.state.dimX;}
6950bfc354e61e174a465893fd0dafe913f1954478Alex Sakhartchouk    uint32_t getDimY() const {return mHal.state.dimY;}
7050bfc354e61e174a465893fd0dafe913f1954478Alex Sakhartchouk    uint32_t getDimZ() const {return mHal.state.dimZ;}
71246fbee66ccb17cb1f08a5420e104a709183bce1Alex Sakhartchouk    bool getDimLOD() const {return mDimLOD;}
7250bfc354e61e174a465893fd0dafe913f1954478Alex Sakhartchouk    bool getDimFaces() const {return mHal.state.faces;}
73a572aca4eb4ddb32c10baa1f529431cfefd756b8Jason Sams    uint32_t getDimYuv() const {return mHal.state.dimYuv;}
74326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
75246fbee66ccb17cb1f08a5420e104a709183bce1Alex Sakhartchouk    uint32_t getLODDimX(uint32_t lod) const {
76246fbee66ccb17cb1f08a5420e104a709183bce1Alex Sakhartchouk        rsAssert(lod < mHal.state.lodCount);
77246fbee66ccb17cb1f08a5420e104a709183bce1Alex Sakhartchouk        return mHal.state.lodDimX[lod];
78246fbee66ccb17cb1f08a5420e104a709183bce1Alex Sakhartchouk    }
79246fbee66ccb17cb1f08a5420e104a709183bce1Alex Sakhartchouk    uint32_t getLODDimY(uint32_t lod) const {
80246fbee66ccb17cb1f08a5420e104a709183bce1Alex Sakhartchouk        rsAssert(lod < mHal.state.lodCount);
81246fbee66ccb17cb1f08a5420e104a709183bce1Alex Sakhartchouk        return mHal.state.lodDimY[lod];
82246fbee66ccb17cb1f08a5420e104a709183bce1Alex Sakhartchouk    }
83246fbee66ccb17cb1f08a5420e104a709183bce1Alex Sakhartchouk    uint32_t getLODDimZ(uint32_t lod) const {
84246fbee66ccb17cb1f08a5420e104a709183bce1Alex Sakhartchouk        rsAssert(lod < mHal.state.lodCount);
85246fbee66ccb17cb1f08a5420e104a709183bce1Alex Sakhartchouk        return mHal.state.lodDimZ[lod];
86246fbee66ccb17cb1f08a5420e104a709183bce1Alex Sakhartchouk    }
87bcac9340126b4b9fabf2015a2f6a984414d87c21Jason Sams
88246fbee66ccb17cb1f08a5420e104a709183bce1Alex Sakhartchouk    uint32_t getLODCount() const {return mHal.state.lodCount;}
89ef21edcc70fc2734a3dc7995d3c3af1f90d16ef8Jason Sams    bool getIsNp2() const;
90326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
91326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    void clear();
92326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    void compute();
93326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
94e12c1c591b4219e80f29c6c0e0c62c9578b75450Jason Sams    void dumpLOGV(const char *prefix) const;
95e3150cfb3edb028407669e4a65e087eae77e718cJason Sams    virtual void serialize(Context *rsc, OStream *stream) const;
96b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_TYPE; }
97fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    static Type *createFromStream(Context *rsc, IStream *stream);
98326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
99c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    ObjectBaseRef<Type> cloneAndResize1D(Context *rsc, uint32_t dimX) const;
100c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    ObjectBaseRef<Type> cloneAndResize2D(Context *rsc, uint32_t dimX, uint32_t dimY) const;
101383e5b1f68c321a77bfd7466fa1171a9bfab4a6fAlex Sakhartchouk
102c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    static ObjectBaseRef<Type> getTypeRef(Context *rsc, const Element *e,
103c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                          uint32_t dimX, uint32_t dimY, uint32_t dimZ,
104a572aca4eb4ddb32c10baa1f529431cfefd756b8Jason Sams                                          bool dimLOD, bool dimFaces, uint32_t dimYuv);
10596abf819e50b59ba8cf886c13f894633eb0a24baJason Sams
106c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    static Type* getType(Context *rsc, const Element *e,
107c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                         uint32_t dimX, uint32_t dimY, uint32_t dimZ,
108a572aca4eb4ddb32c10baa1f529431cfefd756b8Jason Sams                         bool dimLOD, bool dimFaces, uint32_t yuv) {
109a572aca4eb4ddb32c10baa1f529431cfefd756b8Jason Sams        ObjectBaseRef<Type> type = getTypeRef(rsc, e, dimX, dimY, dimZ, dimLOD, dimFaces, yuv);
110c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk        type->incUserRef();
111c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk        return type.get();
112c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    }
113f0c1df480304a72ce41e7d4b088319cbd7f0938aJason Sams
1149f70a4e63825afe9f786483722e1669b3625f5e9Stephen Hines    void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
1159f70a4e63825afe9f786483722e1669b3625f5e9Stephen Hines    void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
116a36c50a6ab87f4c9049318d4c6c8ec7b0a1e6e12Jason Sams    virtual void callUpdateCacheObject(const Context *rsc, void *dstObj) const;
1179f70a4e63825afe9f786483722e1669b3625f5e9Stephen Hines
118326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsprotected:
119326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    void makeLODTable();
120246fbee66ccb17cb1f08a5420e104a709183bce1Alex Sakhartchouk    bool mDimLOD;
121326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
122326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    // Internal structure from most to least significant.
123326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    // * Array dimensions
124326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    // * Faces
125326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    // * Mipmaps
126326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    // * xyz
127326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
128326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    ObjectBaseRef<const Element> mElement;
129326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
130326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    // count of mipmap levels, 0 indicates no mipmapping
131326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
13261656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams    size_t mCellCount;
133225afd317e101a7be5fe02c0a86361146ea89f05Jason Samsprotected:
134c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    virtual void preDestroy() const;
135f0c1df480304a72ce41e7d4b088319cbd7f0938aJason Sams    virtual ~Type();
136225afd317e101a7be5fe02c0a86361146ea89f05Jason Sams
137326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsprivate:
138f0c1df480304a72ce41e7d4b088319cbd7f0938aJason Sams    Type(Context *);
139326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    Type(const Type &);
140326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams};
141326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
142326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
143326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsclass TypeState {
144326e0ddf89e8df2837752fbfd7a014814b32082cJason Samspublic:
145326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    TypeState();
146326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    ~TypeState();
147326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
1488154954868694e1f233d87d4933a474518b1cb81Jason Sams    // Cache of all existing types.
1498154954868694e1f233d87d4933a474518b1cb81Jason Sams    Vector<Type *> mTypes;
150326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams};
151326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
152326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
153326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
154326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
155326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams#endif //ANDROID_STRUCTURED_TYPE
156