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