blob: a60b91e3a2f9ace0c411931eccb6ac88319cb59a [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>
25#include <linux/msm_ion.h>
Arun Kumar K.R2b75da32016-11-11 14:37:20 -080026
Arun Kumar K.R2b75da32016-11-11 14:37:20 -080027//-----------------------------------------------------------------------------
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -080028void free_ion_cookie(int ion_fd, int cookie)
Arun Kumar K.R2b75da32016-11-11 14:37:20 -080029//-----------------------------------------------------------------------------
30{
Rohit Kulkarnibf0829f2018-02-09 11:48:19 -080031 // TODO(user): replace with appropriate handling for new ion apis
32#ifndef TARGET_ION_ABI_VERSION
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -080033 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 Kulkarnibf0829f2018-02-09 11:48:19 -080037#else
38 (void) ion_fd;
39 (void) cookie;
40#endif
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -080041}
Arun Kumar K.R2b75da32016-11-11 14:37:20 -080042
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -080043//-----------------------------------------------------------------------------
44int get_ion_cookie(int ion_fd, int fd)
45//-----------------------------------------------------------------------------
46{
47 int cookie = fd;
48
Rohit Kulkarnibf0829f2018-02-09 11:48:19 -080049 // TODO(user): replace with appropriate handling for new ion apis
50#ifndef TARGET_ION_ABI_VERSION
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -080051 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 Kulkarnibf0829f2018-02-09 11:48:19 -080060#else
61 (void) ion_fd;
62#endif
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -080063 return cookie;
64}
65
66//-----------------------------------------------------------------------------
67EGLImageWrapper::DeleteEGLImageCallback::DeleteEGLImageCallback(int fd)
68//-----------------------------------------------------------------------------
69{
70 ion_fd = fd;
71}
72
73//-----------------------------------------------------------------------------
74void 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//-----------------------------------------------------------------------------
85EGLImageWrapper::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//-----------------------------------------------------------------------------
95EGLImageWrapper::~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//-----------------------------------------------------------------------------
118static EGLImageBuffer* L_wrap(const private_handle_t *src)
119//-----------------------------------------------------------------------------
120{
121 EGLImageBuffer* result = 0;
122
Arun Kumar K.R2b75da32016-11-11 14:37:20 -0800123 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.R4a8b1182017-01-26 19:44:33 -0800128
Arun Kumar K.R2b75da32016-11-11 14:37:20 -0800129 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 Chauhan815c8f62017-05-18 19:51:34 -0700134 new android::GraphicBuffer(src->unaligned_width, src->unaligned_height, src->format,
Naseer Ahmedfeaed062017-01-17 15:34:02 -0500135#ifndef __NOUGAT__
136 1, // Layer count
137#endif
138 flags, src->width /*src->stride*/,
139 native_handle, false);
Arun Kumar K.R2b75da32016-11-11 14:37:20 -0800140
141 result = new EGLImageBuffer(graphicBuffer);
142
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -0800143 return result;
Arun Kumar K.R2b75da32016-11-11 14:37:20 -0800144}
145
146//-----------------------------------------------------------------------------
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -0800147EGLImageBuffer *EGLImageWrapper::wrap(const void *pvt_handle)
Arun Kumar K.R2b75da32016-11-11 14:37:20 -0800148//-----------------------------------------------------------------------------
149{
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -0800150 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 Chauhan1cc416f2017-01-11 18:09:02 -0800164}