blob: 19bf093ce8457e4746847a2550e9da6b71c9abb6 [file] [log] [blame]
Arun Kumar K.R2b75da32016-11-11 14:37:20 -08001/*
Rohit Kulkarnibf0829f2018-02-09 11:48:19 -08002 * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
Arun Kumar K.R2b75da32016-11-11 14:37:20 -08003 * 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.R4a8b1182017-01-26 19:44:33 -080024#include <fcntl.h>
Varun Arora5dd61e02018-02-23 12:25:01 -080025#include <string>
26#include <map>
27#include <utility>
Arun Kumar K.R2b75da32016-11-11 14:37:20 -080028
Varun Arora5dd61e02018-02-23 12:25:01 -080029using std::string;
30using std::map;
31using std::pair;
32
33static string pidString = std::to_string(getpid());
34
35#ifndef TARGET_ION_ABI_VERSION
Arun Kumar K.R2b75da32016-11-11 14:37:20 -080036//-----------------------------------------------------------------------------
Varun Arora5dd61e02018-02-23 12:25:01 -080037static void free_ion_cookie(int ion_fd, int cookie)
Arun Kumar K.R2b75da32016-11-11 14:37:20 -080038//-----------------------------------------------------------------------------
39{
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -080040 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 Arora5dd61e02018-02-23 12:25:01 -080044}
45
46//-----------------------------------------------------------------------------
47static 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 Kulkarnibf0829f2018-02-09 11:48:19 -080064#else
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -080065//-----------------------------------------------------------------------------
Varun Arora5dd61e02018-02-23 12:25:01 -080066static string get_ion_buff_str(int buff_fd)
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -080067//-----------------------------------------------------------------------------
68{
Varun Arora5dd61e02018-02-23 12:25:01 -080069 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.R4a8b1182017-01-26 19:44:33 -080078 }
Varun Arora5dd61e02018-02-23 12:25:01 -080079 }
80
81 return retStr;
82}
83#endif
84
85//-----------------------------------------------------------------------------
86void 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.R4a8b1182017-01-26 19:44:33 -0800105}
106
107//-----------------------------------------------------------------------------
108EGLImageWrapper::EGLImageWrapper()
109//-----------------------------------------------------------------------------
110{
Varun Arora5dd61e02018-02-23 12:25:01 -0800111 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.R4a8b1182017-01-26 19:44:33 -0800119}
120
121//-----------------------------------------------------------------------------
122EGLImageWrapper::~EGLImageWrapper()
123//-----------------------------------------------------------------------------
124{
Varun Arora5dd61e02018-02-23 12:25:01 -0800125 if (eglImageBufferCache != 0) {
126 if (callback != 0) {
127 callback->mapClearPending = true;
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -0800128 }
Varun Arora5dd61e02018-02-23 12:25:01 -0800129 eglImageBufferCache->clear();
130 delete eglImageBufferCache;
131 eglImageBufferCache = 0;
132 buffStrbuffIntMap.clear();
133 }
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -0800134
Varun Arora5dd61e02018-02-23 12:25:01 -0800135 if (callback != 0) {
136 delete callback;
137 callback = 0;
138 }
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -0800139
Varun Arora5dd61e02018-02-23 12:25:01 -0800140#ifndef TARGET_ION_ABI_VERSION
141 if (ion_fd > 0) {
142 close(ion_fd);
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -0800143 ion_fd = -1;
Varun Arora5dd61e02018-02-23 12:25:01 -0800144 }
145#endif
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -0800146}
Varun Arora5dd61e02018-02-23 12:25:01 -0800147
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -0800148//-----------------------------------------------------------------------------
149static EGLImageBuffer* L_wrap(const private_handle_t *src)
150//-----------------------------------------------------------------------------
151{
Varun Arora5dd61e02018-02-23 12:25:01 -0800152 EGLImageBuffer* result = 0;
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -0800153
Varun Arora5dd61e02018-02-23 12:25:01 -0800154 native_handle_t *native_handle = const_cast<private_handle_t *>(src);
Arun Kumar K.R2b75da32016-11-11 14:37:20 -0800155
Varun Arora5dd61e02018-02-23 12:25:01 -0800156 int flags = android::GraphicBuffer::USAGE_HW_TEXTURE |
157 android::GraphicBuffer::USAGE_SW_READ_NEVER |
158 android::GraphicBuffer::USAGE_SW_WRITE_NEVER;
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -0800159
Varun Arora5dd61e02018-02-23 12:25:01 -0800160 if (src->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) {
161 flags |= android::GraphicBuffer::USAGE_PROTECTED;
162 }
Arun Kumar K.R2b75da32016-11-11 14:37:20 -0800163
Varun Arora5dd61e02018-02-23 12:25:01 -0800164 android::sp<android::GraphicBuffer> graphicBuffer =
165 new android::GraphicBuffer(src->unaligned_width, src->unaligned_height, src->format,
Naseer Ahmedfeaed062017-01-17 15:34:02 -0500166#ifndef __NOUGAT__
Varun Arora5dd61e02018-02-23 12:25:01 -0800167 1, // Layer count
Naseer Ahmedfeaed062017-01-17 15:34:02 -0500168#endif
Varun Arora5dd61e02018-02-23 12:25:01 -0800169 flags, src->width /*src->stride*/,
170 native_handle, false);
Arun Kumar K.R2b75da32016-11-11 14:37:20 -0800171
Varun Arora5dd61e02018-02-23 12:25:01 -0800172 result = new EGLImageBuffer(graphicBuffer);
Arun Kumar K.R2b75da32016-11-11 14:37:20 -0800173
Varun Arora5dd61e02018-02-23 12:25:01 -0800174 return result;
Arun Kumar K.R2b75da32016-11-11 14:37:20 -0800175}
176
177//-----------------------------------------------------------------------------
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -0800178EGLImageBuffer *EGLImageWrapper::wrap(const void *pvt_handle)
Arun Kumar K.R2b75da32016-11-11 14:37:20 -0800179//-----------------------------------------------------------------------------
180{
Varun Arora5dd61e02018-02-23 12:25:01 -0800181 const private_handle_t *src = static_cast<const private_handle_t *>(pvt_handle);
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -0800182
Varun Arora5dd61e02018-02-23 12:25:01 -0800183#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.R4a8b1182017-01-26 19:44:33 -0800201 eglImage = L_wrap(src);
Varun Arora5dd61e02018-02-23 12:25:01 -0800202 buffStrbuffIntMap.insert(pair<string, int>(buffStr, buffInt));
203 eglImageBufferCache->put(buffInt, eglImage);
204 buffInt++;
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -0800205 }
Varun Arora5dd61e02018-02-23 12:25:01 -0800206 } else {
207 ALOGE("Could not provide an eglImage for fd = %d, EGLImageWrapper = %p", src->fd, this);
208 }
209#endif
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -0800210
Varun Arora5dd61e02018-02-23 12:25:01 -0800211 return eglImage;
Sushil Chauhan1cc416f2017-01-11 18:09:02 -0800212}