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))); |
| 87 | |
| 88 | // added for gralloc1 |
| 89 | int real_width; // holds width client asked to allocate |
| 90 | int real_height; // holds height client asked to allocate// holds width client asked to allocate |
| 91 | gralloc1_producer_usage_t producer_usage __attribute__((aligned(8))); |
| 92 | gralloc1_consumer_usage_t consumer_usage __attribute__((aligned(8))); |
| 93 | |
| 94 | static const int kNumFds = 2; |
| 95 | static const int kMagic = 'gmsm'; |
| 96 | |
| 97 | static inline int NumInts() { |
| 98 | return ((sizeof(private_handle_t) - sizeof(native_handle_t)) / sizeof(int)) - kNumFds; |
| 99 | } |
| 100 | |
| 101 | private_handle_t(int fd, unsigned int size, int flags, int buf_type, int format, int width, |
| 102 | int height, int meta_fd = -1, unsigned int meta_offset = 0, |
| 103 | uint64_t meta_base = 0, int rw = 0, int rh = 0, |
| 104 | gralloc1_producer_usage_t prod_usage = GRALLOC1_PRODUCER_USAGE_NONE, |
| 105 | gralloc1_consumer_usage_t cons_usage = GRALLOC1_CONSUMER_USAGE_NONE) |
| 106 | : fd(fd), |
| 107 | fd_metadata(meta_fd), |
| 108 | magic(kMagic), |
| 109 | flags(flags), |
| 110 | size(size), |
| 111 | offset(0), |
| 112 | buffer_type(buf_type), |
| 113 | base(0), |
| 114 | offset_metadata(meta_offset), |
| 115 | gpuaddr(0), |
| 116 | format(format), |
| 117 | width(width), |
| 118 | height(height), |
| 119 | base_metadata(meta_base), |
| 120 | real_width(rw), |
| 121 | real_height(rh), |
| 122 | producer_usage(prod_usage), |
| 123 | consumer_usage(cons_usage) { |
| 124 | version = static_cast<int>(sizeof(native_handle)); |
| 125 | numInts = NumInts(); |
| 126 | numFds = kNumFds; |
| 127 | } |
| 128 | |
| 129 | ~private_handle_t() { |
| 130 | magic = 0; |
| 131 | ALOGE_IF(DBG_HANDLE, "deleting buffer handle %p", this); |
| 132 | } |
| 133 | |
| 134 | static int validate(const native_handle *h) { |
| 135 | const private_handle_t *hnd = (const private_handle_t *)h; |
| 136 | if (!h || h->version != sizeof(native_handle) || h->numInts != NumInts() || |
| 137 | h->numFds != kNumFds || hnd->magic != kMagic) { |
| 138 | ALOGE( |
| 139 | "Invalid gralloc handle (at %p): ver(%d/%zu) ints(%d/%d) fds(%d/%d) " |
| 140 | "magic(%c%c%c%c/%c%c%c%c)", |
| 141 | h, h ? h->version : -1, sizeof(native_handle), h ? h->numInts : -1, NumInts(), |
| 142 | h ? h->numFds : -1, kNumFds, |
| 143 | hnd ? (((hnd->magic >> 24) & 0xFF) ? ((hnd->magic >> 24) & 0xFF) : '-') : '?', |
| 144 | hnd ? (((hnd->magic >> 16) & 0xFF) ? ((hnd->magic >> 16) & 0xFF) : '-') : '?', |
| 145 | hnd ? (((hnd->magic >> 8) & 0xFF) ? ((hnd->magic >> 8) & 0xFF) : '-') : '?', |
| 146 | hnd ? (((hnd->magic >> 0) & 0xFF) ? ((hnd->magic >> 0) & 0xFF) : '-') : '?', |
| 147 | (kMagic >> 24) & 0xFF, (kMagic >> 16) & 0xFF, (kMagic >> 8) & 0xFF, (kMagic >> 0) & 0xFF); |
| 148 | return -EINVAL; |
| 149 | } |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | int GetRealWidth() const { return real_width; } |
| 155 | |
| 156 | int GetRealHeight() const { return real_height; } |
| 157 | |
| 158 | int GetColorFormat() const { return format; } |
| 159 | |
| 160 | int GetStride() const { |
| 161 | // In handle we are storing aligned width after allocation. |
| 162 | // Why GetWidth & GetStride?? Are we supposed to maintain unaligned values?? |
| 163 | return width; |
| 164 | } |
| 165 | |
| 166 | gralloc1_consumer_usage_t GetConsumerUsage() const { return consumer_usage; } |
| 167 | |
| 168 | gralloc1_producer_usage_t GetProducerUsage() const { return producer_usage; } |
| 169 | |
| 170 | BackStoreFd GetBackingstore() const { return fd; } |
| 171 | }; |
| 172 | |
| 173 | #endif // __GR_PRIV_HANDLE_H__ |