blob: c38ed7916d0ea59f736af51b664d32f052f014fc [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef GRALLOC_PRIV_H_
19#define GRALLOC_PRIV_H_
20
21#include <stdint.h>
22#include <limits.h>
23#include <sys/cdefs.h>
24#include <hardware/gralloc.h>
25#include <pthread.h>
26#include <errno.h>
27#include <unistd.h>
28
29#include <cutils/native_handle.h>
30
Iliyan Malchev202a77d2012-06-11 14:41:12 -070031#include <cutils/log.h>
32
33enum {
34 /* gralloc usage bits indicating the type
35 * of allocation that should be used */
36
37 /* ADSP heap is deprecated, use only if using pmem */
38 GRALLOC_USAGE_PRIVATE_ADSP_HEAP = GRALLOC_USAGE_PRIVATE_0,
39 /* SF heap is used for application buffers, is not secured */
40 GRALLOC_USAGE_PRIVATE_UI_CONTIG_HEAP = GRALLOC_USAGE_PRIVATE_1,
41 /* SMI heap is deprecated, use only if using pmem */
42 GRALLOC_USAGE_PRIVATE_SMI_HEAP = GRALLOC_USAGE_PRIVATE_2,
43 /* SYSTEM heap comes from kernel vmalloc,
44 * can never be uncached, is not secured*/
45 GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP = GRALLOC_USAGE_PRIVATE_3,
46 /* IOMMU heap comes from manually allocated pages,
47 * can be cached/uncached, is not secured */
48 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP = 0x01000000,
49 /* MM heap is a carveout heap for video, can be secured*/
50 GRALLOC_USAGE_PRIVATE_MM_HEAP = 0x02000000,
51 /* WRITEBACK heap is a carveout heap for writeback, can be secured*/
52 GRALLOC_USAGE_PRIVATE_WRITEBACK_HEAP = 0x04000000,
53 /* CAMERA heap is a carveout heap for camera, is not secured*/
54 GRALLOC_USAGE_PRIVATE_CAMERA_HEAP = 0x08000000,
55
56 /* Set this for allocating uncached memory (using O_DSYNC)
57 * cannot be used with noncontiguous heaps */
58 GRALLOC_USAGE_PRIVATE_UNCACHED = 0x00100000,
59
60 /* This flag needs to be set when using a non-contiguous heap from ION.
61 * If not set, the system heap is assumed to be coming from ashmem
62 */
63 GRALLOC_USAGE_PRIVATE_ION = 0x00200000,
64
65 /* This flag can be set to disable genlock synchronization
66 * for the gralloc buffer. If this flag is set the caller
67 * is required to perform explicit synchronization.
68 * WARNING - flag is outside the standard PRIVATE region
69 * and may need to be moved if the gralloc API changes
70 */
71 GRALLOC_USAGE_PRIVATE_UNSYNCHRONIZED = 0X00400000,
72
73 /* Set this flag when you need to avoid mapping the memory in userspace */
74 GRALLOC_USAGE_PRIVATE_DO_NOT_MAP = 0X00800000,
75
76 /* Buffer content should be displayed on an external display only */
77 GRALLOC_USAGE_EXTERNAL_ONLY = 0x00010000,
78
79 /* Only this buffer content should be displayed on external, even if
80 * other EXTERNAL_ONLY buffers are available. Used during suspend.
81 */
82 GRALLOC_USAGE_EXTERNAL_BLOCK = 0x00020000,
Naseer Ahmed29a26812012-06-14 00:56:20 -070083
84 /* Use this flag to request content protected buffers. Please note
85 * that this flag is different from the GRALLOC_USAGE_PROTECTED flag
86 * which can be used for buffers that are not secured for DRM
87 * but still need to be protected from screen captures
88 * 0x00040000 is reserved and these values are subject to change.
89 */
90 GRALLOC_USAGE_PRIVATE_CP_BUFFER = 0x00080000,
Iliyan Malchev202a77d2012-06-11 14:41:12 -070091};
92
93enum {
94 /* Gralloc perform enums
95 */
96 GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER = 0x080000001,
97};
98
99
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700100#define INTERLACE_MASK 0x80
101#define S3D_FORMAT_MASK 0xFF000
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700102#define DEVICE_PMEM "/dev/pmem"
103#define DEVICE_PMEM_ADSP "/dev/pmem_adsp"
104#define DEVICE_PMEM_SMIPOOL "/dev/pmem_smipool"
105/*****************************************************************************/
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700106enum {
107 /* OEM specific HAL formats */
Naseer Ahmed65f16562012-06-15 20:53:42 -0700108 HAL_PIXEL_FORMAT_NV12_ENCODEABLE = 0x102,
109 HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED = 0x7FA30C03,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700110 HAL_PIXEL_FORMAT_YCbCr_420_SP = 0x109,
111 HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO = 0x10A,
112 HAL_PIXEL_FORMAT_YCrCb_422_SP = 0x10B,
113 HAL_PIXEL_FORMAT_R_8 = 0x10D,
114 HAL_PIXEL_FORMAT_RG_88 = 0x10E,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700115 HAL_PIXEL_FORMAT_YCbCr_444_SP = 0x10F,
116 HAL_PIXEL_FORMAT_YCrCb_444_SP = 0x110,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700117 HAL_PIXEL_FORMAT_INTERLACE = 0x180,
118
119};
120
121/* possible formats for 3D content*/
122enum {
123 HAL_NO_3D = 0x0000,
124 HAL_3D_IN_SIDE_BY_SIDE_L_R = 0x10000,
125 HAL_3D_IN_TOP_BOTTOM = 0x20000,
126 HAL_3D_IN_INTERLEAVE = 0x40000,
127 HAL_3D_IN_SIDE_BY_SIDE_R_L = 0x80000,
128 HAL_3D_OUT_SIDE_BY_SIDE = 0x1000,
129 HAL_3D_OUT_TOP_BOTTOM = 0x2000,
130 HAL_3D_OUT_INTERLEAVE = 0x4000,
131 HAL_3D_OUT_MONOSCOPIC = 0x8000
132};
133
134enum {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700135 BUFFER_TYPE_UI = 0,
136 BUFFER_TYPE_VIDEO
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700137};
138
139/*****************************************************************************/
140
141#ifdef __cplusplus
142struct private_handle_t : public native_handle {
143#else
Naseer Ahmed29a26812012-06-14 00:56:20 -0700144 struct private_handle_t {
145 native_handle_t nativeHandle;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700146#endif
Naseer Ahmed29a26812012-06-14 00:56:20 -0700147 enum {
148 PRIV_FLAGS_FRAMEBUFFER = 0x00000001,
149 PRIV_FLAGS_USES_PMEM = 0x00000002,
150 PRIV_FLAGS_USES_PMEM_ADSP = 0x00000004,
151 PRIV_FLAGS_USES_ION = 0x00000008,
152 PRIV_FLAGS_USES_ASHMEM = 0x00000010,
153 PRIV_FLAGS_NEEDS_FLUSH = 0x00000020,
154 PRIV_FLAGS_DO_NOT_FLUSH = 0x00000040,
155 PRIV_FLAGS_SW_LOCK = 0x00000080,
156 PRIV_FLAGS_NONCONTIGUOUS_MEM = 0x00000100,
157 // Set by HWC when storing the handle
158 PRIV_FLAGS_HWC_LOCK = 0x00000200,
159 PRIV_FLAGS_SECURE_BUFFER = 0x00000400,
160 // For explicit synchronization
161 PRIV_FLAGS_UNSYNCHRONIZED = 0x00000800,
162 // Not mapped in userspace
163 PRIV_FLAGS_NOT_MAPPED = 0x00001000,
164 // Display on external only
165 PRIV_FLAGS_EXTERNAL_ONLY = 0x00002000,
166 // Display only this buffer on external
167 PRIV_FLAGS_EXTERNAL_BLOCK = 0x00004000,
168 };
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700169
Naseer Ahmed29a26812012-06-14 00:56:20 -0700170 // file-descriptors
171 int fd;
172 // genlock handle to be dup'd by the binder
173 int genlockHandle;
174 // ints
175 int magic;
176 int flags;
177 int size;
178 int offset;
179 int bufferType;
180 int base;
181 // The gpu address mapped into the mmu.
182 // If using ashmem, set to 0, they don't care
183 int gpuaddr;
184 int pid;
185 int format;
186 int width;
187 int height;
188 // local fd of the genlock device.
189 int genlockPrivFd;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700190
191#ifdef __cplusplus
Naseer Ahmed29a26812012-06-14 00:56:20 -0700192 static const int sNumInts = 12;
193 static const int sNumFds = 2;
194 static const int sMagic = 'gmsm';
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700195
Naseer Ahmed29a26812012-06-14 00:56:20 -0700196 private_handle_t(int fd, int size, int flags, int bufferType,
197 int format,int width, int height) :
198 fd(fd), genlockHandle(-1), magic(sMagic),
199 flags(flags), size(size), offset(0),
200 bufferType(bufferType), base(0), gpuaddr(0),
201 pid(getpid()), format(format),
202 width(width), height(height), genlockPrivFd(-1)
203 {
204 version = sizeof(native_handle);
205 numInts = sNumInts;
206 numFds = sNumFds;
207 }
208 ~private_handle_t() {
209 magic = 0;
210 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700211
Naseer Ahmed29a26812012-06-14 00:56:20 -0700212 bool usesPhysicallyContiguousMemory() {
213 return (flags & PRIV_FLAGS_USES_PMEM) != 0;
214 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700215
Naseer Ahmed29a26812012-06-14 00:56:20 -0700216 static int validate(const native_handle* h) {
217 const private_handle_t* hnd = (const private_handle_t*)h;
218 if (!h || h->version != sizeof(native_handle) ||
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700219 h->numInts != sNumInts || h->numFds != sNumFds ||
220 hnd->magic != sMagic)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700221 {
222 ALOGD("Invalid gralloc handle (at %p): "
223 "ver(%d/%d) ints(%d/%d) fds(%d/%d) magic(%c%c%c%c/%c%c%c%c)",
224 h,
225 h ? h->version : -1, sizeof(native_handle),
226 h ? h->numInts : -1, sNumInts,
227 h ? h->numFds : -1, sNumFds,
228 hnd ? (((hnd->magic >> 24) & 0xFF)?
229 ((hnd->magic >> 24) & 0xFF) : '-') : '?',
230 hnd ? (((hnd->magic >> 16) & 0xFF)?
231 ((hnd->magic >> 16) & 0xFF) : '-') : '?',
232 hnd ? (((hnd->magic >> 8) & 0xFF)?
233 ((hnd->magic >> 8) & 0xFF) : '-') : '?',
234 hnd ? (((hnd->magic >> 0) & 0xFF)?
235 ((hnd->magic >> 0) & 0xFF) : '-') : '?',
236 (sMagic >> 24) & 0xFF,
237 (sMagic >> 16) & 0xFF,
238 (sMagic >> 8) & 0xFF,
239 (sMagic >> 0) & 0xFF);
240 return -EINVAL;
241 }
242 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700243 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700244
Naseer Ahmed29a26812012-06-14 00:56:20 -0700245 static private_handle_t* dynamicCast(const native_handle* in) {
246 if (validate(in) == 0) {
247 return (private_handle_t*) in;
248 }
249 return NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700250 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700251#endif
Naseer Ahmed29a26812012-06-14 00:56:20 -0700252 };
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700253
254#endif /* GRALLOC_PRIV_H_ */