Jorge E. Moreira | 9dc14e7 | 2017-11-07 16:07:48 -0800 | [diff] [blame] | 1 | /* |
| 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 Hartman | a502026 | 2019-07-26 08:35:28 -0700 | [diff] [blame] | 16 | #include <atomic> |
Jorge E. Moreira | 9dc14e7 | 2017-11-07 16:07:48 -0800 | [diff] [blame] | 17 | #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 Cross | d193549 | 2018-09-06 14:08:55 -0700 | [diff] [blame] | 31 | #include <log/log.h> |
Jorge E. Moreira | 9dc14e7 | 2017-11-07 16:07:48 -0800 | [diff] [blame] | 32 | #include <cutils/atomic.h> |
| 33 | #include <utils/String8.h> |
| 34 | |
| 35 | #include <hardware/hardware.h> |
| 36 | #include <hardware/gralloc.h> |
| 37 | |
Jorge E. Moreira | e201723 | 2017-11-06 15:51:40 -0800 | [diff] [blame] | 38 | #include "common/libs/auto_resources/auto_resources.h" |
Cody Schuffelen | 134ff03 | 2019-11-22 00:25:32 -0800 | [diff] [blame^] | 39 | #include "common/vsoc/lib/screen_region_view.h" |
Jorge E. Moreira | 9334f3d | 2018-01-23 14:55:00 -0800 | [diff] [blame] | 40 | #include "gralloc_vsoc_priv.h" |
| 41 | #include "region_registry.h" |
| 42 | |
Jorge E. Moreira | 57919e8 | 2018-02-13 11:50:34 -0800 | [diff] [blame] | 43 | using vsoc::screen::ScreenRegionView; |
Jorge E. Moreira | 9dc14e7 | 2017-11-07 16:07:48 -0800 | [diff] [blame] | 44 | |
| 45 | /*****************************************************************************/ |
| 46 | |
Jorge E. Moreira | 9334f3d | 2018-01-23 14:55:00 -0800 | [diff] [blame] | 47 | static inline size_t roundUpToPageSize(size_t x) { |
| 48 | return (x + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1); |
| 49 | } |
| 50 | |
Jorge E. Moreira | 9dc14e7 | 2017-11-07 16:07:48 -0800 | [diff] [blame] | 51 | static 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 Hartman | a502026 | 2019-07-26 08:35:28 -0700 | [diff] [blame] | 56 | static std::atomic<int> sequence; |
Jorge E. Moreira | 9dc14e7 | 2017-11-07 16:07:48 -0800 | [diff] [blame] | 57 | |
| 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. Moreira | 7df268b | 2019-04-05 17:51:30 -0700 | [diff] [blame] | 66 | bytes_per_line = ScreenRegionView::align(bytes_per_pixel * w); |
Jorge E. Moreira | 9dc14e7 | 2017-11-07 16:07:48 -0800 | [diff] [blame] | 67 | } else { |
Jorge E. Moreira | 57919e8 | 2018-02-13 11:50:34 -0800 | [diff] [blame] | 68 | bytes_per_line = ScreenRegionView::align(bytes_per_pixel * w); |
Jorge E. Moreira | 9dc14e7 | 2017-11-07 16:07:48 -0800 | [diff] [blame] | 69 | } |
| 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. Moreira | 2b4e9be | 2018-02-01 17:19:05 -0800 | [diff] [blame] | 101 | static 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. Moreira | 9dc14e7 | 2017-11-07 16:07:48 -0800 | [diff] [blame] | 104 | if (!pHandle || !pStrideInPixels) |
| 105 | return -EINVAL; |
| 106 | |
Jorge E. Moreira | 2b4e9be | 2018-02-01 17:19:05 -0800 | [diff] [blame] | 107 | int err = gralloc_alloc_buffer(dev, format, w, h, pHandle, pStrideInPixels); |
Jorge E. Moreira | 9dc14e7 | 2017-11-07 16:07:48 -0800 | [diff] [blame] | 108 | |
| 109 | if (err < 0) { |
| 110 | return err; |
| 111 | } |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static 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. Moreira | 9dc14e7 | 2017-11-07 16:07:48 -0800 | [diff] [blame] | 120 | private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>( |
| 121 | handle); |
Jorge E. Moreira | 2b4e9be | 2018-02-01 17:19:05 -0800 | [diff] [blame] | 122 | int retval = unreference_region(__FUNCTION__, hnd); |
Jorge E. Moreira | 9dc14e7 | 2017-11-07 16:07:48 -0800 | [diff] [blame] | 123 | |
| 124 | close(hnd->fd); |
| 125 | delete hnd; |
| 126 | return retval; |
| 127 | } |
| 128 | |
| 129 | /*****************************************************************************/ |
| 130 | |
| 131 | static 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 | |
| 142 | static 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. Moreira | 9dc14e7 | 2017-11-07 16:07:48 -0800 | [diff] [blame] | 161 | |
| 162 | *device = &dev->device.common; |
| 163 | status = 0; |
| 164 | } else { |
Cody Schuffelen | 134ff03 | 2019-11-22 00:25:32 -0800 | [diff] [blame^] | 165 | status = fb_device_open(module, name, device); |
Jorge E. Moreira | 9dc14e7 | 2017-11-07 16:07:48 -0800 | [diff] [blame] | 166 | } |
| 167 | return status; |
| 168 | } |
| 169 | |
| 170 | /*****************************************************************************/ |
| 171 | |
| 172 | static struct hw_module_methods_t gralloc_module_methods = { |
Jorge E. Moreira | 7737f9a | 2019-10-01 14:10:49 -0700 | [diff] [blame] | 173 | .open = gralloc_device_open |
Jorge E. Moreira | 9dc14e7 | 2017-11-07 16:07:48 -0800 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | struct private_module_t HAL_MODULE_INFO_SYM = { |
Jorge E. Moreira | 7737f9a | 2019-10-01 14:10:49 -0700 | [diff] [blame] | 177 | .base = { |
| 178 | .common = { |
| 179 | .tag = HARDWARE_MODULE_TAG, |
Jorge E. Moreira | 9dc14e7 | 2017-11-07 16:07:48 -0800 | [diff] [blame] | 180 | #ifdef GRALLOC_MODULE_API_VERSION_0_2 |
Jorge E. Moreira | 7737f9a | 2019-10-01 14:10:49 -0700 | [diff] [blame] | 181 | .version_major = GRALLOC_MODULE_API_VERSION_0_2, |
Jorge E. Moreira | 9dc14e7 | 2017-11-07 16:07:48 -0800 | [diff] [blame] | 182 | #else |
Jorge E. Moreira | 7737f9a | 2019-10-01 14:10:49 -0700 | [diff] [blame] | 183 | .version_major = 1, |
Jorge E. Moreira | 9dc14e7 | 2017-11-07 16:07:48 -0800 | [diff] [blame] | 184 | #endif |
Jorge E. Moreira | 7737f9a | 2019-10-01 14:10:49 -0700 | [diff] [blame] | 185 | .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. Moreira | 9dc14e7 | 2017-11-07 16:07:48 -0800 | [diff] [blame] | 192 | }, |
Jorge E. Moreira | 7737f9a | 2019-10-01 14:10:49 -0700 | [diff] [blame] | 193 | .registerBuffer = gralloc_register_buffer, |
| 194 | .unregisterBuffer = gralloc_unregister_buffer, |
| 195 | .lock = gralloc_lock, |
| 196 | .unlock = gralloc_unlock, |
Jorge E. Moreira | 9dc14e7 | 2017-11-07 16:07:48 -0800 | [diff] [blame] | 197 | #ifdef GRALLOC_MODULE_API_VERSION_0_2 |
Jorge E. Moreira | 7737f9a | 2019-10-01 14:10:49 -0700 | [diff] [blame] | 198 | .perform = NULL, |
| 199 | .lock_ycbcr = gralloc_lock_ycbcr, |
Jorge E. Moreira | 9dc14e7 | 2017-11-07 16:07:48 -0800 | [diff] [blame] | 200 | #endif |
Nick Desaulniers | 3bbf9c3 | 2019-11-13 15:13:43 -0800 | [diff] [blame] | 201 | .getTransportSize = gralloc_get_transport_size, |
| 202 | .validateBufferSize = gralloc_validate_buffer_size, |
Jorge E. Moreira | 9dc14e7 | 2017-11-07 16:07:48 -0800 | [diff] [blame] | 203 | }, |
Jorge E. Moreira | 9dc14e7 | 2017-11-07 16:07:48 -0800 | [diff] [blame] | 204 | }; |