Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 1 | /* |
Rohit Kulkarni | bf0829f | 2018-02-09 11:48:19 -0800 | [diff] [blame] | 2 | * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved. |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 3 | * Not a Contribution. |
| 4 | * |
| 5 | * Copyright 2015 The Android Open Source Project |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | |
| 20 | #include "EGLImageWrapper.h" |
| 21 | #include <cutils/native_handle.h> |
| 22 | #include <gralloc_priv.h> |
| 23 | #include <ui/GraphicBuffer.h> |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 24 | #include <fcntl.h> |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 25 | #include <string> |
| 26 | #include <map> |
| 27 | #include <utility> |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 28 | |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 29 | using std::string; |
| 30 | using std::map; |
| 31 | using std::pair; |
| 32 | |
| 33 | static string pidString = std::to_string(getpid()); |
| 34 | |
| 35 | #ifndef TARGET_ION_ABI_VERSION |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 36 | //----------------------------------------------------------------------------- |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 37 | static void free_ion_cookie(int ion_fd, int cookie) |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 38 | //----------------------------------------------------------------------------- |
| 39 | { |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 40 | if (ion_fd && !ioctl(ion_fd, ION_IOC_FREE, &cookie)) { |
| 41 | } else { |
| 42 | ALOGE("ION_IOC_FREE failed: ion_fd = %d, cookie = %d", ion_fd, cookie); |
| 43 | } |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | //----------------------------------------------------------------------------- |
| 47 | static int get_ion_cookie(int ion_fd, int fd) |
| 48 | //----------------------------------------------------------------------------- |
| 49 | { |
| 50 | int cookie = fd; |
| 51 | |
| 52 | struct ion_fd_data fdData; |
| 53 | memset(&fdData, 0, sizeof(fdData)); |
| 54 | fdData.fd = fd; |
| 55 | |
| 56 | if (ion_fd && !ioctl(ion_fd, ION_IOC_IMPORT, &fdData)) { |
| 57 | cookie = fdData.handle; |
| 58 | } else { |
| 59 | ALOGE("ION_IOC_IMPORT failed: ion_fd = %d, fd = %d", ion_fd, fd); |
| 60 | } |
| 61 | |
| 62 | return cookie; |
| 63 | } |
Rohit Kulkarni | bf0829f | 2018-02-09 11:48:19 -0800 | [diff] [blame] | 64 | #else |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 65 | //----------------------------------------------------------------------------- |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 66 | static string get_ion_buff_str(int buff_fd) |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 67 | //----------------------------------------------------------------------------- |
| 68 | { |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 69 | string retStr = {}; |
| 70 | if (buff_fd >= 0) { |
| 71 | string fdString = std::to_string(buff_fd); |
| 72 | string symlinkPath = "/proc/"+pidString+"/fd/"+fdString; |
| 73 | char buffer[1024] = {}; |
| 74 | ssize_t ret = ::readlink(symlinkPath.c_str(), buffer, sizeof(buffer) - 1); |
| 75 | if (ret != -1) { |
| 76 | buffer[ret] = '\0'; |
| 77 | retStr = buffer; |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 78 | } |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | return retStr; |
| 82 | } |
| 83 | #endif |
| 84 | |
| 85 | //----------------------------------------------------------------------------- |
| 86 | void EGLImageWrapper::DeleteEGLImageCallback::operator()(int& buffInt, EGLImageBuffer*& eglImage) |
| 87 | //----------------------------------------------------------------------------- |
| 88 | { |
| 89 | if (eglImage != 0) { |
| 90 | delete eglImage; |
| 91 | } |
| 92 | |
| 93 | #ifndef TARGET_ION_ABI_VERSION |
| 94 | free_ion_cookie(ion_fd, buffInt /* cookie */); |
| 95 | #else |
| 96 | if (!mapClearPending) { |
| 97 | for (auto it = buffStrbuffIntMapPtr->begin(); it != buffStrbuffIntMapPtr->end(); it++) { |
| 98 | if (it->second == buffInt /* counter */) { |
| 99 | buffStrbuffIntMapPtr->erase(it); |
| 100 | return; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | #endif |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | //----------------------------------------------------------------------------- |
| 108 | EGLImageWrapper::EGLImageWrapper() |
| 109 | //----------------------------------------------------------------------------- |
| 110 | { |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 111 | eglImageBufferCache = new android::LruCache<int, EGLImageBuffer*>(32); |
| 112 | callback = new DeleteEGLImageCallback(&buffStrbuffIntMap); |
| 113 | eglImageBufferCache->setOnEntryRemovedListener(callback); |
| 114 | |
| 115 | #ifndef TARGET_ION_ABI_VERSION |
| 116 | ion_fd = open("/dev/ion", O_RDONLY); |
| 117 | callback->ion_fd = ion_fd; |
| 118 | #endif |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | //----------------------------------------------------------------------------- |
| 122 | EGLImageWrapper::~EGLImageWrapper() |
| 123 | //----------------------------------------------------------------------------- |
| 124 | { |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 125 | if (eglImageBufferCache != 0) { |
| 126 | if (callback != 0) { |
| 127 | callback->mapClearPending = true; |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 128 | } |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 129 | eglImageBufferCache->clear(); |
| 130 | delete eglImageBufferCache; |
| 131 | eglImageBufferCache = 0; |
| 132 | buffStrbuffIntMap.clear(); |
| 133 | } |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 134 | |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 135 | if (callback != 0) { |
| 136 | delete callback; |
| 137 | callback = 0; |
| 138 | } |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 139 | |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 140 | #ifndef TARGET_ION_ABI_VERSION |
| 141 | if (ion_fd > 0) { |
| 142 | close(ion_fd); |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 143 | ion_fd = -1; |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 144 | } |
| 145 | #endif |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 146 | } |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 147 | |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 148 | //----------------------------------------------------------------------------- |
| 149 | static EGLImageBuffer* L_wrap(const private_handle_t *src) |
| 150 | //----------------------------------------------------------------------------- |
| 151 | { |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 152 | EGLImageBuffer* result = 0; |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 153 | |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 154 | native_handle_t *native_handle = const_cast<private_handle_t *>(src); |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 155 | |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 156 | int flags = android::GraphicBuffer::USAGE_HW_TEXTURE | |
| 157 | android::GraphicBuffer::USAGE_SW_READ_NEVER | |
| 158 | android::GraphicBuffer::USAGE_SW_WRITE_NEVER; |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 159 | |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 160 | if (src->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) { |
| 161 | flags |= android::GraphicBuffer::USAGE_PROTECTED; |
| 162 | } |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 163 | |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 164 | android::sp<android::GraphicBuffer> graphicBuffer = |
| 165 | new android::GraphicBuffer(src->unaligned_width, src->unaligned_height, src->format, |
Naseer Ahmed | feaed06 | 2017-01-17 15:34:02 -0500 | [diff] [blame] | 166 | #ifndef __NOUGAT__ |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 167 | 1, // Layer count |
Naseer Ahmed | feaed06 | 2017-01-17 15:34:02 -0500 | [diff] [blame] | 168 | #endif |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 169 | flags, src->width /*src->stride*/, |
| 170 | native_handle, false); |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 171 | |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 172 | result = new EGLImageBuffer(graphicBuffer); |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 173 | |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 174 | return result; |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | //----------------------------------------------------------------------------- |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 178 | EGLImageBuffer *EGLImageWrapper::wrap(const void *pvt_handle) |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 179 | //----------------------------------------------------------------------------- |
| 180 | { |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 181 | const private_handle_t *src = static_cast<const private_handle_t *>(pvt_handle); |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 182 | |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 183 | #ifndef TARGET_ION_ABI_VERSION |
| 184 | int ion_cookie = get_ion_cookie(ion_fd, src->fd); |
| 185 | EGLImageBuffer* eglImage = nullptr; |
| 186 | eglImage = eglImageBufferCache->get(ion_cookie); |
| 187 | if (eglImage == 0) { |
| 188 | eglImage = L_wrap(src); |
| 189 | eglImageBufferCache->put(ion_cookie, eglImage); |
| 190 | } else { |
| 191 | free_ion_cookie(ion_fd, ion_cookie); |
| 192 | } |
| 193 | #else |
| 194 | string buffStr = get_ion_buff_str(src->fd); |
| 195 | EGLImageBuffer* eglImage = nullptr; |
| 196 | if (!buffStr.empty()) { |
| 197 | auto it = buffStrbuffIntMap.find(buffStr); |
| 198 | if (it != buffStrbuffIntMap.end()) { |
| 199 | eglImage = eglImageBufferCache->get(it->second); |
| 200 | } else { |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 201 | eglImage = L_wrap(src); |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 202 | buffStrbuffIntMap.insert(pair<string, int>(buffStr, buffInt)); |
| 203 | eglImageBufferCache->put(buffInt, eglImage); |
| 204 | buffInt++; |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 205 | } |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 206 | } else { |
| 207 | ALOGE("Could not provide an eglImage for fd = %d, EGLImageWrapper = %p", src->fd, this); |
| 208 | } |
| 209 | #endif |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 210 | |
Varun Arora | 5dd61e0 | 2018-02-23 12:25:01 -0800 | [diff] [blame] | 211 | return eglImage; |
Sushil Chauhan | 1cc416f | 2017-01-11 18:09:02 -0800 | [diff] [blame] | 212 | } |