keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | */ |
| 16 | #include <string.h> |
| 17 | #include <pthread.h> |
Elliott Hughes | 4d3424d | 2014-02-24 16:23:00 -0800 | [diff] [blame] | 18 | #include <limits.h> |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 19 | #include <cutils/ashmem.h> |
| 20 | #include <unistd.h> |
| 21 | #include <errno.h> |
| 22 | #include <dlfcn.h> |
| 23 | #include <sys/mman.h> |
| 24 | #include "gralloc_cb.h" |
| 25 | #include "HostConnection.h" |
| 26 | #include "glUtils.h" |
| 27 | #include <cutils/log.h> |
| 28 | #include <cutils/properties.h> |
| 29 | |
| 30 | /* Set to 1 or 2 to enable debug traces */ |
| 31 | #define DEBUG 0 |
| 32 | |
| 33 | #if DEBUG >= 1 |
| 34 | # define D(...) ALOGD(__VA_ARGS__) |
| 35 | #else |
| 36 | # define D(...) ((void)0) |
| 37 | #endif |
| 38 | |
| 39 | #if DEBUG >= 2 |
| 40 | # define DD(...) ALOGD(__VA_ARGS__) |
| 41 | #else |
| 42 | # define DD(...) ((void)0) |
| 43 | #endif |
| 44 | |
| 45 | #define DBG_FUNC DBG("%s\n", __FUNCTION__) |
Yahan Zhou | c89acb2 | 2016-07-11 11:51:43 -0700 | [diff] [blame^] | 46 | |
| 47 | #include <hardware/qemu_pipe.h> |
| 48 | |
| 49 | // Associate PID with color buffers |
| 50 | // See the comments in gralloc_proc_init() for more details |
| 51 | #define PID_CMD(func, ...) \ |
| 52 | (sGrallocProcPipe ? rcEnc->func##Pid(rcEnc, __VA_ARGS__, sGrallocProcID) \ |
| 53 | : rcEnc->func(rcEnc, __VA_ARGS__)) |
| 54 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 55 | // |
| 56 | // our private gralloc module structure |
| 57 | // |
| 58 | struct private_module_t { |
| 59 | gralloc_module_t base; |
| 60 | }; |
| 61 | |
| 62 | /* If not NULL, this is a pointer to the fallback module. |
| 63 | * This really is gralloc.default, which we'll use if we detect |
| 64 | * that the emulator we're running in does not support GPU emulation. |
| 65 | */ |
| 66 | static gralloc_module_t* sFallback; |
| 67 | static pthread_once_t sFallbackOnce = PTHREAD_ONCE_INIT; |
| 68 | |
| 69 | static void fallback_init(void); // forward |
| 70 | |
| 71 | |
Yahan Zhou | c89acb2 | 2016-07-11 11:51:43 -0700 | [diff] [blame^] | 72 | static int sGrallocProcPipe = 0; |
| 73 | static pthread_once_t sGrallocProcPipeOnce = PTHREAD_ONCE_INIT; |
| 74 | // sGrallocProcID is a unique ID pre process assigned by the host. |
| 75 | // It is different from getpid(). |
| 76 | static uint64_t sGrallocProcID = 0; |
| 77 | static void gralloc_proc_init(); |
| 78 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 79 | typedef struct _alloc_list_node { |
| 80 | buffer_handle_t handle; |
| 81 | _alloc_list_node *next; |
| 82 | _alloc_list_node *prev; |
| 83 | } AllocListNode; |
| 84 | |
| 85 | // |
| 86 | // Our gralloc device structure (alloc interface) |
| 87 | // |
| 88 | struct gralloc_device_t { |
| 89 | alloc_device_t device; |
| 90 | |
| 91 | AllocListNode *allocListHead; // double linked list of allocated buffers |
| 92 | pthread_mutex_t lock; |
| 93 | }; |
| 94 | |
| 95 | // |
| 96 | // Our framebuffer device structure |
| 97 | // |
| 98 | struct fb_device_t { |
| 99 | framebuffer_device_t device; |
| 100 | }; |
| 101 | |
| 102 | static int map_buffer(cb_handle_t *cb, void **vaddr) |
| 103 | { |
| 104 | if (cb->fd < 0 || cb->ashmemSize <= 0) { |
| 105 | return -EINVAL; |
| 106 | } |
| 107 | |
| 108 | void *addr = mmap(0, cb->ashmemSize, PROT_READ | PROT_WRITE, |
| 109 | MAP_SHARED, cb->fd, 0); |
| 110 | if (addr == MAP_FAILED) { |
| 111 | return -errno; |
| 112 | } |
| 113 | |
| 114 | cb->ashmemBase = intptr_t(addr); |
| 115 | cb->ashmemBasePid = getpid(); |
| 116 | |
| 117 | *vaddr = addr; |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | #define DEFINE_HOST_CONNECTION \ |
| 122 | HostConnection *hostCon = HostConnection::get(); \ |
| 123 | renderControl_encoder_context_t *rcEnc = (hostCon ? hostCon->rcEncoder() : NULL) |
| 124 | |
Lingfeng Yang | 200c716 | 2016-06-13 08:39:28 -0700 | [diff] [blame] | 125 | #define EXIT_GRALLOCONLY_HOST_CONNECTION \ |
| 126 | HostConnection *hostCon = HostConnection::get(); \ |
| 127 | if (hostCon && hostCon->isGrallocOnly()) { \ |
| 128 | ALOGD("%s: exiting HostConnection (is buffer-handling thread)", \ |
| 129 | __FUNCTION__); \ |
| 130 | HostConnection::exit(); \ |
| 131 | } |
| 132 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 133 | #define DEFINE_AND_VALIDATE_HOST_CONNECTION \ |
| 134 | HostConnection *hostCon = HostConnection::get(); \ |
| 135 | if (!hostCon) { \ |
| 136 | ALOGE("gralloc: Failed to get host connection\n"); \ |
| 137 | return -EIO; \ |
| 138 | } \ |
| 139 | renderControl_encoder_context_t *rcEnc = hostCon->rcEncoder(); \ |
| 140 | if (!rcEnc) { \ |
| 141 | ALOGE("gralloc: Failed to get renderControl encoder context\n"); \ |
| 142 | return -EIO; \ |
| 143 | } |
| 144 | |
| 145 | |
| 146 | // |
| 147 | // gralloc device functions (alloc interface) |
| 148 | // |
| 149 | static int gralloc_alloc(alloc_device_t* dev, |
| 150 | int w, int h, int format, int usage, |
| 151 | buffer_handle_t* pHandle, int* pStride) |
| 152 | { |
| 153 | D("gralloc_alloc w=%d h=%d usage=0x%x\n", w, h, usage); |
| 154 | |
| 155 | gralloc_device_t *grdev = (gralloc_device_t *)dev; |
| 156 | if (!grdev || !pHandle || !pStride) { |
| 157 | ALOGE("gralloc_alloc: Bad inputs (grdev: %p, pHandle: %p, pStride: %p", |
| 158 | grdev, pHandle, pStride); |
| 159 | return -EINVAL; |
| 160 | } |
| 161 | |
| 162 | // |
bohu | ad72b3e | 2015-01-26 09:25:23 -0800 | [diff] [blame] | 163 | // Note: in screen capture mode, both sw_write and hw_write will be on |
| 164 | // and this is a valid usage |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 165 | // |
| 166 | bool sw_write = (0 != (usage & GRALLOC_USAGE_SW_WRITE_MASK)); |
| 167 | bool hw_write = (usage & GRALLOC_USAGE_HW_RENDER); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 168 | bool sw_read = (0 != (usage & GRALLOC_USAGE_SW_READ_MASK)); |
Yahan Zhou | 10c7682 | 2016-06-20 12:28:34 -0700 | [diff] [blame] | 169 | #if PLATFORM_SDK_VERSION >= 17 |
| 170 | bool hw_cam_write = (usage & GRALLOC_USAGE_HW_CAMERA_WRITE); |
| 171 | bool hw_cam_read = (usage & GRALLOC_USAGE_HW_CAMERA_READ); |
| 172 | #else // PLATFORM_SDK_VERSION >= 17 |
| 173 | bool hw_cam_write = false; |
| 174 | bool hw_cam_read = false; |
| 175 | #endif // PLATFORM_SDK_VERSION >= 17 |
Yahan Zhou | da1c76d | 2016-06-23 13:58:09 -0700 | [diff] [blame] | 176 | #if PLATFORM_SDK_VERSION >= 16 |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 177 | bool hw_vid_enc_read = usage & GRALLOC_USAGE_HW_VIDEO_ENCODER; |
Yahan Zhou | da1c76d | 2016-06-23 13:58:09 -0700 | [diff] [blame] | 178 | #else // PLATFORM_SDK_VERSION >= 16 |
| 179 | bool hw_vid_enc_read = false; |
| 180 | #endif // PLATFORM_SDK_VERSION >= 16 |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 181 | |
Eino-Ville Talvala | a600078 | 2013-05-04 16:45:22 -0700 | [diff] [blame] | 182 | // Keep around original requested format for later validation |
| 183 | int frameworkFormat = format; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 184 | // Pick the right concrete pixel format given the endpoints as encoded in |
| 185 | // the usage bits. Every end-point pair needs explicit listing here. |
Yahan Zhou | 10c7682 | 2016-06-20 12:28:34 -0700 | [diff] [blame] | 186 | #if PLATFORM_SDK_VERSION >= 17 |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 187 | if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) { |
| 188 | // Camera as producer |
| 189 | if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) { |
| 190 | if (usage & GRALLOC_USAGE_HW_TEXTURE) { |
| 191 | // Camera-to-display is RGBA |
| 192 | format = HAL_PIXEL_FORMAT_RGBA_8888; |
| 193 | } else if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) { |
| 194 | // Camera-to-encoder is NV21 |
| 195 | format = HAL_PIXEL_FORMAT_YCrCb_420_SP; |
| 196 | } else if ((usage & GRALLOC_USAGE_HW_CAMERA_MASK) == |
| 197 | GRALLOC_USAGE_HW_CAMERA_ZSL) { |
| 198 | // Camera-to-ZSL-queue is RGB_888 |
| 199 | format = HAL_PIXEL_FORMAT_RGB_888; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) { |
| 204 | ALOGE("gralloc_alloc: Requested auto format selection, " |
| 205 | "but no known format for this usage: %d x %d, usage %x", |
| 206 | w, h, usage); |
| 207 | return -EINVAL; |
| 208 | } |
Yahan Zhou | 10c7682 | 2016-06-20 12:28:34 -0700 | [diff] [blame] | 209 | } |
| 210 | #if PLATFORM_SDK_VERSION >= 18 |
| 211 | else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) { |
Eino-Ville Talvala | a600078 | 2013-05-04 16:45:22 -0700 | [diff] [blame] | 212 | // Flexible framework-accessible YUV format; map to NV21 for now |
| 213 | if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) { |
| 214 | format = HAL_PIXEL_FORMAT_YCrCb_420_SP; |
| 215 | } |
| 216 | if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) { |
| 217 | ALOGE("gralloc_alloc: Requested YCbCr_420_888, but no known " |
| 218 | "specific format for this usage: %d x %d, usage %x", |
| 219 | w, h, usage); |
| 220 | } |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 221 | } |
Yahan Zhou | 10c7682 | 2016-06-20 12:28:34 -0700 | [diff] [blame] | 222 | #endif // PLATFORM_SDK_VERSION >= 18 |
| 223 | #endif // PLATFORM_SDK_VERSION >= 17 |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 224 | bool yuv_format = false; |
| 225 | |
| 226 | int ashmem_size = 0; |
| 227 | int stride = w; |
| 228 | |
| 229 | GLenum glFormat = 0; |
| 230 | GLenum glType = 0; |
| 231 | |
| 232 | int bpp = 0; |
| 233 | int align = 1; |
| 234 | switch (format) { |
| 235 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 236 | case HAL_PIXEL_FORMAT_RGBX_8888: |
| 237 | case HAL_PIXEL_FORMAT_BGRA_8888: |
| 238 | bpp = 4; |
| 239 | glFormat = GL_RGBA; |
| 240 | glType = GL_UNSIGNED_BYTE; |
| 241 | break; |
| 242 | case HAL_PIXEL_FORMAT_RGB_888: |
| 243 | bpp = 3; |
| 244 | glFormat = GL_RGB; |
| 245 | glType = GL_UNSIGNED_BYTE; |
| 246 | break; |
| 247 | case HAL_PIXEL_FORMAT_RGB_565: |
| 248 | bpp = 2; |
| 249 | glFormat = GL_RGB; |
| 250 | glType = GL_UNSIGNED_SHORT_5_6_5; |
| 251 | break; |
Yahan Zhou | d906928 | 2016-06-17 17:40:14 -0700 | [diff] [blame] | 252 | #if PLATFORM_SDK_VERSION >= 21 |
Eino-Ville Talvala | 4087977 | 2015-02-19 16:53:59 -0800 | [diff] [blame] | 253 | case HAL_PIXEL_FORMAT_RAW16: |
Eino-Ville Talvala | 4b03d2c | 2015-08-05 15:43:30 -0700 | [diff] [blame] | 254 | case HAL_PIXEL_FORMAT_Y16: |
Yahan Zhou | da1c76d | 2016-06-23 13:58:09 -0700 | [diff] [blame] | 255 | #elif PLATFORM_SDK_VERSION >= 16 |
Yahan Zhou | d906928 | 2016-06-17 17:40:14 -0700 | [diff] [blame] | 256 | case HAL_PIXEL_FORMAT_RAW_SENSOR: |
| 257 | #endif |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 258 | bpp = 2; |
| 259 | align = 16*bpp; |
| 260 | if (! ((sw_read || hw_cam_read) && (sw_write || hw_cam_write) ) ) { |
Eino-Ville Talvala | 4b03d2c | 2015-08-05 15:43:30 -0700 | [diff] [blame] | 261 | // Raw sensor data or Y16 only goes between camera and CPU |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 262 | return -EINVAL; |
| 263 | } |
| 264 | // Not expecting to actually create any GL surfaces for this |
| 265 | glFormat = GL_LUMINANCE; |
| 266 | glType = GL_UNSIGNED_SHORT; |
| 267 | break; |
Yahan Zhou | 10c7682 | 2016-06-20 12:28:34 -0700 | [diff] [blame] | 268 | #if PLATFORM_SDK_VERSION >= 17 |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 269 | case HAL_PIXEL_FORMAT_BLOB: |
| 270 | bpp = 1; |
| 271 | if (! (sw_read && hw_cam_write) ) { |
| 272 | // Blob data cannot be used by HW other than camera emulator |
| 273 | return -EINVAL; |
| 274 | } |
| 275 | // Not expecting to actually create any GL surfaces for this |
| 276 | glFormat = GL_LUMINANCE; |
| 277 | glType = GL_UNSIGNED_BYTE; |
| 278 | break; |
Yahan Zhou | 10c7682 | 2016-06-20 12:28:34 -0700 | [diff] [blame] | 279 | #endif // PLATFORM_SDK_VERSION >= 17 |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 280 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| 281 | align = 1; |
| 282 | bpp = 1; // per-channel bpp |
| 283 | yuv_format = true; |
| 284 | // Not expecting to actually create any GL surfaces for this |
| 285 | break; |
| 286 | case HAL_PIXEL_FORMAT_YV12: |
| 287 | align = 16; |
| 288 | bpp = 1; // per-channel bpp |
| 289 | yuv_format = true; |
| 290 | // Not expecting to actually create any GL surfaces for this |
| 291 | break; |
| 292 | default: |
| 293 | ALOGE("gralloc_alloc: Unknown format %d", format); |
| 294 | return -EINVAL; |
| 295 | } |
| 296 | |
| 297 | if (usage & GRALLOC_USAGE_HW_FB) { |
| 298 | // keep space for postCounter |
| 299 | ashmem_size += sizeof(uint32_t); |
| 300 | } |
| 301 | |
| 302 | if (sw_read || sw_write || hw_cam_write || hw_vid_enc_read) { |
| 303 | // keep space for image on guest memory if SW access is needed |
| 304 | // or if the camera is doing writing |
| 305 | if (yuv_format) { |
| 306 | size_t yStride = (w*bpp + (align - 1)) & ~(align-1); |
| 307 | size_t uvStride = (yStride / 2 + (align - 1)) & ~(align-1); |
| 308 | size_t uvHeight = h / 2; |
| 309 | ashmem_size += yStride * h + 2 * (uvHeight * uvStride); |
| 310 | stride = yStride / bpp; |
| 311 | } else { |
| 312 | size_t bpr = (w*bpp + (align-1)) & ~(align-1); |
| 313 | ashmem_size += (bpr * h); |
| 314 | stride = bpr / bpp; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | D("gralloc_alloc format=%d, ashmem_size=%d, stride=%d, tid %d\n", format, |
| 319 | ashmem_size, stride, gettid()); |
| 320 | |
| 321 | // |
| 322 | // Allocate space in ashmem if needed |
| 323 | // |
| 324 | int fd = -1; |
| 325 | if (ashmem_size > 0) { |
| 326 | // round to page size; |
| 327 | ashmem_size = (ashmem_size + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1); |
| 328 | |
| 329 | fd = ashmem_create_region("gralloc-buffer", ashmem_size); |
| 330 | if (fd < 0) { |
| 331 | ALOGE("gralloc_alloc failed to create ashmem region: %s\n", |
| 332 | strerror(errno)); |
| 333 | return -errno; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | cb_handle_t *cb = new cb_handle_t(fd, ashmem_size, usage, |
Eino-Ville Talvala | a600078 | 2013-05-04 16:45:22 -0700 | [diff] [blame] | 338 | w, h, frameworkFormat, format, |
| 339 | glFormat, glType); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 340 | |
| 341 | if (ashmem_size > 0) { |
| 342 | // |
| 343 | // map ashmem region if exist |
| 344 | // |
| 345 | void *vaddr; |
| 346 | int err = map_buffer(cb, &vaddr); |
| 347 | if (err) { |
| 348 | close(fd); |
| 349 | delete cb; |
| 350 | return err; |
| 351 | } |
| 352 | |
| 353 | cb->setFd(fd); |
| 354 | } |
| 355 | |
| 356 | // |
| 357 | // Allocate ColorBuffer handle on the host (only if h/w access is allowed) |
| 358 | // Only do this for some h/w usages, not all. |
Bjoern Johansson | 921d0db | 2016-05-12 10:38:27 -0700 | [diff] [blame] | 359 | // Also do this if we need to read from the surface, in this case the |
| 360 | // rendering will still happen on the host but we also need to be able to |
| 361 | // read back from the color buffer, which requires that there is a buffer |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 362 | // |
Yahan Zhou | da1c76d | 2016-06-23 13:58:09 -0700 | [diff] [blame] | 363 | #if PLATFORM_SDK_VERSION >= 16 |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 364 | if (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER | |
| 365 | GRALLOC_USAGE_HW_2D | GRALLOC_USAGE_HW_COMPOSER | |
Bjoern Johansson | 921d0db | 2016-05-12 10:38:27 -0700 | [diff] [blame] | 366 | GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_SW_READ_MASK) ) { |
Yahan Zhou | da1c76d | 2016-06-23 13:58:09 -0700 | [diff] [blame] | 367 | #else // PLATFORM_SDK_VERSION >= 16 |
| 368 | if (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER | |
| 369 | GRALLOC_USAGE_HW_2D | |
| 370 | GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_SW_READ_MASK) ) { |
| 371 | #endif // PLATFORM_SDK_VERSION >= 16 |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 372 | DEFINE_HOST_CONNECTION; |
| 373 | if (hostCon && rcEnc) { |
Yahan Zhou | c89acb2 | 2016-07-11 11:51:43 -0700 | [diff] [blame^] | 374 | pthread_once(&sGrallocProcPipeOnce, gralloc_proc_init); |
| 375 | cb->hostHandle = PID_CMD(rcCreateColorBuffer, w, h, glFormat); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 376 | D("Created host ColorBuffer 0x%x\n", cb->hostHandle); |
| 377 | } |
| 378 | |
| 379 | if (!cb->hostHandle) { |
| 380 | // Could not create colorbuffer on host !!! |
| 381 | close(fd); |
| 382 | delete cb; |
| 383 | return -EIO; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | // |
| 388 | // alloc succeeded - insert the allocated handle to the allocated list |
| 389 | // |
| 390 | AllocListNode *node = new AllocListNode(); |
| 391 | pthread_mutex_lock(&grdev->lock); |
| 392 | node->handle = cb; |
| 393 | node->next = grdev->allocListHead; |
| 394 | node->prev = NULL; |
| 395 | if (grdev->allocListHead) { |
| 396 | grdev->allocListHead->prev = node; |
| 397 | } |
| 398 | grdev->allocListHead = node; |
| 399 | pthread_mutex_unlock(&grdev->lock); |
| 400 | |
| 401 | *pHandle = cb; |
Yahan Zhou | 10c7682 | 2016-06-20 12:28:34 -0700 | [diff] [blame] | 402 | switch (frameworkFormat) { |
| 403 | #if PLATFORM_SDK_VERSION >= 18 |
| 404 | case HAL_PIXEL_FORMAT_YCbCr_420_888: |
Eino-Ville Talvala | a600078 | 2013-05-04 16:45:22 -0700 | [diff] [blame] | 405 | *pStride = 0; |
Yahan Zhou | 10c7682 | 2016-06-20 12:28:34 -0700 | [diff] [blame] | 406 | break; |
| 407 | #endif // PLATFORM_SDK_VERSION >= 18 |
| 408 | default: |
Eino-Ville Talvala | a600078 | 2013-05-04 16:45:22 -0700 | [diff] [blame] | 409 | *pStride = stride; |
Yahan Zhou | 10c7682 | 2016-06-20 12:28:34 -0700 | [diff] [blame] | 410 | break; |
Eino-Ville Talvala | a600078 | 2013-05-04 16:45:22 -0700 | [diff] [blame] | 411 | } |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | static int gralloc_free(alloc_device_t* dev, |
| 416 | buffer_handle_t handle) |
| 417 | { |
| 418 | const cb_handle_t *cb = (const cb_handle_t *)handle; |
| 419 | if (!cb_handle_t::validate((cb_handle_t*)cb)) { |
| 420 | ERR("gralloc_free: invalid handle"); |
| 421 | return -EINVAL; |
| 422 | } |
| 423 | |
| 424 | if (cb->hostHandle != 0) { |
| 425 | DEFINE_AND_VALIDATE_HOST_CONNECTION; |
| 426 | D("Closing host ColorBuffer 0x%x\n", cb->hostHandle); |
Yahan Zhou | c89acb2 | 2016-07-11 11:51:43 -0700 | [diff] [blame^] | 427 | pthread_once(&sGrallocProcPipeOnce, gralloc_proc_init); |
| 428 | PID_CMD(rcCloseColorBuffer, cb->hostHandle); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | // |
| 432 | // detach and unmap ashmem area if present |
| 433 | // |
| 434 | if (cb->fd > 0) { |
| 435 | if (cb->ashmemSize > 0 && cb->ashmemBase) { |
| 436 | munmap((void *)cb->ashmemBase, cb->ashmemSize); |
| 437 | } |
| 438 | close(cb->fd); |
| 439 | } |
| 440 | |
| 441 | // remove it from the allocated list |
| 442 | gralloc_device_t *grdev = (gralloc_device_t *)dev; |
| 443 | pthread_mutex_lock(&grdev->lock); |
| 444 | AllocListNode *n = grdev->allocListHead; |
| 445 | while( n && n->handle != cb ) { |
| 446 | n = n->next; |
| 447 | } |
| 448 | if (n) { |
| 449 | // buffer found on list - remove it from list |
| 450 | if (n->next) { |
| 451 | n->next->prev = n->prev; |
| 452 | } |
| 453 | if (n->prev) { |
| 454 | n->prev->next = n->next; |
| 455 | } |
| 456 | else { |
| 457 | grdev->allocListHead = n->next; |
| 458 | } |
| 459 | |
| 460 | delete n; |
| 461 | } |
| 462 | pthread_mutex_unlock(&grdev->lock); |
| 463 | |
| 464 | delete cb; |
| 465 | |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | static int gralloc_device_close(struct hw_device_t *dev) |
| 470 | { |
| 471 | gralloc_device_t* d = reinterpret_cast<gralloc_device_t*>(dev); |
| 472 | if (d) { |
| 473 | |
| 474 | // free still allocated buffers |
| 475 | while( d->allocListHead != NULL ) { |
| 476 | gralloc_free(&d->device, d->allocListHead->handle); |
| 477 | } |
| 478 | |
| 479 | // free device |
| 480 | free(d); |
| 481 | } |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | static int fb_compositionComplete(struct framebuffer_device_t* dev) |
| 486 | { |
bohu | 8416d62 | 2014-12-02 11:44:44 -0800 | [diff] [blame] | 487 | (void)dev; |
| 488 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | // |
| 493 | // Framebuffer device functions |
| 494 | // |
| 495 | static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer) |
| 496 | { |
| 497 | fb_device_t *fbdev = (fb_device_t *)dev; |
| 498 | cb_handle_t *cb = (cb_handle_t *)buffer; |
| 499 | |
| 500 | if (!fbdev || !cb_handle_t::validate(cb) || !cb->canBePosted()) { |
| 501 | return -EINVAL; |
| 502 | } |
| 503 | |
| 504 | // Make sure we have host connection |
| 505 | DEFINE_AND_VALIDATE_HOST_CONNECTION; |
| 506 | |
| 507 | // increment the post count of the buffer |
Tina Zhang | 163119f | 2014-03-21 08:14:41 +0800 | [diff] [blame] | 508 | intptr_t *postCountPtr = (intptr_t *)cb->ashmemBase; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 509 | if (!postCountPtr) { |
| 510 | // This should not happen |
| 511 | return -EINVAL; |
| 512 | } |
| 513 | (*postCountPtr)++; |
| 514 | |
| 515 | // send post request to host |
| 516 | rcEnc->rcFBPost(rcEnc, cb->hostHandle); |
| 517 | hostCon->flush(); |
| 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | static int fb_setUpdateRect(struct framebuffer_device_t* dev, |
| 523 | int l, int t, int w, int h) |
| 524 | { |
| 525 | fb_device_t *fbdev = (fb_device_t *)dev; |
| 526 | |
bohu | 8416d62 | 2014-12-02 11:44:44 -0800 | [diff] [blame] | 527 | (void)l; |
| 528 | (void)t; |
| 529 | (void)w; |
| 530 | (void)h; |
| 531 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 532 | if (!fbdev) { |
| 533 | return -EINVAL; |
| 534 | } |
| 535 | |
| 536 | // Make sure we have host connection |
| 537 | DEFINE_AND_VALIDATE_HOST_CONNECTION; |
| 538 | |
| 539 | // send request to host |
| 540 | // TODO: XXX - should be implemented |
| 541 | //rcEnc->rc_XXX |
| 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | static int fb_setSwapInterval(struct framebuffer_device_t* dev, |
| 547 | int interval) |
| 548 | { |
| 549 | fb_device_t *fbdev = (fb_device_t *)dev; |
| 550 | |
| 551 | if (!fbdev) { |
| 552 | return -EINVAL; |
| 553 | } |
| 554 | |
| 555 | // Make sure we have host connection |
| 556 | DEFINE_AND_VALIDATE_HOST_CONNECTION; |
| 557 | |
| 558 | // send request to host |
| 559 | rcEnc->rcFBSetSwapInterval(rcEnc, interval); |
| 560 | hostCon->flush(); |
| 561 | |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | static int fb_close(struct hw_device_t *dev) |
| 566 | { |
| 567 | fb_device_t *fbdev = (fb_device_t *)dev; |
| 568 | |
| 569 | delete fbdev; |
| 570 | |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | |
| 575 | // |
| 576 | // gralloc module functions - refcount + locking interface |
| 577 | // |
| 578 | static int gralloc_register_buffer(gralloc_module_t const* module, |
| 579 | buffer_handle_t handle) |
| 580 | { |
| 581 | pthread_once(&sFallbackOnce, fallback_init); |
| 582 | if (sFallback != NULL) { |
| 583 | return sFallback->registerBuffer(sFallback, handle); |
| 584 | } |
Yahan Zhou | c89acb2 | 2016-07-11 11:51:43 -0700 | [diff] [blame^] | 585 | pthread_once(&sGrallocProcPipeOnce, gralloc_proc_init); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 586 | |
| 587 | D("gralloc_register_buffer(%p) called", handle); |
| 588 | |
| 589 | private_module_t *gr = (private_module_t *)module; |
| 590 | cb_handle_t *cb = (cb_handle_t *)handle; |
| 591 | if (!gr || !cb_handle_t::validate(cb)) { |
| 592 | ERR("gralloc_register_buffer(%p): invalid buffer", cb); |
| 593 | return -EINVAL; |
| 594 | } |
| 595 | |
| 596 | if (cb->hostHandle != 0) { |
| 597 | DEFINE_AND_VALIDATE_HOST_CONNECTION; |
| 598 | D("Opening host ColorBuffer 0x%x\n", cb->hostHandle); |
Yahan Zhou | c89acb2 | 2016-07-11 11:51:43 -0700 | [diff] [blame^] | 599 | PID_CMD(rcOpenColorBuffer2, cb->hostHandle); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | // |
| 603 | // if the color buffer has ashmem region and it is not mapped in this |
| 604 | // process map it now. |
| 605 | // |
| 606 | if (cb->ashmemSize > 0 && cb->mappedPid != getpid()) { |
| 607 | void *vaddr; |
| 608 | int err = map_buffer(cb, &vaddr); |
| 609 | if (err) { |
| 610 | ERR("gralloc_register_buffer(%p): map failed: %s", cb, strerror(-err)); |
| 611 | return -err; |
| 612 | } |
| 613 | cb->mappedPid = getpid(); |
| 614 | } |
| 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | static int gralloc_unregister_buffer(gralloc_module_t const* module, |
| 620 | buffer_handle_t handle) |
| 621 | { |
| 622 | if (sFallback != NULL) { |
| 623 | return sFallback->unregisterBuffer(sFallback, handle); |
| 624 | } |
| 625 | |
| 626 | private_module_t *gr = (private_module_t *)module; |
| 627 | cb_handle_t *cb = (cb_handle_t *)handle; |
| 628 | if (!gr || !cb_handle_t::validate(cb)) { |
| 629 | ERR("gralloc_unregister_buffer(%p): invalid buffer", cb); |
| 630 | return -EINVAL; |
| 631 | } |
| 632 | |
| 633 | if (cb->hostHandle != 0) { |
| 634 | DEFINE_AND_VALIDATE_HOST_CONNECTION; |
| 635 | D("Closing host ColorBuffer 0x%x\n", cb->hostHandle); |
Yahan Zhou | c89acb2 | 2016-07-11 11:51:43 -0700 | [diff] [blame^] | 636 | pthread_once(&sGrallocProcPipeOnce, gralloc_proc_init); |
| 637 | PID_CMD(rcCloseColorBuffer, cb->hostHandle); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | // |
| 641 | // unmap ashmem region if it was previously mapped in this process |
| 642 | // (through register_buffer) |
| 643 | // |
| 644 | if (cb->ashmemSize > 0 && cb->mappedPid == getpid()) { |
| 645 | void *vaddr; |
| 646 | int err = munmap((void *)cb->ashmemBase, cb->ashmemSize); |
| 647 | if (err) { |
| 648 | ERR("gralloc_unregister_buffer(%p): unmap failed", cb); |
| 649 | return -EINVAL; |
| 650 | } |
Eino-Ville Talvala | a600078 | 2013-05-04 16:45:22 -0700 | [diff] [blame] | 651 | cb->ashmemBase = 0; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 652 | cb->mappedPid = 0; |
| 653 | } |
| 654 | |
| 655 | D("gralloc_unregister_buffer(%p) done\n", cb); |
| 656 | |
Lingfeng Yang | 200c716 | 2016-06-13 08:39:28 -0700 | [diff] [blame] | 657 | EXIT_GRALLOCONLY_HOST_CONNECTION; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | static int gralloc_lock(gralloc_module_t const* module, |
| 662 | buffer_handle_t handle, int usage, |
| 663 | int l, int t, int w, int h, |
| 664 | void** vaddr) |
| 665 | { |
| 666 | if (sFallback != NULL) { |
| 667 | return sFallback->lock(sFallback, handle, usage, l, t, w, h, vaddr); |
| 668 | } |
| 669 | |
| 670 | private_module_t *gr = (private_module_t *)module; |
| 671 | cb_handle_t *cb = (cb_handle_t *)handle; |
| 672 | if (!gr || !cb_handle_t::validate(cb)) { |
| 673 | ALOGE("gralloc_lock bad handle\n"); |
| 674 | return -EINVAL; |
| 675 | } |
| 676 | |
Eino-Ville Talvala | a600078 | 2013-05-04 16:45:22 -0700 | [diff] [blame] | 677 | // validate format |
Yahan Zhou | 10c7682 | 2016-06-20 12:28:34 -0700 | [diff] [blame] | 678 | #if PLATFORM_SDK_VERSION >= 18 |
Eino-Ville Talvala | a600078 | 2013-05-04 16:45:22 -0700 | [diff] [blame] | 679 | if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) { |
| 680 | ALOGE("gralloc_lock can't be used with YCbCr_420_888 format"); |
| 681 | return -EINVAL; |
| 682 | } |
Yahan Zhou | 10c7682 | 2016-06-20 12:28:34 -0700 | [diff] [blame] | 683 | #endif // PLATFORM_SDK_VERSION >= 18 |
Eino-Ville Talvala | a600078 | 2013-05-04 16:45:22 -0700 | [diff] [blame] | 684 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 685 | // Validate usage, |
| 686 | // 1. cannot be locked for hw access |
| 687 | // 2. lock for either sw read or write. |
| 688 | // 3. locked sw access must match usage during alloc time. |
| 689 | bool sw_read = (0 != (usage & GRALLOC_USAGE_SW_READ_MASK)); |
| 690 | bool sw_write = (0 != (usage & GRALLOC_USAGE_SW_WRITE_MASK)); |
| 691 | bool hw_read = (usage & GRALLOC_USAGE_HW_TEXTURE); |
| 692 | bool hw_write = (usage & GRALLOC_USAGE_HW_RENDER); |
Yahan Zhou | 10c7682 | 2016-06-20 12:28:34 -0700 | [diff] [blame] | 693 | #if PLATFORM_SDK_VERSION >= 17 |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 694 | bool hw_cam_write = (usage & GRALLOC_USAGE_HW_CAMERA_WRITE); |
| 695 | bool hw_cam_read = (usage & GRALLOC_USAGE_HW_CAMERA_READ); |
Yahan Zhou | 10c7682 | 2016-06-20 12:28:34 -0700 | [diff] [blame] | 696 | #else // PLATFORM_SDK_VERSION >= 17 |
| 697 | bool hw_cam_write = false; |
| 698 | bool hw_cam_read = false; |
| 699 | #endif // PLATFORM_SDK_VERSION >= 17 |
Yahan Zhou | da1c76d | 2016-06-23 13:58:09 -0700 | [diff] [blame] | 700 | |
| 701 | #if PLATFORM_SDK_VERSION >= 16 |
| 702 | bool hw_vid_enc_read = (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER); |
| 703 | #else // PLATFORM_SDK_VERSION >= 16 |
| 704 | bool hw_vid_enc_read = false; |
| 705 | #endif // PLATFORM_SDK_VERSION >= 16 |
| 706 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 707 | bool sw_read_allowed = (0 != (cb->usage & GRALLOC_USAGE_SW_READ_MASK)); |
| 708 | bool sw_write_allowed = (0 != (cb->usage & GRALLOC_USAGE_SW_WRITE_MASK)); |
| 709 | |
| 710 | if ( (hw_read || hw_write) || |
| 711 | (!sw_read && !sw_write && |
| 712 | !hw_cam_write && !hw_cam_read && |
| 713 | !hw_vid_enc_read) || |
| 714 | (sw_read && !sw_read_allowed) || |
| 715 | (sw_write && !sw_write_allowed) ) { |
| 716 | ALOGE("gralloc_lock usage mismatch usage=0x%x cb->usage=0x%x\n", usage, |
| 717 | cb->usage); |
| 718 | return -EINVAL; |
| 719 | } |
| 720 | |
Tina Zhang | 163119f | 2014-03-21 08:14:41 +0800 | [diff] [blame] | 721 | intptr_t postCount = 0; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 722 | void *cpu_addr = NULL; |
| 723 | |
| 724 | // |
| 725 | // make sure ashmem area is mapped if needed |
| 726 | // |
| 727 | if (cb->canBePosted() || sw_read || sw_write || |
| 728 | hw_cam_write || hw_cam_read || |
| 729 | hw_vid_enc_read) { |
| 730 | if (cb->ashmemBasePid != getpid() || !cb->ashmemBase) { |
| 731 | return -EACCES; |
| 732 | } |
| 733 | |
| 734 | if (cb->canBePosted()) { |
Tina Zhang | 163119f | 2014-03-21 08:14:41 +0800 | [diff] [blame] | 735 | postCount = *((intptr_t *)cb->ashmemBase); |
| 736 | cpu_addr = (void *)(cb->ashmemBase + sizeof(intptr_t)); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 737 | } |
| 738 | else { |
| 739 | cpu_addr = (void *)(cb->ashmemBase); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | if (cb->hostHandle) { |
| 744 | // Make sure we have host connection |
| 745 | DEFINE_AND_VALIDATE_HOST_CONNECTION; |
| 746 | |
| 747 | // |
| 748 | // flush color buffer write cache on host and get its sync status. |
| 749 | // |
| 750 | int hostSyncStatus = rcEnc->rcColorBufferCacheFlush(rcEnc, cb->hostHandle, |
| 751 | postCount, |
| 752 | sw_read); |
| 753 | if (hostSyncStatus < 0) { |
| 754 | // host failed the color buffer sync - probably since it was already |
| 755 | // locked for write access. fail the lock. |
| 756 | ALOGE("gralloc_lock cacheFlush failed postCount=%d sw_read=%d\n", |
| 757 | postCount, sw_read); |
| 758 | return -EBUSY; |
| 759 | } |
| 760 | |
Bjoern Johansson | 921d0db | 2016-05-12 10:38:27 -0700 | [diff] [blame] | 761 | if (sw_read) { |
bohu | ad72b3e | 2015-01-26 09:25:23 -0800 | [diff] [blame] | 762 | D("gralloc_lock read back color buffer %d %d\n", cb->width, cb->height); |
bohu | ad72b3e | 2015-01-26 09:25:23 -0800 | [diff] [blame] | 763 | rcEnc->rcReadColorBuffer(rcEnc, cb->hostHandle, |
Bjoern Johansson | d16ee72 | 2016-06-13 16:09:20 -0700 | [diff] [blame] | 764 | 0, 0, cb->width, cb->height, cb->glFormat, cb->glType, cpu_addr); |
bohu | ad72b3e | 2015-01-26 09:25:23 -0800 | [diff] [blame] | 765 | } |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | // |
| 769 | // is virtual address required ? |
| 770 | // |
| 771 | if (sw_read || sw_write || hw_cam_write || hw_cam_read || hw_vid_enc_read) { |
| 772 | *vaddr = cpu_addr; |
| 773 | } |
| 774 | |
| 775 | if (sw_write || hw_cam_write) { |
| 776 | // |
| 777 | // Keep locked region if locked for s/w write access. |
| 778 | // |
| 779 | cb->lockedLeft = l; |
| 780 | cb->lockedTop = t; |
| 781 | cb->lockedWidth = w; |
| 782 | cb->lockedHeight = h; |
| 783 | } |
| 784 | |
| 785 | DD("gralloc_lock success. vaddr: %p, *vaddr: %p, usage: %x, cpu_addr: %p", |
| 786 | vaddr, vaddr ? *vaddr : 0, usage, cpu_addr); |
| 787 | |
| 788 | return 0; |
| 789 | } |
| 790 | |
| 791 | static int gralloc_unlock(gralloc_module_t const* module, |
| 792 | buffer_handle_t handle) |
| 793 | { |
| 794 | if (sFallback != NULL) { |
| 795 | return sFallback->unlock(sFallback, handle); |
| 796 | } |
| 797 | |
| 798 | private_module_t *gr = (private_module_t *)module; |
| 799 | cb_handle_t *cb = (cb_handle_t *)handle; |
| 800 | if (!gr || !cb_handle_t::validate(cb)) { |
| 801 | return -EINVAL; |
| 802 | } |
| 803 | |
| 804 | // |
| 805 | // if buffer was locked for s/w write, we need to update the host with |
| 806 | // the updated data |
| 807 | // |
Lingfeng Yang | 0da8a24 | 2016-05-25 08:31:06 -0700 | [diff] [blame] | 808 | if (cb->hostHandle) { |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 809 | |
| 810 | // Make sure we have host connection |
| 811 | DEFINE_AND_VALIDATE_HOST_CONNECTION; |
| 812 | |
| 813 | void *cpu_addr; |
| 814 | if (cb->canBePosted()) { |
| 815 | cpu_addr = (void *)(cb->ashmemBase + sizeof(int)); |
| 816 | } |
| 817 | else { |
| 818 | cpu_addr = (void *)(cb->ashmemBase); |
| 819 | } |
| 820 | |
| 821 | if (cb->lockedWidth < cb->width || cb->lockedHeight < cb->height) { |
| 822 | int bpp = glUtilsPixelBitSize(cb->glFormat, cb->glType) >> 3; |
| 823 | char *tmpBuf = new char[cb->lockedWidth * cb->lockedHeight * bpp]; |
| 824 | |
| 825 | int dst_line_len = cb->lockedWidth * bpp; |
| 826 | int src_line_len = cb->width * bpp; |
| 827 | char *src = (char *)cpu_addr + cb->lockedTop*src_line_len + cb->lockedLeft*bpp; |
| 828 | char *dst = tmpBuf; |
| 829 | for (int y=0; y<cb->lockedHeight; y++) { |
| 830 | memcpy(dst, src, dst_line_len); |
| 831 | src += src_line_len; |
| 832 | dst += dst_line_len; |
| 833 | } |
| 834 | |
| 835 | rcEnc->rcUpdateColorBuffer(rcEnc, cb->hostHandle, |
| 836 | cb->lockedLeft, cb->lockedTop, |
| 837 | cb->lockedWidth, cb->lockedHeight, |
| 838 | cb->glFormat, cb->glType, |
| 839 | tmpBuf); |
| 840 | |
| 841 | delete [] tmpBuf; |
| 842 | } |
| 843 | else { |
| 844 | rcEnc->rcUpdateColorBuffer(rcEnc, cb->hostHandle, 0, 0, |
| 845 | cb->width, cb->height, |
| 846 | cb->glFormat, cb->glType, |
| 847 | cpu_addr); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | cb->lockedWidth = cb->lockedHeight = 0; |
| 852 | return 0; |
| 853 | } |
| 854 | |
Yahan Zhou | 10c7682 | 2016-06-20 12:28:34 -0700 | [diff] [blame] | 855 | #if PLATFORM_SDK_VERSION >= 18 |
Eino-Ville Talvala | a600078 | 2013-05-04 16:45:22 -0700 | [diff] [blame] | 856 | static int gralloc_lock_ycbcr(gralloc_module_t const* module, |
| 857 | buffer_handle_t handle, int usage, |
| 858 | int l, int t, int w, int h, |
| 859 | android_ycbcr *ycbcr) |
| 860 | { |
| 861 | // Not supporting fallback module for YCbCr |
| 862 | if (sFallback != NULL) { |
| 863 | return -EINVAL; |
| 864 | } |
| 865 | |
| 866 | if (!ycbcr) { |
| 867 | ALOGE("gralloc_lock_ycbcr got NULL ycbcr struct"); |
| 868 | return -EINVAL; |
| 869 | } |
| 870 | |
| 871 | private_module_t *gr = (private_module_t *)module; |
| 872 | cb_handle_t *cb = (cb_handle_t *)handle; |
| 873 | if (!gr || !cb_handle_t::validate(cb)) { |
| 874 | ALOGE("gralloc_lock_ycbcr bad handle\n"); |
| 875 | return -EINVAL; |
| 876 | } |
| 877 | |
| 878 | if (cb->frameworkFormat != HAL_PIXEL_FORMAT_YCbCr_420_888) { |
| 879 | ALOGE("gralloc_lock_ycbcr can only be used with " |
| 880 | "HAL_PIXEL_FORMAT_YCbCr_420_888, got %x instead", |
| 881 | cb->frameworkFormat); |
| 882 | return -EINVAL; |
| 883 | } |
| 884 | |
| 885 | // Validate usage |
| 886 | // For now, only allow camera write, software read. |
| 887 | bool sw_read = (0 != (usage & GRALLOC_USAGE_SW_READ_MASK)); |
| 888 | bool hw_cam_write = (usage & GRALLOC_USAGE_HW_CAMERA_WRITE); |
| 889 | bool sw_read_allowed = (0 != (cb->usage & GRALLOC_USAGE_SW_READ_MASK)); |
| 890 | |
| 891 | if ( (!hw_cam_write && !sw_read) || |
| 892 | (sw_read && !sw_read_allowed) ) { |
| 893 | ALOGE("gralloc_lock_ycbcr usage mismatch usage:0x%x cb->usage:0x%x\n", |
| 894 | usage, cb->usage); |
| 895 | return -EINVAL; |
| 896 | } |
| 897 | |
| 898 | // Make sure memory is mapped, get address |
| 899 | if (cb->ashmemBasePid != getpid() || !cb->ashmemBase) { |
| 900 | return -EACCES; |
| 901 | } |
| 902 | |
| 903 | uint8_t *cpu_addr = NULL; |
| 904 | |
| 905 | if (cb->canBePosted()) { |
| 906 | cpu_addr = (uint8_t *)(cb->ashmemBase + sizeof(int)); |
| 907 | } |
| 908 | else { |
| 909 | cpu_addr = (uint8_t *)(cb->ashmemBase); |
| 910 | } |
| 911 | |
| 912 | // Calculate offsets to underlying YUV data |
| 913 | size_t yStride; |
| 914 | size_t cStride; |
| 915 | size_t yOffset; |
| 916 | size_t uOffset; |
| 917 | size_t vOffset; |
| 918 | size_t cStep; |
| 919 | switch (cb->format) { |
| 920 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| 921 | yStride = cb->width; |
| 922 | cStride = cb->width; |
| 923 | yOffset = 0; |
| 924 | vOffset = yStride * cb->height; |
| 925 | uOffset = vOffset + 1; |
| 926 | cStep = 2; |
| 927 | break; |
| 928 | default: |
| 929 | ALOGE("gralloc_lock_ycbcr unexpected internal format %x", |
| 930 | cb->format); |
| 931 | return -EINVAL; |
| 932 | } |
| 933 | |
| 934 | ycbcr->y = cpu_addr + yOffset; |
| 935 | ycbcr->cb = cpu_addr + uOffset; |
| 936 | ycbcr->cr = cpu_addr + vOffset; |
| 937 | ycbcr->ystride = yStride; |
| 938 | ycbcr->cstride = cStride; |
| 939 | ycbcr->chroma_step = cStep; |
| 940 | |
| 941 | // Zero out reserved fields |
| 942 | memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved)); |
| 943 | |
| 944 | // |
| 945 | // Keep locked region if locked for s/w write access. |
| 946 | // |
| 947 | cb->lockedLeft = l; |
| 948 | cb->lockedTop = t; |
| 949 | cb->lockedWidth = w; |
| 950 | cb->lockedHeight = h; |
| 951 | |
| 952 | DD("gralloc_lock_ycbcr success. usage: %x, ycbcr.y: %p, .cb: %p, .cr: %p, " |
| 953 | ".ystride: %d , .cstride: %d, .chroma_step: %d", usage, |
| 954 | ycbcr->y, ycbcr->cb, ycbcr->cr, ycbcr->ystride, ycbcr->cstride, |
| 955 | ycbcr->chroma_step); |
| 956 | |
| 957 | return 0; |
| 958 | } |
Yahan Zhou | 10c7682 | 2016-06-20 12:28:34 -0700 | [diff] [blame] | 959 | #endif // PLATFORM_SDK_VERSION >= 18 |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 960 | |
| 961 | static int gralloc_device_open(const hw_module_t* module, |
| 962 | const char* name, |
| 963 | hw_device_t** device) |
| 964 | { |
| 965 | int status = -EINVAL; |
| 966 | |
| 967 | D("gralloc_device_open %s\n", name); |
| 968 | |
| 969 | pthread_once( &sFallbackOnce, fallback_init ); |
| 970 | if (sFallback != NULL) { |
| 971 | return sFallback->common.methods->open(&sFallback->common, name, device); |
| 972 | } |
| 973 | |
| 974 | if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) { |
| 975 | |
| 976 | // Create host connection and keep it in the TLS. |
| 977 | // return error if connection with host can not be established |
| 978 | HostConnection *hostCon = HostConnection::get(); |
| 979 | if (!hostCon) { |
| 980 | ALOGE("gralloc: failed to get host connection while opening %s\n", name); |
| 981 | return -EIO; |
| 982 | } |
| 983 | |
| 984 | // |
| 985 | // Allocate memory for the gralloc device (alloc interface) |
| 986 | // |
| 987 | gralloc_device_t *dev; |
| 988 | dev = (gralloc_device_t*)malloc(sizeof(gralloc_device_t)); |
| 989 | if (NULL == dev) { |
| 990 | return -ENOMEM; |
| 991 | } |
| 992 | |
| 993 | // Initialize our device structure |
| 994 | // |
| 995 | dev->device.common.tag = HARDWARE_DEVICE_TAG; |
| 996 | dev->device.common.version = 0; |
| 997 | dev->device.common.module = const_cast<hw_module_t*>(module); |
| 998 | dev->device.common.close = gralloc_device_close; |
| 999 | |
| 1000 | dev->device.alloc = gralloc_alloc; |
| 1001 | dev->device.free = gralloc_free; |
| 1002 | dev->allocListHead = NULL; |
| 1003 | pthread_mutex_init(&dev->lock, NULL); |
| 1004 | |
| 1005 | *device = &dev->device.common; |
| 1006 | status = 0; |
| 1007 | } |
| 1008 | else if (!strcmp(name, GRALLOC_HARDWARE_FB0)) { |
| 1009 | |
| 1010 | // return error if connection with host can not be established |
| 1011 | DEFINE_AND_VALIDATE_HOST_CONNECTION; |
| 1012 | |
| 1013 | // |
| 1014 | // Query the host for Framebuffer attributes |
| 1015 | // |
| 1016 | D("gralloc: query Frabuffer attribs\n"); |
| 1017 | EGLint width = rcEnc->rcGetFBParam(rcEnc, FB_WIDTH); |
| 1018 | D("gralloc: width=%d\n", width); |
| 1019 | EGLint height = rcEnc->rcGetFBParam(rcEnc, FB_HEIGHT); |
| 1020 | D("gralloc: height=%d\n", height); |
| 1021 | EGLint xdpi = rcEnc->rcGetFBParam(rcEnc, FB_XDPI); |
| 1022 | D("gralloc: xdpi=%d\n", xdpi); |
| 1023 | EGLint ydpi = rcEnc->rcGetFBParam(rcEnc, FB_YDPI); |
| 1024 | D("gralloc: ydpi=%d\n", ydpi); |
| 1025 | EGLint fps = rcEnc->rcGetFBParam(rcEnc, FB_FPS); |
| 1026 | D("gralloc: fps=%d\n", fps); |
| 1027 | EGLint min_si = rcEnc->rcGetFBParam(rcEnc, FB_MIN_SWAP_INTERVAL); |
| 1028 | D("gralloc: min_swap=%d\n", min_si); |
| 1029 | EGLint max_si = rcEnc->rcGetFBParam(rcEnc, FB_MAX_SWAP_INTERVAL); |
| 1030 | D("gralloc: max_swap=%d\n", max_si); |
| 1031 | |
| 1032 | // |
| 1033 | // Allocate memory for the framebuffer device |
| 1034 | // |
| 1035 | fb_device_t *dev; |
| 1036 | dev = (fb_device_t*)malloc(sizeof(fb_device_t)); |
| 1037 | if (NULL == dev) { |
| 1038 | return -ENOMEM; |
| 1039 | } |
| 1040 | memset(dev, 0, sizeof(fb_device_t)); |
| 1041 | |
| 1042 | // Initialize our device structure |
| 1043 | // |
| 1044 | dev->device.common.tag = HARDWARE_DEVICE_TAG; |
| 1045 | dev->device.common.version = 0; |
| 1046 | dev->device.common.module = const_cast<hw_module_t*>(module); |
| 1047 | dev->device.common.close = fb_close; |
| 1048 | dev->device.setSwapInterval = fb_setSwapInterval; |
| 1049 | dev->device.post = fb_post; |
| 1050 | dev->device.setUpdateRect = 0; //fb_setUpdateRect; |
| 1051 | dev->device.compositionComplete = fb_compositionComplete; //XXX: this is a dummy |
| 1052 | |
| 1053 | const_cast<uint32_t&>(dev->device.flags) = 0; |
| 1054 | const_cast<uint32_t&>(dev->device.width) = width; |
| 1055 | const_cast<uint32_t&>(dev->device.height) = height; |
| 1056 | const_cast<int&>(dev->device.stride) = width; |
| 1057 | const_cast<int&>(dev->device.format) = HAL_PIXEL_FORMAT_RGBA_8888; |
| 1058 | const_cast<float&>(dev->device.xdpi) = xdpi; |
| 1059 | const_cast<float&>(dev->device.ydpi) = ydpi; |
| 1060 | const_cast<float&>(dev->device.fps) = fps; |
| 1061 | const_cast<int&>(dev->device.minSwapInterval) = min_si; |
| 1062 | const_cast<int&>(dev->device.maxSwapInterval) = max_si; |
| 1063 | *device = &dev->device.common; |
| 1064 | |
| 1065 | status = 0; |
| 1066 | } |
| 1067 | |
| 1068 | return status; |
| 1069 | } |
| 1070 | |
| 1071 | // |
| 1072 | // define the HMI symbol - our module interface |
| 1073 | // |
| 1074 | static struct hw_module_methods_t gralloc_module_methods = { |
| 1075 | open: gralloc_device_open |
| 1076 | }; |
| 1077 | |
| 1078 | struct private_module_t HAL_MODULE_INFO_SYM = { |
| 1079 | base: { |
| 1080 | common: { |
| 1081 | tag: HARDWARE_MODULE_TAG, |
Yahan Zhou | 10c7682 | 2016-06-20 12:28:34 -0700 | [diff] [blame] | 1082 | #if PLATFORM_SDK_VERSION >= 18 |
Eino-Ville Talvala | a600078 | 2013-05-04 16:45:22 -0700 | [diff] [blame] | 1083 | module_api_version: GRALLOC_MODULE_API_VERSION_0_2, |
| 1084 | hal_api_version: 0, |
Yahan Zhou | da1c76d | 2016-06-23 13:58:09 -0700 | [diff] [blame] | 1085 | #elif PLATFORM_SDK_VERSION >= 16 |
| 1086 | module_api_version: 1, |
| 1087 | hal_api_version: 0, |
| 1088 | #else // PLATFORM_SDK_VERSION |
| 1089 | version_major: 1, |
| 1090 | version_minor: 0, |
| 1091 | #endif // PLATFORM_SDK_VERSION |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 1092 | id: GRALLOC_HARDWARE_MODULE_ID, |
| 1093 | name: "Graphics Memory Allocator Module", |
| 1094 | author: "The Android Open Source Project", |
| 1095 | methods: &gralloc_module_methods, |
| 1096 | dso: NULL, |
| 1097 | reserved: {0, } |
| 1098 | }, |
| 1099 | registerBuffer: gralloc_register_buffer, |
| 1100 | unregisterBuffer: gralloc_unregister_buffer, |
| 1101 | lock: gralloc_lock, |
| 1102 | unlock: gralloc_unlock, |
Eino-Ville Talvala | a600078 | 2013-05-04 16:45:22 -0700 | [diff] [blame] | 1103 | perform: NULL, |
Yahan Zhou | 10c7682 | 2016-06-20 12:28:34 -0700 | [diff] [blame] | 1104 | #if PLATFORM_SDK_VERSION >= 18 |
Eino-Ville Talvala | a600078 | 2013-05-04 16:45:22 -0700 | [diff] [blame] | 1105 | lock_ycbcr: gralloc_lock_ycbcr, |
Yahan Zhou | 10c7682 | 2016-06-20 12:28:34 -0700 | [diff] [blame] | 1106 | #endif // PLATFORM_SDK_VERSION >= 18 |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 1107 | } |
| 1108 | }; |
| 1109 | |
| 1110 | /* This function is called once to detect whether the emulator supports |
| 1111 | * GPU emulation (this is done by looking at the qemu.gles kernel |
Nicolas Capens | 2000774 | 2015-11-03 12:24:12 -0500 | [diff] [blame] | 1112 | * parameter, which must be == 1 if this is the case). |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 1113 | * |
| 1114 | * If not, then load gralloc.default instead as a fallback. |
| 1115 | */ |
| 1116 | static void |
| 1117 | fallback_init(void) |
| 1118 | { |
| 1119 | char prop[PROPERTY_VALUE_MAX]; |
| 1120 | void* module; |
| 1121 | |
Nicolas Capens | 2000774 | 2015-11-03 12:24:12 -0500 | [diff] [blame] | 1122 | // qemu.gles=0 -> no GLES 2.x support (only 1.x through software). |
| 1123 | // qemu.gles=1 -> host-side GPU emulation through EmuGL |
| 1124 | // qemu.gles=2 -> guest-side GPU emulation. |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 1125 | property_get("ro.kernel.qemu.gles", prop, "0"); |
Nicolas Capens | 2000774 | 2015-11-03 12:24:12 -0500 | [diff] [blame] | 1126 | if (atoi(prop) == 1) { |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 1127 | return; |
| 1128 | } |
Nicolas Capens | 2000774 | 2015-11-03 12:24:12 -0500 | [diff] [blame] | 1129 | ALOGD("Emulator without host-side GPU emulation detected."); |
Tina Zhang | c950e0d | 2014-04-18 14:21:23 +0800 | [diff] [blame] | 1130 | #if __LP64__ |
| 1131 | module = dlopen("/system/lib64/hw/gralloc.default.so", RTLD_LAZY|RTLD_LOCAL); |
| 1132 | #else |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 1133 | module = dlopen("/system/lib/hw/gralloc.default.so", RTLD_LAZY|RTLD_LOCAL); |
Tina Zhang | c950e0d | 2014-04-18 14:21:23 +0800 | [diff] [blame] | 1134 | #endif |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 1135 | if (module != NULL) { |
| 1136 | sFallback = reinterpret_cast<gralloc_module_t*>(dlsym(module, HAL_MODULE_INFO_SYM_AS_STR)); |
| 1137 | if (sFallback == NULL) { |
| 1138 | dlclose(module); |
| 1139 | } |
| 1140 | } |
| 1141 | if (sFallback == NULL) { |
| 1142 | ALOGE("Could not find software fallback module!?"); |
| 1143 | } |
| 1144 | } |
Yahan Zhou | c89acb2 | 2016-07-11 11:51:43 -0700 | [diff] [blame^] | 1145 | |
| 1146 | // The host associates color buffers with PID for memory cleanup. |
| 1147 | // It will fallback to the default path if host does not support it. |
| 1148 | // We discussed PID reuse issue when designing this component, but we considered |
| 1149 | // it not to be an issue because as new processes fork in, PIDs will increase to |
| 1150 | // a system-dependent limit and then wrap around. |
| 1151 | static void gralloc_proc_init() { |
| 1152 | sGrallocProcPipe = qemu_pipe_open("grallocPipe"); |
| 1153 | if (sGrallocProcPipe < 0) { |
| 1154 | sGrallocProcPipe = 0; |
| 1155 | ALOGW("Gralloc pipe failed"); |
| 1156 | return; |
| 1157 | } |
| 1158 | int32_t pid = static_cast<int32_t>(getpid()); |
| 1159 | ssize_t stat = 0; |
| 1160 | do { |
| 1161 | stat = ::write(sGrallocProcPipe, (const char*)&pid, sizeof(pid)); |
| 1162 | } while (stat < 0 && errno == EINTR); |
| 1163 | |
| 1164 | if (stat != 4) { // failed |
| 1165 | close(sGrallocProcPipe); |
| 1166 | sGrallocProcPipe = 0; |
| 1167 | ALOGW("Gralloc pipe failed"); |
| 1168 | return; |
| 1169 | } |
| 1170 | |
| 1171 | do { |
| 1172 | stat = ::read(sGrallocProcPipe, (char*)&sGrallocProcID, |
| 1173 | sizeof(sGrallocProcID)); |
| 1174 | } while (stat < 0 && errno == EINTR); |
| 1175 | |
| 1176 | if (stat != 8) { |
| 1177 | close(sGrallocProcPipe); |
| 1178 | sGrallocProcPipe = 0; |
| 1179 | ALOGW("Gralloc pipe failed"); |
| 1180 | return; |
| 1181 | } |
| 1182 | } |