rsAllocation.h revision eb4fe18dd88634330f9566cbb9e785d8c7ec5813
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
28class Allocation : public ObjectBase {
29    // The graphics equilivent of malloc.  The allocation contains a structure of elements.
30
31public:
32    struct Hal {
33        void * drv;
34
35        struct State {
36            ObjectBaseRef<const Type> type;
37
38            uint32_t usageFlags;
39            RsAllocationMipmapControl mipmapControl;
40
41            // Cached fields from the Type and Element
42            // to prevent pointer chasing in critical loops.
43            uint32_t dimensionX;
44            uint32_t dimensionY;
45            uint32_t dimensionZ;
46            uint32_t elementSizeBytes;
47            bool hasMipmaps;
48            bool hasFaces;
49            bool hasReferences;
50        };
51        State state;
52
53        struct DrvState {
54            void * mallocPtr;
55        } drvState;
56
57    };
58    Hal mHal;
59
60    static Allocation * createAllocation(Context *rsc, const Type *, uint32_t usages,
61                                  RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE);
62
63    virtual ~Allocation();
64    void updateCache();
65
66    void * getPtr() const {return mHal.drvState.mallocPtr;}
67    const Type * getType() const {return mHal.state.type.get();}
68
69    void syncAll(Context *rsc, RsAllocationUsageType src);
70
71    void copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len);
72
73    void resize1D(Context *rsc, uint32_t dimX);
74    void resize2D(Context *rsc, uint32_t dimX, uint32_t dimY);
75
76    void data(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, const void *data, uint32_t sizeBytes);
77    void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
78                 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes);
79    void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, RsAllocationCubemapFace face,
80                 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes);
81
82    void elementData(Context *rsc, uint32_t x,
83                        const void *data, uint32_t elementOff, uint32_t sizeBytes);
84    void elementData(Context *rsc, uint32_t x, uint32_t y,
85                        const void *data, uint32_t elementOff, uint32_t sizeBytes);
86
87    void read(void *data);
88
89    void addProgramToDirty(const Program *);
90    void removeProgramToDirty(const Program *);
91
92    virtual void dumpLOGV(const char *prefix) const;
93    virtual void serialize(OStream *stream) const;
94    virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; }
95    static Allocation *createFromStream(Context *rsc, IStream *stream);
96
97    bool getIsScript() const {
98        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0;
99    }
100    bool getIsTexture() const {
101        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0;
102    }
103    bool getIsRenderTarget() const {
104        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) != 0;
105    }
106    bool getIsBufferObject() const {
107        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0;
108    }
109
110    void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
111    void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
112
113    void sendDirty(const Context *rsc) const;
114    bool getHasGraphicsMipmaps() const {
115        return mHal.state.mipmapControl != RS_ALLOCATION_MIPMAP_NONE;
116    }
117
118
119protected:
120    Vector<const Program *> mToDirtyList;
121
122private:
123    Allocation(Context *rsc, const Type *, uint32_t usages, RsAllocationMipmapControl mc);
124
125    void upload2DTexture(bool isFirstUpload);
126    void update2DTexture(const void *ptr, uint32_t xoff, uint32_t yoff,
127                         uint32_t lod, RsAllocationCubemapFace face, uint32_t w, uint32_t h);
128
129    void allocScriptMemory();
130    void freeScriptMemory();
131
132};
133
134}
135}
136#endif
137
138