blob: 1a0423773647c51be61dafe8db432fd74957ded2 [file] [log] [blame]
Naseer Ahmede36f2242017-12-01 15:33:56 -05001/*
Tharaga Balachandran576571c2020-01-23 18:41:10 -05002 * Copyright (c) 2018-2020 The Linux Foundation. All rights reserved.
Naseer Ahmede36f2242017-12-01 15:33:56 -05003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation. nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#define DEBUG 0
31#include "QtiAllocator.h"
32
Tharaga Balachandran576571c2020-01-23 18:41:10 -050033#include <cutils/properties.h>
Naseer Ahmede36f2242017-12-01 15:33:56 -050034#include <log/log.h>
Tharaga Balachandran74ab1112020-01-08 17:17:56 -050035#include <vendor/qti/hardware/display/mapper/3.0/IQtiMapper.h>
36#include <vendor/qti/hardware/display/mapper/4.0/IQtiMapper.h>
37
Naseer Ahmede36f2242017-12-01 15:33:56 -050038#include <vector>
39
Tharaga Balachandran74ab1112020-01-08 17:17:56 -050040#include "QtiMapper.h"
41#include "QtiMapper4.h"
Naseer Ahmede36f2242017-12-01 15:33:56 -050042#include "gr_utils.h"
43
Tharaga Balachandran576571c2020-01-23 18:41:10 -050044static void get_properties(gralloc::GrallocProperties *props) {
45 char property[PROPERTY_VALUE_MAX];
46 property_get("vendor.gralloc.use_system_heap_for_sensors", property, "1");
47 if (!(strncmp(property, "0", PROPERTY_VALUE_MAX))) {
48 props->use_system_heap_for_sensors = false;
49 }
50
51 property_get("vendor.gralloc.disable_ubwc", property, "0");
52 if (!(strncmp(property, "1", PROPERTY_VALUE_MAX)) ||
53 !(strncmp(property, "true", PROPERTY_VALUE_MAX))) {
54 props->ubwc_disable = true;
55 }
56
57 property_get("vendor.gralloc.disable_ahardware_buffer", property, "0");
58 if (!(strncmp(property, "1", PROPERTY_VALUE_MAX)) ||
59 !(strncmp(property, "true", PROPERTY_VALUE_MAX))) {
60 props->ahardware_buffer_disable = true;
61 }
62}
63
Naseer Ahmede36f2242017-12-01 15:33:56 -050064namespace vendor {
65namespace qti {
66namespace hardware {
67namespace display {
68namespace allocator {
Tharaga Balachandranab150ab2019-09-26 19:17:58 -040069namespace V3_0 {
Naseer Ahmede36f2242017-12-01 15:33:56 -050070namespace implementation {
71
72using android::hardware::hidl_handle;
73using gralloc::BufferDescriptor;
Tharaga Balachandran74ab1112020-01-08 17:17:56 -050074using IMapper_3_0_Error = android::hardware::graphics::mapper::V3_0::Error;
75using gralloc::Error;
Naseer Ahmede36f2242017-12-01 15:33:56 -050076
77QtiAllocator::QtiAllocator() {
Tharaga Balachandran576571c2020-01-23 18:41:10 -050078 gralloc::GrallocProperties properties;
79 get_properties(&properties);
Naseer Ahmede36f2242017-12-01 15:33:56 -050080 buf_mgr_ = BufferManager::GetInstance();
Tharaga Balachandran576571c2020-01-23 18:41:10 -050081 buf_mgr_->SetGrallocDebugProperties(properties);
Naseer Ahmede36f2242017-12-01 15:33:56 -050082}
83
84// Methods from ::android::hardware::graphics::allocator::V2_0::IAllocator follow.
85Return<void> QtiAllocator::dumpDebugInfo(dumpDebugInfo_cb hidl_cb) {
86 std::ostringstream os;
87 buf_mgr_->Dump(&os);
88 hidl_string reply;
89 reply.setToExternal(os.str().c_str(), os.str().length());
90 hidl_cb(reply);
91 return Void();
92}
93
94Return<void> QtiAllocator::allocate(const hidl_vec<uint32_t> &descriptor, uint32_t count,
95 allocate_cb hidl_cb) {
96 ALOGD_IF(DEBUG, "Allocating buffers count: %d", count);
97 gralloc::BufferDescriptor desc;
98
Tharaga Balachandran74ab1112020-01-08 17:17:56 -050099 auto err = ::vendor::qti::hardware::display::mapper::V3_0::implementation::QtiMapper::Decode(
100 descriptor, &desc);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500101 if (err != Error::NONE) {
Tharaga Balachandran74ab1112020-01-08 17:17:56 -0500102 hidl_cb(static_cast<IMapper_3_0_Error>(err), 0, hidl_vec<hidl_handle>());
Naseer Ahmede36f2242017-12-01 15:33:56 -0500103 return Void();
104 }
105
106 std::vector<hidl_handle> buffers;
107 buffers.reserve(count);
108 for (uint32_t i = 0; i < count; i++) {
109 buffer_handle_t buffer;
110 ALOGD_IF(DEBUG, "buffer: %p", &buffer);
111 err = buf_mgr_->AllocateBuffer(desc, &buffer);
112 if (err != Error::NONE) {
113 break;
114 }
115 buffers.emplace_back(hidl_handle(buffer));
116 }
117
118 uint32_t stride = 0;
119 hidl_vec<hidl_handle> hidl_buffers;
120 if (err == Error::NONE && buffers.size() > 0) {
121 stride = static_cast<uint32_t>(PRIV_HANDLE_CONST(buffers[0].getNativeHandle())->width);
122 hidl_buffers.setToExternal(buffers.data(), buffers.size());
123 }
Tharaga Balachandran74ab1112020-01-08 17:17:56 -0500124 hidl_cb(static_cast<IMapper_3_0_Error>(err), stride, hidl_buffers);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500125
126 for (const auto &b : buffers) {
127 buf_mgr_->ReleaseBuffer(PRIV_HANDLE_CONST(b.getNativeHandle()));
128 }
129
130 return Void();
131}
132
Tharaga Balachandran74ab1112020-01-08 17:17:56 -0500133} // namespace implementation
134} // namespace V3_0
135} // namespace allocator
136} // namespace display
137} // namespace hardware
138} // namespace qti
139} // namespace vendor
Naseer Ahmede36f2242017-12-01 15:33:56 -0500140
Tharaga Balachandran74ab1112020-01-08 17:17:56 -0500141namespace vendor {
142namespace qti {
143namespace hardware {
144namespace display {
145namespace allocator {
146namespace V4_0 {
147namespace implementation {
148
149using android::hardware::hidl_handle;
150using gralloc::BufferDescriptor;
151using IMapper_4_0_Error = android::hardware::graphics::mapper::V4_0::Error;
152using gralloc::Error;
153
154QtiAllocator::QtiAllocator() {
155 gralloc::GrallocProperties properties;
156 get_properties(&properties);
157 buf_mgr_ = BufferManager::GetInstance();
158 buf_mgr_->SetGrallocDebugProperties(properties);
159}
160
161Return<void> QtiAllocator::allocate(const hidl_vec<uint8_t> &descriptor, uint32_t count,
162 allocate_cb hidl_cb) {
163 ALOGD_IF(DEBUG, "Allocating buffers count: %d", count);
164 gralloc::BufferDescriptor desc;
165
166 auto err = ::vendor::qti::hardware::display::mapper::V4_0::implementation::QtiMapper::Decode(
167 descriptor, &desc);
168 if (err != Error::NONE) {
169 hidl_cb(static_cast<IMapper_4_0_Error>(err), 0, hidl_vec<hidl_handle>());
170 return Void();
171 }
172
173 std::vector<hidl_handle> buffers;
174 buffers.reserve(count);
175 for (uint32_t i = 0; i < count; i++) {
176 buffer_handle_t buffer;
177 ALOGD_IF(DEBUG, "buffer: %p", &buffer);
178 err = buf_mgr_->AllocateBuffer(desc, &buffer);
179 if (err != Error::NONE) {
180 break;
181 }
182 buffers.emplace_back(hidl_handle(buffer));
183 }
184
185 uint32_t stride = 0;
186 hidl_vec<hidl_handle> hidl_buffers;
187 if (err == Error::NONE && buffers.size() > 0) {
188 stride = static_cast<uint32_t>(PRIV_HANDLE_CONST(buffers[0].getNativeHandle())->width);
189 hidl_buffers.setToExternal(buffers.data(), buffers.size());
190 }
191 hidl_cb(static_cast<IMapper_4_0_Error>(err), stride, hidl_buffers);
192
193 for (const auto &b : buffers) {
194 buf_mgr_->ReleaseBuffer(PRIV_HANDLE_CONST(b.getNativeHandle()));
195 }
196
197 return Void();
Naseer Ahmede36f2242017-12-01 15:33:56 -0500198}
199
200} // namespace implementation
Tharaga Balachandran74ab1112020-01-08 17:17:56 -0500201} // namespace V4_0
Naseer Ahmede36f2242017-12-01 15:33:56 -0500202} // namespace allocator
203} // namespace display
204} // namespace hardware
205} // namespace qti
206} // namespace vendor