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