1 /*
2 * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
3 *
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>
34 #include <vector>
35
36 #include "gr_utils.h"
37
38 namespace vendor {
39 namespace qti {
40 namespace hardware {
41 namespace display {
42 namespace allocator {
43 namespace V3_0 {
44 namespace implementation {
45
46 using android::hardware::hidl_handle;
47 using gralloc::BufferDescriptor;
48
QtiAllocator()49 QtiAllocator::QtiAllocator() {
50 buf_mgr_ = BufferManager::GetInstance();
51 }
52
53 // Methods from ::android::hardware::graphics::allocator::V2_0::IAllocator follow.
dumpDebugInfo(dumpDebugInfo_cb hidl_cb)54 Return<void> QtiAllocator::dumpDebugInfo(dumpDebugInfo_cb hidl_cb) {
55 std::ostringstream os;
56 buf_mgr_->Dump(&os);
57 hidl_string reply;
58 reply.setToExternal(os.str().c_str(), os.str().length());
59 hidl_cb(reply);
60 return Void();
61 }
62
allocate(const hidl_vec<uint32_t> & descriptor,uint32_t count,allocate_cb hidl_cb)63 Return<void> QtiAllocator::allocate(const hidl_vec<uint32_t> &descriptor, uint32_t count,
64 allocate_cb hidl_cb) {
65 ALOGD_IF(DEBUG, "Allocating buffers count: %d", count);
66 gralloc::BufferDescriptor desc;
67
68 auto err = desc.Decode(descriptor);
69 if (err != Error::NONE) {
70 hidl_cb(err, 0, hidl_vec<hidl_handle>());
71 return Void();
72 }
73
74 std::vector<hidl_handle> buffers;
75 buffers.reserve(count);
76 for (uint32_t i = 0; i < count; i++) {
77 buffer_handle_t buffer;
78 ALOGD_IF(DEBUG, "buffer: %p", &buffer);
79 err = buf_mgr_->AllocateBuffer(desc, &buffer);
80 if (err != Error::NONE) {
81 break;
82 }
83 buffers.emplace_back(hidl_handle(buffer));
84 }
85
86 uint32_t stride = 0;
87 hidl_vec<hidl_handle> hidl_buffers;
88 if (err == Error::NONE && buffers.size() > 0) {
89 stride = static_cast<uint32_t>(PRIV_HANDLE_CONST(buffers[0].getNativeHandle())->width);
90 hidl_buffers.setToExternal(buffers.data(), buffers.size());
91 }
92 hidl_cb(err, stride, hidl_buffers);
93
94 for (const auto &b : buffers) {
95 buf_mgr_->ReleaseBuffer(PRIV_HANDLE_CONST(b.getNativeHandle()));
96 }
97
98 return Void();
99 }
100
101 // Methods from ::android::hidl::base::V1_0::IBase follow.
102
HIDL_FETCH_IQtiAllocator(const char *)103 IQtiAllocator *HIDL_FETCH_IQtiAllocator(const char * /* name */) {
104 return new QtiAllocator();
105 }
106
107 } // namespace implementation
108 } // namespace V3_0
109 } // namespace allocator
110 } // namespace display
111 } // namespace hardware
112 } // namespace qti
113 } // namespace vendor
114