android_natives.h revision 5b5c9144872b4e31ba5a041dce585a8ddbbe495d
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
26extern "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
42struct android_native_buffer_t;
43
44// ---------------------------------------------------------------------------
45
46struct 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};
60
61// ---------------------------------------------------------------------------
62
63/* attributes queriable with query() */
64enum {
65    NATIVE_WINDOW_WIDTH     = 0,
66    NATIVE_WINDOW_HEIGHT    = 1
67};
68
69struct android_native_window_t
70{
71#ifdef __cplusplus
72    android_native_window_t()
73        : flags(0), minSwapInterval(0), maxSwapInterval(0), xdpi(0), ydpi(0)
74    {
75        common.magic = ANDROID_NATIVE_WINDOW_MAGIC;
76        common.version = sizeof(android_native_window_t);
77        memset(common.reserved, 0, sizeof(common.reserved));
78    }
79#endif
80
81    struct android_native_base_t common;
82
83    /* flags describing some attributes of this surface or its updater */
84    const uint32_t flags;
85
86    /* min swap interval supported by this updated */
87    const int   minSwapInterval;
88
89    /* max swap interval supported by this updated */
90    const int   maxSwapInterval;
91
92    /* horizontal and vertical resolution in DPI */
93    const float xdpi;
94    const float ydpi;
95
96    /* Some storage reserved for the OEM's driver. */
97    intptr_t    oem[4];
98
99
100    /*
101     * Set the swap interval for this surface.
102     *
103     * Returns 0 on success or -errno on error.
104     */
105    int     (*setSwapInterval)(struct android_native_window_t* window,
106                int interval);
107
108    /*
109     * hook called by EGL to acquire a buffer. After this call, the buffer
110     * is not locked, so its content cannot be modified.
111     * this call may block if no buffers are available.
112     *
113     * Returns 0 on success or -errno on error.
114     */
115    int     (*dequeueBuffer)(struct android_native_window_t* window,
116                struct android_native_buffer_t** buffer);
117
118    /*
119     * hook called by EGL to lock a buffer. This MUST be called before modifying
120     * the content of a buffer. The buffer must have been acquired with
121     * dequeueBuffer first.
122     *
123     * Returns 0 on success or -errno on error.
124     */
125    int     (*lockBuffer)(struct android_native_window_t* window,
126                struct android_native_buffer_t* buffer);
127   /*
128    * hook called by EGL when modifications to the render buffer are done.
129    * This unlocks and post the buffer.
130    *
131    * Buffers MUST be queued in the same order than they were dequeued.
132    *
133    * Returns 0 on success or -errno on error.
134    */
135    int     (*queueBuffer)(struct android_native_window_t* window,
136                struct android_native_buffer_t* buffer);
137
138    /*
139     * hook used to retrieve information about the native window.
140     *
141     * Returns 0 on success or -errno on error.
142     */
143    int     (*query)(struct android_native_window_t* window,
144            int what, int* value);
145
146    void* reserved_proc[4];
147};
148
149// ---------------------------------------------------------------------------
150
151/* FIXME: this is legacy for pixmaps */
152struct egl_native_pixmap_t
153{
154    int32_t     version;    /* must be 32 */
155    int32_t     width;
156    int32_t     height;
157    int32_t     stride;
158    uint8_t*    data;
159    uint8_t     format;
160    uint8_t     rfu[3];
161    union {
162        uint32_t    compressedFormat;
163        int32_t     vstride;
164    };
165    int32_t     reserved;
166};
167
168/*****************************************************************************/
169
170#ifdef __cplusplus
171}
172#endif
173
174
175/*****************************************************************************/
176
177#ifdef __cplusplus
178
179#include <utils/RefBase.h>
180
181namespace android {
182
183/*
184 * This helper class turns an EGL android_native_xxx type into a C++
185 * reference-counted object; with proper type conversions.
186 */
187template <typename NATIVE_TYPE, typename TYPE, typename REF>
188class EGLNativeBase : public NATIVE_TYPE, public REF
189{
190protected:
191    typedef EGLNativeBase<NATIVE_TYPE, TYPE, REF> BASE;
192    EGLNativeBase() : NATIVE_TYPE(), REF() {
193        NATIVE_TYPE::common.incRef = incRef;
194        NATIVE_TYPE::common.decRef = decRef;
195    }
196    static inline TYPE* getSelf(NATIVE_TYPE* self) {
197        return static_cast<TYPE*>(self);
198    }
199    static inline TYPE const* getSelf(NATIVE_TYPE const* self) {
200        return static_cast<TYPE const *>(self);
201    }
202    static inline TYPE* getSelf(android_native_base_t* base) {
203        return getSelf(reinterpret_cast<NATIVE_TYPE*>(base));
204    }
205    static inline TYPE const * getSelf(android_native_base_t const* base) {
206        return getSelf(reinterpret_cast<NATIVE_TYPE const*>(base));
207    }
208    static void incRef(android_native_base_t* base) {
209        EGLNativeBase* self = getSelf(base);
210        self->incStrong(self);
211    }
212    static void decRef(android_native_base_t* base) {
213        EGLNativeBase* self = getSelf(base);
214        self->decStrong(self);
215    }
216};
217
218} // namespace android
219#endif // __cplusplus
220
221/*****************************************************************************/
222
223#endif /* ANDROID_ANDROID_NATIVES_H */
224