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 
17 #include "Connection.h"
18 
19 namespace android {
20 namespace hardware {
21 namespace media {
22 namespace bufferpool {
23 namespace V1_0 {
24 namespace implementation {
25 
26 // Methods from ::android::hardware::media::bufferpool::V1_0::IConnection follow.
fetch(uint64_t transactionId,uint32_t bufferId,fetch_cb _hidl_cb)27 Return<void> Connection::fetch(uint64_t transactionId, uint32_t bufferId, fetch_cb _hidl_cb) {
28     ResultStatus status = ResultStatus::CRITICAL_ERROR;
29     if (mInitialized && mAccessor) {
30         if (bufferId != SYNC_BUFFERID) {
31             const native_handle_t *handle = nullptr;
32             status = mAccessor->fetch(
33                     mConnectionId, transactionId, bufferId, &handle);
34             if (status == ResultStatus::OK) {
35                 Buffer buffer = {};
36                 buffer.id = bufferId;
37                 buffer.buffer = handle;
38                 _hidl_cb(status, buffer);
39                 return Void();
40             }
41         } else {
42             mAccessor->cleanUp(false);
43         }
44     }
45 
46     Buffer buffer = {};
47     buffer.id = 0;
48     buffer.buffer = nullptr;
49 
50     _hidl_cb(status, buffer);
51     return Void();
52 }
53 
Connection()54 Connection::Connection() : mInitialized(false), mConnectionId(-1LL) {}
55 
~Connection()56 Connection::~Connection() {
57     if (mInitialized && mAccessor) {
58         mAccessor->close(mConnectionId);
59     }
60 }
61 
initialize(const sp<Accessor> & accessor,ConnectionId connectionId)62 void Connection::initialize(
63         const sp<Accessor>& accessor, ConnectionId connectionId) {
64     if (!mInitialized) {
65         mAccessor = accessor;
66         mConnectionId = connectionId;
67         mInitialized = true;
68     }
69 }
70 
allocate(const std::vector<uint8_t> & params,BufferId * bufferId,const native_handle_t ** handle)71 ResultStatus Connection::allocate(
72         const std::vector<uint8_t> &params, BufferId *bufferId,
73         const native_handle_t **handle) {
74     if (mInitialized && mAccessor) {
75         return mAccessor->allocate(mConnectionId, params, bufferId, handle);
76     }
77     return ResultStatus::CRITICAL_ERROR;
78 }
79 
cleanUp(bool clearCache)80 void Connection::cleanUp(bool clearCache) {
81     if (mInitialized && mAccessor) {
82         mAccessor->cleanUp(clearCache);
83     }
84 }
85 
86 // Methods from ::android::hidl::base::V1_0::IBase follow.
87 
88 //IConnection* HIDL_FETCH_IConnection(const char* /* name */) {
89 //    return new Connection();
90 //}
91 
92 }  // namespace implementation
93 }  // namespace V1_0
94 }  // namespace bufferpool
95 }  // namespace media
96 }  // namespace hardware
97 }  // namespace android
98