rsAllocation.h revision 41e373d91a60043afa0f9abd026218b49cbc1201
1/*
2 * Copyright (C) 2009 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_ALLOCATION_H
18#define ANDROID_STRUCTURED_ALLOCATION_H
19
20#include "rsType.h"
21
22// ---------------------------------------------------------------------------
23namespace android {
24namespace renderscript {
25
26class Program;
27
28/*****************************************************************************
29 * CAUTION
30 *
31 * Any layout changes for this class may require a corresponding change to be
32 * made to frameworks/compile/libbcc/lib/ScriptCRT/rs_core.c, which contains
33 * a partial copy of the information below.
34 *
35 *****************************************************************************/
36class Allocation : public ObjectBase {
37    // The graphics equivalent of malloc.  The allocation contains a structure of elements.
38
39public:
40    struct Hal {
41        void * drv;
42
43        struct State {
44            const Type * type;
45
46            uint32_t usageFlags;
47            RsAllocationMipmapControl mipmapControl;
48
49            // Cached fields from the Type and Element
50            // to prevent pointer chasing in critical loops.
51            uint32_t dimensionX;
52            uint32_t dimensionY;
53            uint32_t dimensionZ;
54            uint32_t elementSizeBytes;
55            bool hasMipmaps;
56            bool hasFaces;
57            bool hasReferences;
58            int32_t surfaceTextureID;
59
60            void * usrPtr;
61        };
62        State state;
63
64        struct DrvState {
65            void * mallocPtr;
66        } drvState;
67
68    };
69    Hal mHal;
70
71    static Allocation * createAllocation(Context *rsc, const Type *, uint32_t usages,
72                                         RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE,
73                                         void *ptr = 0);
74    virtual ~Allocation();
75    void updateCache();
76
77    void * getPtr() const {return mHal.drvState.mallocPtr;}
78    const Type * getType() const {return mHal.state.type;}
79
80    void syncAll(Context *rsc, RsAllocationUsageType src);
81
82    void copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len);
83
84    void resize1D(Context *rsc, uint32_t dimX);
85    void resize2D(Context *rsc, uint32_t dimX, uint32_t dimY);
86
87    void data(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, const void *data, uint32_t sizeBytes);
88    void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
89                 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes);
90    void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, RsAllocationCubemapFace face,
91                 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes);
92
93    void elementData(Context *rsc, uint32_t x,
94                        const void *data, uint32_t elementOff, uint32_t sizeBytes);
95    void elementData(Context *rsc, uint32_t x, uint32_t y,
96                        const void *data, uint32_t elementOff, uint32_t sizeBytes);
97
98    void read(void *data);
99
100    void addProgramToDirty(const Program *);
101    void removeProgramToDirty(const Program *);
102
103    virtual void dumpLOGV(const char *prefix) const;
104    virtual void serialize(OStream *stream) const;
105    virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; }
106    static Allocation *createFromStream(Context *rsc, IStream *stream);
107
108    bool getIsScript() const {
109        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0;
110    }
111    bool getIsTexture() const {
112        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0;
113    }
114    bool getIsRenderTarget() const {
115        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) != 0;
116    }
117    bool getIsBufferObject() const {
118        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0;
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    virtual bool freeChildren();
124
125    void sendDirty(const Context *rsc) const;
126    bool getHasGraphicsMipmaps() const {
127        return mHal.state.mipmapControl != RS_ALLOCATION_MIPMAP_NONE;
128    }
129
130    int32_t getSurfaceTextureID(const Context *rsc);
131
132protected:
133    Vector<const Program *> mToDirtyList;
134    ObjectBaseRef<const Type> mType;
135    void setType(const Type *t) {
136        mType.set(t);
137        mHal.state.type = t;
138    }
139
140private:
141    void freeChildrenUnlocked();
142    Allocation(Context *rsc, const Type *, uint32_t usages, RsAllocationMipmapControl mc, void *ptr);
143
144    uint32_t getPackedSize() const;
145    static void writePackedData(const Type *type, uint8_t *dst, const uint8_t *src, bool dstPadded);
146    void unpackVec3Allocation(const void *data, uint32_t dataSize);
147    void packVec3Allocation(OStream *stream) const;
148};
149
150}
151}
152#endif
153
154