Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 1 | /* |
Ramakant Singh | 20b6c8c | 2019-01-02 11:53:59 +0530 | [diff] [blame] | 2 | * Copyright (c) 2011-2019, The Linux Foundation. All rights reserved. |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 3 | * Not a Contribution |
| 4 | * |
| 5 | * Copyright (C) 2008 The Android Open Source Project |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | |
| 20 | #ifndef __GR_PRIV_HANDLE_H__ |
| 21 | #define __GR_PRIV_HANDLE_H__ |
| 22 | |
Naseer Ahmed | 04a804a | 2018-03-06 20:41:14 -0500 | [diff] [blame] | 23 | #include <log/log.h> |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 24 | #include <hardware/gralloc.h> |
Naseer Ahmed | a422f35 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 25 | #include <hardware/gralloc1.h> |
Abhishek Jain | ac3641e | 2019-01-07 15:07:41 +0530 | [diff] [blame] | 26 | #ifdef __cplusplus |
Naseer Ahmed | 699b457 | 2017-03-09 12:28:45 -0500 | [diff] [blame] | 27 | #include <cinttypes> |
Abhishek Jain | ac3641e | 2019-01-07 15:07:41 +0530 | [diff] [blame] | 28 | #endif |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 29 | |
| 30 | #define GRALLOC1_FUNCTION_PERFORM 0x00001000 |
| 31 | |
| 32 | #define DBG_HANDLE false |
| 33 | |
| 34 | typedef gralloc1_error_t (*GRALLOC1_PFN_PERFORM)(gralloc1_device_t *device, int operation, ...); |
| 35 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 36 | #define PRIV_HANDLE_CONST(exp) static_cast<const private_handle_t *>(exp) |
| 37 | |
Mohan Pallaka | 8181cef | 2018-08-21 14:41:12 -0700 | [diff] [blame] | 38 | #pragma pack(push, 4) |
Abhishek Jain | ac3641e | 2019-01-07 15:07:41 +0530 | [diff] [blame] | 39 | #ifdef __cplusplus |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 40 | struct private_handle_t : public native_handle_t { |
Abhishek Jain | ac3641e | 2019-01-07 15:07:41 +0530 | [diff] [blame] | 41 | #else |
| 42 | struct private_handle_t { |
| 43 | native_handle_t nativeHandle; |
| 44 | #endif |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 45 | enum { |
| 46 | PRIV_FLAGS_FRAMEBUFFER = 0x00000001, |
| 47 | PRIV_FLAGS_USES_ION = 0x00000008, |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 48 | PRIV_FLAGS_NEEDS_FLUSH = 0x00000020, |
| 49 | PRIV_FLAGS_INTERNAL_ONLY = 0x00000040, |
| 50 | PRIV_FLAGS_NON_CPU_WRITER = 0x00000080, |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 51 | PRIV_FLAGS_CACHED = 0x00000200, |
| 52 | PRIV_FLAGS_SECURE_BUFFER = 0x00000400, |
| 53 | PRIV_FLAGS_EXTERNAL_ONLY = 0x00002000, |
| 54 | PRIV_FLAGS_PROTECTED_BUFFER = 0x00004000, |
| 55 | PRIV_FLAGS_VIDEO_ENCODER = 0x00010000, |
| 56 | PRIV_FLAGS_CAMERA_WRITE = 0x00020000, |
| 57 | PRIV_FLAGS_CAMERA_READ = 0x00040000, |
| 58 | PRIV_FLAGS_HW_COMPOSER = 0x00080000, |
| 59 | PRIV_FLAGS_HW_TEXTURE = 0x00100000, |
| 60 | PRIV_FLAGS_ITU_R_601 = 0x00200000, // Unused from display |
| 61 | PRIV_FLAGS_ITU_R_601_FR = 0x00400000, // Unused from display |
| 62 | PRIV_FLAGS_ITU_R_709 = 0x00800000, // Unused from display |
| 63 | PRIV_FLAGS_SECURE_DISPLAY = 0x01000000, |
| 64 | PRIV_FLAGS_TILE_RENDERED = 0x02000000, |
| 65 | PRIV_FLAGS_CPU_RENDERED = 0x04000000, |
| 66 | PRIV_FLAGS_UBWC_ALIGNED = 0x08000000, |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 67 | PRIV_FLAGS_DISP_CONSUMER = 0x10000000, |
Naseer Ahmed | a422f35 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 68 | PRIV_FLAGS_CLIENT_ALLOCATED = 0x20000000, // Ion buffer allocated outside of gralloc |
Mohan Pallaka | 5d81443 | 2018-08-01 10:40:13 -0700 | [diff] [blame] | 69 | PRIV_FLAGS_UBWC_ALIGNED_PI = 0x40000000, // PI format |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 70 | }; |
| 71 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 72 | // file-descriptors dup'd over IPC |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 73 | int fd; |
| 74 | int fd_metadata; |
| 75 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 76 | // values sent over IPC |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 77 | int magic; |
| 78 | int flags; |
Naseer Ahmed | a422f35 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 79 | int width; // holds width of the actual buffer allocated |
| 80 | int height; // holds height of the actual buffer allocated |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 81 | int unaligned_width; // holds width client asked to allocate |
| 82 | int unaligned_height; // holds height client asked to allocate |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 83 | int format; |
| 84 | int buffer_type; |
Naseer Ahmed | a422f35 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 85 | unsigned int layer_count; |
Mohan Pallaka | 8181cef | 2018-08-21 14:41:12 -0700 | [diff] [blame] | 86 | uint64_t id; |
| 87 | uint64_t usage; |
Naseer Ahmed | a422f35 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 88 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 89 | unsigned int size; |
| 90 | unsigned int offset; |
| 91 | unsigned int offset_metadata; |
Mohan Pallaka | 8181cef | 2018-08-21 14:41:12 -0700 | [diff] [blame] | 92 | uint64_t base; |
| 93 | uint64_t base_metadata; |
| 94 | uint64_t gpuaddr; |
Abhishek Jain | ac3641e | 2019-01-07 15:07:41 +0530 | [diff] [blame] | 95 | #ifdef __cplusplus |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 96 | static const int kNumFds = 2; |
| 97 | static const int kMagic = 'gmsm'; |
| 98 | |
| 99 | static inline int NumInts() { |
Naseer Ahmed | a422f35 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 100 | return ((sizeof(private_handle_t) - sizeof(native_handle_t)) / sizeof(int)) - kNumFds; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 101 | } |
| 102 | |
Naseer Ahmed | a422f35 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 103 | private_handle_t(int fd, int meta_fd, int flags, int width, int height, int uw, int uh, |
| 104 | int format, int buf_type, unsigned int size, uint64_t usage = 0) |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 105 | : fd(fd), |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 106 | fd_metadata(meta_fd), |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 107 | magic(kMagic), |
| 108 | flags(flags), |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 109 | width(width), |
| 110 | height(height), |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 111 | unaligned_width(uw), |
| 112 | unaligned_height(uh), |
| 113 | format(format), |
| 114 | buffer_type(buf_type), |
Naseer Ahmed | a422f35 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 115 | layer_count(1), |
| 116 | id(0), |
| 117 | usage(usage), |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 118 | size(size), |
| 119 | offset(0), |
| 120 | offset_metadata(0), |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 121 | base(0), |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 122 | base_metadata(0), |
Naseer Ahmed | a422f35 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 123 | gpuaddr(0) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 124 | version = static_cast<int>(sizeof(native_handle)); |
| 125 | numInts = NumInts(); |
| 126 | numFds = kNumFds; |
| 127 | } |
| 128 | |
Naseer Ahmed | a422f35 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 129 | // Legacy constructor used by some clients |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 130 | private_handle_t(int fd, unsigned int size, int usage, int buf_type, int format, int w, int h) |
Naseer Ahmed | a422f35 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 131 | : private_handle_t(fd, -1, PRIV_FLAGS_CLIENT_ALLOCATED, w, h, 0, 0, format, buf_type, size, |
| 132 | static_cast<uint64_t>(usage)) {} |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 133 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 134 | ~private_handle_t() { |
| 135 | magic = 0; |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 136 | ALOGE_IF(DBG_HANDLE, "Deleting buffer handle %p", this); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static int validate(const native_handle *h) { |
Naseer Ahmed | a422f35 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 140 | auto *hnd = static_cast<const private_handle_t *>(h); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 141 | if (!h || h->version != sizeof(native_handle) || h->numInts != NumInts() || |
Ramakant Singh | 20b6c8c | 2019-01-02 11:53:59 +0530 | [diff] [blame] | 142 | h->numFds != kNumFds) { |
| 143 | ALOGE("Invalid gralloc handle (at %p): ver(%d/%zu) ints(%d/%d) fds(%d/%d)", |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 144 | h, h ? h->version : -1, sizeof(native_handle), h ? h->numInts : -1, NumInts(), |
Ramakant Singh | 20b6c8c | 2019-01-02 11:53:59 +0530 | [diff] [blame] | 145 | h ? h->numFds : -1, kNumFds); |
| 146 | return -EINVAL; |
| 147 | } |
| 148 | if (hnd->magic != kMagic) { |
| 149 | ALOGE("handle = %p invalid magic(%c%c%c%c/%c%c%c%c)",hnd, |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 150 | hnd ? (((hnd->magic >> 24) & 0xFF) ? ((hnd->magic >> 24) & 0xFF) : '-') : '?', |
| 151 | hnd ? (((hnd->magic >> 16) & 0xFF) ? ((hnd->magic >> 16) & 0xFF) : '-') : '?', |
| 152 | hnd ? (((hnd->magic >> 8) & 0xFF) ? ((hnd->magic >> 8) & 0xFF) : '-') : '?', |
| 153 | hnd ? (((hnd->magic >> 0) & 0xFF) ? ((hnd->magic >> 0) & 0xFF) : '-') : '?', |
| 154 | (kMagic >> 24) & 0xFF, (kMagic >> 16) & 0xFF, (kMagic >> 8) & 0xFF, (kMagic >> 0) & 0xFF); |
| 155 | return -EINVAL; |
| 156 | } |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
Naseer Ahmed | 699b457 | 2017-03-09 12:28:45 -0500 | [diff] [blame] | 161 | static void Dump(const private_handle_t *hnd) { |
Naseer Ahmed | a422f35 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 162 | ALOGD("handle id:%" PRIu64 |
| 163 | " wxh:%dx%d uwxuh:%dx%d size: %d fd:%d fd_meta:%d flags:0x%x " |
| 164 | "usage:0x%" PRIx64 " format:0x%x layer_count: %d", |
Naseer Ahmed | 699b457 | 2017-03-09 12:28:45 -0500 | [diff] [blame] | 165 | hnd->id, hnd->width, hnd->height, hnd->unaligned_width, hnd->unaligned_height, hnd->size, |
Naseer Ahmed | a422f35 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 166 | hnd->fd, hnd->fd_metadata, hnd->flags, hnd->usage, hnd->format, hnd->layer_count); |
Naseer Ahmed | 699b457 | 2017-03-09 12:28:45 -0500 | [diff] [blame] | 167 | } |
| 168 | |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 169 | int GetUnalignedWidth() const { return unaligned_width; } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 170 | |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 171 | int GetUnalignedHeight() const { return unaligned_height; } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 172 | |
| 173 | int GetColorFormat() const { return format; } |
| 174 | |
Naseer Ahmed | baa39c5 | 2017-03-27 14:00:07 -0400 | [diff] [blame] | 175 | unsigned int GetLayerCount() const { return layer_count; } |
| 176 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 177 | int GetStride() const { |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 178 | // In handle we currently store aligned width after allocation. |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 179 | return width; |
| 180 | } |
| 181 | |
Naseer Ahmed | a422f35 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 182 | uint64_t GetUsage() const { return usage; } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 183 | |
Naseer Ahmed | 8f9c7c3 | 2017-02-21 16:45:14 -0500 | [diff] [blame] | 184 | uint64_t GetBackingstore() const { return id; } |
Abhishek Jain | ac3641e | 2019-01-07 15:07:41 +0530 | [diff] [blame] | 185 | #endif |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 186 | }; |
Mohan Pallaka | 8181cef | 2018-08-21 14:41:12 -0700 | [diff] [blame] | 187 | #pragma pack(pop) |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 188 | |
| 189 | #endif // __GR_PRIV_HANDLE_H__ |