gralloc_handle.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
  3. * Copyright (C) 2010-2011 LunarG Inc.
  4. * Copyright (C) 2016 Linaro, Ltd., Rob Herring <robh@kernel.org>
  5. * Copyright (C) 2018 Collabora, Robert Foss <robert.foss@collabora.com>
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef __ANDROID_GRALLOC_HANDLE_H__
  26. #define __ANDROID_GRALLOC_HANDLE_H__
  27. #include <cutils/native_handle.h>
  28. #include <stdint.h>
  29. /* support users of drm_gralloc/gbm_gralloc */
  30. #define gralloc_gbm_handle_t gralloc_handle_t
  31. #define gralloc_drm_handle_t gralloc_handle_t
  32. struct gralloc_handle_t {
  33. native_handle_t base;
  34. /* dma-buf file descriptor
  35. * Must be located first since, native_handle_t is allocated
  36. * using native_handle_create(), which allocates space for
  37. * sizeof(native_handle_t) + sizeof(int) * (numFds + numInts)
  38. * numFds = GRALLOC_HANDLE_NUM_FDS
  39. * numInts = GRALLOC_HANDLE_NUM_INTS
  40. * Where numFds represents the number of FDs and
  41. * numInts represents the space needed for the
  42. * remainder of this struct.
  43. * And the FDs are expected to be found first following
  44. * native_handle_t.
  45. */
  46. int prime_fd;
  47. /* api variables */
  48. uint32_t magic; /* differentiate between allocator impls */
  49. uint32_t version; /* api version */
  50. uint32_t width; /* width of buffer in pixels */
  51. uint32_t height; /* height of buffer in pixels */
  52. uint32_t format; /* pixel format (Android) */
  53. uint32_t usage; /* android libhardware usage flags */
  54. uint32_t stride; /* the stride in bytes */
  55. int data_owner; /* owner of data (for validation) */
  56. uint64_t modifier __attribute__((aligned(8))); /* buffer modifiers */
  57. union {
  58. void *data; /* pointer to struct gralloc_gbm_bo_t */
  59. uint64_t reserved;
  60. } __attribute__((aligned(8)));
  61. };
  62. #define GRALLOC_HANDLE_VERSION 4
  63. #define GRALLOC_HANDLE_MAGIC 0x60585350
  64. #define GRALLOC_HANDLE_NUM_FDS 1
  65. #define GRALLOC_HANDLE_NUM_INTS ( \
  66. ((sizeof(struct gralloc_handle_t) - sizeof(native_handle_t))/sizeof(int)) \
  67. - GRALLOC_HANDLE_NUM_FDS)
  68. static inline struct gralloc_handle_t *gralloc_handle(buffer_handle_t handle)
  69. {
  70. return (struct gralloc_handle_t *)handle;
  71. }
  72. /**
  73. * Create a buffer handle.
  74. */
  75. static inline native_handle_t *gralloc_handle_create(int32_t width,
  76. int32_t height,
  77. int32_t hal_format,
  78. int32_t usage)
  79. {
  80. struct gralloc_handle_t *handle;
  81. native_handle_t *nhandle = native_handle_create(GRALLOC_HANDLE_NUM_FDS,
  82. GRALLOC_HANDLE_NUM_INTS);
  83. if (!nhandle)
  84. return NULL;
  85. handle = gralloc_handle(nhandle);
  86. handle->magic = GRALLOC_HANDLE_MAGIC;
  87. handle->version = GRALLOC_HANDLE_VERSION;
  88. handle->width = width;
  89. handle->height = height;
  90. handle->format = hal_format;
  91. handle->usage = usage;
  92. handle->prime_fd = -1;
  93. return nhandle;
  94. }
  95. #endif