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