blob: 8a0cbd2ef1be978a8729c4338a8882b2cd202831 [file] [log] [blame]
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Greg Hartmana5020262019-07-26 08:35:28 -070016#include <atomic>
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -080017#include <limits.h>
18#include <unistd.h>
19#include <fcntl.h>
20#include <errno.h>
21#include <pthread.h>
22#include <stdlib.h>
23#include <string.h>
24
25#include <sys/mman.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28#include <sys/ioctl.h>
29
30#include <cutils/ashmem.h>
Colin Crossd1935492018-09-06 14:08:55 -070031#include <log/log.h>
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -080032#include <cutils/atomic.h>
33#include <utils/String8.h>
34
35#include <hardware/hardware.h>
36#include <hardware/gralloc.h>
37
Jorge E. Moreirae2017232017-11-06 15:51:40 -080038#include "common/libs/auto_resources/auto_resources.h"
Cody Schuffelen134ff032019-11-22 00:25:32 -080039#include "common/vsoc/lib/screen_region_view.h"
Jorge E. Moreira9334f3d2018-01-23 14:55:00 -080040#include "gralloc_vsoc_priv.h"
41#include "region_registry.h"
42
Jorge E. Moreira57919e82018-02-13 11:50:34 -080043using vsoc::screen::ScreenRegionView;
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -080044
45/*****************************************************************************/
46
Jorge E. Moreira9334f3d2018-01-23 14:55:00 -080047static inline size_t roundUpToPageSize(size_t x) {
48 return (x + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
49}
50
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -080051static int gralloc_alloc_buffer(
52 alloc_device_t* /*dev*/, int format, int w, int h,
53 buffer_handle_t* pHandle, int* pStrideInPixels) {
54 int err = 0;
55 int fd = -1;
Greg Hartmana5020262019-07-26 08:35:28 -070056 static std::atomic<int> sequence;
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -080057
58 int bytes_per_pixel = formatToBytesPerPixel(format);
59 int bytes_per_line;
60 int stride_in_pixels;
61 int size = 0;
62 // SwiftShader can't handle RGB_888, so fail fast and hard if we try to create
63 // a gralloc buffer in this format.
64 ALOG_ASSERT(format != HAL_PIXEL_FORMAT_RGB_888);
65 if (format == HAL_PIXEL_FORMAT_YV12) {
Jorge E. Moreira7df268b2019-04-05 17:51:30 -070066 bytes_per_line = ScreenRegionView::align(bytes_per_pixel * w);
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -080067 } else {
Jorge E. Moreira57919e82018-02-13 11:50:34 -080068 bytes_per_line = ScreenRegionView::align(bytes_per_pixel * w);
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -080069 }
70 size = roundUpToPageSize(size + formatToBytesPerFrame(format, w, h));
71 size += PAGE_SIZE;
72 fd = ashmem_create_region(
73 android::String8::format(
74 "gralloc-%d.%d", getpid(), sequence++).string(),
75 size);
76 if (fd < 0) {
77 ALOGE("couldn't create ashmem (%s)", strerror(-errno));
78 err = -errno;
79 }
80
81 if (err == 0) {
82 stride_in_pixels = bytes_per_line / bytes_per_pixel;
83 private_handle_t* hnd =
84 new private_handle_t(fd, size, format, w, h, stride_in_pixels, 0);
85 void* base = reference_region(__FUNCTION__, hnd);
86 if (base) {
87 *pHandle = hnd;
88 *pStrideInPixels = stride_in_pixels;
89 } else {
90 err = -EIO;
91 }
92 }
93
94 ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
95
96 return err;
97}
98
99/*****************************************************************************/
100
Jorge E. Moreira2b4e9be2018-02-01 17:19:05 -0800101static int gralloc_alloc(alloc_device_t* dev, int w, int h, int format,
102 int /*usage*/, buffer_handle_t* pHandle,
103 int* pStrideInPixels) {
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800104 if (!pHandle || !pStrideInPixels)
105 return -EINVAL;
106
Jorge E. Moreira2b4e9be2018-02-01 17:19:05 -0800107 int err = gralloc_alloc_buffer(dev, format, w, h, pHandle, pStrideInPixels);
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800108
109 if (err < 0) {
110 return err;
111 }
112 return 0;
113}
114
115static int gralloc_free(alloc_device_t* /*dev*/, buffer_handle_t handle) {
116 if (private_handle_t::validate(handle) < 0) {
117 return -EINVAL;
118 }
119
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800120 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(
121 handle);
Jorge E. Moreira2b4e9be2018-02-01 17:19:05 -0800122 int retval = unreference_region(__FUNCTION__, hnd);
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800123
124 close(hnd->fd);
125 delete hnd;
126 return retval;
127}
128
129/*****************************************************************************/
130
131static int gralloc_close(struct hw_device_t *dev) {
132 priv_alloc_device_t* ctx = reinterpret_cast<priv_alloc_device_t*>(dev);
133 if (ctx) {
134 /* TODO: keep a list of all buffer_handle_t created, and free them
135 * all here.
136 */
137 free(ctx);
138 }
139 return 0;
140}
141
142static int gralloc_device_open(
143 const hw_module_t* module, const char* name, hw_device_t** device) {
144 int status = -EINVAL;
145 if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
146 priv_alloc_device_t *dev;
147 dev = (priv_alloc_device_t*) malloc(sizeof(*dev));
148 LOG_FATAL_IF(!dev, "%s: malloc returned NULL.", __FUNCTION__);
149
150 /* initialize our state here */
151 memset(dev, 0, sizeof(*dev));
152
153 /* initialize the procs */
154 dev->device.common.tag = HARDWARE_DEVICE_TAG;
155 dev->device.common.version = 0;
156 dev->device.common.module = const_cast<hw_module_t*>(module);
157 dev->device.common.close = gralloc_close;
158
159 dev->device.alloc = gralloc_alloc;
160 dev->device.free = gralloc_free;
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800161
162 *device = &dev->device.common;
163 status = 0;
164 } else {
Cody Schuffelen134ff032019-11-22 00:25:32 -0800165 status = fb_device_open(module, name, device);
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800166 }
167 return status;
168}
169
170/*****************************************************************************/
171
172static struct hw_module_methods_t gralloc_module_methods = {
Jorge E. Moreira7737f9a2019-10-01 14:10:49 -0700173 .open = gralloc_device_open
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800174};
175
176struct private_module_t HAL_MODULE_INFO_SYM = {
Jorge E. Moreira7737f9a2019-10-01 14:10:49 -0700177 .base = {
178 .common = {
179 .tag = HARDWARE_MODULE_TAG,
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800180#ifdef GRALLOC_MODULE_API_VERSION_0_2
Jorge E. Moreira7737f9a2019-10-01 14:10:49 -0700181 .version_major = GRALLOC_MODULE_API_VERSION_0_2,
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800182#else
Jorge E. Moreira7737f9a2019-10-01 14:10:49 -0700183 .version_major = 1,
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800184#endif
Jorge E. Moreira7737f9a2019-10-01 14:10:49 -0700185 .version_minor = 0,
186 .id = GRALLOC_HARDWARE_MODULE_ID,
187 .name = "VSOC X86 Graphics Memory Allocator Module",
188 .author = "The Android Open Source Project",
189 .methods = &gralloc_module_methods,
190 .dso = NULL,
191 .reserved = {0},
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800192 },
Jorge E. Moreira7737f9a2019-10-01 14:10:49 -0700193 .registerBuffer = gralloc_register_buffer,
194 .unregisterBuffer = gralloc_unregister_buffer,
195 .lock = gralloc_lock,
196 .unlock = gralloc_unlock,
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800197#ifdef GRALLOC_MODULE_API_VERSION_0_2
Jorge E. Moreira7737f9a2019-10-01 14:10:49 -0700198 .perform = NULL,
199 .lock_ycbcr = gralloc_lock_ycbcr,
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800200#endif
Nick Desaulniers3bbf9c32019-11-13 15:13:43 -0800201 .getTransportSize = gralloc_get_transport_size,
202 .validateBufferSize = gralloc_validate_buffer_size,
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800203 },
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800204};