• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2011-2017, 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 <log/log.h>
25 #include <hardware/gralloc1.h>
26 #include <hardware/gralloc.h>
27 #ifdef __cplusplus
28 #include <cinttypes>
29 #endif
30 
31 #define GRALLOC1_FUNCTION_PERFORM 0x00001000
32 
33 #define DBG_HANDLE false
34 
35 typedef gralloc1_error_t (*GRALLOC1_PFN_PERFORM)(gralloc1_device_t *device, int operation, ...);
36 
37 #define PRIV_HANDLE_CONST(exp) static_cast<const private_handle_t *>(exp)
38 
39 #ifdef __cplusplus
40 struct private_handle_t : public native_handle_t {
41 #else
42 struct private_handle_t {
43         native_handle_t nativeHandle;
44 #endif
45   enum {
46     PRIV_FLAGS_FRAMEBUFFER = 0x00000001,
47     PRIV_FLAGS_USES_ION = 0x00000008,
48     PRIV_FLAGS_NEEDS_FLUSH = 0x00000020,
49     PRIV_FLAGS_INTERNAL_ONLY = 0x00000040,
50     PRIV_FLAGS_NON_CPU_WRITER = 0x00000080,
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,
67     PRIV_FLAGS_DISP_CONSUMER = 0x10000000,
68     PRIV_FLAGS_CLIENT_ALLOCATED = 0x20000000,   // Ion buffer allocated outside of gralloc
69   };
70 
71   // file-descriptors dup'd over IPC
72   int fd;
73   int fd_metadata;
74 
75   // values sent over IPC
76   int magic;
77   int flags;
78   int width;        // holds width of the actual buffer allocated
79   int height;       // holds height of the  actual buffer allocated
80   int unaligned_width;   // holds width client asked to allocate
81   int unaligned_height;  // holds height client asked to allocate
82   int format;
83   int buffer_type;
84   unsigned int size;
85   unsigned int offset;
86   unsigned int offset_metadata;
87   uint64_t base                            __attribute__((aligned(8)));
88   uint64_t base_metadata                   __attribute__((aligned(8)));
89   uint64_t gpuaddr                         __attribute__((aligned(8)));
90   uint64_t id                              __attribute__((aligned(8)));
91   gralloc1_producer_usage_t producer_usage __attribute__((aligned(8)));
92   gralloc1_consumer_usage_t consumer_usage __attribute__((aligned(8)));
93   unsigned int layer_count;
94 #ifdef __cplusplus
95   static const int kNumFds = 2;
96   static const int kMagic = 'gmsm';
97 
NumIntsprivate_handle_t98   static inline int NumInts() {
99     return ((sizeof(private_handle_t) - sizeof(native_handle_t)) / sizeof(int))
100         - kNumFds;
101   }
102 
103   private_handle_t(int fd,
104                    int meta_fd,
105                    int flags,
106                    int width,
107                    int height,
108                    int uw,
109                    int uh,
110                    int format,
111                    int buf_type,
112                    unsigned int size,
113                    gralloc1_producer_usage_t prod_usage = GRALLOC1_PRODUCER_USAGE_NONE,
114                    gralloc1_consumer_usage_t cons_usage = GRALLOC1_CONSUMER_USAGE_NONE)
fdprivate_handle_t115       : fd(fd),
116         fd_metadata(meta_fd),
117         magic(kMagic),
118         flags(flags),
119         width(width),
120         height(height),
121         unaligned_width(uw),
122         unaligned_height(uh),
123         format(format),
124         buffer_type(buf_type),
125         size(size),
126         offset(0),
127         offset_metadata(0),
128         base(0),
129         base_metadata(0),
130         gpuaddr(0),
131         id(0),
132         producer_usage(prod_usage),
133         consumer_usage(cons_usage),
134         layer_count(1) {
135     version = static_cast<int>(sizeof(native_handle));
136     numInts = NumInts();
137     numFds = kNumFds;
138   }
139 
140 // Legacy constructor used by some clients
private_handle_tprivate_handle_t141   private_handle_t(int fd, unsigned int size, int usage, int buf_type, int format, int w, int h)
142   : private_handle_t(fd, -1, PRIV_FLAGS_CLIENT_ALLOCATED, w, h, 0, 0, format, buf_type, size,
143                      static_cast<gralloc1_producer_usage_t>(usage),
144                      static_cast<gralloc1_consumer_usage_t>(usage)) {
145   }
146 
~private_handle_tprivate_handle_t147   ~private_handle_t() {
148     magic = 0;
149     ALOGE_IF(DBG_HANDLE, "Deleting buffer handle %p", this);
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 || hnd->magic != kMagic) {
156       ALOGE(
157           "Invalid gralloc handle (at %p): ver(%d/%zu) ints(%d/%d) fds(%d/%d) "
158           "magic(%c%c%c%c/%c%c%c%c)",
159           h, h ? h->version : -1, sizeof(native_handle), h ? h->numInts : -1, NumInts(),
160           h ? h->numFds : -1, kNumFds,
161           hnd ? (((hnd->magic >> 24) & 0xFF) ? ((hnd->magic >> 24) & 0xFF) : '-') : '?',
162           hnd ? (((hnd->magic >> 16) & 0xFF) ? ((hnd->magic >> 16) & 0xFF) : '-') : '?',
163           hnd ? (((hnd->magic >> 8) & 0xFF) ? ((hnd->magic >> 8) & 0xFF) : '-') : '?',
164           hnd ? (((hnd->magic >> 0) & 0xFF) ? ((hnd->magic >> 0) & 0xFF) : '-') : '?',
165           (kMagic >> 24) & 0xFF, (kMagic >> 16) & 0xFF, (kMagic >> 8) & 0xFF, (kMagic >> 0) & 0xFF);
166       return -EINVAL;
167     }
168 
169     return 0;
170   }
Dumpprivate_handle_t171   static void Dump(const private_handle_t *hnd) {
172     ALOGD("handle id:%" PRIu64 " wxh:%dx%d uwxuh:%dx%d size: %d fd:%d fd_meta:%d flags:0x%x "
173           "prod_usage:0x%" PRIx64" cons_usage:0x%" PRIx64 " format:0x%x layer_count: %d",
174           hnd->id, hnd->width, hnd->height, hnd->unaligned_width, hnd->unaligned_height, hnd->size,
175           hnd->fd, hnd->fd_metadata, hnd->flags, hnd->producer_usage, hnd->consumer_usage,
176           hnd->format, hnd->layer_count);
177   }
178 
GetUnalignedWidthprivate_handle_t179   int GetUnalignedWidth() const { return unaligned_width; }
180 
GetUnalignedHeightprivate_handle_t181   int GetUnalignedHeight() const { return unaligned_height; }
182 
GetColorFormatprivate_handle_t183   int GetColorFormat() const { return format; }
184 
GetLayerCountprivate_handle_t185   unsigned int GetLayerCount() const { return layer_count; }
186 
GetStrideprivate_handle_t187   int GetStride() const {
188     // In handle we currently store aligned width after allocation.
189     return width;
190   }
191 
GetConsumerUsageprivate_handle_t192   gralloc1_consumer_usage_t GetConsumerUsage() const { return consumer_usage; }
193 
GetProducerUsageprivate_handle_t194   gralloc1_producer_usage_t GetProducerUsage() const { return producer_usage; }
195 
GetBackingstoreprivate_handle_t196   uint64_t GetBackingstore() const { return id; }
197 #endif
198 };
199 
200 #endif  // __GR_PRIV_HANDLE_H__
201