Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011-2016, The Linux Foundation. All rights reserved. |
| 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 | |
| 23 | #include <cutils/log.h> |
| 24 | #include <hardware/gralloc1.h> |
| 25 | |
| 26 | #define GRALLOC1_FUNCTION_PERFORM 0x00001000 |
| 27 | |
| 28 | #define DBG_HANDLE false |
| 29 | |
| 30 | typedef gralloc1_error_t (*GRALLOC1_PFN_PERFORM)(gralloc1_device_t *device, int operation, ...); |
| 31 | |
| 32 | typedef int BackStoreFd; |
| 33 | |
| 34 | #define PRIV_HANDLE_CONST(exp) static_cast<const private_handle_t *>(exp) |
| 35 | |
| 36 | struct private_handle_t : public native_handle_t { |
| 37 | // TODO(user): Moving PRIV_FLAGS to #defs & check for each PRIV_FLAG and remove unused. |
| 38 | enum { |
| 39 | PRIV_FLAGS_FRAMEBUFFER = 0x00000001, |
| 40 | PRIV_FLAGS_USES_ION = 0x00000008, |
| 41 | PRIV_FLAGS_USES_ASHMEM = 0x00000010, |
| 42 | PRIV_FLAGS_NEEDS_FLUSH = 0x00000020, |
| 43 | PRIV_FLAGS_INTERNAL_ONLY = 0x00000040, |
| 44 | PRIV_FLAGS_NON_CPU_WRITER = 0x00000080, |
| 45 | PRIV_FLAGS_NONCONTIGUOUS_MEM = 0x00000100, |
| 46 | PRIV_FLAGS_CACHED = 0x00000200, |
| 47 | PRIV_FLAGS_SECURE_BUFFER = 0x00000400, |
| 48 | PRIV_FLAGS_EXTERNAL_ONLY = 0x00002000, |
| 49 | PRIV_FLAGS_PROTECTED_BUFFER = 0x00004000, |
| 50 | PRIV_FLAGS_VIDEO_ENCODER = 0x00010000, |
| 51 | PRIV_FLAGS_CAMERA_WRITE = 0x00020000, |
| 52 | PRIV_FLAGS_CAMERA_READ = 0x00040000, |
| 53 | PRIV_FLAGS_HW_COMPOSER = 0x00080000, |
| 54 | PRIV_FLAGS_HW_TEXTURE = 0x00100000, |
| 55 | PRIV_FLAGS_ITU_R_601 = 0x00200000, // Unused from display |
| 56 | PRIV_FLAGS_ITU_R_601_FR = 0x00400000, // Unused from display |
| 57 | PRIV_FLAGS_ITU_R_709 = 0x00800000, // Unused from display |
| 58 | PRIV_FLAGS_SECURE_DISPLAY = 0x01000000, |
| 59 | PRIV_FLAGS_TILE_RENDERED = 0x02000000, |
| 60 | PRIV_FLAGS_CPU_RENDERED = 0x04000000, |
| 61 | PRIV_FLAGS_UBWC_ALIGNED = 0x08000000, |
| 62 | PRIV_FLAGS_DISP_CONSUMER = 0x10000000 |
| 63 | }; |
| 64 | |
| 65 | // file-descriptors |
| 66 | int fd; |
| 67 | int fd_metadata; |
| 68 | |
| 69 | // ints |
| 70 | int magic; |
| 71 | int flags; |
| 72 | unsigned int size; |
| 73 | unsigned int offset; |
| 74 | int buffer_type; |
| 75 | uint64_t base __attribute__((aligned(8))); |
| 76 | unsigned int offset_metadata; |
| 77 | |
| 78 | // The gpu address mapped into the mmu. |
| 79 | uint64_t gpuaddr __attribute__((aligned(8))); |
| 80 | |
| 81 | int format; |
| 82 | int width; // holds width of the actual buffer allocated |
| 83 | int height; // holds height of the actual buffer allocated |
| 84 | |
| 85 | int stride; |
| 86 | uint64_t base_metadata __attribute__((aligned(8))); |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 87 | unsigned int fb_id; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 88 | |
| 89 | // added for gralloc1 |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 90 | int unaligned_width; // holds width client asked to allocate |
| 91 | int unaligned_height; // holds height client asked to allocate |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 92 | gralloc1_producer_usage_t producer_usage __attribute__((aligned(8))); |
| 93 | gralloc1_consumer_usage_t consumer_usage __attribute__((aligned(8))); |
| 94 | |
| 95 | static const int kNumFds = 2; |
| 96 | static const int kMagic = 'gmsm'; |
| 97 | |
| 98 | static inline int NumInts() { |
| 99 | return ((sizeof(private_handle_t) - sizeof(native_handle_t)) / sizeof(int)) - kNumFds; |
| 100 | } |
| 101 | |
| 102 | private_handle_t(int fd, unsigned int size, int flags, int buf_type, int format, int width, |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 103 | int height) |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 104 | : fd(fd), |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 105 | fd_metadata(-1), |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 106 | magic(kMagic), |
| 107 | flags(flags), |
| 108 | size(size), |
| 109 | offset(0), |
| 110 | buffer_type(buf_type), |
| 111 | base(0), |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 112 | offset_metadata(0), |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 113 | gpuaddr(0), |
| 114 | format(format), |
| 115 | width(width), |
| 116 | height(height), |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 117 | base_metadata(0), |
| 118 | unaligned_width(width), |
| 119 | unaligned_height(height), |
| 120 | producer_usage(GRALLOC1_PRODUCER_USAGE_NONE), |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 121 | consumer_usage(GRALLOC1_CONSUMER_USAGE_NONE), |
| 122 | fb_id(0) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 123 | version = static_cast<int>(sizeof(native_handle)); |
| 124 | numInts = NumInts(); |
| 125 | numFds = kNumFds; |
| 126 | } |
| 127 | |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 128 | private_handle_t(int fd, unsigned int size, int flags, int buf_type, int format, int width, |
| 129 | int height, int meta_fd, unsigned int meta_offset, uint64_t meta_base) |
| 130 | : private_handle_t(fd, size, flags, buf_type, format, width, height) { |
| 131 | fd_metadata = meta_fd; |
| 132 | offset_metadata = meta_offset; |
| 133 | base_metadata = meta_base; |
| 134 | } |
| 135 | |
| 136 | private_handle_t(int fd, unsigned int size, int flags, int buf_type, int format, int width, |
| 137 | int height, int meta_fd, unsigned int meta_offset, uint64_t meta_base, |
| 138 | int unaligned_w , int unaligned_h, |
| 139 | gralloc1_producer_usage_t prod_usage, gralloc1_consumer_usage_t cons_usage) |
| 140 | : private_handle_t(fd, size, flags, buf_type, format, width, height, meta_fd, meta_offset |
| 141 | meta_base) { |
| 142 | unaligned_width = unaligned_w; |
| 143 | unaligned_height = unaligned_h; |
| 144 | producer_usage = prod_usage; |
| 145 | consumer_usage = cons_usage; |
| 146 | } |
| 147 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 148 | ~private_handle_t() { |
| 149 | magic = 0; |
| 150 | ALOGE_IF(DBG_HANDLE, "deleting buffer handle %p", this); |
| 151 | } |
| 152 | |
| 153 | static int validate(const native_handle *h) { |
| 154 | const private_handle_t *hnd = (const private_handle_t *)h; |
| 155 | if (!h || h->version != sizeof(native_handle) || h->numInts != NumInts() || |
| 156 | h->numFds != kNumFds || hnd->magic != kMagic) { |
| 157 | ALOGE( |
| 158 | "Invalid gralloc handle (at %p): ver(%d/%zu) ints(%d/%d) fds(%d/%d) " |
| 159 | "magic(%c%c%c%c/%c%c%c%c)", |
| 160 | h, h ? h->version : -1, sizeof(native_handle), h ? h->numInts : -1, NumInts(), |
| 161 | h ? h->numFds : -1, kNumFds, |
| 162 | hnd ? (((hnd->magic >> 24) & 0xFF) ? ((hnd->magic >> 24) & 0xFF) : '-') : '?', |
| 163 | hnd ? (((hnd->magic >> 16) & 0xFF) ? ((hnd->magic >> 16) & 0xFF) : '-') : '?', |
| 164 | hnd ? (((hnd->magic >> 8) & 0xFF) ? ((hnd->magic >> 8) & 0xFF) : '-') : '?', |
| 165 | hnd ? (((hnd->magic >> 0) & 0xFF) ? ((hnd->magic >> 0) & 0xFF) : '-') : '?', |
| 166 | (kMagic >> 24) & 0xFF, (kMagic >> 16) & 0xFF, (kMagic >> 8) & 0xFF, (kMagic >> 0) & 0xFF); |
| 167 | return -EINVAL; |
| 168 | } |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 173 | int GetUnalignedWidth() const { return unaligned_width; } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 174 | |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 175 | int GetUnalignedHeight() const { return unaligned_height; } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 176 | |
| 177 | int GetColorFormat() const { return format; } |
| 178 | |
| 179 | int GetStride() const { |
| 180 | // In handle we are storing aligned width after allocation. |
| 181 | // Why GetWidth & GetStride?? Are we supposed to maintain unaligned values?? |
| 182 | return width; |
| 183 | } |
| 184 | |
| 185 | gralloc1_consumer_usage_t GetConsumerUsage() const { return consumer_usage; } |
| 186 | |
| 187 | gralloc1_producer_usage_t GetProducerUsage() const { return producer_usage; } |
| 188 | |
| 189 | BackStoreFd GetBackingstore() const { return fd; } |
| 190 | }; |
| 191 | |
| 192 | #endif // __GR_PRIV_HANDLE_H__ |