1 /*
2  * Copyright (C) 2022 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 #pragma once
18 
19 #include <memory>
20 #include <aidl/android/hardware/media/bufferpool2/IAccessor.h>
21 #include <aidl/android/hardware/media/bufferpool2/IObserver.h>
22 #include <bufferpool2/BufferPoolTypes.h>
23 
24 namespace aidl::android::hardware::media::bufferpool2::implementation {
25 
26 using aidl::android::hardware::media::bufferpool2::IAccessor;
27 using aidl::android::hardware::media::bufferpool2::IObserver;
28 
29 struct Accessor;
30 
31 /**
32  * A buffer pool client for a buffer pool. For a specific buffer pool, at most
33  * one buffer pool client exists per process. This class will not be exposed
34  * outside. A buffer pool client will be used via ClientManager.
35  */
36 class BufferPoolClient {
37 public:
38     /**
39      * Creates a buffer pool client from a local buffer pool
40      * (via ClientManager#create).
41      */
42     explicit BufferPoolClient(const std::shared_ptr<Accessor> &accessor,
43                               const std::shared_ptr<IObserver> &observer);
44 
45     /**
46      * Creates a buffer pool client from a remote buffer pool
47      * (via ClientManager#registerSender).
48      * Note: A buffer pool client created with remote buffer pool cannot
49      * allocate a buffer.
50      */
51     explicit BufferPoolClient(const std::shared_ptr<IAccessor> &accessor,
52                               const std::shared_ptr<IObserver> &observer);
53 
54     /** Destructs a buffer pool client. */
55     ~BufferPoolClient();
56 
57 private:
58     bool isValid();
59 
60     bool isLocal();
61 
62     bool isActive(int64_t *lastTransactionMs, bool clearCache);
63 
64     ConnectionId getConnectionId();
65 
66     BufferPoolStatus getAccessor(std::shared_ptr<IAccessor> *accessor);
67 
68     void receiveInvalidation(uint32_t msgId);
69 
70     BufferPoolStatus flush();
71 
72     BufferPoolStatus allocate(const std::vector<uint8_t> &params,
73                           native_handle_t **handle,
74                           std::shared_ptr<BufferPoolData> *buffer);
75 
76     BufferPoolStatus receive(TransactionId transactionId,
77                          BufferId bufferId,
78                          int64_t timestampMs,
79                          native_handle_t **handle,
80                          std::shared_ptr<BufferPoolData> *buffer);
81 
82     BufferPoolStatus postSend(ConnectionId receiver,
83                           const std::shared_ptr<BufferPoolData> &buffer,
84                           TransactionId *transactionId,
85                           int64_t *timestampMs);
86 
87     class Impl;
88     std::shared_ptr<Impl> mImpl;
89 
90     friend struct ClientManager;
91     friend struct Observer;
92 };
93 
94 }  // namespace aidl::android::hardware::bufferpool2::implementation
95