OSDN Git Service

Set up Android-specific debugging
[android-x86/external-swiftshader.git] / src / OpenGL / libEGL / Image.hpp
1 #ifndef egl_Image_hpp\r
2 #define egl_Image_hpp\r
3 \r
4 #include "Renderer/Surface.hpp"\r
5 \r
6 #if defined(__ANDROID__)\r
7 #include <hardware/gralloc.h>\r
8 #include <system/window.h>\r
9 #endif\r
10 \r
11 #ifdef __ANDROID__\r
12 #include "../../Common/DebugAndroid.hpp"\r
13 #else\r
14 #include <assert.h>\r
15 #endif\r
16 \r
17 namespace egl\r
18 {\r
19 // Types common between gl.h and gl2.h\r
20 // We can't include either header in EGL\r
21 typedef unsigned int GLenum;\r
22 typedef int GLint;\r
23 typedef int GLsizei;\r
24 \r
25 class Texture;\r
26 \r
27 class Image : public sw::Surface\r
28 {\r
29 public:\r
30         Image(sw::Resource *resource, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, sw::Format internalFormat)\r
31                 : width(width), height(height), format(format), type(type), internalFormat(internalFormat), depth(depth)\r
32                 , sw::Surface(resource, width, height, depth, internalFormat, true, true)\r
33         {\r
34                 shared = false;\r
35 \r
36                 #if defined(__ANDROID__)\r
37                         nativeBuffer = 0;\r
38                         gralloc = 0;\r
39                 #endif\r
40         }\r
41 \r
42         Image(sw::Resource *resource, int width, int height, int depth, sw::Format internalFormat, bool lockable, bool renderTarget)\r
43                 : width(width), height(height), format(0 /*GL_NONE*/), type(0 /*GL_NONE*/), internalFormat(internalFormat), depth(depth)\r
44                 , sw::Surface(resource, width, height, depth, internalFormat, lockable, renderTarget)\r
45         {\r
46                 shared = false;\r
47 \r
48                 #if defined(__ANDROID__)\r
49                         nativeBuffer = 0;\r
50                         gralloc = 0;\r
51                 #endif\r
52         }\r
53 \r
54         GLsizei getWidth() const\r
55         {\r
56                 return width;\r
57         }\r
58 \r
59         GLsizei getHeight() const\r
60         {\r
61                 return height;\r
62         }\r
63 \r
64         int getDepth() const\r
65         {\r
66                 // FIXME: add member if the depth dimension (for 3D textures or 2D testure arrays)\r
67                 // and multi sample depth are ever simultaneously required.\r
68                 return depth;\r
69         }\r
70 \r
71         GLenum getFormat() const\r
72         {\r
73                 return format;\r
74         }\r
75 \r
76         GLenum getType() const\r
77         {\r
78                 return type;\r
79         }\r
80 \r
81         sw::Format getInternalFormat() const\r
82         {\r
83                 return internalFormat;\r
84         }\r
85 \r
86         bool isShared() const\r
87     {\r
88         return shared;\r
89     }\r
90 \r
91     void markShared()\r
92     {\r
93         shared = true;\r
94     }\r
95 \r
96         void *lock(unsigned int left, unsigned int top, sw::Lock lock)\r
97         {\r
98                 #if defined(__ANDROID__)\r
99                         if(nativeBuffer)   // Lock the buffer from ANativeWindowBuffer\r
100                         {\r
101                                 return lockNativeBuffer(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);\r
102                         }\r
103                 #endif\r
104 \r
105                 return lockExternal(left, top, 0, lock, sw::PUBLIC);\r
106         }\r
107 \r
108         unsigned int getPitch() const\r
109         {\r
110                 return getExternalPitchB();\r
111         }\r
112 \r
113         void unlock()\r
114         {\r
115                 #if defined(__ANDROID__)\r
116                         if(nativeBuffer)   // Unlock the buffer from ANativeWindowBuffer\r
117                         {\r
118                                 return unlockNativeBuffer();\r
119                         }\r
120                 #endif\r
121 \r
122                 unlockExternal();\r
123         }\r
124 \r
125         virtual void addRef() = 0;\r
126         virtual void release() = 0;\r
127         virtual void unbind(const Texture *parent) = 0;   // Break parent ownership and release\r
128 \r
129         void destroyShared()   // Release a shared image\r
130         {\r
131                 #if defined(__ANDROID__)\r
132                         if(nativeBuffer)\r
133                         {\r
134                                 nativeBuffer->common.decRef(&nativeBuffer->common);\r
135                         }\r
136                 #endif\r
137 \r
138                 assert(shared);\r
139                 shared = false;\r
140                 release();\r
141         }\r
142 \r
143         virtual void loadImageData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, GLint unpackAlignment, const void *input) = 0;\r
144         virtual void loadCompressedData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLsizei imageSize, const void *pixels) = 0;\r
145 \r
146         #if defined(__ANDROID__)\r
147         void setNativeBuffer(ANativeWindowBuffer* buffer)\r
148         {\r
149                 nativeBuffer = buffer;\r
150                 nativeBuffer->common.incRef(&nativeBuffer->common);\r
151         }\r
152 \r
153         virtual void *lockInternal(int x, int y, int z, sw::Lock lock, sw::Accessor client)\r
154         {\r
155                 if(nativeBuffer)   // Lock the buffer from ANativeWindowBuffer\r
156                 {\r
157                         return lockNativeBuffer(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);\r
158                 }\r
159                 return sw::Surface::lockInternal(x, y, z, lock, client);\r
160         }\r
161 \r
162         virtual void unlockInternal()\r
163         {\r
164                 if(nativeBuffer)   // Unlock the buffer from ANativeWindowBuffer\r
165                 {\r
166                         return unlockNativeBuffer();\r
167                 }\r
168                 return sw::Surface::unlockInternal();\r
169         }\r
170         #endif\r
171 \r
172 protected:\r
173         virtual ~Image()\r
174         {\r
175         }\r
176 \r
177         const GLsizei width;\r
178         const GLsizei height;\r
179         const GLenum format;\r
180         const GLenum type;\r
181         const sw::Format internalFormat;\r
182         const int depth;\r
183 \r
184         bool shared;   // Used as an EGLImage\r
185 \r
186         #if defined(__ANDROID__)\r
187         ANativeWindowBuffer *nativeBuffer;\r
188         gralloc_module_t const *gralloc;\r
189 \r
190         void initGralloc()\r
191         {\r
192                 hw_module_t const *module;\r
193                 hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);\r
194                 gralloc = reinterpret_cast<gralloc_module_t const*>(module);\r
195         }\r
196 \r
197         void* lockNativeBuffer(int usage)\r
198         {\r
199                 if(!gralloc)\r
200                 {\r
201                         initGralloc();\r
202                 }\r
203 \r
204                 void *buffer = 0;\r
205                 gralloc->lock(gralloc, nativeBuffer->handle, usage, 0, 0, nativeBuffer->width, nativeBuffer->height, &buffer);\r
206                 return buffer;\r
207         }\r
208 \r
209         void unlockNativeBuffer()\r
210         {\r
211                 if(!gralloc)\r
212                 {\r
213                         initGralloc();\r
214                 }\r
215 \r
216                 gralloc->unlock(gralloc, nativeBuffer->handle);\r
217         }\r
218         #endif\r
219 };\r
220 }\r
221 \r
222 #endif   // egl_Image_hpp\r