blob: d8298af309f1c96e75918d07526c9b7facaab1a4 [file] [log] [blame]
Sungtak Lee8fc3ca42022-12-07 07:45:45 +00001/*
Sungtak Lee76937c62022-12-07 11:42:03 +00002 * Copyright (C) 2022 The Android Open Source Project
Sungtak Lee8fc3ca42022-12-07 07:45:45 +00003 *
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
Sungtak Lee76937c62022-12-07 11:42:03 +000017#pragma once
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000018
Sungtak Lee76937c62022-12-07 11:42:03 +000019#include <memory>
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000020
Sungtak Lee76937c62022-12-07 11:42:03 +000021#include <aidl/android/hardware/media/bufferpool2/BnConnection.h>
22#include <bufferpool2/BufferPoolTypes.h>
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000023
Sungtak Lee76937c62022-12-07 11:42:03 +000024namespace aidl::android::hardware::media::bufferpool2::implementation {
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000025
Sungtak Lee76937c62022-12-07 11:42:03 +000026struct Accessor;
27
28struct Connection : public BnConnection {
29 // Methods from ::aidl::android::hardware::media::bufferpool2::IConnection.
30 ::ndk::ScopedAStatus fetch(const std::vector<::aidl::android::hardware::media::bufferpool2::IConnection::FetchInfo>& in_fetchInfos, std::vector<::aidl::android::hardware::media::bufferpool2::IConnection::FetchResult>* _aidl_return) override;
31
32 // Methods from ::aidl::android::hardware::media::bufferpool2::IConnection.
33 ::ndk::ScopedAStatus sync() override;
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000034
35 /**
36 * Invalidates all buffers which are active and/or are ready to be recycled.
37 */
Sungtak Lee76937c62022-12-07 11:42:03 +000038 BufferPoolStatus flush();
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000039
40 /**
41 * Allocates a buffer using the specified parameters. Recycles a buffer if
42 * it is possible. The returned buffer can be transferred to other remote
43 * clients(Connection).
44 *
45 * @param params allocation parameters.
46 * @param bufferId Id of the allocated buffer.
47 * @param handle native handle of the allocated buffer.
48 *
49 * @return OK if a buffer is successfully allocated.
50 * NO_MEMORY when there is no memory.
51 * CRITICAL_ERROR otherwise.
52 */
Sungtak Lee76937c62022-12-07 11:42:03 +000053 BufferPoolStatus allocate(const std::vector<uint8_t> &params,
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000054 BufferId *bufferId, const native_handle_t **handle);
55
56 /**
57 * Processes pending buffer status messages and performs periodic cache cleaning
58 * from bufferpool.
59 *
60 * @param clearCache if clearCache is true, bufferpool frees all buffers
61 * waiting to be recycled.
62 */
63 void cleanUp(bool clearCache);
64
65 /** Destructs a connection. */
66 ~Connection();
67
68 /** Creates a connection. */
69 Connection();
70
71 /**
72 * Initializes with the specified buffer pool and the connection id.
73 * The connection id should be unique in the whole system.
74 *
75 * @param accessor the specified buffer pool.
76 * @param connectionId Id.
77 */
Sungtak Lee76937c62022-12-07 11:42:03 +000078 void initialize(const std::shared_ptr<Accessor> &accessor, ConnectionId connectionId);
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000079
80 enum : uint32_t {
81 SYNC_BUFFERID = UINT32_MAX,
82 };
83
84private:
85 bool mInitialized;
Sungtak Lee76937c62022-12-07 11:42:03 +000086 std::shared_ptr<Accessor> mAccessor;
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000087 ConnectionId mConnectionId;
Sungtak Lee76937c62022-12-07 11:42:03 +000088
89 bool fetch(
90 uint64_t transactionId,
91 uint32_t bufferId,
92 std::vector<::aidl::android::hardware::media::bufferpool2::IConnection::FetchResult>
93 *result);
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000094};
95
Sungtak Lee76937c62022-12-07 11:42:03 +000096} // namespace aidl::android::hardware::media::bufferpool2::implementation