blob: 28e71eb9384a9ffdb23926f54f2c56a73559fe96 [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. Moreira9334f3d2018-01-23 14:55:00 -080038#include "gralloc_vsoc_priv.h"
39#include "region_registry.h"
40
Jorge E. Moreira57919e82018-02-13 11:50:34 -080041using vsoc::screen::ScreenRegionView;
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -080042
43/*****************************************************************************/
44
Jorge E. Moreira9334f3d2018-01-23 14:55:00 -080045static inline size_t roundUpToPageSize(size_t x) {
46 return (x + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
47}
48
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -080049static int gralloc_alloc_buffer(
50 alloc_device_t* /*dev*/, int format, int w, int h,
51 buffer_handle_t* pHandle, int* pStrideInPixels) {
52 int err = 0;
53 int fd = -1;
Greg Hartmana5020262019-07-26 08:35:28 -070054 static std::atomic<int> sequence;
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -080055
56 int bytes_per_pixel = formatToBytesPerPixel(format);
57 int bytes_per_line;
58 int stride_in_pixels;
59 int size = 0;
60 // SwiftShader can't handle RGB_888, so fail fast and hard if we try to create
61 // a gralloc buffer in this format.
62 ALOG_ASSERT(format != HAL_PIXEL_FORMAT_RGB_888);
63 if (format == HAL_PIXEL_FORMAT_YV12) {
Jorge E. Moreira7df268b2019-04-05 17:51:30 -070064 bytes_per_line = ScreenRegionView::align(bytes_per_pixel * w);
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -080065 } else {
Jorge E. Moreira57919e82018-02-13 11:50:34 -080066 bytes_per_line = ScreenRegionView::align(bytes_per_pixel * w);
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -080067 }
68 size = roundUpToPageSize(size + formatToBytesPerFrame(format, w, h));
69 size += PAGE_SIZE;
70 fd = ashmem_create_region(
71 android::String8::format(
72 "gralloc-%d.%d", getpid(), sequence++).string(),
73 size);
74 if (fd < 0) {
75 ALOGE("couldn't create ashmem (%s)", strerror(-errno));
76 err = -errno;
77 }
78
79 if (err == 0) {
80 stride_in_pixels = bytes_per_line / bytes_per_pixel;
81 private_handle_t* hnd =
82 new private_handle_t(fd, size, format, w, h, stride_in_pixels, 0);
83 void* base = reference_region(__FUNCTION__, hnd);
84 if (base) {
85 *pHandle = hnd;
86 *pStrideInPixels = stride_in_pixels;
87 } else {
88 err = -EIO;
89 }
90 }
91
92 ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
93
94 return err;
95}
96
97/*****************************************************************************/
98
Jorge E. Moreira2b4e9be2018-02-01 17:19:05 -080099static int gralloc_alloc(alloc_device_t* dev, int w, int h, int format,
100 int /*usage*/, buffer_handle_t* pHandle,
101 int* pStrideInPixels) {
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800102 if (!pHandle || !pStrideInPixels)
103 return -EINVAL;
104
Jorge E. Moreira2b4e9be2018-02-01 17:19:05 -0800105 int err = gralloc_alloc_buffer(dev, format, w, h, pHandle, pStrideInPixels);
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800106
107 if (err < 0) {
108 return err;
109 }
110 return 0;
111}
112
113static int gralloc_free(alloc_device_t* /*dev*/, buffer_handle_t handle) {
114 if (private_handle_t::validate(handle) < 0) {
115 return -EINVAL;
116 }
117
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800118 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(
119 handle);
Jorge E. Moreira2b4e9be2018-02-01 17:19:05 -0800120 int retval = unreference_region(__FUNCTION__, hnd);
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800121
122 close(hnd->fd);
123 delete hnd;
124 return retval;
125}
126
127/*****************************************************************************/
128
129static int gralloc_close(struct hw_device_t *dev) {
130 priv_alloc_device_t* ctx = reinterpret_cast<priv_alloc_device_t*>(dev);
131 if (ctx) {
132 /* TODO: keep a list of all buffer_handle_t created, and free them
133 * all here.
134 */
135 free(ctx);
136 }
137 return 0;
138}
139
140static int gralloc_device_open(
141 const hw_module_t* module, const char* name, hw_device_t** device) {
142 int status = -EINVAL;
143 if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
144 priv_alloc_device_t *dev;
145 dev = (priv_alloc_device_t*) malloc(sizeof(*dev));
146 LOG_FATAL_IF(!dev, "%s: malloc returned NULL.", __FUNCTION__);
147
148 /* initialize our state here */
149 memset(dev, 0, sizeof(*dev));
150
151 /* initialize the procs */
152 dev->device.common.tag = HARDWARE_DEVICE_TAG;
153 dev->device.common.version = 0;
154 dev->device.common.module = const_cast<hw_module_t*>(module);
155 dev->device.common.close = gralloc_close;
156
157 dev->device.alloc = gralloc_alloc;
158 dev->device.free = gralloc_free;
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800159
160 *device = &dev->device.common;
161 status = 0;
162 } else {
Cody Schuffelenec675622019-11-22 17:39:14 +0000163 ALOGE("Need to create framebuffer, but it is unsupported");
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800164 }
165 return status;
166}
167
168/*****************************************************************************/
169
170static struct hw_module_methods_t gralloc_module_methods = {
Jorge E. Moreira7737f9a2019-10-01 14:10:49 -0700171 .open = gralloc_device_open
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800172};
173
174struct private_module_t HAL_MODULE_INFO_SYM = {
Jorge E. Moreira7737f9a2019-10-01 14:10:49 -0700175 .base = {
176 .common = {
177 .tag = HARDWARE_MODULE_TAG,
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800178#ifdef GRALLOC_MODULE_API_VERSION_0_2
Jorge E. Moreira7737f9a2019-10-01 14:10:49 -0700179 .version_major = GRALLOC_MODULE_API_VERSION_0_2,
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800180#else
Jorge E. Moreira7737f9a2019-10-01 14:10:49 -0700181 .version_major = 1,
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800182#endif
Jorge E. Moreira7737f9a2019-10-01 14:10:49 -0700183 .version_minor = 0,
184 .id = GRALLOC_HARDWARE_MODULE_ID,
185 .name = "VSOC X86 Graphics Memory Allocator Module",
186 .author = "The Android Open Source Project",
187 .methods = &gralloc_module_methods,
188 .dso = NULL,
189 .reserved = {0},
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800190 },
Jorge E. Moreira7737f9a2019-10-01 14:10:49 -0700191 .registerBuffer = gralloc_register_buffer,
192 .unregisterBuffer = gralloc_unregister_buffer,
193 .lock = gralloc_lock,
194 .unlock = gralloc_unlock,
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800195#ifdef GRALLOC_MODULE_API_VERSION_0_2
Jorge E. Moreira7737f9a2019-10-01 14:10:49 -0700196 .perform = NULL,
197 .lock_ycbcr = gralloc_lock_ycbcr,
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800198#endif
Nick Desaulniers3bbf9c32019-11-13 15:13:43 -0800199 .getTransportSize = gralloc_get_transport_size,
200 .validateBufferSize = gralloc_validate_buffer_size,
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800201 },
Jorge E. Moreira9dc14e72017-11-07 16:07:48 -0800202};