17ec681f3Smrg/*
27ec681f3Smrg * Copyright (C) 2010 The Android Open Source Project
37ec681f3Smrg *
47ec681f3Smrg * Licensed under the Apache License, Version 2.0 (the "License");
57ec681f3Smrg * you may not use this file except in compliance with the License.
67ec681f3Smrg * You may obtain a copy of the License at
77ec681f3Smrg *
87ec681f3Smrg *      http://www.apache.org/licenses/LICENSE-2.0
97ec681f3Smrg *
107ec681f3Smrg * Unless required by applicable law or agreed to in writing, software
117ec681f3Smrg * distributed under the License is distributed on an "AS IS" BASIS,
127ec681f3Smrg * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137ec681f3Smrg * See the License for the specific language governing permissions and
147ec681f3Smrg * limitations under the License.
157ec681f3Smrg */
167ec681f3Smrg
177ec681f3Smrg/**
187ec681f3Smrg * @defgroup ANativeWindow Native Window
197ec681f3Smrg *
207ec681f3Smrg * ANativeWindow represents the producer end of an image queue.
217ec681f3Smrg * It is the C counterpart of the android.view.Surface object in Java,
227ec681f3Smrg * and can be converted both ways. Depending on the consumer, images
237ec681f3Smrg * submitted to ANativeWindow can be shown on the display or sent to
247ec681f3Smrg * other consumers, such as video encoders.
257ec681f3Smrg * @{
267ec681f3Smrg */
277ec681f3Smrg
287ec681f3Smrg/**
297ec681f3Smrg * @file native_window.h
307ec681f3Smrg * @brief API for accessing a native window.
317ec681f3Smrg */
327ec681f3Smrg
337ec681f3Smrg#ifndef ANDROID_NATIVE_WINDOW_H
347ec681f3Smrg#define ANDROID_NATIVE_WINDOW_H
357ec681f3Smrg
367ec681f3Smrg#include <stdint.h>
377ec681f3Smrg#include <sys/cdefs.h>
387ec681f3Smrg
397ec681f3Smrg#include <android/data_space.h>
407ec681f3Smrg#include <android/hardware_buffer.h>
417ec681f3Smrg#include <android/rect.h>
427ec681f3Smrg
437ec681f3Smrg#ifdef __cplusplus
447ec681f3Smrgextern "C" {
457ec681f3Smrg#endif
467ec681f3Smrg
477ec681f3Smrg/**
487ec681f3Smrg * Legacy window pixel format names, kept for backwards compatibility.
497ec681f3Smrg * New code and APIs should use AHARDWAREBUFFER_FORMAT_*.
507ec681f3Smrg */
517ec681f3Smrgenum ANativeWindow_LegacyFormat {
527ec681f3Smrg    // NOTE: these values must match the values from graphics/common/x.x/types.hal
537ec681f3Smrg
547ec681f3Smrg    /** Red: 8 bits, Green: 8 bits, Blue: 8 bits, Alpha: 8 bits. **/
557ec681f3Smrg    WINDOW_FORMAT_RGBA_8888          = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
567ec681f3Smrg    /** Red: 8 bits, Green: 8 bits, Blue: 8 bits, Unused: 8 bits. **/
577ec681f3Smrg    WINDOW_FORMAT_RGBX_8888          = AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM,
587ec681f3Smrg    /** Red: 5 bits, Green: 6 bits, Blue: 5 bits. **/
597ec681f3Smrg    WINDOW_FORMAT_RGB_565            = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM,
607ec681f3Smrg};
617ec681f3Smrg
627ec681f3Smrg/**
637ec681f3Smrg * Transforms that can be applied to buffers as they are displayed to a window.
647ec681f3Smrg *
657ec681f3Smrg * Supported transforms are any combination of horizontal mirror, vertical
667ec681f3Smrg * mirror, and clockwise 90 degree rotation, in that order. Rotations of 180
677ec681f3Smrg * and 270 degrees are made up of those basic transforms.
687ec681f3Smrg */
697ec681f3Smrgenum ANativeWindowTransform {
707ec681f3Smrg    ANATIVEWINDOW_TRANSFORM_IDENTITY            = 0x00,
717ec681f3Smrg    ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL   = 0x01,
727ec681f3Smrg    ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL     = 0x02,
737ec681f3Smrg    ANATIVEWINDOW_TRANSFORM_ROTATE_90           = 0x04,
747ec681f3Smrg
757ec681f3Smrg    ANATIVEWINDOW_TRANSFORM_ROTATE_180          = ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL |
767ec681f3Smrg                                                  ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL,
777ec681f3Smrg    ANATIVEWINDOW_TRANSFORM_ROTATE_270          = ANATIVEWINDOW_TRANSFORM_ROTATE_180 |
787ec681f3Smrg                                                  ANATIVEWINDOW_TRANSFORM_ROTATE_90,
797ec681f3Smrg};
807ec681f3Smrg
817ec681f3Smrgstruct ANativeWindow;
827ec681f3Smrg/**
837ec681f3Smrg * Opaque type that provides access to a native window.
847ec681f3Smrg *
857ec681f3Smrg * A pointer can be obtained using {@link ANativeWindow_fromSurface()}.
867ec681f3Smrg */
877ec681f3Smrgtypedef struct ANativeWindow ANativeWindow;
887ec681f3Smrg
897ec681f3Smrg/**
907ec681f3Smrg * Struct that represents a windows buffer.
917ec681f3Smrg *
927ec681f3Smrg * A pointer can be obtained using {@link ANativeWindow_lock()}.
937ec681f3Smrg */
947ec681f3Smrgtypedef struct ANativeWindow_Buffer {
957ec681f3Smrg    /// The number of pixels that are shown horizontally.
967ec681f3Smrg    int32_t width;
977ec681f3Smrg
987ec681f3Smrg    /// The number of pixels that are shown vertically.
997ec681f3Smrg    int32_t height;
1007ec681f3Smrg
1017ec681f3Smrg    /// The number of *pixels* that a line in the buffer takes in
1027ec681f3Smrg    /// memory. This may be >= width.
1037ec681f3Smrg    int32_t stride;
1047ec681f3Smrg
1057ec681f3Smrg    /// The format of the buffer. One of AHardwareBuffer_Format.
1067ec681f3Smrg    int32_t format;
1077ec681f3Smrg
1087ec681f3Smrg    /// The actual bits.
1097ec681f3Smrg    void* bits;
1107ec681f3Smrg
1117ec681f3Smrg    /// Do not touch.
1127ec681f3Smrg    uint32_t reserved[6];
1137ec681f3Smrg} ANativeWindow_Buffer;
1147ec681f3Smrg
1157ec681f3Smrg/**
1167ec681f3Smrg * Acquire a reference on the given {@link ANativeWindow} object. This prevents the object
1177ec681f3Smrg * from being deleted until the reference is removed.
1187ec681f3Smrg */
1197ec681f3Smrgvoid ANativeWindow_acquire(ANativeWindow* window);
1207ec681f3Smrg
1217ec681f3Smrg/**
1227ec681f3Smrg * Remove a reference that was previously acquired with {@link ANativeWindow_acquire()}.
1237ec681f3Smrg */
1247ec681f3Smrgvoid ANativeWindow_release(ANativeWindow* window);
1257ec681f3Smrg
1267ec681f3Smrg/**
1277ec681f3Smrg * Return the current width in pixels of the window surface.
1287ec681f3Smrg *
1297ec681f3Smrg * \return negative value on error.
1307ec681f3Smrg */
1317ec681f3Smrgint32_t ANativeWindow_getWidth(ANativeWindow* window);
1327ec681f3Smrg
1337ec681f3Smrg/**
1347ec681f3Smrg * Return the current height in pixels of the window surface.
1357ec681f3Smrg *
1367ec681f3Smrg * \return a negative value on error.
1377ec681f3Smrg */
1387ec681f3Smrgint32_t ANativeWindow_getHeight(ANativeWindow* window);
1397ec681f3Smrg
1407ec681f3Smrg/**
1417ec681f3Smrg * Return the current pixel format (AHARDWAREBUFFER_FORMAT_*) of the window surface.
1427ec681f3Smrg *
1437ec681f3Smrg * \return a negative value on error.
1447ec681f3Smrg */
1457ec681f3Smrgint32_t ANativeWindow_getFormat(ANativeWindow* window);
1467ec681f3Smrg
1477ec681f3Smrg/**
1487ec681f3Smrg * Change the format and size of the window buffers.
1497ec681f3Smrg *
1507ec681f3Smrg * The width and height control the number of pixels in the buffers, not the
1517ec681f3Smrg * dimensions of the window on screen. If these are different than the
1527ec681f3Smrg * window's physical size, then its buffer will be scaled to match that size
1537ec681f3Smrg * when compositing it to the screen. The width and height must be either both zero
1547ec681f3Smrg * or both non-zero.
1557ec681f3Smrg *
1567ec681f3Smrg * For all of these parameters, if 0 is supplied then the window's base
1577ec681f3Smrg * value will come back in force.
1587ec681f3Smrg *
1597ec681f3Smrg * \param width width of the buffers in pixels.
1607ec681f3Smrg * \param height height of the buffers in pixels.
1617ec681f3Smrg * \param format one of the AHardwareBuffer_Format constants.
1627ec681f3Smrg * \return 0 for success, or a negative value on error.
1637ec681f3Smrg */
1647ec681f3Smrgint32_t ANativeWindow_setBuffersGeometry(ANativeWindow* window,
1657ec681f3Smrg        int32_t width, int32_t height, int32_t format);
1667ec681f3Smrg
1677ec681f3Smrg/**
1687ec681f3Smrg * Lock the window's next drawing surface for writing.
1697ec681f3Smrg * inOutDirtyBounds is used as an in/out parameter, upon entering the
1707ec681f3Smrg * function, it contains the dirty region, that is, the region the caller
1717ec681f3Smrg * intends to redraw. When the function returns, inOutDirtyBounds is updated
1727ec681f3Smrg * with the actual area the caller needs to redraw -- this region is often
1737ec681f3Smrg * extended by {@link ANativeWindow_lock}.
1747ec681f3Smrg *
1757ec681f3Smrg * \return 0 for success, or a negative value on error.
1767ec681f3Smrg */
1777ec681f3Smrgint32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer,
1787ec681f3Smrg        ARect* inOutDirtyBounds);
1797ec681f3Smrg
1807ec681f3Smrg/**
1817ec681f3Smrg * Unlock the window's drawing surface after previously locking it,
1827ec681f3Smrg * posting the new buffer to the display.
1837ec681f3Smrg *
1847ec681f3Smrg * \return 0 for success, or a negative value on error.
1857ec681f3Smrg */
1867ec681f3Smrgint32_t ANativeWindow_unlockAndPost(ANativeWindow* window);
1877ec681f3Smrg
1887ec681f3Smrg/**
1897ec681f3Smrg * Set a transform that will be applied to future buffers posted to the window.
1907ec681f3Smrg *
1917ec681f3Smrg * Available since API level 26.
1927ec681f3Smrg *
1937ec681f3Smrg * \param transform combination of {@link ANativeWindowTransform} flags
1947ec681f3Smrg * \return 0 for success, or -EINVAL if \p transform is invalid
1957ec681f3Smrg */
1967ec681f3Smrgint32_t ANativeWindow_setBuffersTransform(ANativeWindow* window, int32_t transform) __INTRODUCED_IN(26);
1977ec681f3Smrg
1987ec681f3Smrg/**
1997ec681f3Smrg * All buffers queued after this call will be associated with the dataSpace
2007ec681f3Smrg * parameter specified.
2017ec681f3Smrg *
2027ec681f3Smrg * dataSpace specifies additional information about the buffer.
2037ec681f3Smrg * For example, it can be used to convey the color space of the image data in
2047ec681f3Smrg * the buffer, or it can be used to indicate that the buffers contain depth
2057ec681f3Smrg * measurement data instead of color images. The default dataSpace is 0,
2067ec681f3Smrg * ADATASPACE_UNKNOWN, unless it has been overridden by the producer.
2077ec681f3Smrg *
2087ec681f3Smrg * Available since API level 28.
2097ec681f3Smrg *
2107ec681f3Smrg * \param dataSpace data space of all buffers queued after this call.
2117ec681f3Smrg * \return 0 for success, -EINVAL if window is invalid or the dataspace is not
2127ec681f3Smrg * supported.
2137ec681f3Smrg */
2147ec681f3Smrgint32_t ANativeWindow_setBuffersDataSpace(ANativeWindow* window, int32_t dataSpace) __INTRODUCED_IN(28);
2157ec681f3Smrg
2167ec681f3Smrg/**
2177ec681f3Smrg * Get the dataspace of the buffers in window.
2187ec681f3Smrg *
2197ec681f3Smrg * Available since API level 28.
2207ec681f3Smrg *
2217ec681f3Smrg * \return the dataspace of buffers in window, ADATASPACE_UNKNOWN is returned if
2227ec681f3Smrg * dataspace is unknown, or -EINVAL if window is invalid.
2237ec681f3Smrg */
2247ec681f3Smrgint32_t ANativeWindow_getBuffersDataSpace(ANativeWindow* window) __INTRODUCED_IN(28);
2257ec681f3Smrg
2267ec681f3Smrg/** Compatibility value for ANativeWindow_setFrameRate. */
2277ec681f3Smrgenum ANativeWindow_FrameRateCompatibility {
2287ec681f3Smrg    /**
2297ec681f3Smrg     * There are no inherent restrictions on the frame rate of this window. When
2307ec681f3Smrg     * the system selects a frame rate other than what the app requested, the
2317ec681f3Smrg     * app will be able to run at the system frame rate without requiring pull
2327ec681f3Smrg     * down. This value should be used when displaying game content, UIs, and
2337ec681f3Smrg     * anything that isn't video.
2347ec681f3Smrg     */
2357ec681f3Smrg    ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT = 0,
2367ec681f3Smrg    /**
2377ec681f3Smrg     * This window is being used to display content with an inherently fixed
2387ec681f3Smrg     * frame rate, e.g.\ a video that has a specific frame rate. When the system
2397ec681f3Smrg     * selects a frame rate other than what the app requested, the app will need
2407ec681f3Smrg     * to do pull down or use some other technique to adapt to the system's
2417ec681f3Smrg     * frame rate. The user experience is likely to be worse (e.g. more frame
2427ec681f3Smrg     * stuttering) than it would be if the system had chosen the app's requested
2437ec681f3Smrg     * frame rate. This value should be used for video content.
2447ec681f3Smrg     */
2457ec681f3Smrg    ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE = 1
2467ec681f3Smrg};
2477ec681f3Smrg
2487ec681f3Smrg/**
2497ec681f3Smrg * Sets the intended frame rate for this window.
2507ec681f3Smrg *
2517ec681f3Smrg * On devices that are capable of running the display at different refresh
2527ec681f3Smrg * rates, the system may choose a display refresh rate to better match this
2537ec681f3Smrg * window's frame rate. Usage of this API won't introduce frame rate throttling,
2547ec681f3Smrg * or affect other aspects of the application's frame production
2557ec681f3Smrg * pipeline. However, because the system may change the display refresh rate,
2567ec681f3Smrg * calls to this function may result in changes to Choreographer callback
2577ec681f3Smrg * timings, and changes to the time interval at which the system releases
2587ec681f3Smrg * buffers back to the application.
2597ec681f3Smrg *
2607ec681f3Smrg * Note that this only has an effect for windows presented on the display. If
2617ec681f3Smrg * this ANativeWindow is consumed by something other than the system compositor,
2627ec681f3Smrg * e.g. a media codec, this call has no effect.
2637ec681f3Smrg *
2647ec681f3Smrg * Available since API level 30.
2657ec681f3Smrg *
2667ec681f3Smrg * \param frameRate The intended frame rate of this window, in frames per
2677ec681f3Smrg * second. 0 is a special value that indicates the app will accept the system's
2687ec681f3Smrg * choice for the display frame rate, which is the default behavior if this
2697ec681f3Smrg * function isn't called. The frameRate param does <em>not</em> need to be a
2707ec681f3Smrg * valid refresh rate for this device's display - e.g., it's fine to pass 30fps
2717ec681f3Smrg * to a device that can only run the display at 60fps.
2727ec681f3Smrg *
2737ec681f3Smrg * \param compatibility The frame rate compatibility of this window. The
2747ec681f3Smrg * compatibility value may influence the system's choice of display refresh
2757ec681f3Smrg * rate. See the ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_* values for more info.
2767ec681f3Smrg *
2777ec681f3Smrg * \return 0 for success, -EINVAL if the window, frame rate, or compatibility
2787ec681f3Smrg * value are invalid.
2797ec681f3Smrg */
2807ec681f3Smrgint32_t ANativeWindow_setFrameRate(ANativeWindow* window, float frameRate, int8_t compatibility)
2817ec681f3Smrg        __INTRODUCED_IN(30);
2827ec681f3Smrg
2837ec681f3Smrg/**
2847ec681f3Smrg * Provides a hint to the window that buffers should be preallocated ahead of
2857ec681f3Smrg * time. Note that the window implementation is not guaranteed to preallocate
2867ec681f3Smrg * any buffers, for instance if an implementation disallows allocation of new
2877ec681f3Smrg * buffers, or if there is insufficient memory in the system to preallocate
2887ec681f3Smrg * additional buffers
2897ec681f3Smrg *
2907ec681f3Smrg * Available since API level 30.
2917ec681f3Smrg */
2927ec681f3Smrgvoid ANativeWindow_tryAllocateBuffers(ANativeWindow* window);
2937ec681f3Smrg
2947ec681f3Smrg#ifdef __cplusplus
2957ec681f3Smrg};
2967ec681f3Smrg#endif
2977ec681f3Smrg
2987ec681f3Smrg#endif // ANDROID_NATIVE_WINDOW_H
2997ec681f3Smrg
3007ec681f3Smrg/** @} */
301