blob: 529889f6e5df04f7dcdb9e5143164160b9177926 [file] [log] [blame]
Naseer Ahmede36f2242017-12-01 15:33:56 -05001/*
Ashish Kumarf04c3de2018-08-01 14:45:14 +05302 * Copyright (c) 2018-2019 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
33#include <log/log.h>
Tharaga Balachandran74ab1112020-01-08 17:17:56 -050034#include <vendor/qti/hardware/display/mapper/3.0/IQtiMapper.h>
35#include <vendor/qti/hardware/display/mapper/4.0/IQtiMapper.h>
36
Naseer Ahmede36f2242017-12-01 15:33:56 -050037#include <vector>
38
Tharaga Balachandran74ab1112020-01-08 17:17:56 -050039#include "QtiMapper.h"
40#include "QtiMapper4.h"
Naseer Ahmede36f2242017-12-01 15:33:56 -050041#include "gr_utils.h"
42
43namespace vendor {
44namespace qti {
45namespace hardware {
46namespace display {
47namespace allocator {
Tharaga Balachandranab150ab2019-09-26 19:17:58 -040048namespace V3_0 {
Naseer Ahmede36f2242017-12-01 15:33:56 -050049namespace implementation {
50
51using android::hardware::hidl_handle;
52using gralloc::BufferDescriptor;
Tharaga Balachandran74ab1112020-01-08 17:17:56 -050053using IMapper_3_0_Error = android::hardware::graphics::mapper::V3_0::Error;
54using gralloc::Error;
Naseer Ahmede36f2242017-12-01 15:33:56 -050055
56QtiAllocator::QtiAllocator() {
57 buf_mgr_ = BufferManager::GetInstance();
58}
59
60// Methods from ::android::hardware::graphics::allocator::V2_0::IAllocator follow.
61Return<void> QtiAllocator::dumpDebugInfo(dumpDebugInfo_cb hidl_cb) {
62 std::ostringstream os;
63 buf_mgr_->Dump(&os);
64 hidl_string reply;
65 reply.setToExternal(os.str().c_str(), os.str().length());
66 hidl_cb(reply);
67 return Void();
68}
69
70Return<void> QtiAllocator::allocate(const hidl_vec<uint32_t> &descriptor, uint32_t count,
71 allocate_cb hidl_cb) {
72 ALOGD_IF(DEBUG, "Allocating buffers count: %d", count);
73 gralloc::BufferDescriptor desc;
74
Tharaga Balachandran74ab1112020-01-08 17:17:56 -050075 auto err = ::vendor::qti::hardware::display::mapper::V3_0::implementation::QtiMapper::Decode(
76 descriptor, &desc);
Naseer Ahmede36f2242017-12-01 15:33:56 -050077 if (err != Error::NONE) {
Tharaga Balachandran74ab1112020-01-08 17:17:56 -050078 hidl_cb(static_cast<IMapper_3_0_Error>(err), 0, hidl_vec<hidl_handle>());
Naseer Ahmede36f2242017-12-01 15:33:56 -050079 return Void();
80 }
81
82 std::vector<hidl_handle> buffers;
83 buffers.reserve(count);
84 for (uint32_t i = 0; i < count; i++) {
85 buffer_handle_t buffer;
86 ALOGD_IF(DEBUG, "buffer: %p", &buffer);
87 err = buf_mgr_->AllocateBuffer(desc, &buffer);
88 if (err != Error::NONE) {
89 break;
90 }
91 buffers.emplace_back(hidl_handle(buffer));
92 }
93
94 uint32_t stride = 0;
95 hidl_vec<hidl_handle> hidl_buffers;
96 if (err == Error::NONE && buffers.size() > 0) {
97 stride = static_cast<uint32_t>(PRIV_HANDLE_CONST(buffers[0].getNativeHandle())->width);
98 hidl_buffers.setToExternal(buffers.data(), buffers.size());
99 }
Tharaga Balachandran74ab1112020-01-08 17:17:56 -0500100 hidl_cb(static_cast<IMapper_3_0_Error>(err), stride, hidl_buffers);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500101
102 for (const auto &b : buffers) {
103 buf_mgr_->ReleaseBuffer(PRIV_HANDLE_CONST(b.getNativeHandle()));
104 }
105
106 return Void();
107}
108
Tharaga Balachandran74ab1112020-01-08 17:17:56 -0500109} // namespace implementation
110} // namespace V3_0
111} // namespace allocator
112} // namespace display
113} // namespace hardware
114} // namespace qti
115} // namespace vendor
Naseer Ahmede36f2242017-12-01 15:33:56 -0500116
Tharaga Balachandran74ab1112020-01-08 17:17:56 -0500117namespace vendor {
118namespace qti {
119namespace hardware {
120namespace display {
121namespace allocator {
122namespace V4_0 {
123namespace implementation {
124
125using android::hardware::hidl_handle;
126using gralloc::BufferDescriptor;
127using IMapper_4_0_Error = android::hardware::graphics::mapper::V4_0::Error;
128using gralloc::Error;
129
130QtiAllocator::QtiAllocator() {
131 gralloc::GrallocProperties properties;
132 get_properties(&properties);
133 buf_mgr_ = BufferManager::GetInstance();
134 buf_mgr_->SetGrallocDebugProperties(properties);
135}
136
137Return<void> QtiAllocator::allocate(const hidl_vec<uint8_t> &descriptor, uint32_t count,
138 allocate_cb hidl_cb) {
139 ALOGD_IF(DEBUG, "Allocating buffers count: %d", count);
140 gralloc::BufferDescriptor desc;
141
142 auto err = ::vendor::qti::hardware::display::mapper::V4_0::implementation::QtiMapper::Decode(
143 descriptor, &desc);
144 if (err != Error::NONE) {
145 hidl_cb(static_cast<IMapper_4_0_Error>(err), 0, hidl_vec<hidl_handle>());
146 return Void();
147 }
148
149 std::vector<hidl_handle> buffers;
150 buffers.reserve(count);
151 for (uint32_t i = 0; i < count; i++) {
152 buffer_handle_t buffer;
153 ALOGD_IF(DEBUG, "buffer: %p", &buffer);
154 err = buf_mgr_->AllocateBuffer(desc, &buffer);
155 if (err != Error::NONE) {
156 break;
157 }
158 buffers.emplace_back(hidl_handle(buffer));
159 }
160
161 uint32_t stride = 0;
162 hidl_vec<hidl_handle> hidl_buffers;
163 if (err == Error::NONE && buffers.size() > 0) {
164 stride = static_cast<uint32_t>(PRIV_HANDLE_CONST(buffers[0].getNativeHandle())->width);
165 hidl_buffers.setToExternal(buffers.data(), buffers.size());
166 }
167 hidl_cb(static_cast<IMapper_4_0_Error>(err), stride, hidl_buffers);
168
169 for (const auto &b : buffers) {
170 buf_mgr_->ReleaseBuffer(PRIV_HANDLE_CONST(b.getNativeHandle()));
171 }
172
173 return Void();
Naseer Ahmede36f2242017-12-01 15:33:56 -0500174}
175
176} // namespace implementation
Tharaga Balachandran74ab1112020-01-08 17:17:56 -0500177} // namespace V4_0
Naseer Ahmede36f2242017-12-01 15:33:56 -0500178} // namespace allocator
179} // namespace display
180} // namespace hardware
181} // namespace qti
182} // namespace vendor