• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ANDROID_NATIVES_H
18 #define ANDROID_ANDROID_NATIVES_H
19 
20 #include <sys/types.h>
21 #include <string.h>
22 
23 #include <hardware/gralloc.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /*****************************************************************************/
30 
31 #define ANDROID_NATIVE_MAKE_CONSTANT(a,b,c,d) \
32     (((unsigned)(a)<<24)|((unsigned)(b)<<16)|((unsigned)(c)<<8)|(unsigned)(d))
33 
34 #define ANDROID_NATIVE_WINDOW_MAGIC \
35     ANDROID_NATIVE_MAKE_CONSTANT('_','w','n','d')
36 
37 #define ANDROID_NATIVE_BUFFER_MAGIC \
38     ANDROID_NATIVE_MAKE_CONSTANT('_','b','f','r')
39 
40 // ---------------------------------------------------------------------------
41 
42 struct android_native_buffer_t;
43 
44 // ---------------------------------------------------------------------------
45 
46 typedef struct android_native_base_t
47 {
48     /* a magic value defined by the actual EGL native type */
49     int magic;
50 
51     /* the sizeof() of the actual EGL native type */
52     int version;
53 
54     void* reserved[4];
55 
56     /* reference-counting interface */
57     void (*incRef)(struct android_native_base_t* base);
58     void (*decRef)(struct android_native_base_t* base);
59 } android_native_base_t;
60 
61 // ---------------------------------------------------------------------------
62 
63 /* attributes queriable with query() */
64 enum {
65     NATIVE_WINDOW_WIDTH     = 0,
66     NATIVE_WINDOW_HEIGHT    = 1,
67     NATIVE_WINDOW_FORMAT    = 2,
68 };
69 
70 /* valid operations for the (*perform)() hook */
71 enum {
72     NATIVE_WINDOW_SET_USAGE = 0
73 };
74 
75 typedef struct android_native_window_t
76 {
77 #ifdef __cplusplus
android_native_window_tandroid_native_window_t78     android_native_window_t()
79         : flags(0), minSwapInterval(0), maxSwapInterval(0), xdpi(0), ydpi(0)
80     {
81         common.magic = ANDROID_NATIVE_WINDOW_MAGIC;
82         common.version = sizeof(android_native_window_t);
83         memset(common.reserved, 0, sizeof(common.reserved));
84     }
85 #endif
86 
87     struct android_native_base_t common;
88 
89     /* flags describing some attributes of this surface or its updater */
90     const uint32_t flags;
91 
92     /* min swap interval supported by this updated */
93     const int   minSwapInterval;
94 
95     /* max swap interval supported by this updated */
96     const int   maxSwapInterval;
97 
98     /* horizontal and vertical resolution in DPI */
99     const float xdpi;
100     const float ydpi;
101 
102     /* Some storage reserved for the OEM's driver. */
103     intptr_t    oem[4];
104 
105 
106     /*
107      * Set the swap interval for this surface.
108      *
109      * Returns 0 on success or -errno on error.
110      */
111     int     (*setSwapInterval)(struct android_native_window_t* window,
112                 int interval);
113 
114     /*
115      * hook called by EGL to acquire a buffer. After this call, the buffer
116      * is not locked, so its content cannot be modified.
117      * this call may block if no buffers are available.
118      *
119      * Returns 0 on success or -errno on error.
120      */
121     int     (*dequeueBuffer)(struct android_native_window_t* window,
122                 struct android_native_buffer_t** buffer);
123 
124     /*
125      * hook called by EGL to lock a buffer. This MUST be called before modifying
126      * the content of a buffer. The buffer must have been acquired with
127      * dequeueBuffer first.
128      *
129      * Returns 0 on success or -errno on error.
130      */
131     int     (*lockBuffer)(struct android_native_window_t* window,
132                 struct android_native_buffer_t* buffer);
133    /*
134     * hook called by EGL when modifications to the render buffer are done.
135     * This unlocks and post the buffer.
136     *
137     * Buffers MUST be queued in the same order than they were dequeued.
138     *
139     * Returns 0 on success or -errno on error.
140     */
141     int     (*queueBuffer)(struct android_native_window_t* window,
142                 struct android_native_buffer_t* buffer);
143 
144     /*
145      * hook used to retrieve information about the native window.
146      *
147      * Returns 0 on success or -errno on error.
148      */
149     int     (*query)(struct android_native_window_t* window,
150                 int what, int* value);
151 
152     /*
153      * hook used to perform various operations on the surface.
154      * (*perform)() is a generic mechanism to add functionality to
155      * android_native_window_t while keeping backward binary compatibility.
156      *
157      * This hook should not be called directly, instead use the helper functions
158      * defined below.
159      *
160      * The valid operations are:
161      *     NATIVE_WINDOW_SET_USAGE
162      *
163      */
164 
165     int     (*perform)(struct android_native_window_t* window,
166                 int operation, ... );
167 
168     void* reserved_proc[3];
169 } android_native_window_t;
170 
171 
172 /*
173  *  native_window_set_usage() sets the intended usage flags for the next
174  *  buffers acquired with (*lockBuffer)() and on.
175  *  By default (if this function is never called), a usage of
176  *      GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE
177  *  is assumed.
178  *  Calling this function will usually cause following buffers to be
179  *  reallocated.
180  */
181 
native_window_set_usage(android_native_window_t * window,int usage)182 static inline int native_window_set_usage(
183         android_native_window_t* window, int usage)
184 {
185     return window->perform(window, NATIVE_WINDOW_SET_USAGE, usage);
186 }
187 
188 
189 // ---------------------------------------------------------------------------
190 
191 /* FIXME: this is legacy for pixmaps */
192 typedef struct egl_native_pixmap_t
193 {
194     int32_t     version;    /* must be 32 */
195     int32_t     width;
196     int32_t     height;
197     int32_t     stride;
198     uint8_t*    data;
199     uint8_t     format;
200     uint8_t     rfu[3];
201     union {
202         uint32_t    compressedFormat;
203         int32_t     vstride;
204     };
205     int32_t     reserved;
206 } egl_native_pixmap_t;
207 
208 /*****************************************************************************/
209 
210 #ifdef __cplusplus
211 }
212 #endif
213 
214 
215 /*****************************************************************************/
216 
217 #ifdef __cplusplus
218 
219 #include <utils/RefBase.h>
220 
221 namespace android {
222 
223 /*
224  * This helper class turns an EGL android_native_xxx type into a C++
225  * reference-counted object; with proper type conversions.
226  */
227 template <typename NATIVE_TYPE, typename TYPE, typename REF>
228 class EGLNativeBase : public NATIVE_TYPE, public REF
229 {
230 protected:
231     typedef EGLNativeBase<NATIVE_TYPE, TYPE, REF> BASE;
EGLNativeBase()232     EGLNativeBase() : NATIVE_TYPE(), REF() {
233         NATIVE_TYPE::common.incRef = incRef;
234         NATIVE_TYPE::common.decRef = decRef;
235     }
getSelf(NATIVE_TYPE * self)236     static inline TYPE* getSelf(NATIVE_TYPE* self) {
237         return static_cast<TYPE*>(self);
238     }
getSelf(NATIVE_TYPE const * self)239     static inline TYPE const* getSelf(NATIVE_TYPE const* self) {
240         return static_cast<TYPE const *>(self);
241     }
getSelf(android_native_base_t * base)242     static inline TYPE* getSelf(android_native_base_t* base) {
243         return getSelf(reinterpret_cast<NATIVE_TYPE*>(base));
244     }
getSelf(android_native_base_t const * base)245     static inline TYPE const * getSelf(android_native_base_t const* base) {
246         return getSelf(reinterpret_cast<NATIVE_TYPE const*>(base));
247     }
incRef(android_native_base_t * base)248     static void incRef(android_native_base_t* base) {
249         EGLNativeBase* self = getSelf(base);
250         self->incStrong(self);
251     }
decRef(android_native_base_t * base)252     static void decRef(android_native_base_t* base) {
253         EGLNativeBase* self = getSelf(base);
254         self->decStrong(self);
255     }
256 };
257 
258 } // namespace android
259 #endif // __cplusplus
260 
261 /*****************************************************************************/
262 
263 #endif /* ANDROID_ANDROID_NATIVES_H */
264