blob: 6a12b2bbb5792ff5aec5accb44586d1e61be1baf [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Greg Hartmanf66c1102015-04-17 17:29:14 -070015#include "GrallocAndroid.hpp"
Alistair Strachanc28d28a2018-03-24 12:24:00 -070016#include "Debug.hpp"
Greg Hartmanf66c1102015-04-17 17:29:14 -070017
Alistair Strachanc28d28a2018-03-24 12:24:00 -070018#ifdef HAVE_GRALLOC1
Jason Macnak1de497c2020-04-08 11:31:50 -070019# include <sync/sync.h>
20#endif
21#ifdef HAVE_GRALLOC3
22using V3Error = android::hardware::graphics::mapper::V3_0::Error;
23using V3Mapper = android::hardware::graphics::mapper::V3_0::IMapper;
24using android::hardware::hidl_handle;
Alistair Strachanc28d28a2018-03-24 12:24:00 -070025#endif
Greg Hartmanf66c1102015-04-17 17:29:14 -070026
Nicolas Capens83dfdcb2015-09-04 13:31:30 -040027GrallocModule *GrallocModule::getInstance()
Greg Hartmanf66c1102015-04-17 17:29:14 -070028{
Nicolas Capens0bac2852016-05-07 06:09:58 -040029 static GrallocModule instance;
30 return &instance;
Greg Hartmanf66c1102015-04-17 17:29:14 -070031}
32
33GrallocModule::GrallocModule()
34{
Jason Macnak1de497c2020-04-08 11:31:50 -070035#ifdef HAVE_GRALLOC3
36 m_gralloc3_mapper = V3Mapper::getService();
37 if(m_gralloc3_mapper != nullptr)
38 {
39 return;
40 }
41#endif
42
Nicolas Capens0bac2852016-05-07 06:09:58 -040043 const hw_module_t *module = nullptr;
44 hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
Nicolas Capens83dfdcb2015-09-04 13:31:30 -040045
Jaesung Chungfccddfe2017-06-22 20:02:09 +090046 m_major_version = (module->module_api_version >> 8) & 0xff;
47 switch(m_major_version)
Nicolas Capens0bac2852016-05-07 06:09:58 -040048 {
Jason Macnak1de497c2020-04-08 11:31:50 -070049 case 0:
50 m_module = reinterpret_cast<const gralloc_module_t *>(module);
51 break;
52 case 1:
Jaesung Chungfccddfe2017-06-22 20:02:09 +090053#ifdef HAVE_GRALLOC1
Jason Macnak1de497c2020-04-08 11:31:50 -070054 gralloc1_open(module, &m_gralloc1_device);
55 m_gralloc1_lock = (GRALLOC1_PFN_LOCK)m_gralloc1_device->getFunction(m_gralloc1_device, GRALLOC1_FUNCTION_LOCK);
56 m_gralloc1_unlock = (GRALLOC1_PFN_UNLOCK)m_gralloc1_device->getFunction(m_gralloc1_device, GRALLOC1_FUNCTION_UNLOCK);
57 break;
Jaesung Chungfccddfe2017-06-22 20:02:09 +090058#endif
Jason Macnak1de497c2020-04-08 11:31:50 -070059 default:
60 TRACE("unknown gralloc major version (%d)", m_major_version);
61 break;
Nicolas Capens0bac2852016-05-07 06:09:58 -040062 }
Greg Hartmanf66c1102015-04-17 17:29:14 -070063}
Alistair Strachanc28d28a2018-03-24 12:24:00 -070064
Jason Macnak1de497c2020-04-08 11:31:50 -070065int GrallocModule::import(buffer_handle_t handle, buffer_handle_t *imported_handle)
66{
67#ifdef HAVE_GRALLOC3
68 if(m_gralloc3_mapper != nullptr)
69 {
70 V3Error error;
71 auto ret = m_gralloc3_mapper->importBuffer(handle,
72 [&](const auto &tmp_err, const auto &tmp_buf) {
73 error = tmp_err;
74 if(error == V3Error::NONE)
75 {
76 *imported_handle = static_cast<buffer_handle_t>(tmp_buf);
77 }
78 });
79 return ret.isOk() && error == V3Error::NONE ? 0 : -1;
80 }
81#endif
82
83 *imported_handle = handle;
84 return 0;
85}
86
87int GrallocModule::release(buffer_handle_t handle)
88{
89#ifdef HAVE_GRALLOC3
90 if(m_gralloc3_mapper != nullptr)
91 {
92 native_handle_t *native_handle = const_cast<native_handle_t *>(handle);
93 return m_gralloc3_mapper->freeBuffer(native_handle).isOk() ? 0 : 1;
94 }
95#endif
96
97 return 0;
98}
99
Alistair Strachanc28d28a2018-03-24 12:24:00 -0700100int GrallocModule::lock(buffer_handle_t handle, int usage, int left, int top, int width, int height, void **vaddr)
101{
Jason Macnak1de497c2020-04-08 11:31:50 -0700102#ifdef HAVE_GRALLOC3
103 if(m_gralloc3_mapper != nullptr)
104 {
105 native_handle_t *native_handle = const_cast<native_handle_t *>(handle);
106
107 V3Mapper::Rect rect;
108 rect.left = left;
109 rect.top = top;
110 rect.width = width;
111 rect.height = height;
112
113 hidl_handle empty_fence_handle;
114
115 V3Error error;
116 auto ret = m_gralloc3_mapper->lock(native_handle, usage, rect, empty_fence_handle,
117 [&](const auto &tmp_err,
118 const auto &tmp_vaddr,
119 const auto & /*bytes_per_pixel*/,
120 const auto & /*bytes_per_stride*/) {
121 error = tmp_err;
122 if(tmp_err == V3Error::NONE)
123 {
124 *vaddr = tmp_vaddr;
125 }
126 });
127 return ret.isOk() && error == V3Error::NONE ? 0 : -1;
128 }
129#endif
130
Alistair Strachanc28d28a2018-03-24 12:24:00 -0700131 switch(m_major_version)
132 {
Jason Macnak1de497c2020-04-08 11:31:50 -0700133 case 0:
Alistair Strachanc28d28a2018-03-24 12:24:00 -0700134 {
135 return m_module->lock(m_module, handle, usage, left, top, width, height, vaddr);
136 }
Jason Macnak1de497c2020-04-08 11:31:50 -0700137 case 1:
Alistair Strachanc28d28a2018-03-24 12:24:00 -0700138#ifdef HAVE_GRALLOC1
139 {
140 gralloc1_rect_t outRect{};
141 outRect.left = left;
142 outRect.top = top;
143 outRect.width = width;
144 outRect.height = height;
145 return m_gralloc1_lock(m_gralloc1_device, handle, usage, usage, &outRect, vaddr, -1);
146 }
147#endif
Jason Macnak1de497c2020-04-08 11:31:50 -0700148 default:
Alistair Strachanc28d28a2018-03-24 12:24:00 -0700149 {
150 TRACE("no gralloc module to lock");
151 return -1;
152 }
153 }
154}
155
156int GrallocModule::unlock(buffer_handle_t handle)
157{
Jason Macnak1de497c2020-04-08 11:31:50 -0700158#ifdef HAVE_GRALLOC3
159 if(m_gralloc3_mapper != nullptr)
160 {
161 native_handle_t *native_handle = const_cast<native_handle_t *>(handle);
162
163 V3Error error;
164 auto ret = m_gralloc3_mapper->unlock(native_handle,
165 [&](const auto &tmp_err, const auto &) {
166 error = tmp_err;
167 });
168 return ret.isOk() && error == V3Error::NONE ? 0 : -1;
169 }
170#endif
171
Alistair Strachanc28d28a2018-03-24 12:24:00 -0700172 switch(m_major_version)
173 {
Jason Macnak1de497c2020-04-08 11:31:50 -0700174 case 0:
Alistair Strachanc28d28a2018-03-24 12:24:00 -0700175 {
176 return m_module->unlock(m_module, handle);
177 }
Jason Macnak1de497c2020-04-08 11:31:50 -0700178 case 1:
Alistair Strachanc28d28a2018-03-24 12:24:00 -0700179#ifdef HAVE_GRALLOC1
180 {
181 int32_t fenceFd = -1;
182 int error = m_gralloc1_unlock(m_gralloc1_device, handle, &fenceFd);
Jason Macnak1de497c2020-04-08 11:31:50 -0700183 if(!error)
Alistair Strachanc28d28a2018-03-24 12:24:00 -0700184 {
185 sync_wait(fenceFd, -1);
186 close(fenceFd);
187 }
188 return error;
189 }
190#endif
Jason Macnak1de497c2020-04-08 11:31:50 -0700191 default:
Alistair Strachanc28d28a2018-03-24 12:24:00 -0700192 {
193 TRACE("no gralloc module to unlock");
194 return -1;
195 }
196 }
197}