1 // Copyright 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
15 
16 #include "AndroidPipe.h"
17 
18 #include <fstream>
19 #include <functional>
20 
21 namespace android {
22 namespace emulation {
23 
24 using OnLastColorBufferRef = std::function<void(uint32_t)>;
25 
26 // This is a pipe for refcounting guest side objects.
27 class RefcountPipe final : public AndroidPipe {
28 public:
29     class Service final : public AndroidPipe::Service {
30     public:
31         Service();
32         AndroidPipe* create(void* hwPipe, const char* args, enum AndroidPipeFlags flags) override;
33         AndroidPipe* load(void* hwPipe,
34                           const char* args,
35                           android::base::Stream* stream) override;
36         bool canLoad() const override;
37     };
38 
39     RefcountPipe(void* hwPipe,
40                  Service* svc,
41                  base::Stream* loadStream = nullptr);
42     ~RefcountPipe();
43     void onGuestClose(PipeCloseReason reason) override;
44     unsigned onGuestPoll() const override;
45     int onGuestRecv(AndroidPipeBuffer* buffers, int numBuffers) override;
46     int onGuestSend(const AndroidPipeBuffer* buffers, int numBuffers,
47                     void** newPipePtr) override;
onGuestWantWakeOn(int flags)48     void onGuestWantWakeOn(int flags) override {}
49     void onSave(base::Stream* stream) override;
50 
51 private:
52     uint32_t mHandle;
53 };
54 
55 void registerRefcountPipeService();
56 
57 void registerOnLastRefCallback(OnLastColorBufferRef func);
58 
59 }  // namespace emulation
60 }  // namespace android
61