blob: 39e3460b1d1c406c37a711fe04757e84da34b77b [file] [log] [blame]
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05301/*
Ramakant Singh20b6c8c2019-01-02 11:53:59 +05302 * Copyright (c) 2011-2019, The Linux Foundation. All rights reserved.
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05303 * 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
Naseer Ahmed04a804a2018-03-06 20:41:14 -050023#include <log/log.h>
Naseer Ahmede69031e2016-11-22 20:05:16 -050024#include <hardware/gralloc.h>
Naseer Ahmeda422f352017-12-01 15:33:56 -050025#include <hardware/gralloc1.h>
Naseer Ahmed699b4572017-03-09 12:28:45 -050026#include <cinttypes>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053027
28#define GRALLOC1_FUNCTION_PERFORM 0x00001000
29
30#define DBG_HANDLE false
31
32typedef gralloc1_error_t (*GRALLOC1_PFN_PERFORM)(gralloc1_device_t *device, int operation, ...);
33
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053034#define PRIV_HANDLE_CONST(exp) static_cast<const private_handle_t *>(exp)
35
Mohan Pallaka8181cef2018-08-21 14:41:12 -070036#pragma pack(push, 4)
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053037struct private_handle_t : public native_handle_t {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053038 enum {
39 PRIV_FLAGS_FRAMEBUFFER = 0x00000001,
40 PRIV_FLAGS_USES_ION = 0x00000008,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053041 PRIV_FLAGS_NEEDS_FLUSH = 0x00000020,
42 PRIV_FLAGS_INTERNAL_ONLY = 0x00000040,
43 PRIV_FLAGS_NON_CPU_WRITER = 0x00000080,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053044 PRIV_FLAGS_CACHED = 0x00000200,
45 PRIV_FLAGS_SECURE_BUFFER = 0x00000400,
46 PRIV_FLAGS_EXTERNAL_ONLY = 0x00002000,
47 PRIV_FLAGS_PROTECTED_BUFFER = 0x00004000,
48 PRIV_FLAGS_VIDEO_ENCODER = 0x00010000,
49 PRIV_FLAGS_CAMERA_WRITE = 0x00020000,
50 PRIV_FLAGS_CAMERA_READ = 0x00040000,
51 PRIV_FLAGS_HW_COMPOSER = 0x00080000,
52 PRIV_FLAGS_HW_TEXTURE = 0x00100000,
53 PRIV_FLAGS_ITU_R_601 = 0x00200000, // Unused from display
54 PRIV_FLAGS_ITU_R_601_FR = 0x00400000, // Unused from display
55 PRIV_FLAGS_ITU_R_709 = 0x00800000, // Unused from display
56 PRIV_FLAGS_SECURE_DISPLAY = 0x01000000,
57 PRIV_FLAGS_TILE_RENDERED = 0x02000000,
58 PRIV_FLAGS_CPU_RENDERED = 0x04000000,
59 PRIV_FLAGS_UBWC_ALIGNED = 0x08000000,
Naseer Ahmede69031e2016-11-22 20:05:16 -050060 PRIV_FLAGS_DISP_CONSUMER = 0x10000000,
Naseer Ahmeda422f352017-12-01 15:33:56 -050061 PRIV_FLAGS_CLIENT_ALLOCATED = 0x20000000, // Ion buffer allocated outside of gralloc
Mohan Pallaka5d814432018-08-01 10:40:13 -070062 PRIV_FLAGS_UBWC_ALIGNED_PI = 0x40000000, // PI format
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053063 };
64
Naseer Ahmede69031e2016-11-22 20:05:16 -050065 // file-descriptors dup'd over IPC
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053066 int fd;
67 int fd_metadata;
68
Naseer Ahmede69031e2016-11-22 20:05:16 -050069 // values sent over IPC
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053070 int magic;
71 int flags;
Naseer Ahmeda422f352017-12-01 15:33:56 -050072 int width; // holds width of the actual buffer allocated
73 int height; // holds height of the actual buffer allocated
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -070074 int unaligned_width; // holds width client asked to allocate
75 int unaligned_height; // holds height client asked to allocate
Naseer Ahmede69031e2016-11-22 20:05:16 -050076 int format;
77 int buffer_type;
Naseer Ahmeda422f352017-12-01 15:33:56 -050078 unsigned int layer_count;
Mohan Pallaka8181cef2018-08-21 14:41:12 -070079 uint64_t id;
80 uint64_t usage;
Naseer Ahmeda422f352017-12-01 15:33:56 -050081
Naseer Ahmede69031e2016-11-22 20:05:16 -050082 unsigned int size;
83 unsigned int offset;
84 unsigned int offset_metadata;
Mohan Pallaka8181cef2018-08-21 14:41:12 -070085 uint64_t base;
86 uint64_t base_metadata;
87 uint64_t gpuaddr;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053088
89 static const int kNumFds = 2;
90 static const int kMagic = 'gmsm';
91
92 static inline int NumInts() {
Naseer Ahmeda422f352017-12-01 15:33:56 -050093 return ((sizeof(private_handle_t) - sizeof(native_handle_t)) / sizeof(int)) - kNumFds;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053094 }
95
Naseer Ahmeda422f352017-12-01 15:33:56 -050096 private_handle_t(int fd, int meta_fd, int flags, int width, int height, int uw, int uh,
97 int format, int buf_type, unsigned int size, uint64_t usage = 0)
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053098 : fd(fd),
Naseer Ahmede69031e2016-11-22 20:05:16 -050099 fd_metadata(meta_fd),
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530100 magic(kMagic),
101 flags(flags),
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530102 width(width),
103 height(height),
Naseer Ahmede69031e2016-11-22 20:05:16 -0500104 unaligned_width(uw),
105 unaligned_height(uh),
106 format(format),
107 buffer_type(buf_type),
Naseer Ahmeda422f352017-12-01 15:33:56 -0500108 layer_count(1),
109 id(0),
110 usage(usage),
Naseer Ahmede69031e2016-11-22 20:05:16 -0500111 size(size),
112 offset(0),
113 offset_metadata(0),
Naseer Ahmede69031e2016-11-22 20:05:16 -0500114 base(0),
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700115 base_metadata(0),
Naseer Ahmeda422f352017-12-01 15:33:56 -0500116 gpuaddr(0) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530117 version = static_cast<int>(sizeof(native_handle));
118 numInts = NumInts();
119 numFds = kNumFds;
120 }
121
Naseer Ahmeda422f352017-12-01 15:33:56 -0500122 // Legacy constructor used by some clients
Naseer Ahmede69031e2016-11-22 20:05:16 -0500123 private_handle_t(int fd, unsigned int size, int usage, int buf_type, int format, int w, int h)
Naseer Ahmeda422f352017-12-01 15:33:56 -0500124 : private_handle_t(fd, -1, PRIV_FLAGS_CLIENT_ALLOCATED, w, h, 0, 0, format, buf_type, size,
125 static_cast<uint64_t>(usage)) {}
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700126
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530127 ~private_handle_t() {
128 magic = 0;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500129 ALOGE_IF(DBG_HANDLE, "Deleting buffer handle %p", this);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530130 }
131
132 static int validate(const native_handle *h) {
Naseer Ahmeda422f352017-12-01 15:33:56 -0500133 auto *hnd = static_cast<const private_handle_t *>(h);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530134 if (!h || h->version != sizeof(native_handle) || h->numInts != NumInts() ||
Ramakant Singh20b6c8c2019-01-02 11:53:59 +0530135 h->numFds != kNumFds) {
136 ALOGE("Invalid gralloc handle (at %p): ver(%d/%zu) ints(%d/%d) fds(%d/%d)",
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530137 h, h ? h->version : -1, sizeof(native_handle), h ? h->numInts : -1, NumInts(),
Ramakant Singh20b6c8c2019-01-02 11:53:59 +0530138 h ? h->numFds : -1, kNumFds);
139 return -EINVAL;
140 }
141 if (hnd->magic != kMagic) {
142 ALOGE("handle = %p invalid magic(%c%c%c%c/%c%c%c%c)",hnd,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530143 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
Naseer Ahmed699b4572017-03-09 12:28:45 -0500154 static void Dump(const private_handle_t *hnd) {
Naseer Ahmeda422f352017-12-01 15:33:56 -0500155 ALOGD("handle id:%" PRIu64
156 " wxh:%dx%d uwxuh:%dx%d size: %d fd:%d fd_meta:%d flags:0x%x "
157 "usage:0x%" PRIx64 " format:0x%x layer_count: %d",
Naseer Ahmed699b4572017-03-09 12:28:45 -0500158 hnd->id, hnd->width, hnd->height, hnd->unaligned_width, hnd->unaligned_height, hnd->size,
Naseer Ahmeda422f352017-12-01 15:33:56 -0500159 hnd->fd, hnd->fd_metadata, hnd->flags, hnd->usage, hnd->format, hnd->layer_count);
Naseer Ahmed699b4572017-03-09 12:28:45 -0500160 }
161
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700162 int GetUnalignedWidth() const { return unaligned_width; }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530163
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700164 int GetUnalignedHeight() const { return unaligned_height; }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530165
166 int GetColorFormat() const { return format; }
167
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400168 unsigned int GetLayerCount() const { return layer_count; }
169
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530170 int GetStride() const {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500171 // In handle we currently store aligned width after allocation.
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530172 return width;
173 }
174
Naseer Ahmeda422f352017-12-01 15:33:56 -0500175 uint64_t GetUsage() const { return usage; }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530176
Naseer Ahmed8f9c7c32017-02-21 16:45:14 -0500177 uint64_t GetBackingstore() const { return id; }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530178};
Mohan Pallaka8181cef2018-08-21 14:41:12 -0700179#pragma pack(pop)
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530180
181#endif // __GR_PRIV_HANDLE_H__