17ec681f3Smrg/*
27ec681f3Smrg * Copyright (C) 2008 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#ifndef ANDROID_GRALLOC_INTERFACE_H
197ec681f3Smrg#define ANDROID_GRALLOC_INTERFACE_H
207ec681f3Smrg
217ec681f3Smrg#include <system/graphics.h>
227ec681f3Smrg#include <hardware/hardware.h>
237ec681f3Smrg
247ec681f3Smrg#include <stdint.h>
257ec681f3Smrg#include <sys/cdefs.h>
267ec681f3Smrg#include <sys/types.h>
277ec681f3Smrg
287ec681f3Smrg#include <cutils/native_handle.h>
297ec681f3Smrg
307ec681f3Smrg#include <hardware/hardware.h>
317ec681f3Smrg#include <hardware/fb.h>
327ec681f3Smrg
337ec681f3Smrg__BEGIN_DECLS
347ec681f3Smrg
357ec681f3Smrg/**
367ec681f3Smrg * Module versioning information for the Gralloc hardware module, based on
377ec681f3Smrg * gralloc_module_t.common.module_api_version.
387ec681f3Smrg *
397ec681f3Smrg * Version History:
407ec681f3Smrg *
417ec681f3Smrg * GRALLOC_MODULE_API_VERSION_0_1:
427ec681f3Smrg * Initial Gralloc hardware module API.
437ec681f3Smrg *
447ec681f3Smrg * GRALLOC_MODULE_API_VERSION_0_2:
457ec681f3Smrg * Add support for flexible YCbCr format with (*lock_ycbcr)() method.
467ec681f3Smrg *
477ec681f3Smrg * GRALLOC_MODULE_API_VERSION_0_3:
487ec681f3Smrg * Add support for fence passing to/from lock/unlock.
497ec681f3Smrg */
507ec681f3Smrg
517ec681f3Smrg#define GRALLOC_MODULE_API_VERSION_0_1  HARDWARE_MODULE_API_VERSION(0, 1)
527ec681f3Smrg#define GRALLOC_MODULE_API_VERSION_0_2  HARDWARE_MODULE_API_VERSION(0, 2)
537ec681f3Smrg#define GRALLOC_MODULE_API_VERSION_0_3  HARDWARE_MODULE_API_VERSION(0, 3)
547ec681f3Smrg
557ec681f3Smrg#define GRALLOC_DEVICE_API_VERSION_0_1  HARDWARE_DEVICE_API_VERSION(0, 1)
567ec681f3Smrg
577ec681f3Smrg/**
587ec681f3Smrg * The id of this module
597ec681f3Smrg */
607ec681f3Smrg#define GRALLOC_HARDWARE_MODULE_ID "gralloc"
617ec681f3Smrg
627ec681f3Smrg/**
637ec681f3Smrg * Name of the graphics device to open
647ec681f3Smrg */
657ec681f3Smrg
667ec681f3Smrg#define GRALLOC_HARDWARE_GPU0 "gpu0"
677ec681f3Smrg
687ec681f3Smrgenum {
697ec681f3Smrg    /* buffer is never read in software */
707ec681f3Smrg    GRALLOC_USAGE_SW_READ_NEVER         = 0x00000000U,
717ec681f3Smrg    /* buffer is rarely read in software */
727ec681f3Smrg    GRALLOC_USAGE_SW_READ_RARELY        = 0x00000002U,
737ec681f3Smrg    /* buffer is often read in software */
747ec681f3Smrg    GRALLOC_USAGE_SW_READ_OFTEN         = 0x00000003U,
757ec681f3Smrg    /* mask for the software read values */
767ec681f3Smrg    GRALLOC_USAGE_SW_READ_MASK          = 0x0000000FU,
777ec681f3Smrg
787ec681f3Smrg    /* buffer is never written in software */
797ec681f3Smrg    GRALLOC_USAGE_SW_WRITE_NEVER        = 0x00000000U,
807ec681f3Smrg    /* buffer is rarely written in software */
817ec681f3Smrg    GRALLOC_USAGE_SW_WRITE_RARELY       = 0x00000020U,
827ec681f3Smrg    /* buffer is often written in software */
837ec681f3Smrg    GRALLOC_USAGE_SW_WRITE_OFTEN        = 0x00000030U,
847ec681f3Smrg    /* mask for the software write values */
857ec681f3Smrg    GRALLOC_USAGE_SW_WRITE_MASK         = 0x000000F0U,
867ec681f3Smrg
877ec681f3Smrg    /* buffer will be used as an OpenGL ES texture */
887ec681f3Smrg    GRALLOC_USAGE_HW_TEXTURE            = 0x00000100U,
897ec681f3Smrg    /* buffer will be used as an OpenGL ES render target */
907ec681f3Smrg    GRALLOC_USAGE_HW_RENDER             = 0x00000200U,
917ec681f3Smrg    /* buffer will be used by the 2D hardware blitter */
927ec681f3Smrg    GRALLOC_USAGE_HW_2D                 = 0x00000400U,
937ec681f3Smrg    /* buffer will be used by the HWComposer HAL module */
947ec681f3Smrg    GRALLOC_USAGE_HW_COMPOSER           = 0x00000800U,
957ec681f3Smrg    /* buffer will be used with the framebuffer device */
967ec681f3Smrg    GRALLOC_USAGE_HW_FB                 = 0x00001000U,
977ec681f3Smrg
987ec681f3Smrg    /* buffer should be displayed full-screen on an external display when
997ec681f3Smrg     * possible */
1007ec681f3Smrg    GRALLOC_USAGE_EXTERNAL_DISP         = 0x00002000U,
1017ec681f3Smrg
1027ec681f3Smrg    /* Must have a hardware-protected path to external display sink for
1037ec681f3Smrg     * this buffer.  If a hardware-protected path is not available, then
1047ec681f3Smrg     * either don't composite only this buffer (preferred) to the
1057ec681f3Smrg     * external sink, or (less desirable) do not route the entire
1067ec681f3Smrg     * composition to the external sink.  */
1077ec681f3Smrg    GRALLOC_USAGE_PROTECTED             = 0x00004000U,
1087ec681f3Smrg
1097ec681f3Smrg    /* buffer may be used as a cursor */
1107ec681f3Smrg    GRALLOC_USAGE_CURSOR                = 0x00008000U,
1117ec681f3Smrg
1127ec681f3Smrg    /* buffer will be used with the HW video encoder */
1137ec681f3Smrg    GRALLOC_USAGE_HW_VIDEO_ENCODER      = 0x00010000U,
1147ec681f3Smrg    /* buffer will be written by the HW camera pipeline */
1157ec681f3Smrg    GRALLOC_USAGE_HW_CAMERA_WRITE       = 0x00020000U,
1167ec681f3Smrg    /* buffer will be read by the HW camera pipeline */
1177ec681f3Smrg    GRALLOC_USAGE_HW_CAMERA_READ        = 0x00040000U,
1187ec681f3Smrg    /* buffer will be used as part of zero-shutter-lag queue */
1197ec681f3Smrg    GRALLOC_USAGE_HW_CAMERA_ZSL         = 0x00060000U,
1207ec681f3Smrg    /* mask for the camera access values */
1217ec681f3Smrg    GRALLOC_USAGE_HW_CAMERA_MASK        = 0x00060000U,
1227ec681f3Smrg    /* mask for the software usage bit-mask */
1237ec681f3Smrg    GRALLOC_USAGE_HW_MASK               = 0x00071F00U,
1247ec681f3Smrg
1257ec681f3Smrg    /* buffer will be used as a RenderScript Allocation */
1267ec681f3Smrg    GRALLOC_USAGE_RENDERSCRIPT          = 0x00100000U,
1277ec681f3Smrg
1287ec681f3Smrg    /* Set by the consumer to indicate to the producer that they may attach a
1297ec681f3Smrg     * buffer that they did not detach from the BufferQueue. Will be filtered
1307ec681f3Smrg     * out by GRALLOC_USAGE_ALLOC_MASK, so gralloc modules will not need to
1317ec681f3Smrg     * handle this flag. */
1327ec681f3Smrg    GRALLOC_USAGE_FOREIGN_BUFFERS       = 0x00200000U,
1337ec681f3Smrg
1347ec681f3Smrg    /* buffer will be used as input to HW HEIC image encoder */
1357ec681f3Smrg    GRALLOC_USAGE_HW_IMAGE_ENCODER      = 0x08000000U,
1367ec681f3Smrg
1377ec681f3Smrg    /* Mask of all flags which could be passed to a gralloc module for buffer
1387ec681f3Smrg     * allocation. Any flags not in this mask do not need to be handled by
1397ec681f3Smrg     * gralloc modules. */
1407ec681f3Smrg    GRALLOC_USAGE_ALLOC_MASK            = ~(GRALLOC_USAGE_FOREIGN_BUFFERS),
1417ec681f3Smrg
1427ec681f3Smrg    /* implementation-specific private usage flags */
1437ec681f3Smrg    GRALLOC_USAGE_PRIVATE_0             = 0x10000000U,
1447ec681f3Smrg    GRALLOC_USAGE_PRIVATE_1             = 0x20000000U,
1457ec681f3Smrg    GRALLOC_USAGE_PRIVATE_2             = 0x40000000U,
1467ec681f3Smrg    GRALLOC_USAGE_PRIVATE_3             = 0x80000000U,
1477ec681f3Smrg    GRALLOC_USAGE_PRIVATE_MASK          = 0xF0000000U,
1487ec681f3Smrg};
1497ec681f3Smrg
1507ec681f3Smrg/*****************************************************************************/
1517ec681f3Smrg
1527ec681f3Smrg/**
1537ec681f3Smrg * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
1547ec681f3Smrg * and the fields of this data structure must begin with hw_module_t
1557ec681f3Smrg * followed by module specific information.
1567ec681f3Smrg */
1577ec681f3Smrgtypedef struct gralloc_module_t {
1587ec681f3Smrg    struct hw_module_t common;
1597ec681f3Smrg
1607ec681f3Smrg    /*
1617ec681f3Smrg     * (*registerBuffer)() must be called before a buffer_handle_t that has not
1627ec681f3Smrg     * been created with (*alloc_device_t::alloc)() can be used.
1637ec681f3Smrg     *
1647ec681f3Smrg     * This is intended to be used with buffer_handle_t's that have been
1657ec681f3Smrg     * received in this process through IPC.
1667ec681f3Smrg     *
1677ec681f3Smrg     * This function checks that the handle is indeed a valid one and prepares
1687ec681f3Smrg     * it for use with (*lock)() and (*unlock)().
1697ec681f3Smrg     *
1707ec681f3Smrg     * It is not necessary to call (*registerBuffer)() on a handle created
1717ec681f3Smrg     * with (*alloc_device_t::alloc)().
1727ec681f3Smrg     *
1737ec681f3Smrg     * returns an error if this buffer_handle_t is not valid.
1747ec681f3Smrg     */
1757ec681f3Smrg    int (*registerBuffer)(struct gralloc_module_t const* module,
1767ec681f3Smrg            buffer_handle_t handle);
1777ec681f3Smrg
1787ec681f3Smrg    /*
1797ec681f3Smrg     * (*unregisterBuffer)() is called once this handle is no longer needed in
1807ec681f3Smrg     * this process. After this call, it is an error to call (*lock)(),
1817ec681f3Smrg     * (*unlock)(), or (*registerBuffer)().
1827ec681f3Smrg     *
1837ec681f3Smrg     * This function doesn't close or free the handle itself; this is done
1847ec681f3Smrg     * by other means, usually through libcutils's native_handle_close() and
1857ec681f3Smrg     * native_handle_free().
1867ec681f3Smrg     *
1877ec681f3Smrg     * It is an error to call (*unregisterBuffer)() on a buffer that wasn't
1887ec681f3Smrg     * explicitly registered first.
1897ec681f3Smrg     */
1907ec681f3Smrg    int (*unregisterBuffer)(struct gralloc_module_t const* module,
1917ec681f3Smrg            buffer_handle_t handle);
1927ec681f3Smrg
1937ec681f3Smrg    /*
1947ec681f3Smrg     * The (*lock)() method is called before a buffer is accessed for the
1957ec681f3Smrg     * specified usage. This call may block, for instance if the h/w needs
1967ec681f3Smrg     * to finish rendering or if CPU caches need to be synchronized.
1977ec681f3Smrg     *
1987ec681f3Smrg     * The caller promises to modify only pixels in the area specified
1997ec681f3Smrg     * by (l,t,w,h).
2007ec681f3Smrg     *
2017ec681f3Smrg     * The content of the buffer outside of the specified area is NOT modified
2027ec681f3Smrg     * by this call.
2037ec681f3Smrg     *
2047ec681f3Smrg     * If usage specifies GRALLOC_USAGE_SW_*, vaddr is filled with the address
2057ec681f3Smrg     * of the buffer in virtual memory.
2067ec681f3Smrg     *
2077ec681f3Smrg     * Note calling (*lock)() on HAL_PIXEL_FORMAT_YCbCr_*_888 buffers will fail
2087ec681f3Smrg     * and return -EINVAL.  These buffers must be locked with (*lock_ycbcr)()
2097ec681f3Smrg     * instead.
2107ec681f3Smrg     *
2117ec681f3Smrg     * THREADING CONSIDERATIONS:
2127ec681f3Smrg     *
2137ec681f3Smrg     * It is legal for several different threads to lock a buffer from
2147ec681f3Smrg     * read access, none of the threads are blocked.
2157ec681f3Smrg     *
2167ec681f3Smrg     * However, locking a buffer simultaneously for write or read/write is
2177ec681f3Smrg     * undefined, but:
2187ec681f3Smrg     * - shall not result in termination of the process
2197ec681f3Smrg     * - shall not block the caller
2207ec681f3Smrg     * It is acceptable to return an error or to leave the buffer's content
2217ec681f3Smrg     * into an indeterminate state.
2227ec681f3Smrg     *
2237ec681f3Smrg     * If the buffer was created with a usage mask incompatible with the
2247ec681f3Smrg     * requested usage flags here, -EINVAL is returned.
2257ec681f3Smrg     *
2267ec681f3Smrg     */
2277ec681f3Smrg
2287ec681f3Smrg    int (*lock)(struct gralloc_module_t const* module,
2297ec681f3Smrg            buffer_handle_t handle, int usage,
2307ec681f3Smrg            int l, int t, int w, int h,
2317ec681f3Smrg            void** vaddr);
2327ec681f3Smrg
2337ec681f3Smrg
2347ec681f3Smrg    /*
2357ec681f3Smrg     * The (*unlock)() method must be called after all changes to the buffer
2367ec681f3Smrg     * are completed.
2377ec681f3Smrg     */
2387ec681f3Smrg
2397ec681f3Smrg    int (*unlock)(struct gralloc_module_t const* module,
2407ec681f3Smrg            buffer_handle_t handle);
2417ec681f3Smrg
2427ec681f3Smrg
2437ec681f3Smrg    /* reserved for future use */
2447ec681f3Smrg    int (*perform)(struct gralloc_module_t const* module,
2457ec681f3Smrg            int operation, ... );
2467ec681f3Smrg
2477ec681f3Smrg    /*
2487ec681f3Smrg     * The (*lock_ycbcr)() method is like the (*lock)() method, with the
2497ec681f3Smrg     * difference that it fills a struct ycbcr with a description of the buffer
2507ec681f3Smrg     * layout, and zeroes out the reserved fields.
2517ec681f3Smrg     *
2527ec681f3Smrg     * If the buffer format is not compatible with a flexible YUV format (e.g.
2537ec681f3Smrg     * the buffer layout cannot be represented with the ycbcr struct), it
2547ec681f3Smrg     * will return -EINVAL.
2557ec681f3Smrg     *
2567ec681f3Smrg     * This method must work on buffers with HAL_PIXEL_FORMAT_YCbCr_*_888
2577ec681f3Smrg     * if supported by the device, as well as with any other format that is
2587ec681f3Smrg     * requested by the multimedia codecs when they are configured with a
2597ec681f3Smrg     * flexible-YUV-compatible color-format with android native buffers.
2607ec681f3Smrg     *
2617ec681f3Smrg     * Note that this method may also be called on buffers of other formats,
2627ec681f3Smrg     * including non-YUV formats.
2637ec681f3Smrg     *
2647ec681f3Smrg     * Added in GRALLOC_MODULE_API_VERSION_0_2.
2657ec681f3Smrg     */
2667ec681f3Smrg
2677ec681f3Smrg    int (*lock_ycbcr)(struct gralloc_module_t const* module,
2687ec681f3Smrg            buffer_handle_t handle, int usage,
2697ec681f3Smrg            int l, int t, int w, int h,
2707ec681f3Smrg            struct android_ycbcr *ycbcr);
2717ec681f3Smrg
2727ec681f3Smrg    /*
2737ec681f3Smrg     * The (*lockAsync)() method is like the (*lock)() method except
2747ec681f3Smrg     * that the buffer's sync fence object is passed into the lock
2757ec681f3Smrg     * call instead of requiring the caller to wait for completion.
2767ec681f3Smrg     *
2777ec681f3Smrg     * The gralloc implementation takes ownership of the fenceFd and
2787ec681f3Smrg     * is responsible for closing it when no longer needed.
2797ec681f3Smrg     *
2807ec681f3Smrg     * Added in GRALLOC_MODULE_API_VERSION_0_3.
2817ec681f3Smrg     */
2827ec681f3Smrg    int (*lockAsync)(struct gralloc_module_t const* module,
2837ec681f3Smrg            buffer_handle_t handle, int usage,
2847ec681f3Smrg            int l, int t, int w, int h,
2857ec681f3Smrg            void** vaddr, int fenceFd);
2867ec681f3Smrg
2877ec681f3Smrg    /*
2887ec681f3Smrg     * The (*unlockAsync)() method is like the (*unlock)() method
2897ec681f3Smrg     * except that a buffer sync fence object is returned from the
2907ec681f3Smrg     * lock call, representing the completion of any pending work
2917ec681f3Smrg     * performed by the gralloc implementation.
2927ec681f3Smrg     *
2937ec681f3Smrg     * The caller takes ownership of the fenceFd and is responsible
2947ec681f3Smrg     * for closing it when no longer needed.
2957ec681f3Smrg     *
2967ec681f3Smrg     * Added in GRALLOC_MODULE_API_VERSION_0_3.
2977ec681f3Smrg     */
2987ec681f3Smrg    int (*unlockAsync)(struct gralloc_module_t const* module,
2997ec681f3Smrg            buffer_handle_t handle, int* fenceFd);
3007ec681f3Smrg
3017ec681f3Smrg    /*
3027ec681f3Smrg     * The (*lockAsync_ycbcr)() method is like the (*lock_ycbcr)()
3037ec681f3Smrg     * method except that the buffer's sync fence object is passed
3047ec681f3Smrg     * into the lock call instead of requiring the caller to wait for
3057ec681f3Smrg     * completion.
3067ec681f3Smrg     *
3077ec681f3Smrg     * The gralloc implementation takes ownership of the fenceFd and
3087ec681f3Smrg     * is responsible for closing it when no longer needed.
3097ec681f3Smrg     *
3107ec681f3Smrg     * Added in GRALLOC_MODULE_API_VERSION_0_3.
3117ec681f3Smrg     */
3127ec681f3Smrg    int (*lockAsync_ycbcr)(struct gralloc_module_t const* module,
3137ec681f3Smrg            buffer_handle_t handle, int usage,
3147ec681f3Smrg            int l, int t, int w, int h,
3157ec681f3Smrg            struct android_ycbcr *ycbcr, int fenceFd);
3167ec681f3Smrg
3177ec681f3Smrg    /* getTransportSize(..., outNumFds, outNumInts)
3187ec681f3Smrg     * This function is mandatory on devices running IMapper2.1 or higher.
3197ec681f3Smrg     *
3207ec681f3Smrg     * Get the transport size of a buffer. An imported buffer handle is a raw
3217ec681f3Smrg     * buffer handle with the process-local runtime data appended. This
3227ec681f3Smrg     * function, for example, allows a caller to omit the process-local
3237ec681f3Smrg     * runtime data at the tail when serializing the imported buffer handle.
3247ec681f3Smrg     *
3257ec681f3Smrg     * Note that a client might or might not omit the process-local runtime
3267ec681f3Smrg     * data when sending an imported buffer handle. The mapper must support
3277ec681f3Smrg     * both cases on the receiving end.
3287ec681f3Smrg     */
3297ec681f3Smrg    int32_t (*getTransportSize)(
3307ec681f3Smrg            struct gralloc_module_t const* module, buffer_handle_t handle, uint32_t *outNumFds,
3317ec681f3Smrg            uint32_t *outNumInts);
3327ec681f3Smrg
3337ec681f3Smrg    /* validateBufferSize(..., w, h, format, usage, stride)
3347ec681f3Smrg     * This function is mandatory on devices running IMapper2.1 or higher.
3357ec681f3Smrg     *
3367ec681f3Smrg     * Validate that the buffer can be safely accessed by a caller who assumes
3377ec681f3Smrg     * the specified width, height, format, usage, and stride. This must at least validate
3387ec681f3Smrg     * that the buffer size is large enough. Validating the buffer against
3397ec681f3Smrg     * individual buffer attributes is optional.
3407ec681f3Smrg     */
3417ec681f3Smrg    int32_t (*validateBufferSize)(
3427ec681f3Smrg            struct gralloc_module_t const* device, buffer_handle_t handle,
3437ec681f3Smrg            uint32_t w, uint32_t h, int32_t format, int usage,
3447ec681f3Smrg            uint32_t stride);
3457ec681f3Smrg
3467ec681f3Smrg    /* reserved for future use */
3477ec681f3Smrg    void* reserved_proc[1];
3487ec681f3Smrg
3497ec681f3Smrg} gralloc_module_t;
3507ec681f3Smrg
3517ec681f3Smrg/*****************************************************************************/
3527ec681f3Smrg
3537ec681f3Smrg/**
3547ec681f3Smrg * Every device data structure must begin with hw_device_t
3557ec681f3Smrg * followed by module specific public methods and attributes.
3567ec681f3Smrg */
3577ec681f3Smrg
3587ec681f3Smrgtypedef struct alloc_device_t {
3597ec681f3Smrg    struct hw_device_t common;
3607ec681f3Smrg
3617ec681f3Smrg    /*
3627ec681f3Smrg     * (*alloc)() Allocates a buffer in graphic memory with the requested
3637ec681f3Smrg     * parameters and returns a buffer_handle_t and the stride in pixels to
3647ec681f3Smrg     * allow the implementation to satisfy hardware constraints on the width
3657ec681f3Smrg     * of a pixmap (eg: it may have to be multiple of 8 pixels).
3667ec681f3Smrg     * The CALLER TAKES OWNERSHIP of the buffer_handle_t.
3677ec681f3Smrg     *
3687ec681f3Smrg     * If format is HAL_PIXEL_FORMAT_YCbCr_420_888, the returned stride must be
3697ec681f3Smrg     * 0, since the actual strides are available from the android_ycbcr
3707ec681f3Smrg     * structure.
3717ec681f3Smrg     *
3727ec681f3Smrg     * Returns 0 on success or -errno on error.
3737ec681f3Smrg     */
3747ec681f3Smrg
3757ec681f3Smrg    int (*alloc)(struct alloc_device_t* dev,
3767ec681f3Smrg            int w, int h, int format, int usage,
3777ec681f3Smrg            buffer_handle_t* handle, int* stride);
3787ec681f3Smrg
3797ec681f3Smrg    /*
3807ec681f3Smrg     * (*free)() Frees a previously allocated buffer.
3817ec681f3Smrg     * Behavior is undefined if the buffer is still mapped in any process,
3827ec681f3Smrg     * but shall not result in termination of the program or security breaches
3837ec681f3Smrg     * (allowing a process to get access to another process' buffers).
3847ec681f3Smrg     * THIS FUNCTION TAKES OWNERSHIP of the buffer_handle_t which becomes
3857ec681f3Smrg     * invalid after the call.
3867ec681f3Smrg     *
3877ec681f3Smrg     * Returns 0 on success or -errno on error.
3887ec681f3Smrg     */
3897ec681f3Smrg    int (*free)(struct alloc_device_t* dev,
3907ec681f3Smrg            buffer_handle_t handle);
3917ec681f3Smrg
3927ec681f3Smrg    /* This hook is OPTIONAL.
3937ec681f3Smrg     *
3947ec681f3Smrg     * If non NULL it will be caused by SurfaceFlinger on dumpsys
3957ec681f3Smrg     */
3967ec681f3Smrg    void (*dump)(struct alloc_device_t *dev, char *buff, int buff_len);
3977ec681f3Smrg
3987ec681f3Smrg    void* reserved_proc[7];
3997ec681f3Smrg} alloc_device_t;
4007ec681f3Smrg
4017ec681f3Smrg
4027ec681f3Smrg/** convenience API for opening and closing a supported device */
4037ec681f3Smrg
4047ec681f3Smrgstatic inline int gralloc_open(const struct hw_module_t* module,
4057ec681f3Smrg        struct alloc_device_t** device) {
4067ec681f3Smrg    return module->methods->open(module,
4077ec681f3Smrg            GRALLOC_HARDWARE_GPU0, TO_HW_DEVICE_T_OPEN(device));
4087ec681f3Smrg}
4097ec681f3Smrg
4107ec681f3Smrgstatic inline int gralloc_close(struct alloc_device_t* device) {
4117ec681f3Smrg    return device->common.close(&device->common);
4127ec681f3Smrg}
4137ec681f3Smrg
4147ec681f3Smrg/**
4157ec681f3Smrg * map_usage_to_memtrack should be called after allocating a gralloc buffer.
4167ec681f3Smrg *
4177ec681f3Smrg * @param usage - it is the flag used when alloc function is called.
4187ec681f3Smrg *
4197ec681f3Smrg * This function maps the gralloc usage flags to appropriate memtrack bucket.
4207ec681f3Smrg * GrallocHAL implementers and users should make an additional ION_IOCTL_TAG
4217ec681f3Smrg * call using the memtrack tag returned by this function. This will help the
4227ec681f3Smrg * in-kernel memtack to categorize the memory allocated by different processes
4237ec681f3Smrg * according to their usage.
4247ec681f3Smrg *
4257ec681f3Smrg */
4267ec681f3Smrgstatic inline const char* map_usage_to_memtrack(uint32_t usage) {
4277ec681f3Smrg    usage &= GRALLOC_USAGE_ALLOC_MASK;
4287ec681f3Smrg
4297ec681f3Smrg    if ((usage & GRALLOC_USAGE_HW_CAMERA_WRITE) != 0) {
4307ec681f3Smrg        return "camera";
4317ec681f3Smrg    } else if ((usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) != 0 ||
4327ec681f3Smrg            (usage & GRALLOC_USAGE_EXTERNAL_DISP) != 0) {
4337ec681f3Smrg        return "video";
4347ec681f3Smrg    } else if ((usage & GRALLOC_USAGE_HW_RENDER) != 0 ||
4357ec681f3Smrg            (usage & GRALLOC_USAGE_HW_TEXTURE) != 0) {
4367ec681f3Smrg        return "gl";
4377ec681f3Smrg    } else if ((usage & GRALLOC_USAGE_HW_CAMERA_READ) != 0) {
4387ec681f3Smrg        return "camera";
4397ec681f3Smrg    } else if ((usage & GRALLOC_USAGE_SW_READ_MASK) != 0 ||
4407ec681f3Smrg            (usage & GRALLOC_USAGE_SW_WRITE_MASK) != 0) {
4417ec681f3Smrg        return "cpu";
4427ec681f3Smrg    }
4437ec681f3Smrg    return "graphics";
4447ec681f3Smrg}
4457ec681f3Smrg
4467ec681f3Smrg__END_DECLS
4477ec681f3Smrg
4487ec681f3Smrg#endif  // ANDROID_GRALLOC_INTERFACE_H
449