michael@0: /* michael@0: * Copyright (C) 2009 The Android Open Source Project michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #ifndef ANDROID_ANDROID_NATIVES_H michael@0: #define ANDROID_ANDROID_NATIVES_H michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: michael@0: #include michael@0: michael@0: #ifdef __cplusplus michael@0: extern "C" { michael@0: #endif michael@0: michael@0: /*****************************************************************************/ michael@0: michael@0: #define ANDROID_NATIVE_MAKE_CONSTANT(a,b,c,d) \ michael@0: (((unsigned)(a)<<24)|((unsigned)(b)<<16)|((unsigned)(c)<<8)|(unsigned)(d)) michael@0: michael@0: #define ANDROID_NATIVE_WINDOW_MAGIC \ michael@0: ANDROID_NATIVE_MAKE_CONSTANT('_','w','n','d') michael@0: michael@0: #define ANDROID_NATIVE_BUFFER_MAGIC \ michael@0: ANDROID_NATIVE_MAKE_CONSTANT('_','b','f','r') michael@0: michael@0: // --------------------------------------------------------------------------- michael@0: michael@0: struct android_native_buffer_t; michael@0: michael@0: typedef struct android_native_rect_t michael@0: { michael@0: int32_t left; michael@0: int32_t top; michael@0: int32_t right; michael@0: int32_t bottom; michael@0: } android_native_rect_t; michael@0: michael@0: // --------------------------------------------------------------------------- michael@0: michael@0: typedef struct android_native_base_t michael@0: { michael@0: /* a magic value defined by the actual EGL native type */ michael@0: int magic; michael@0: michael@0: /* the sizeof() of the actual EGL native type */ michael@0: int version; michael@0: michael@0: void* reserved[4]; michael@0: michael@0: /* reference-counting interface */ michael@0: void (*incRef)(struct android_native_base_t* base); michael@0: void (*decRef)(struct android_native_base_t* base); michael@0: } android_native_base_t; michael@0: michael@0: // --------------------------------------------------------------------------- michael@0: michael@0: /* attributes queriable with query() */ michael@0: enum { michael@0: NATIVE_WINDOW_WIDTH = 0, michael@0: NATIVE_WINDOW_HEIGHT, michael@0: NATIVE_WINDOW_FORMAT, michael@0: }; michael@0: michael@0: /* valid operations for the (*perform)() hook */ michael@0: enum { michael@0: NATIVE_WINDOW_SET_USAGE = 0, michael@0: NATIVE_WINDOW_CONNECT, michael@0: NATIVE_WINDOW_DISCONNECT, michael@0: NATIVE_WINDOW_SET_CROP, michael@0: NATIVE_WINDOW_SET_BUFFER_COUNT, michael@0: NATIVE_WINDOW_SET_BUFFERS_GEOMETRY, michael@0: NATIVE_WINDOW_SET_BUFFERS_TRANSFORM, michael@0: }; michael@0: michael@0: /* parameter for NATIVE_WINDOW_[DIS]CONNECT */ michael@0: enum { michael@0: NATIVE_WINDOW_API_EGL = 1 michael@0: }; michael@0: michael@0: /* parameter for NATIVE_WINDOW_SET_BUFFERS_TRANSFORM */ michael@0: enum { michael@0: /* flip source image horizontally */ michael@0: NATIVE_WINDOW_TRANSFORM_FLIP_H = HAL_TRANSFORM_FLIP_H , michael@0: /* flip source image vertically */ michael@0: NATIVE_WINDOW_TRANSFORM_FLIP_V = HAL_TRANSFORM_FLIP_V, michael@0: /* rotate source image 90 degrees clock-wise */ michael@0: NATIVE_WINDOW_TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90, michael@0: /* rotate source image 180 degrees */ michael@0: NATIVE_WINDOW_TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180, michael@0: /* rotate source image 270 degrees clock-wise */ michael@0: NATIVE_WINDOW_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270, michael@0: }; michael@0: michael@0: struct ANativeWindow michael@0: { michael@0: #ifdef __cplusplus michael@0: ANativeWindow() michael@0: : flags(0), minSwapInterval(0), maxSwapInterval(0), xdpi(0), ydpi(0) michael@0: { michael@0: common.magic = ANDROID_NATIVE_WINDOW_MAGIC; michael@0: common.version = sizeof(ANativeWindow); michael@0: memset(common.reserved, 0, sizeof(common.reserved)); michael@0: } michael@0: michael@0: // Implement the methods that sp expects so that it michael@0: // can be used to automatically refcount ANativeWindow's. michael@0: void incStrong(const void* id) const { michael@0: common.incRef(const_cast(&common)); michael@0: } michael@0: void decStrong(const void* id) const { michael@0: common.decRef(const_cast(&common)); michael@0: } michael@0: #endif michael@0: michael@0: struct android_native_base_t common; michael@0: michael@0: /* flags describing some attributes of this surface or its updater */ michael@0: const uint32_t flags; michael@0: michael@0: /* min swap interval supported by this updated */ michael@0: const int minSwapInterval; michael@0: michael@0: /* max swap interval supported by this updated */ michael@0: const int maxSwapInterval; michael@0: michael@0: /* horizontal and vertical resolution in DPI */ michael@0: const float xdpi; michael@0: const float ydpi; michael@0: michael@0: /* Some storage reserved for the OEM's driver. */ michael@0: intptr_t oem[4]; michael@0: michael@0: michael@0: /* michael@0: * Set the swap interval for this surface. michael@0: * michael@0: * Returns 0 on success or -errno on error. michael@0: */ michael@0: int (*setSwapInterval)(struct ANativeWindow* window, michael@0: int interval); michael@0: michael@0: /* michael@0: * hook called by EGL to acquire a buffer. After this call, the buffer michael@0: * is not locked, so its content cannot be modified. michael@0: * this call may block if no buffers are available. michael@0: * michael@0: * Returns 0 on success or -errno on error. michael@0: */ michael@0: int (*dequeueBuffer)(struct ANativeWindow* window, michael@0: struct android_native_buffer_t** buffer); michael@0: michael@0: /* michael@0: * hook called by EGL to lock a buffer. This MUST be called before modifying michael@0: * the content of a buffer. The buffer must have been acquired with michael@0: * dequeueBuffer first. michael@0: * michael@0: * Returns 0 on success or -errno on error. michael@0: */ michael@0: int (*lockBuffer)(struct ANativeWindow* window, michael@0: struct android_native_buffer_t* buffer); michael@0: /* michael@0: * hook called by EGL when modifications to the render buffer are done. michael@0: * This unlocks and post the buffer. michael@0: * michael@0: * Buffers MUST be queued in the same order than they were dequeued. michael@0: * michael@0: * Returns 0 on success or -errno on error. michael@0: */ michael@0: int (*queueBuffer)(struct ANativeWindow* window, michael@0: struct android_native_buffer_t* buffer); michael@0: michael@0: /* michael@0: * hook used to retrieve information about the native window. michael@0: * michael@0: * Returns 0 on success or -errno on error. michael@0: */ michael@0: int (*query)(struct ANativeWindow* window, michael@0: int what, int* value); michael@0: michael@0: /* michael@0: * hook used to perform various operations on the surface. michael@0: * (*perform)() is a generic mechanism to add functionality to michael@0: * ANativeWindow while keeping backward binary compatibility. michael@0: * michael@0: * This hook should not be called directly, instead use the helper functions michael@0: * defined below. michael@0: * michael@0: * (*perform)() returns -ENOENT if the 'what' parameter is not supported michael@0: * by the surface's implementation. michael@0: * michael@0: * The valid operations are: michael@0: * NATIVE_WINDOW_SET_USAGE michael@0: * NATIVE_WINDOW_CONNECT michael@0: * NATIVE_WINDOW_DISCONNECT michael@0: * NATIVE_WINDOW_SET_CROP michael@0: * NATIVE_WINDOW_SET_BUFFER_COUNT michael@0: * NATIVE_WINDOW_SET_BUFFERS_GEOMETRY michael@0: * NATIVE_WINDOW_SET_BUFFERS_TRANSFORM michael@0: * michael@0: */ michael@0: michael@0: int (*perform)(struct ANativeWindow* window, michael@0: int operation, ... ); michael@0: michael@0: /* michael@0: * hook used to cancel a buffer that has been dequeued. michael@0: * No synchronization is performed between dequeue() and cancel(), so michael@0: * either external synchronization is needed, or these functions must be michael@0: * called from the same thread. michael@0: */ michael@0: int (*cancelBuffer)(struct ANativeWindow* window, michael@0: struct android_native_buffer_t* buffer); michael@0: michael@0: michael@0: void* reserved_proc[2]; michael@0: }; michael@0: michael@0: // Backwards compatibility... please switch to ANativeWindow. michael@0: typedef struct ANativeWindow android_native_window_t; michael@0: michael@0: /* michael@0: * native_window_set_usage(..., usage) michael@0: * Sets the intended usage flags for the next buffers michael@0: * acquired with (*lockBuffer)() and on. michael@0: * By default (if this function is never called), a usage of michael@0: * GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE michael@0: * is assumed. michael@0: * Calling this function will usually cause following buffers to be michael@0: * reallocated. michael@0: */ michael@0: michael@0: static inline int native_window_set_usage( michael@0: ANativeWindow* window, int usage) michael@0: { michael@0: return window->perform(window, NATIVE_WINDOW_SET_USAGE, usage); michael@0: } michael@0: michael@0: /* michael@0: * native_window_connect(..., NATIVE_WINDOW_API_EGL) michael@0: * Must be called by EGL when the window is made current. michael@0: * Returns -EINVAL if for some reason the window cannot be connected, which michael@0: * can happen if it's connected to some other API. michael@0: */ michael@0: static inline int native_window_connect( michael@0: ANativeWindow* window, int api) michael@0: { michael@0: return window->perform(window, NATIVE_WINDOW_CONNECT, api); michael@0: } michael@0: michael@0: /* michael@0: * native_window_disconnect(..., NATIVE_WINDOW_API_EGL) michael@0: * Must be called by EGL when the window is made not current. michael@0: * An error is returned if for instance the window wasn't connected in the michael@0: * first place. michael@0: */ michael@0: static inline int native_window_disconnect( michael@0: ANativeWindow* window, int api) michael@0: { michael@0: return window->perform(window, NATIVE_WINDOW_DISCONNECT, api); michael@0: } michael@0: michael@0: /* michael@0: * native_window_set_crop(..., crop) michael@0: * Sets which region of the next queued buffers needs to be considered. michael@0: * A buffer's crop region is scaled to match the surface's size. michael@0: * michael@0: * The specified crop region applies to all buffers queued after it is called. michael@0: * michael@0: * if 'crop' is NULL, subsequently queued buffers won't be cropped. michael@0: * michael@0: * An error is returned if for instance the crop region is invalid, michael@0: * out of the buffer's bound or if the window is invalid. michael@0: */ michael@0: static inline int native_window_set_crop( michael@0: ANativeWindow* window, michael@0: android_native_rect_t const * crop) michael@0: { michael@0: return window->perform(window, NATIVE_WINDOW_SET_CROP, crop); michael@0: } michael@0: michael@0: /* michael@0: * native_window_set_buffer_count(..., count) michael@0: * Sets the number of buffers associated with this native window. michael@0: */ michael@0: static inline int native_window_set_buffer_count( michael@0: ANativeWindow* window, michael@0: size_t bufferCount) michael@0: { michael@0: return window->perform(window, NATIVE_WINDOW_SET_BUFFER_COUNT, bufferCount); michael@0: } michael@0: michael@0: /* michael@0: * native_window_set_buffers_geometry(..., int w, int h, int format) michael@0: * All buffers dequeued after this call will have the geometry specified. michael@0: * In particular, all buffers will have a fixed-size, independent form the michael@0: * native-window size. They will be appropriately scaled to the window-size michael@0: * upon composition. michael@0: * michael@0: * If all parameters are 0, the normal behavior is restored. That is, michael@0: * dequeued buffers following this call will be sized to the window's size. michael@0: * michael@0: */ michael@0: static inline int native_window_set_buffers_geometry( michael@0: ANativeWindow* window, michael@0: int w, int h, int format) michael@0: { michael@0: return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_GEOMETRY, michael@0: w, h, format); michael@0: } michael@0: michael@0: /* michael@0: * native_window_set_buffers_transform(..., int transform) michael@0: * All buffers queued after this call will be displayed transformed according michael@0: * to the transform parameter specified. michael@0: */ michael@0: static inline int native_window_set_buffers_transform( michael@0: ANativeWindow* window, michael@0: int transform) michael@0: { michael@0: return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_TRANSFORM, michael@0: transform); michael@0: } michael@0: michael@0: // --------------------------------------------------------------------------- michael@0: michael@0: /* FIXME: this is legacy for pixmaps */ michael@0: typedef struct egl_native_pixmap_t michael@0: { michael@0: int32_t version; /* must be 32 */ michael@0: int32_t width; michael@0: int32_t height; michael@0: int32_t stride; michael@0: uint8_t* data; michael@0: uint8_t format; michael@0: uint8_t rfu[3]; michael@0: union { michael@0: uint32_t compressedFormat; michael@0: int32_t vstride; michael@0: }; michael@0: int32_t reserved; michael@0: } egl_native_pixmap_t; michael@0: michael@0: /*****************************************************************************/ michael@0: michael@0: #ifdef __cplusplus michael@0: } michael@0: #endif michael@0: michael@0: michael@0: /*****************************************************************************/ michael@0: michael@0: #ifdef __cplusplus michael@0: michael@0: #include michael@0: michael@0: namespace android { michael@0: michael@0: /* michael@0: * This helper class turns an EGL android_native_xxx type into a C++ michael@0: * reference-counted object; with proper type conversions. michael@0: */ michael@0: template michael@0: class EGLNativeBase : public NATIVE_TYPE, public REF michael@0: { michael@0: public: michael@0: // Disambiguate between the incStrong in REF and NATIVE_TYPE michael@0: void incStrong(const void* id) const { michael@0: REF::incStrong(id); michael@0: } michael@0: void decStrong(const void* id) const { michael@0: REF::decStrong(id); michael@0: } michael@0: michael@0: protected: michael@0: typedef EGLNativeBase BASE; michael@0: EGLNativeBase() : NATIVE_TYPE(), REF() { michael@0: NATIVE_TYPE::common.incRef = incRef; michael@0: NATIVE_TYPE::common.decRef = decRef; michael@0: } michael@0: static inline TYPE* getSelf(NATIVE_TYPE* self) { michael@0: return static_cast(self); michael@0: } michael@0: static inline TYPE const* getSelf(NATIVE_TYPE const* self) { michael@0: return static_cast(self); michael@0: } michael@0: static inline TYPE* getSelf(android_native_base_t* base) { michael@0: return getSelf(reinterpret_cast(base)); michael@0: } michael@0: static inline TYPE const * getSelf(android_native_base_t const* base) { michael@0: return getSelf(reinterpret_cast(base)); michael@0: } michael@0: static void incRef(android_native_base_t* base) { michael@0: EGLNativeBase* self = getSelf(base); michael@0: self->incStrong(self); michael@0: } michael@0: static void decRef(android_native_base_t* base) { michael@0: EGLNativeBase* self = getSelf(base); michael@0: self->decStrong(self); michael@0: } michael@0: }; michael@0: michael@0: } // namespace android michael@0: #endif // __cplusplus michael@0: michael@0: /*****************************************************************************/ michael@0: michael@0: #endif /* ANDROID_ANDROID_NATIVES_H */