blob: 3bfd9cbe7e426665619d59eb643f746ab9d6d6ec [file] [log] [blame]
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Fan Xu93c94902018-11-01 12:22:05 -070017#include <android/hardware_buffer.h>
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -070018#include <bufferhub/BufferHubService.h>
Fan Xu18d90ea2018-11-06 15:46:44 -080019#include <cutils/native_handle.h>
Fan Xu93c94902018-11-01 12:22:05 -070020#include <log/log.h>
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -070021
22namespace android {
23namespace frameworks {
24namespace bufferhub {
25namespace V1_0 {
26namespace implementation {
27
Fan Xuca70b7b2018-10-31 13:20:12 -070028using hardware::Void;
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -070029
Fan Xu93c94902018-11-01 12:22:05 -070030Return<void> BufferHubService::allocateBuffer(const HardwareBufferDescription& description,
31 const uint32_t userMetadataSize,
Fan Xuca70b7b2018-10-31 13:20:12 -070032 allocateBuffer_cb _hidl_cb) {
Fan Xu93c94902018-11-01 12:22:05 -070033 AHardwareBuffer_Desc desc;
34 memcpy(&desc, &description, sizeof(AHardwareBuffer_Desc));
35
36 std::shared_ptr<BufferNode> node =
37 std::make_shared<BufferNode>(desc.width, desc.height, desc.layers, desc.format,
38 desc.usage, userMetadataSize);
39 if (node == nullptr || !node->IsValid()) {
40 ALOGE("%s: creating BufferNode failed.", __FUNCTION__);
41 _hidl_cb(/*bufferClient=*/nullptr, /*status=*/BufferHubStatus::ALLOCATION_FAILED);
42 return Void();
43 }
44
Fan Xu18d90ea2018-11-06 15:46:44 -080045 sp<BufferClient> client = BufferClient::create(this, node);
Fan Xu93c94902018-11-01 12:22:05 -070046 // Add it to list for bookkeeping and dumpsys.
47 std::lock_guard<std::mutex> lock(mClientListMutex);
48 mClientList.push_back(client);
49
50 _hidl_cb(/*bufferClient=*/client, /*status=*/BufferHubStatus::NO_ERROR);
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -070051 return Void();
52}
53
Fan Xuca70b7b2018-10-31 13:20:12 -070054Return<void> BufferHubService::importBuffer(const hidl_handle& /*nativeHandle*/,
55 importBuffer_cb _hidl_cb) {
56 // TODO(b/118614157): implement buffer import
57 _hidl_cb(/*bufferClient=*/nullptr, /*status=*/BufferHubStatus::NO_ERROR);
58 return Void();
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -070059}
60
Fan Xu18d90ea2018-11-06 15:46:44 -080061hidl_handle BufferHubService::registerToken(const BufferClient* client) {
62 uint32_t token;
63 std::lock_guard<std::mutex> lock(mTokenMapMutex);
64 do {
65 token = mTokenEngine();
66 } while (mTokenMap.find(token) != mTokenMap.end());
67
68 // native_handle_t use int[], so here need one slots to fit in uint32_t
69 native_handle_t* handle = native_handle_create(/*numFds=*/0, /*numInts=*/1);
70 handle->data[0] = token;
71
72 // returnToken owns the native_handle_t* thus doing lifecycle management
73 hidl_handle returnToken;
74 returnToken.setTo(handle, /*shoudOwn=*/true);
75
76 mTokenMap.emplace(token, client);
77 return returnToken;
78}
79
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -070080} // namespace implementation
81} // namespace V1_0
82} // namespace bufferhub
83} // namespace frameworks
84} // namespace android