17ec681f3Smrg/*
27ec681f3Smrg * Copyright (C) 2017 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#ifndef ANDROID_VNDK_NATIVEWINDOW_ANATIVEWINDOW_H
187ec681f3Smrg#define ANDROID_VNDK_NATIVEWINDOW_ANATIVEWINDOW_H
197ec681f3Smrg
207ec681f3Smrg#include <nativebase/nativebase.h>
217ec681f3Smrg
227ec681f3Smrg// vndk is a superset of the NDK
237ec681f3Smrg#include <android/native_window.h>
247ec681f3Smrg
257ec681f3Smrg
267ec681f3Smrg__BEGIN_DECLS
277ec681f3Smrg
287ec681f3Smrg/*
297ec681f3Smrg * Convert this ANativeWindowBuffer into a AHardwareBuffer
307ec681f3Smrg */
317ec681f3SmrgAHardwareBuffer* ANativeWindowBuffer_getHardwareBuffer(ANativeWindowBuffer* anwb);
327ec681f3Smrg
337ec681f3Smrg/*****************************************************************************/
347ec681f3Smrg
357ec681f3Smrg/*
367ec681f3Smrg * Stores a value into one of the 4 available slots
377ec681f3Smrg * Retrieve the value with ANativeWindow_OemStorageGet()
387ec681f3Smrg *
397ec681f3Smrg * slot: 0 to 3
407ec681f3Smrg *
417ec681f3Smrg * Returns 0 on success or -errno on error.
427ec681f3Smrg */
437ec681f3Smrgint ANativeWindow_OemStorageSet(ANativeWindow* window, uint32_t slot, intptr_t value);
447ec681f3Smrg
457ec681f3Smrg
467ec681f3Smrg/*
477ec681f3Smrg * Retrieves a value from one of the 4 available slots
487ec681f3Smrg * By default the returned value is 0 if it wasn't set by ANativeWindow_OemStorageSet()
497ec681f3Smrg *
507ec681f3Smrg * slot: 0 to 3
517ec681f3Smrg *
527ec681f3Smrg * Returns 0 on success or -errno on error.
537ec681f3Smrg */
547ec681f3Smrgint ANativeWindow_OemStorageGet(ANativeWindow* window, uint32_t slot, intptr_t* value);
557ec681f3Smrg
567ec681f3Smrg
577ec681f3Smrg/*
587ec681f3Smrg * Set the swap interval for this surface.
597ec681f3Smrg *
607ec681f3Smrg * Returns 0 on success or -errno on error.
617ec681f3Smrg */
627ec681f3Smrgint ANativeWindow_setSwapInterval(ANativeWindow* window, int interval);
637ec681f3Smrg
647ec681f3Smrg
657ec681f3Smrg/*
667ec681f3Smrg * queries that can be used with ANativeWindow_query() and ANativeWindow_queryf()
677ec681f3Smrg */
687ec681f3Smrgenum ANativeWindowQuery {
697ec681f3Smrg    /* The minimum number of buffers that must remain un-dequeued after a buffer
707ec681f3Smrg     * has been queued.  This value applies only if set_buffer_count was used to
717ec681f3Smrg     * override the number of buffers and if a buffer has since been queued.
727ec681f3Smrg     * Users of the set_buffer_count ANativeWindow method should query this
737ec681f3Smrg     * value before calling set_buffer_count.  If it is necessary to have N
747ec681f3Smrg     * buffers simultaneously dequeued as part of the steady-state operation,
757ec681f3Smrg     * and this query returns M then N+M buffers should be requested via
767ec681f3Smrg     * native_window_set_buffer_count.
777ec681f3Smrg     *
787ec681f3Smrg     * Note that this value does NOT apply until a single buffer has been
797ec681f3Smrg     * queued.  In particular this means that it is possible to:
807ec681f3Smrg     *
817ec681f3Smrg     * 1. Query M = min undequeued buffers
827ec681f3Smrg     * 2. Set the buffer count to N + M
837ec681f3Smrg     * 3. Dequeue all N + M buffers
847ec681f3Smrg     * 4. Cancel M buffers
857ec681f3Smrg     * 5. Queue, dequeue, queue, dequeue, ad infinitum
867ec681f3Smrg     */
877ec681f3Smrg    ANATIVEWINDOW_QUERY_MIN_UNDEQUEUED_BUFFERS = 3,
887ec681f3Smrg
897ec681f3Smrg    /*
907ec681f3Smrg     * Default width of ANativeWindow buffers, these are the
917ec681f3Smrg     * dimensions of the window buffers irrespective of the
927ec681f3Smrg     * ANativeWindow_setBuffersDimensions() call and match the native window
937ec681f3Smrg     * size.
947ec681f3Smrg     */
957ec681f3Smrg    ANATIVEWINDOW_QUERY_DEFAULT_WIDTH = 6,
967ec681f3Smrg    ANATIVEWINDOW_QUERY_DEFAULT_HEIGHT = 7,
977ec681f3Smrg
987ec681f3Smrg    /*
997ec681f3Smrg     * transformation that will most-likely be applied to buffers. This is only
1007ec681f3Smrg     * a hint, the actual transformation applied might be different.
1017ec681f3Smrg     *
1027ec681f3Smrg     * INTENDED USE:
1037ec681f3Smrg     *
1047ec681f3Smrg     * The transform hint can be used by a producer, for instance the GLES
1057ec681f3Smrg     * driver, to pre-rotate the rendering such that the final transformation
1067ec681f3Smrg     * in the composer is identity. This can be very useful when used in
1077ec681f3Smrg     * conjunction with the h/w composer HAL, in situations where it
1087ec681f3Smrg     * cannot handle arbitrary rotations.
1097ec681f3Smrg     *
1107ec681f3Smrg     * 1. Before dequeuing a buffer, the GL driver (or any other ANW client)
1117ec681f3Smrg     *    queries the ANW for NATIVE_WINDOW_TRANSFORM_HINT.
1127ec681f3Smrg     *
1137ec681f3Smrg     * 2. The GL driver overrides the width and height of the ANW to
1147ec681f3Smrg     *    account for NATIVE_WINDOW_TRANSFORM_HINT. This is done by querying
1157ec681f3Smrg     *    NATIVE_WINDOW_DEFAULT_{WIDTH | HEIGHT}, swapping the dimensions
1167ec681f3Smrg     *    according to NATIVE_WINDOW_TRANSFORM_HINT and calling
1177ec681f3Smrg     *    native_window_set_buffers_dimensions().
1187ec681f3Smrg     *
1197ec681f3Smrg     * 3. The GL driver dequeues a buffer of the new pre-rotated size.
1207ec681f3Smrg     *
1217ec681f3Smrg     * 4. The GL driver renders to the buffer such that the image is
1227ec681f3Smrg     *    already transformed, that is applying NATIVE_WINDOW_TRANSFORM_HINT
1237ec681f3Smrg     *    to the rendering.
1247ec681f3Smrg     *
1257ec681f3Smrg     * 5. The GL driver calls native_window_set_transform to apply
1267ec681f3Smrg     *    inverse transformation to the buffer it just rendered.
1277ec681f3Smrg     *    In order to do this, the GL driver needs
1287ec681f3Smrg     *    to calculate the inverse of NATIVE_WINDOW_TRANSFORM_HINT, this is
1297ec681f3Smrg     *    done easily:
1307ec681f3Smrg     *
1317ec681f3Smrg     *        int hintTransform, inverseTransform;
1327ec681f3Smrg     *        query(..., NATIVE_WINDOW_TRANSFORM_HINT, &hintTransform);
1337ec681f3Smrg     *        inverseTransform = hintTransform;
1347ec681f3Smrg     *        if (hintTransform & HAL_TRANSFORM_ROT_90)
1357ec681f3Smrg     *            inverseTransform ^= HAL_TRANSFORM_ROT_180;
1367ec681f3Smrg     *
1377ec681f3Smrg     *
1387ec681f3Smrg     * 6. The GL driver queues the pre-transformed buffer.
1397ec681f3Smrg     *
1407ec681f3Smrg     * 7. The composer combines the buffer transform with the display
1417ec681f3Smrg     *    transform.  If the buffer transform happens to cancel out the
1427ec681f3Smrg     *    display transform then no rotation is needed.
1437ec681f3Smrg     *
1447ec681f3Smrg     */
1457ec681f3Smrg    ANATIVEWINDOW_QUERY_TRANSFORM_HINT = 8,
1467ec681f3Smrg
1477ec681f3Smrg    /*
1487ec681f3Smrg     * Returns the age of the contents of the most recently dequeued buffer as
1497ec681f3Smrg     * the number of frames that have elapsed since it was last queued. For
1507ec681f3Smrg     * example, if the window is double-buffered, the age of any given buffer in
1517ec681f3Smrg     * steady state will be 2. If the dequeued buffer has never been queued, its
1527ec681f3Smrg     * age will be 0.
1537ec681f3Smrg     */
1547ec681f3Smrg    ANATIVEWINDOW_QUERY_BUFFER_AGE = 13,
1557ec681f3Smrg
1567ec681f3Smrg    /* min swap interval supported by this compositor */
1577ec681f3Smrg    ANATIVEWINDOW_QUERY_MIN_SWAP_INTERVAL = 0x10000,
1587ec681f3Smrg
1597ec681f3Smrg    /* max swap interval supported by this compositor */
1607ec681f3Smrg    ANATIVEWINDOW_QUERY_MAX_SWAP_INTERVAL = 0x10001,
1617ec681f3Smrg
1627ec681f3Smrg    /* horizontal resolution in DPI. value is float, use queryf() */
1637ec681f3Smrg    ANATIVEWINDOW_QUERY_XDPI = 0x10002,
1647ec681f3Smrg
1657ec681f3Smrg    /* vertical resolution in DPI. value is float, use queryf() */
1667ec681f3Smrg    ANATIVEWINDOW_QUERY_YDPI = 0x10003,
1677ec681f3Smrg};
1687ec681f3Smrg
1697ec681f3Smrgtypedef enum ANativeWindowQuery ANativeWindowQuery;
1707ec681f3Smrg
1717ec681f3Smrg/*
1727ec681f3Smrg * hook used to retrieve information about the native window.
1737ec681f3Smrg *
1747ec681f3Smrg * Returns 0 on success or -errno on error.
1757ec681f3Smrg */
1767ec681f3Smrgint ANativeWindow_query(const ANativeWindow* window, ANativeWindowQuery query, int* value);
1777ec681f3Smrgint ANativeWindow_queryf(const ANativeWindow* window, ANativeWindowQuery query, float* value);
1787ec681f3Smrg
1797ec681f3Smrg
1807ec681f3Smrg/*
1817ec681f3Smrg * Hook called by EGL to acquire a buffer. This call may block if no
1827ec681f3Smrg * buffers are available.
1837ec681f3Smrg *
1847ec681f3Smrg * The window holds a reference to the buffer between dequeueBuffer and
1857ec681f3Smrg * either queueBuffer or cancelBuffer, so clients only need their own
1867ec681f3Smrg * reference if they might use the buffer after queueing or canceling it.
1877ec681f3Smrg * Holding a reference to a buffer after queueing or canceling it is only
1887ec681f3Smrg * allowed if a specific buffer count has been set.
1897ec681f3Smrg *
1907ec681f3Smrg * The libsync fence file descriptor returned in the int pointed to by the
1917ec681f3Smrg * fenceFd argument will refer to the fence that must signal before the
1927ec681f3Smrg * dequeued buffer may be written to.  A value of -1 indicates that the
1937ec681f3Smrg * caller may access the buffer immediately without waiting on a fence.  If
1947ec681f3Smrg * a valid file descriptor is returned (i.e. any value except -1) then the
1957ec681f3Smrg * caller is responsible for closing the file descriptor.
1967ec681f3Smrg *
1977ec681f3Smrg * Returns 0 on success or -errno on error.
1987ec681f3Smrg */
1997ec681f3Smrgint ANativeWindow_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer, int* fenceFd);
2007ec681f3Smrg
2017ec681f3Smrg
2027ec681f3Smrg/*
2037ec681f3Smrg * Hook called by EGL when modifications to the render buffer are done.
2047ec681f3Smrg * This unlocks and post the buffer.
2057ec681f3Smrg *
2067ec681f3Smrg * The window holds a reference to the buffer between dequeueBuffer and
2077ec681f3Smrg * either queueBuffer or cancelBuffer, so clients only need their own
2087ec681f3Smrg * reference if they might use the buffer after queueing or canceling it.
2097ec681f3Smrg * Holding a reference to a buffer after queueing or canceling it is only
2107ec681f3Smrg * allowed if a specific buffer count has been set.
2117ec681f3Smrg *
2127ec681f3Smrg * The fenceFd argument specifies a libsync fence file descriptor for a
2137ec681f3Smrg * fence that must signal before the buffer can be accessed.  If the buffer
2147ec681f3Smrg * can be accessed immediately then a value of -1 should be used.  The
2157ec681f3Smrg * caller must not use the file descriptor after it is passed to
2167ec681f3Smrg * queueBuffer, and the ANativeWindow implementation is responsible for
2177ec681f3Smrg * closing it.
2187ec681f3Smrg *
2197ec681f3Smrg * Returns 0 on success or -errno on error.
2207ec681f3Smrg */
2217ec681f3Smrgint ANativeWindow_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd);
2227ec681f3Smrg
2237ec681f3Smrg
2247ec681f3Smrg/*
2257ec681f3Smrg * Hook used to cancel a buffer that has been dequeued.
2267ec681f3Smrg * No synchronization is performed between dequeue() and cancel(), so
2277ec681f3Smrg * either external synchronization is needed, or these functions must be
2287ec681f3Smrg * called from the same thread.
2297ec681f3Smrg *
2307ec681f3Smrg * The window holds a reference to the buffer between dequeueBuffer and
2317ec681f3Smrg * either queueBuffer or cancelBuffer, so clients only need their own
2327ec681f3Smrg * reference if they might use the buffer after queueing or canceling it.
2337ec681f3Smrg * Holding a reference to a buffer after queueing or canceling it is only
2347ec681f3Smrg * allowed if a specific buffer count has been set.
2357ec681f3Smrg *
2367ec681f3Smrg * The fenceFd argument specifies a libsync fence file decsriptor for a
2377ec681f3Smrg * fence that must signal before the buffer can be accessed.  If the buffer
2387ec681f3Smrg * can be accessed immediately then a value of -1 should be used.
2397ec681f3Smrg *
2407ec681f3Smrg * Note that if the client has not waited on the fence that was returned
2417ec681f3Smrg * from dequeueBuffer, that same fence should be passed to cancelBuffer to
2427ec681f3Smrg * ensure that future uses of the buffer are preceded by a wait on that
2437ec681f3Smrg * fence.  The caller must not use the file descriptor after it is passed
2447ec681f3Smrg * to cancelBuffer, and the ANativeWindow implementation is responsible for
2457ec681f3Smrg * closing it.
2467ec681f3Smrg *
2477ec681f3Smrg * Returns 0 on success or -errno on error.
2487ec681f3Smrg */
2497ec681f3Smrgint ANativeWindow_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd);
2507ec681f3Smrg
2517ec681f3Smrg/*
2527ec681f3Smrg *  Sets the intended usage flags for the next buffers.
2537ec681f3Smrg *
2547ec681f3Smrg *  usage: one of AHARDWAREBUFFER_USAGE_* constant
2557ec681f3Smrg *
2567ec681f3Smrg *  By default (if this function is never called), a usage of
2577ec681f3Smrg *      AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT
2587ec681f3Smrg *  is assumed.
2597ec681f3Smrg *
2607ec681f3Smrg *  Calling this function will usually cause following buffers to be
2617ec681f3Smrg *  reallocated.
2627ec681f3Smrg */
2637ec681f3Smrgint ANativeWindow_setUsage(ANativeWindow* window, uint64_t usage);
2647ec681f3Smrg
2657ec681f3Smrg
2667ec681f3Smrg/*
2677ec681f3Smrg * Sets the number of buffers associated with this native window.
2687ec681f3Smrg */
2697ec681f3Smrgint ANativeWindow_setBufferCount(ANativeWindow* window, size_t bufferCount);
2707ec681f3Smrg
2717ec681f3Smrg
2727ec681f3Smrg/*
2737ec681f3Smrg * All buffers dequeued after this call will have the dimensions specified.
2747ec681f3Smrg * In particular, all buffers will have a fixed-size, independent from the
2757ec681f3Smrg * native-window size. They will be scaled according to the scaling mode
2767ec681f3Smrg * (see native_window_set_scaling_mode) upon window composition.
2777ec681f3Smrg *
2787ec681f3Smrg * If w and h are 0, the normal behavior is restored. That is, dequeued buffers
2797ec681f3Smrg * following this call will be sized to match the window's size.
2807ec681f3Smrg *
2817ec681f3Smrg * Calling this function will reset the window crop to a NULL value, which
2827ec681f3Smrg * disables cropping of the buffers.
2837ec681f3Smrg */
2847ec681f3Smrgint ANativeWindow_setBuffersDimensions(ANativeWindow* window, uint32_t w, uint32_t h);
2857ec681f3Smrg
2867ec681f3Smrg
2877ec681f3Smrg/*
2887ec681f3Smrg * All buffers dequeued after this call will have the format specified.
2897ec681f3Smrg * format: one of AHARDWAREBUFFER_FORMAT_* constant
2907ec681f3Smrg *
2917ec681f3Smrg * If the specified format is 0, the default buffer format will be used.
2927ec681f3Smrg */
2937ec681f3Smrgint ANativeWindow_setBuffersFormat(ANativeWindow* window, int format);
2947ec681f3Smrg
2957ec681f3Smrg
2967ec681f3Smrg/*
2977ec681f3Smrg * All buffers queued after this call will be associated with the timestamp in nanosecond
2987ec681f3Smrg * parameter specified. If the timestamp is set to NATIVE_WINDOW_TIMESTAMP_AUTO
2997ec681f3Smrg * (the default), timestamps will be generated automatically when queueBuffer is
3007ec681f3Smrg * called. The timestamp is measured in nanoseconds, and is normally monotonically
3017ec681f3Smrg * increasing. The timestamp should be unaffected by time-of-day adjustments,
3027ec681f3Smrg * and for a camera should be strictly monotonic but for a media player may be
3037ec681f3Smrg * reset when the position is set.
3047ec681f3Smrg */
3057ec681f3Smrgint ANativeWindow_setBuffersTimestamp(ANativeWindow* window, int64_t timestamp);
3067ec681f3Smrg
3077ec681f3Smrg
3087ec681f3Smrg/*
3097ec681f3Smrg * Enable/disable shared buffer mode
3107ec681f3Smrg */
3117ec681f3Smrgint ANativeWindow_setSharedBufferMode(ANativeWindow* window, bool sharedBufferMode);
3127ec681f3Smrg
3137ec681f3Smrg
3147ec681f3Smrg/*
3157ec681f3Smrg * Enable/disable auto refresh when in shared buffer mode
3167ec681f3Smrg */
3177ec681f3Smrgint ANativeWindow_setAutoRefresh(ANativeWindow* window, bool autoRefresh);
3187ec681f3Smrg
3197ec681f3Smrg/*
3207ec681f3Smrg * Enable/disable the auto prerotation at buffer allocation when the buffer size
3217ec681f3Smrg * is driven by the consumer.
3227ec681f3Smrg *
3237ec681f3Smrg * When buffer size is driven by the consumer and the transform hint specifies
3247ec681f3Smrg * a 90 or 270 degree rotation, if auto prerotation is enabled, the width and
3257ec681f3Smrg * height used for dequeueBuffer will be additionally swapped.
3267ec681f3Smrg */
3277ec681f3Smrgint ANativeWindow_setAutoPrerotation(ANativeWindow* window, bool autoPrerotation);
3287ec681f3Smrg
3297ec681f3Smrg/*****************************************************************************/
3307ec681f3Smrg
3317ec681f3Smrg__END_DECLS
3327ec681f3Smrg
3337ec681f3Smrg#endif /* ANDROID_VNDK_NATIVEWINDOW_ANATIVEWINDOW_H */
334