blob: ee38b4dcb4d05a3280df7de3ec23ab3ae2234276 [file] [log] [blame]
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05301/*
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
30typedef gralloc1_error_t (*GRALLOC1_PFN_PERFORM)(gralloc1_device_t *device, int operation, ...);
31
32typedef int BackStoreFd;
33
34#define PRIV_HANDLE_CONST(exp) static_cast<const private_handle_t *>(exp)
35
36struct 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
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -070089 int unaligned_width; // holds width client asked to allocate
90 int unaligned_height; // holds height client asked to allocate
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053091 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,
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700102 int height)
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530103 : fd(fd),
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700104 fd_metadata(-1),
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530105 magic(kMagic),
106 flags(flags),
107 size(size),
108 offset(0),
109 buffer_type(buf_type),
110 base(0),
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700111 offset_metadata(0),
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530112 gpuaddr(0),
113 format(format),
114 width(width),
115 height(height),
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700116 base_metadata(0),
117 unaligned_width(width),
118 unaligned_height(height),
119 producer_usage(GRALLOC1_PRODUCER_USAGE_NONE),
120 consumer_usage(GRALLOC1_CONSUMER_USAGE_NONE) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530121 version = static_cast<int>(sizeof(native_handle));
122 numInts = NumInts();
123 numFds = kNumFds;
124 }
125
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700126 private_handle_t(int fd, unsigned int size, int flags, int buf_type, int format, int width,
127 int height, int meta_fd, unsigned int meta_offset, uint64_t meta_base)
128 : private_handle_t(fd, size, flags, buf_type, format, width, height) {
129 fd_metadata = meta_fd;
130 offset_metadata = meta_offset;
131 base_metadata = meta_base;
132 }
133
134 private_handle_t(int fd, unsigned int size, int flags, int buf_type, int format, int width,
135 int height, int meta_fd, unsigned int meta_offset, uint64_t meta_base,
136 int unaligned_w , int unaligned_h,
137 gralloc1_producer_usage_t prod_usage, gralloc1_consumer_usage_t cons_usage)
138 : private_handle_t(fd, size, flags, buf_type, format, width, height, meta_fd, meta_offset
139 meta_base) {
140 unaligned_width = unaligned_w;
141 unaligned_height = unaligned_h;
142 producer_usage = prod_usage;
143 consumer_usage = cons_usage;
144 }
145
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530146 ~private_handle_t() {
147 magic = 0;
148 ALOGE_IF(DBG_HANDLE, "deleting buffer handle %p", this);
149 }
150
151 static int validate(const native_handle *h) {
152 const private_handle_t *hnd = (const private_handle_t *)h;
153 if (!h || h->version != sizeof(native_handle) || h->numInts != NumInts() ||
154 h->numFds != kNumFds || hnd->magic != kMagic) {
155 ALOGE(
156 "Invalid gralloc handle (at %p): ver(%d/%zu) ints(%d/%d) fds(%d/%d) "
157 "magic(%c%c%c%c/%c%c%c%c)",
158 h, h ? h->version : -1, sizeof(native_handle), h ? h->numInts : -1, NumInts(),
159 h ? h->numFds : -1, kNumFds,
160 hnd ? (((hnd->magic >> 24) & 0xFF) ? ((hnd->magic >> 24) & 0xFF) : '-') : '?',
161 hnd ? (((hnd->magic >> 16) & 0xFF) ? ((hnd->magic >> 16) & 0xFF) : '-') : '?',
162 hnd ? (((hnd->magic >> 8) & 0xFF) ? ((hnd->magic >> 8) & 0xFF) : '-') : '?',
163 hnd ? (((hnd->magic >> 0) & 0xFF) ? ((hnd->magic >> 0) & 0xFF) : '-') : '?',
164 (kMagic >> 24) & 0xFF, (kMagic >> 16) & 0xFF, (kMagic >> 8) & 0xFF, (kMagic >> 0) & 0xFF);
165 return -EINVAL;
166 }
167
168 return 0;
169 }
170
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700171 int GetUnalignedWidth() const { return unaligned_width; }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530172
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700173 int GetUnalignedHeight() const { return unaligned_height; }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530174
175 int GetColorFormat() const { return format; }
176
177 int GetStride() const {
178 // In handle we are storing aligned width after allocation.
179 // Why GetWidth & GetStride?? Are we supposed to maintain unaligned values??
180 return width;
181 }
182
183 gralloc1_consumer_usage_t GetConsumerUsage() const { return consumer_usage; }
184
185 gralloc1_producer_usage_t GetProducerUsage() const { return producer_usage; }
186
187 BackStoreFd GetBackingstore() const { return fd; }
188};
189
190#endif // __GR_PRIV_HANDLE_H__