1 /*
2  * Copyright (c) 2016-2020, The Linux Foundation. All rights reserved.
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 <qdMetaData.h>
24 #include <ui/GraphicBuffer.h>
25 #include <fcntl.h>
26 #include <string>
27 #include <map>
28 #include <utility>
29 
30 using std::string;
31 using std::map;
32 using std::pair;
33 
34 static string pidString = std::to_string(getpid());
35 
36 //-----------------------------------------------------------------------------
get_ion_buff_str(int buff_fd)37 static string get_ion_buff_str(int buff_fd)
38 //-----------------------------------------------------------------------------
39 {
40   string retStr = {};
41   if (buff_fd >= 0) {
42     string fdString = std::to_string(buff_fd);
43     string symlinkPath = "/proc/"+pidString+"/fd/"+fdString;
44     char buffer[1024] = {};
45     ssize_t ret = ::readlink(symlinkPath.c_str(), buffer, sizeof(buffer) - 1);
46     if (ret != -1) {
47       buffer[ret] = '\0';
48       retStr = buffer;
49     }
50   }
51 
52   return retStr;
53 }
54 
55 //-----------------------------------------------------------------------------
operator ()(int & buffInt,EGLImageBuffer * & eglImage)56 void EGLImageWrapper::DeleteEGLImageCallback::operator()(int& buffInt, EGLImageBuffer*& eglImage)
57 //-----------------------------------------------------------------------------
58 {
59   if (eglImage != 0) {
60     delete eglImage;
61   }
62 
63   if (!mapClearPending) {
64     for (auto it = buffStrbuffIntMapPtr->begin(); it != buffStrbuffIntMapPtr->end(); it++) {
65       if (it->second == buffInt /* counter */) {
66         buffStrbuffIntMapPtr->erase(it);
67         return;
68       }
69     }
70   }
71 }
72 
73 //-----------------------------------------------------------------------------
EGLImageWrapper()74 EGLImageWrapper::EGLImageWrapper()
75 //-----------------------------------------------------------------------------
76 {
77   Init();
78 }
79 
80 //-----------------------------------------------------------------------------
~EGLImageWrapper()81 EGLImageWrapper::~EGLImageWrapper()
82 //-----------------------------------------------------------------------------
83 {
84   Deinit();
85 }
86 
87 //-----------------------------------------------------------------------------
Init()88 void EGLImageWrapper::Init()
89 //-----------------------------------------------------------------------------
90 {
91   eglImageBufferCache = new android::LruCache<int, EGLImageBuffer*>(32);
92   callback = new DeleteEGLImageCallback(&buffStrbuffIntMap);
93   eglImageBufferCache->setOnEntryRemovedListener(callback);
94 }
95 
96 //-----------------------------------------------------------------------------
Deinit()97 void EGLImageWrapper::Deinit()
98 //-----------------------------------------------------------------------------
99 {
100   if (eglImageBufferCache != 0) {
101     if (callback != 0) {
102       callback->mapClearPending = true;
103     }
104     eglImageBufferCache->clear();
105     delete eglImageBufferCache;
106     eglImageBufferCache = 0;
107     buffStrbuffIntMap.clear();
108   }
109 
110   if (callback != 0) {
111     delete callback;
112     callback = 0;
113   }
114 
115 }
116 
117 //-----------------------------------------------------------------------------
L_wrap(const private_handle_t * src)118 static EGLImageBuffer* L_wrap(const private_handle_t *src)
119 //-----------------------------------------------------------------------------
120 {
121   EGLImageBuffer* result = 0;
122 
123   uint32_t unaligned_width = src->unaligned_width;
124   uint32_t unaligned_height = src->unaligned_height;
125   uint32_t stride = src->width;
126   native_handle_t *native_handle = const_cast<private_handle_t *>(src);
127 
128   BufferDim_t custom_dim;
129   if(!getMetaData(const_cast<private_handle_t *>(src), GET_BUFFER_GEOMETRY, &custom_dim)) {
130     unaligned_width = custom_dim.sliceWidth;
131     unaligned_height = custom_dim.sliceHeight;
132     uint32_t aligned_height = 0;
133     gralloc::BufferInfo info(unaligned_width, unaligned_height, src->format, src->usage);
134     gralloc::GetAlignedWidthAndHeight(info, &stride, &aligned_height);
135   }
136 
137   int flags = android::GraphicBuffer::USAGE_HW_TEXTURE |
138               android::GraphicBuffer::USAGE_SW_READ_NEVER |
139               android::GraphicBuffer::USAGE_SW_WRITE_NEVER;
140 
141   if (src->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) {
142     flags |= android::GraphicBuffer::USAGE_PROTECTED;
143   }
144 
145   android::sp<android::GraphicBuffer> graphicBuffer =
146     new android::GraphicBuffer(unaligned_width, unaligned_height, src->format,
147 #ifndef __NOUGAT__
148                                1,  // Layer count
149 #endif
150                                flags, stride /*src->stride*/,
151                                native_handle, false);
152 
153   result = new EGLImageBuffer(graphicBuffer);
154 
155   return result;
156 }
157 
158 //-----------------------------------------------------------------------------
wrap(const void * pvt_handle)159 EGLImageBuffer *EGLImageWrapper::wrap(const void *pvt_handle)
160 //-----------------------------------------------------------------------------
161 {
162   const private_handle_t *src = static_cast<const private_handle_t *>(pvt_handle);
163 
164   string buffStr = get_ion_buff_str(src->fd);
165   EGLImageBuffer* eglImage = nullptr;
166   if (!buffStr.empty()) {
167     auto it = buffStrbuffIntMap.find(buffStr);
168     if (it != buffStrbuffIntMap.end()) {
169       eglImage = eglImageBufferCache->get(it->second);
170     } else {
171         eglImage = L_wrap(src);
172         buffStrbuffIntMap.insert(pair<string, int>(buffStr, buffInt));
173         eglImageBufferCache->put(buffInt, eglImage);
174         buffInt++;
175     }
176   } else {
177     ALOGE("Could not provide an eglImage for fd = %d, EGLImageWrapper = %p", src->fd, this);
178   }
179 
180   return eglImage;
181 }
182