rsAllocation.h revision 733396b67724162844ea2785c7495115dc5ee8d8
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_ALLOCATION_H
18#define ANDROID_STRUCTURED_ALLOCATION_H
19
20#include "rsType.h"
21
22struct ANativeWindow;
23
24// ---------------------------------------------------------------------------
25namespace android {
26class GLConsumer;
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 yuv;
58            uint32_t elementSizeBytes;
59            bool hasMipmaps;
60            bool hasFaces;
61            bool hasReferences;
62            void * userProvidedPtr;
63            int32_t surfaceTextureID;
64            ANativeWindow *wndSurface;
65            GLConsumer *surfaceTexture;
66        };
67        State state;
68
69        struct DrvState {
70            struct LodState {
71                void * mallocPtr;
72                size_t stride;
73                uint32_t dimX;
74                uint32_t dimY;
75                uint32_t dimZ;
76            } lod[android::renderscript::Allocation::MAX_LOD];
77            size_t faceOffset;
78            uint32_t lodCount;
79            uint32_t faceCount;
80        };
81        mutable DrvState drvState;
82
83    };
84    Hal mHal;
85
86    static Allocation * createAllocation(Context *rsc, const Type *, uint32_t usages,
87                                         RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE,
88                                         void *ptr = 0);
89    virtual ~Allocation();
90    void updateCache();
91
92    const Type * getType() const {return mHal.state.type;}
93
94    void syncAll(Context *rsc, RsAllocationUsageType src);
95
96    void copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len);
97
98    void resize1D(Context *rsc, uint32_t dimX);
99    void resize2D(Context *rsc, uint32_t dimX, uint32_t dimY);
100
101    void data(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, const void *data, size_t sizeBytes);
102    void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
103              uint32_t w, uint32_t h, const void *data, size_t sizeBytes);
104    void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
105              uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride);
106    void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, RsAllocationCubemapFace face,
107              uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes);
108
109    void read(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, void *data, size_t sizeBytes);
110    void readUnchecked(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, void *data, size_t sizeBytes);
111    void read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
112              uint32_t w, uint32_t h, void *data, size_t sizeBytes);
113    void read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
114              uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride);
115    void read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, RsAllocationCubemapFace face,
116              uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes);
117
118    void elementData(Context *rsc, uint32_t x,
119                     const void *data, uint32_t elementOff, size_t sizeBytes);
120    void elementData(Context *rsc, uint32_t x, uint32_t y,
121                     const void *data, uint32_t elementOff, size_t sizeBytes);
122
123    void addProgramToDirty(const Program *);
124    void removeProgramToDirty(const Program *);
125
126    virtual void dumpLOGV(const char *prefix) const;
127    virtual void serialize(Context *rsc, OStream *stream) const;
128    virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; }
129    static Allocation *createFromStream(Context *rsc, IStream *stream);
130
131    bool getIsScript() const {
132        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0;
133    }
134    bool getIsTexture() const {
135        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0;
136    }
137    bool getIsRenderTarget() const {
138        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) != 0;
139    }
140    bool getIsBufferObject() const {
141        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0;
142    }
143
144    void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
145    void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
146    virtual bool freeChildren();
147
148    void sendDirty(const Context *rsc) const;
149    bool getHasGraphicsMipmaps() const {
150        return mHal.state.mipmapControl != RS_ALLOCATION_MIPMAP_NONE;
151    }
152
153    void * getSurface(const Context *rsc);
154    void setSurfaceTexture(const Context *rsc, GLConsumer *st);
155    void setSurface(const Context *rsc, RsNativeWindow sur);
156    void ioSend(const Context *rsc);
157    void ioReceive(const Context *rsc);
158
159protected:
160    Vector<const Program *> mToDirtyList;
161    ObjectBaseRef<const Type> mType;
162    void setType(const Type *t) {
163        mType.set(t);
164        mHal.state.type = t;
165    }
166
167private:
168    void freeChildrenUnlocked();
169    Allocation(Context *rsc, const Type *, uint32_t usages, RsAllocationMipmapControl mc, void *ptr);
170
171    uint32_t getPackedSize() const;
172    static void writePackedData(Context *rsc, const Type *type, uint8_t *dst,
173                                const uint8_t *src, bool dstPadded);
174    void unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize);
175    void packVec3Allocation(Context *rsc, OStream *stream) const;
176};
177
178}
179}
180#endif
181
182