rsAllocation.h revision 509ea5c832a865bc9083d53f1f058377a689bab3
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    const static int MAX_LOD = 16;
45
46    struct Hal {
47        void * drv;
48
49        struct State {
50            const Type * type;
51
52            uint32_t usageFlags;
53            RsAllocationMipmapControl mipmapControl;
54
55            // Cached fields from the Type and Element
56            // to prevent pointer chasing in critical loops.
57            uint32_t dimensionX;
58            uint32_t dimensionY;
59            uint32_t dimensionZ;
60            uint32_t elementSizeBytes;
61            bool hasMipmaps;
62            bool hasFaces;
63            bool hasReferences;
64            void * unused_01;
65            int32_t surfaceTextureID;
66            ANativeWindow *wndSurface;
67            SurfaceTexture *surfaceTexture;
68            RsDataType eType;
69        };
70        State state;
71
72        struct DrvState {
73            mutable void * mallocPtrLOD0;
74            mutable uint32_t strideLOD0;
75        } drvState;
76
77    };
78    Hal mHal;
79
80    static Allocation * createAllocation(Context *rsc, const Type *, uint32_t usages,
81                                         RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE,
82                                         void *ptr = 0);
83    virtual ~Allocation();
84    void updateCache();
85
86    const Type * getType() const {return mHal.state.type;}
87
88    void syncAll(Context *rsc, RsAllocationUsageType src);
89
90    void copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len);
91
92    void resize1D(Context *rsc, uint32_t dimX);
93    void resize2D(Context *rsc, uint32_t dimX, uint32_t dimY);
94
95    void data(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, const void *data, size_t sizeBytes);
96    void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
97                 uint32_t w, uint32_t h, const void *data, size_t sizeBytes);
98    void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, RsAllocationCubemapFace face,
99                 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes);
100
101    void read(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, void *data, size_t sizeBytes);
102    void readUnchecked(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, void *data, size_t sizeBytes);
103    void read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
104                 uint32_t w, uint32_t h, void *data, size_t sizeBytes);
105    void read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, RsAllocationCubemapFace face,
106                 uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes);
107
108    void elementData(Context *rsc, uint32_t x,
109                     const void *data, uint32_t elementOff, size_t sizeBytes);
110    void elementData(Context *rsc, uint32_t x, uint32_t y,
111                     const void *data, uint32_t elementOff, size_t sizeBytes);
112
113    void addProgramToDirty(const Program *);
114    void removeProgramToDirty(const Program *);
115
116    virtual void dumpLOGV(const char *prefix) const;
117    virtual void serialize(Context *rsc, OStream *stream) const;
118    virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; }
119    static Allocation *createFromStream(Context *rsc, IStream *stream);
120
121    bool getIsScript() const {
122        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0;
123    }
124    bool getIsTexture() const {
125        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0;
126    }
127    bool getIsRenderTarget() const {
128        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) != 0;
129    }
130    bool getIsBufferObject() const {
131        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0;
132    }
133
134    void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
135    void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
136    virtual bool freeChildren();
137
138    void sendDirty(const Context *rsc) const;
139    bool getHasGraphicsMipmaps() const {
140        return mHal.state.mipmapControl != RS_ALLOCATION_MIPMAP_NONE;
141    }
142
143    int32_t getSurfaceTextureID(const Context *rsc);
144    void setSurfaceTexture(const Context *rsc, SurfaceTexture *st);
145    void setSurface(const Context *rsc, RsNativeWindow sur);
146    void ioSend(const Context *rsc);
147    void ioReceive(const Context *rsc);
148
149protected:
150    Vector<const Program *> mToDirtyList;
151    ObjectBaseRef<const Type> mType;
152    void setType(const Type *t) {
153        mType.set(t);
154        mHal.state.type = t;
155    }
156
157private:
158    void freeChildrenUnlocked();
159    Allocation(Context *rsc, const Type *, uint32_t usages, RsAllocationMipmapControl mc, void *ptr);
160
161    uint32_t getPackedSize() const;
162    static void writePackedData(Context *rsc, const Type *type, uint8_t *dst,
163                                const uint8_t *src, bool dstPadded);
164    void unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize);
165    void packVec3Allocation(Context *rsc, OStream *stream) const;
166};
167
168}
169}
170#endif
171
172