keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | |
| 17 | #ifndef __GRALLOC_CB_H__ |
| 18 | #define __GRALLOC_CB_H__ |
| 19 | |
| 20 | #include <hardware/hardware.h> |
| 21 | #include <hardware/gralloc.h> |
| 22 | #include <cutils/native_handle.h> |
| 23 | |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 24 | #include "goldfish_dma.h" |
Weilun Du | 8b38e8b | 2018-10-03 10:25:21 -0700 | [diff] [blame^] | 25 | #include "qemu_pipe.h" |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 26 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 27 | #define BUFFER_HANDLE_MAGIC ((int)0xabfabfab) |
| 28 | #define CB_HANDLE_NUM_INTS(nfds) (int)((sizeof(cb_handle_t) - (nfds)*sizeof(int)) / sizeof(int)) |
| 29 | |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 30 | // Tell the emulator which gralloc formats |
| 31 | // need special handling. |
| 32 | enum EmulatorFrameworkFormat { |
| 33 | FRAMEWORK_FORMAT_GL_COMPATIBLE = 0, |
| 34 | FRAMEWORK_FORMAT_YV12 = 1, |
| 35 | FRAMEWORK_FORMAT_YUV_420_888 = 2, |
| 36 | }; |
| 37 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 38 | // |
| 39 | // Our buffer handle structure |
| 40 | // |
| 41 | struct cb_handle_t : public native_handle { |
| 42 | |
| 43 | cb_handle_t(int p_fd, int p_ashmemSize, int p_usage, |
Eino-Ville Talvala | a600078 | 2013-05-04 16:45:22 -0700 | [diff] [blame] | 44 | int p_width, int p_height, int p_frameworkFormat, |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 45 | int p_format, int p_glFormat, int p_glType, |
| 46 | EmulatorFrameworkFormat p_emuFrameworkFormat) : |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 47 | fd(p_fd), |
| 48 | magic(BUFFER_HANDLE_MAGIC), |
| 49 | usage(p_usage), |
| 50 | width(p_width), |
| 51 | height(p_height), |
Eino-Ville Talvala | a600078 | 2013-05-04 16:45:22 -0700 | [diff] [blame] | 52 | frameworkFormat(p_frameworkFormat), |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 53 | format(p_format), |
| 54 | glFormat(p_glFormat), |
| 55 | glType(p_glType), |
| 56 | ashmemSize(p_ashmemSize), |
| 57 | ashmemBase(0), |
| 58 | ashmemBasePid(0), |
| 59 | mappedPid(0), |
| 60 | lockedLeft(0), |
| 61 | lockedTop(0), |
| 62 | lockedWidth(0), |
| 63 | lockedHeight(0), |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 64 | hostHandle(0), |
| 65 | emuFrameworkFormat(p_emuFrameworkFormat) |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 66 | { |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 67 | goldfish_dma.fd = -1; |
Weilun Du | 8b38e8b | 2018-10-03 10:25:21 -0700 | [diff] [blame^] | 68 | refcount_pipe_fd = QEMU_PIPE_INVALID_HANDLE; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 69 | version = sizeof(native_handle); |
| 70 | numFds = 0; |
| 71 | numInts = CB_HANDLE_NUM_INTS(numFds); |
| 72 | } |
| 73 | |
| 74 | ~cb_handle_t() { |
| 75 | magic = 0; |
| 76 | } |
| 77 | |
| 78 | void setFd(int p_fd) { |
| 79 | if (p_fd >= 0) { |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 80 | numFds++; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 81 | } |
| 82 | fd = p_fd; |
| 83 | numInts = CB_HANDLE_NUM_INTS(numFds); |
| 84 | } |
| 85 | |
Weilun Du | 8b38e8b | 2018-10-03 10:25:21 -0700 | [diff] [blame^] | 86 | bool hasRefcountPipe() { |
| 87 | return qemu_pipe_valid(refcount_pipe_fd); |
| 88 | } |
| 89 | |
| 90 | void setRefcountPipeFd(QEMU_PIPE_HANDLE fd) { |
| 91 | if (qemu_pipe_valid(fd)) { |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 92 | numFds++; |
| 93 | } |
Weilun Du | 8b38e8b | 2018-10-03 10:25:21 -0700 | [diff] [blame^] | 94 | refcount_pipe_fd = fd; |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 95 | numInts = CB_HANDLE_NUM_INTS(numFds); |
| 96 | } |
| 97 | |
Alex Vallée | 0c7d275 | 2014-09-24 14:05:22 -0400 | [diff] [blame] | 98 | static bool validate(const cb_handle_t* hnd) { |
| 99 | return (hnd && |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 100 | hnd->version == sizeof(native_handle) && |
| 101 | hnd->magic == BUFFER_HANDLE_MAGIC && |
| 102 | hnd->numInts == CB_HANDLE_NUM_INTS(hnd->numFds)); |
| 103 | } |
| 104 | |
| 105 | bool canBePosted() { |
| 106 | return (0 != (usage & GRALLOC_USAGE_HW_FB)); |
| 107 | } |
| 108 | |
| 109 | // file-descriptors |
| 110 | int fd; // ashmem fd (-1 of ashmem region did not allocated, i.e. no SW access needed) |
Weilun Du | 8b38e8b | 2018-10-03 10:25:21 -0700 | [diff] [blame^] | 111 | QEMU_PIPE_HANDLE refcount_pipe_fd; // goldfish pipe service for gralloc refcounting fd. |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 112 | |
| 113 | // ints |
| 114 | int magic; // magic number in order to validate a pointer to be a cb_handle_t |
| 115 | int usage; // usage bits the buffer was created with |
| 116 | int width; // buffer width |
| 117 | int height; // buffer height |
Eino-Ville Talvala | a600078 | 2013-05-04 16:45:22 -0700 | [diff] [blame] | 118 | int frameworkFormat; // format requested by the Android framework |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 119 | int format; // real internal pixel format format |
| 120 | int glFormat; // OpenGL format enum used for host h/w color buffer |
| 121 | int glType; // OpenGL type enum used when uploading to host |
| 122 | int ashmemSize; // ashmem region size for the buffer (0 unless is HW_FB buffer or |
| 123 | // s/w access is needed) |
Tina Zhang | 82bacbd | 2014-05-26 14:03:48 +0800 | [diff] [blame] | 124 | union { |
| 125 | intptr_t ashmemBase; // CPU address of the mapped ashmem region |
| 126 | uint64_t padding; // enforce same size on 32-bit/64-bit |
| 127 | } __attribute__((aligned(8))); |
| 128 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 129 | int ashmemBasePid; // process id which mapped the ashmem region |
| 130 | int mappedPid; // process id which succeeded gralloc_register call |
| 131 | int lockedLeft; // region of buffer locked for s/w write |
| 132 | int lockedTop; |
| 133 | int lockedWidth; |
| 134 | int lockedHeight; |
| 135 | uint32_t hostHandle; |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 136 | |
| 137 | goldfish_dma_context goldfish_dma; |
| 138 | uint32_t goldfish_dma_buf_size; |
| 139 | EmulatorFrameworkFormat emuFrameworkFormat; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | |
| 143 | #endif //__GRALLOC_CB_H__ |