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> |
| 25 | #include <linux/msm_ion.h> |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 26 | |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 27 | //----------------------------------------------------------------------------- |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 28 | void free_ion_cookie(int ion_fd, int cookie) |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 29 | //----------------------------------------------------------------------------- |
| 30 | { |
Rohit Kulkarni | bf0829f | 2018-02-09 11:48:19 -0800 | [diff] [blame^] | 31 | // TODO(user): replace with appropriate handling for new ion apis |
| 32 | #ifndef TARGET_ION_ABI_VERSION |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 33 | if (ion_fd && !ioctl(ion_fd, ION_IOC_FREE, &cookie)) { |
| 34 | } else { |
| 35 | ALOGE("ION_IOC_FREE failed: ion_fd = %d, cookie = %d", ion_fd, cookie); |
| 36 | } |
Rohit Kulkarni | bf0829f | 2018-02-09 11:48:19 -0800 | [diff] [blame^] | 37 | #else |
| 38 | (void) ion_fd; |
| 39 | (void) cookie; |
| 40 | #endif |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 41 | } |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 42 | |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 43 | //----------------------------------------------------------------------------- |
| 44 | int get_ion_cookie(int ion_fd, int fd) |
| 45 | //----------------------------------------------------------------------------- |
| 46 | { |
| 47 | int cookie = fd; |
| 48 | |
Rohit Kulkarni | bf0829f | 2018-02-09 11:48:19 -0800 | [diff] [blame^] | 49 | // TODO(user): replace with appropriate handling for new ion apis |
| 50 | #ifndef TARGET_ION_ABI_VERSION |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 51 | struct ion_fd_data fdData; |
| 52 | memset(&fdData, 0, sizeof(fdData)); |
| 53 | fdData.fd = fd; |
| 54 | |
| 55 | if (ion_fd && !ioctl(ion_fd, ION_IOC_IMPORT, &fdData)) { |
| 56 | cookie = fdData.handle; |
| 57 | } else { |
| 58 | ALOGE("ION_IOC_IMPORT failed: ion_fd = %d, fd = %d", ion_fd, fd); |
| 59 | } |
Rohit Kulkarni | bf0829f | 2018-02-09 11:48:19 -0800 | [diff] [blame^] | 60 | #else |
| 61 | (void) ion_fd; |
| 62 | #endif |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 63 | return cookie; |
| 64 | } |
| 65 | |
| 66 | //----------------------------------------------------------------------------- |
| 67 | EGLImageWrapper::DeleteEGLImageCallback::DeleteEGLImageCallback(int fd) |
| 68 | //----------------------------------------------------------------------------- |
| 69 | { |
| 70 | ion_fd = fd; |
| 71 | } |
| 72 | |
| 73 | //----------------------------------------------------------------------------- |
| 74 | void EGLImageWrapper::DeleteEGLImageCallback::operator()(int& k, EGLImageBuffer*& eglImage) |
| 75 | //----------------------------------------------------------------------------- |
| 76 | { |
| 77 | free_ion_cookie(ion_fd, k); |
| 78 | if( eglImage != 0 ) |
| 79 | { |
| 80 | delete eglImage; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | //----------------------------------------------------------------------------- |
| 85 | EGLImageWrapper::EGLImageWrapper() |
| 86 | //----------------------------------------------------------------------------- |
| 87 | { |
| 88 | eglImageBufferMap = new android::LruCache<int, EGLImageBuffer*>(32); |
| 89 | ion_fd = open("/dev/ion", O_RDONLY); |
| 90 | callback = new DeleteEGLImageCallback(ion_fd); |
| 91 | eglImageBufferMap->setOnEntryRemovedListener(callback); |
| 92 | } |
| 93 | |
| 94 | //----------------------------------------------------------------------------- |
| 95 | EGLImageWrapper::~EGLImageWrapper() |
| 96 | //----------------------------------------------------------------------------- |
| 97 | { |
| 98 | if( eglImageBufferMap != 0 ) |
| 99 | { |
| 100 | eglImageBufferMap->clear(); |
| 101 | delete eglImageBufferMap; |
| 102 | eglImageBufferMap = 0; |
| 103 | } |
| 104 | |
| 105 | if( callback != 0 ) |
| 106 | { |
| 107 | delete callback; |
| 108 | callback = 0; |
| 109 | } |
| 110 | |
| 111 | if( ion_fd > 0 ) |
| 112 | { |
| 113 | close(ion_fd); |
| 114 | } |
| 115 | ion_fd = -1; |
| 116 | } |
| 117 | //----------------------------------------------------------------------------- |
| 118 | static EGLImageBuffer* L_wrap(const private_handle_t *src) |
| 119 | //----------------------------------------------------------------------------- |
| 120 | { |
| 121 | EGLImageBuffer* result = 0; |
| 122 | |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 123 | native_handle_t *native_handle = const_cast<private_handle_t *>(src); |
| 124 | |
| 125 | int flags = android::GraphicBuffer::USAGE_HW_TEXTURE | |
| 126 | android::GraphicBuffer::USAGE_SW_READ_NEVER | |
| 127 | android::GraphicBuffer::USAGE_SW_WRITE_NEVER; |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 128 | |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 129 | if (src->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) { |
| 130 | flags |= android::GraphicBuffer::USAGE_PROTECTED; |
| 131 | } |
| 132 | |
| 133 | android::sp<android::GraphicBuffer> graphicBuffer = |
Sushil Chauhan | 815c8f6 | 2017-05-18 19:51:34 -0700 | [diff] [blame] | 134 | new android::GraphicBuffer(src->unaligned_width, src->unaligned_height, src->format, |
Naseer Ahmed | feaed06 | 2017-01-17 15:34:02 -0500 | [diff] [blame] | 135 | #ifndef __NOUGAT__ |
| 136 | 1, // Layer count |
| 137 | #endif |
| 138 | flags, src->width /*src->stride*/, |
| 139 | native_handle, false); |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 140 | |
| 141 | result = new EGLImageBuffer(graphicBuffer); |
| 142 | |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 143 | return result; |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | //----------------------------------------------------------------------------- |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 147 | EGLImageBuffer *EGLImageWrapper::wrap(const void *pvt_handle) |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 148 | //----------------------------------------------------------------------------- |
| 149 | { |
Arun Kumar K.R | 4a8b118 | 2017-01-26 19:44:33 -0800 | [diff] [blame] | 150 | const private_handle_t *src = static_cast<const private_handle_t *>(pvt_handle); |
| 151 | |
| 152 | int ion_cookie = get_ion_cookie(ion_fd, src->fd); |
| 153 | EGLImageBuffer* eglImage = eglImageBufferMap->get(ion_cookie); |
| 154 | if( eglImage == 0 ) |
| 155 | { |
| 156 | eglImage = L_wrap(src); |
| 157 | eglImageBufferMap->put(ion_cookie, eglImage); |
| 158 | } |
| 159 | else { |
| 160 | free_ion_cookie(ion_fd, ion_cookie); |
| 161 | } |
| 162 | |
| 163 | return eglImage; |
Sushil Chauhan | 1cc416f | 2017-01-11 18:09:02 -0800 | [diff] [blame] | 164 | } |