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