Jiwen 'Steve' Cai | d9f2abe | 2018-10-20 17:03:13 -0700 | [diff] [blame] | 1 | /* |
| 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 Xu | 93c9490 | 2018-11-01 12:22:05 -0700 | [diff] [blame] | 17 | #include <android/hardware_buffer.h> |
Jiwen 'Steve' Cai | d9f2abe | 2018-10-20 17:03:13 -0700 | [diff] [blame] | 18 | #include <bufferhub/BufferHubService.h> |
Fan Xu | 18d90ea | 2018-11-06 15:46:44 -0800 | [diff] [blame] | 19 | #include <cutils/native_handle.h> |
Fan Xu | 93c9490 | 2018-11-01 12:22:05 -0700 | [diff] [blame] | 20 | #include <log/log.h> |
Jiwen 'Steve' Cai | d9f2abe | 2018-10-20 17:03:13 -0700 | [diff] [blame] | 21 | |
| 22 | namespace android { |
| 23 | namespace frameworks { |
| 24 | namespace bufferhub { |
| 25 | namespace V1_0 { |
| 26 | namespace implementation { |
| 27 | |
Fan Xu | ca70b7b | 2018-10-31 13:20:12 -0700 | [diff] [blame] | 28 | using hardware::Void; |
Jiwen 'Steve' Cai | d9f2abe | 2018-10-20 17:03:13 -0700 | [diff] [blame] | 29 | |
Fan Xu | 93c9490 | 2018-11-01 12:22:05 -0700 | [diff] [blame] | 30 | Return<void> BufferHubService::allocateBuffer(const HardwareBufferDescription& description, |
| 31 | const uint32_t userMetadataSize, |
Fan Xu | ca70b7b | 2018-10-31 13:20:12 -0700 | [diff] [blame] | 32 | allocateBuffer_cb _hidl_cb) { |
Fan Xu | 93c9490 | 2018-11-01 12:22:05 -0700 | [diff] [blame] | 33 | 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, |
Fan Xu | ffde786 | 2018-11-08 16:29:13 -0800 | [diff] [blame] | 38 | desc.usage, userMetadataSize, nodeIdGenerator.getId()); |
Fan Xu | 93c9490 | 2018-11-01 12:22:05 -0700 | [diff] [blame] | 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 Xu | 18d90ea | 2018-11-06 15:46:44 -0800 | [diff] [blame] | 45 | sp<BufferClient> client = BufferClient::create(this, node); |
Fan Xu | 93c9490 | 2018-11-01 12:22:05 -0700 | [diff] [blame] | 46 | // 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' Cai | d9f2abe | 2018-10-20 17:03:13 -0700 | [diff] [blame] | 51 | return Void(); |
| 52 | } |
| 53 | |
Fan Xu | ca70b7b | 2018-10-31 13:20:12 -0700 | [diff] [blame] | 54 | Return<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' Cai | d9f2abe | 2018-10-20 17:03:13 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Fan Xu | d6cd6ba | 2018-11-15 16:46:55 -0800 | [diff] [blame^] | 61 | hidl_handle BufferHubService::registerToken(const wp<BufferClient>& client) { |
Fan Xu | 18d90ea | 2018-11-06 15:46:44 -0800 | [diff] [blame] | 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' Cai | d9f2abe | 2018-10-20 17:03:13 -0700 | [diff] [blame] | 80 | } // namespace implementation |
| 81 | } // namespace V1_0 |
| 82 | } // namespace bufferhub |
| 83 | } // namespace frameworks |
| 84 | } // namespace android |