blob: 18465b8b84499219e5af7f70cc5e063537a5ba2f [file] [log] [blame]
Arun Kumar K.R2b75da32016-11-11 14:37:20 -08001/*
Sushil Chauhan1cc416f2017-01-11 18:09:02 -08002 * Copyright (c) 2016-2017, 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{
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -080031 if (ion_fd && !ioctl(ion_fd, ION_IOC_FREE, &cookie)) {
32 } else {
33 ALOGE("ION_IOC_FREE failed: ion_fd = %d, cookie = %d", ion_fd, cookie);
34 }
35}
Arun Kumar K.R2b75da32016-11-11 14:37:20 -080036
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -080037//-----------------------------------------------------------------------------
38int get_ion_cookie(int ion_fd, int fd)
39//-----------------------------------------------------------------------------
40{
41 int cookie = fd;
42
43 struct ion_fd_data fdData;
44 memset(&fdData, 0, sizeof(fdData));
45 fdData.fd = fd;
46
47 if (ion_fd && !ioctl(ion_fd, ION_IOC_IMPORT, &fdData)) {
48 cookie = fdData.handle;
49 } else {
50 ALOGE("ION_IOC_IMPORT failed: ion_fd = %d, fd = %d", ion_fd, fd);
51 }
52
53 return cookie;
54}
55
56//-----------------------------------------------------------------------------
57EGLImageWrapper::DeleteEGLImageCallback::DeleteEGLImageCallback(int fd)
58//-----------------------------------------------------------------------------
59{
60 ion_fd = fd;
61}
62
63//-----------------------------------------------------------------------------
64void EGLImageWrapper::DeleteEGLImageCallback::operator()(int& k, EGLImageBuffer*& eglImage)
65//-----------------------------------------------------------------------------
66{
67 free_ion_cookie(ion_fd, k);
68 if( eglImage != 0 )
69 {
70 delete eglImage;
71 }
72}
73
74//-----------------------------------------------------------------------------
75EGLImageWrapper::EGLImageWrapper()
76//-----------------------------------------------------------------------------
77{
78 eglImageBufferMap = new android::LruCache<int, EGLImageBuffer*>(32);
79 ion_fd = open("/dev/ion", O_RDONLY);
80 callback = new DeleteEGLImageCallback(ion_fd);
81 eglImageBufferMap->setOnEntryRemovedListener(callback);
82}
83
84//-----------------------------------------------------------------------------
85EGLImageWrapper::~EGLImageWrapper()
86//-----------------------------------------------------------------------------
87{
88 if( eglImageBufferMap != 0 )
89 {
90 eglImageBufferMap->clear();
91 delete eglImageBufferMap;
92 eglImageBufferMap = 0;
93 }
94
95 if( callback != 0 )
96 {
97 delete callback;
98 callback = 0;
99 }
100
101 if( ion_fd > 0 )
102 {
103 close(ion_fd);
104 }
105 ion_fd = -1;
106}
107//-----------------------------------------------------------------------------
108static EGLImageBuffer* L_wrap(const private_handle_t *src)
109//-----------------------------------------------------------------------------
110{
111 EGLImageBuffer* result = 0;
112
Arun Kumar K.R2b75da32016-11-11 14:37:20 -0800113 native_handle_t *native_handle = const_cast<private_handle_t *>(src);
114
115 int flags = android::GraphicBuffer::USAGE_HW_TEXTURE |
116 android::GraphicBuffer::USAGE_SW_READ_NEVER |
117 android::GraphicBuffer::USAGE_SW_WRITE_NEVER;
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -0800118
Arun Kumar K.R2b75da32016-11-11 14:37:20 -0800119 if (src->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) {
120 flags |= android::GraphicBuffer::USAGE_PROTECTED;
121 }
122
123 android::sp<android::GraphicBuffer> graphicBuffer =
124 new android::GraphicBuffer(src->width, src->height, src->format, flags,
125 src->width /*src->stride*/, native_handle, false);
126
127 result = new EGLImageBuffer(graphicBuffer);
128
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -0800129 return result;
Arun Kumar K.R2b75da32016-11-11 14:37:20 -0800130}
131
132//-----------------------------------------------------------------------------
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -0800133EGLImageBuffer *EGLImageWrapper::wrap(const void *pvt_handle)
Arun Kumar K.R2b75da32016-11-11 14:37:20 -0800134//-----------------------------------------------------------------------------
135{
Arun Kumar K.R4a8b1182017-01-26 19:44:33 -0800136 const private_handle_t *src = static_cast<const private_handle_t *>(pvt_handle);
137
138 int ion_cookie = get_ion_cookie(ion_fd, src->fd);
139 EGLImageBuffer* eglImage = eglImageBufferMap->get(ion_cookie);
140 if( eglImage == 0 )
141 {
142 eglImage = L_wrap(src);
143 eglImageBufferMap->put(ion_cookie, eglImage);
144 }
145 else {
146 free_ion_cookie(ion_fd, ion_cookie);
147 }
148
149 return eglImage;
Sushil Chauhan1cc416f2017-01-11 18:09:02 -0800150}