blob: 5cf629257377c4e0c029a1edf12915266205d870 [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
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
Jason Sams83f1c632009-10-26 15:19:28 -070026class Program;
Jason Samsd19f10d2009-05-22 14:03:28 -070027
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080028class Allocation : public ObjectBase {
Jason Samsd19f10d2009-05-22 14:03:28 -070029 // The graphics equilivent of malloc. The allocation contains a structure of elements.
30
Jason Samsd19f10d2009-05-22 14:03:28 -070031public:
Jason Samse4a06c52011-03-16 16:29:28 -070032 struct Hal {
33 void * drv;
34
35 struct State {
36 ObjectBaseRef<const Type> type;
Jason Samse4a06c52011-03-16 16:29:28 -070037
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;
Jason Sams7e8aae72011-05-26 16:33:01 -070052
53 struct DrvState {
54 void * mallocPtr;
55 } drvState;
56
Jason Samse4a06c52011-03-16 16:29:28 -070057 };
58 Hal mHal;
59
Jason Sams7e8aae72011-05-26 16:33:01 -070060 static Allocation * createAllocation(Context *rsc, const Type *, uint32_t usages,
61 RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE);
Jason Sams8a64743f2010-03-01 15:31:04 -080062
Jason Samsd19f10d2009-05-22 14:03:28 -070063 virtual ~Allocation();
Jason Samse4a06c52011-03-16 16:29:28 -070064 void updateCache();
Jason Samsd19f10d2009-05-22 14:03:28 -070065
Jason Sams7e8aae72011-05-26 16:33:01 -070066 void * getPtr() const {return mHal.drvState.mallocPtr;}
Jason Samse4a06c52011-03-16 16:29:28 -070067 const Type * getType() const {return mHal.state.type.get();}
Jason Samsd19f10d2009-05-22 14:03:28 -070068
Jason Sams5476b452010-12-08 16:14:36 -080069 void syncAll(Context *rsc, RsAllocationUsageType src);
70
Jason Sams5edc6082010-10-05 13:32:49 -070071 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);
Jason Samsd19f10d2009-05-22 14:03:28 -070075
Jason Sams49a05d72010-12-29 14:31:29 -080076 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,
Jason Sams07ae4062009-08-27 20:23:34 -070078 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes);
Jason Sams49a05d72010-12-29 14:31:29 -080079 void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, RsAllocationCubemapFace face,
Jason Sams07ae4062009-08-27 20:23:34 -070080 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -070081
Jason Sams49a05d72010-12-29 14:31:29 -080082 void elementData(Context *rsc, uint32_t x,
Jason Sams49bdaf02010-08-31 13:50:42 -070083 const void *data, uint32_t elementOff, uint32_t sizeBytes);
Jason Sams49a05d72010-12-29 14:31:29 -080084 void elementData(Context *rsc, uint32_t x, uint32_t y,
Jason Sams49bdaf02010-08-31 13:50:42 -070085 const void *data, uint32_t elementOff, uint32_t sizeBytes);
86
Jason Sams40a29e82009-08-10 14:55:26 -070087 void read(void *data);
88
Jason Sams83f1c632009-10-26 15:19:28 -070089 void addProgramToDirty(const Program *);
90 void removeProgramToDirty(const Program *);
Jason Sams1bada8c2009-08-09 17:01:55 -070091
Jason Sams715333b2009-11-17 17:26:46 -080092 virtual void dumpLOGV(const char *prefix) const;
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070093 virtual void serialize(OStream *stream) const;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070094 virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; }
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070095 static Allocation *createFromStream(Context *rsc, IStream *stream);
Jason Sams715333b2009-11-17 17:26:46 -080096
Jason Sams5e0035a2010-12-13 17:11:21 -080097 bool getIsScript() const {
Jason Samse4a06c52011-03-16 16:29:28 -070098 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0;
Jason Sams5e0035a2010-12-13 17:11:21 -080099 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800100 bool getIsTexture() const {
Jason Samse4a06c52011-03-16 16:29:28 -0700101 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0;
Jason Samsd4b23b52010-12-13 15:32:35 -0800102 }
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700103 bool getIsRenderTarget() const {
104 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) != 0;
105 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800106 bool getIsBufferObject() const {
Jason Samse4a06c52011-03-16 16:29:28 -0700107 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0;
Jason Samsd4b23b52010-12-13 15:32:35 -0800108 }
Jason Samseeeaccc2010-06-25 12:45:41 -0700109
Jason Sams5edc6082010-10-05 13:32:49 -0700110 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;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700112
Jason Sams7e8aae72011-05-26 16:33:01 -0700113 void sendDirty(const Context *rsc) const;
Jason Sams5e0035a2010-12-13 17:11:21 -0800114 bool getHasGraphicsMipmaps() const {
Jason Samse4a06c52011-03-16 16:29:28 -0700115 return mHal.state.mipmapControl != RS_ALLOCATION_MIPMAP_NONE;
Jason Sams5e0035a2010-12-13 17:11:21 -0800116 }
117
Jason Sams83f1c632009-10-26 15:19:28 -0700118
Alex Sakhartchouk8e954662010-09-01 16:34:48 -0700119protected:
Jason Sams83f1c632009-10-26 15:19:28 -0700120 Vector<const Program *> mToDirtyList;
121
Jason Sams8a64743f2010-03-01 15:31:04 -0800122private:
Jason Sams7e8aae72011-05-26 16:33:01 -0700123 Allocation(Context *rsc, const Type *, uint32_t usages, RsAllocationMipmapControl mc);
124
Jason Samsef70a202011-01-13 17:38:18 -0800125 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);
Jason Sams5e0035a2010-12-13 17:11:21 -0800128
129 void allocScriptMemory();
130 void freeScriptMemory();
131
Jason Samsd19f10d2009-05-22 14:03:28 -0700132};
133
134}
135}
136#endif
137