1 /*
2 * Copyright (C) 2011 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 #ifndef __COMMON_HOST_CONNECTION_H
17 #define __COMMON_HOST_CONNECTION_H
18 
19 #if defined(ANDROID)
20 #include "gfxstream/guest/ANativeWindow.h"
21 #include "gfxstream/guest/GfxStreamGralloc.h"
22 #endif
23 
24 #include <cstring>
25 #include <memory>
26 #include <mutex>
27 #include <optional>
28 #include <string>
29 
30 #include "ExtendedRenderControl.h"
31 #include "Sync.h"
32 #include "VirtGpu.h"
33 
34 class GLEncoder;
35 struct gl_client_context_t;
36 class GL2Encoder;
37 struct gl2_client_context_t;
38 
39 struct EGLThreadInfo;
40 
41 enum HostConnectionType {
42     HOST_CONNECTION_QEMU_PIPE = 1,
43     HOST_CONNECTION_ADDRESS_SPACE = 2,
44     HOST_CONNECTION_VIRTIO_GPU_PIPE = 3,
45     HOST_CONNECTION_VIRTIO_GPU_ADDRESS_SPACE = 4,
46 };
47 
48 class HostConnection
49 {
50 public:
51     static HostConnection *get();
52     static HostConnection* getOrCreate(enum VirtGpuCapset capset = kCapsetNone);
53     static HostConnection* getWithThreadInfo(EGLThreadInfo* tInfo, enum VirtGpuCapset capset);
54     static void exit();
55 
56     static std::unique_ptr<HostConnection> createUnique(enum VirtGpuCapset capset);
57     HostConnection(const HostConnection&) = delete;
58 
59     ~HostConnection();
60 
61     GLEncoder *glEncoder();
62     GL2Encoder *gl2Encoder();
63     ExtendedRCEncoderContext *rcEncoder();
64 
65 #if defined(ANDROID)
anwHelper()66     gfxstream::ANativeWindowHelper* anwHelper() { return m_anwHelper.get(); }
grallocHelper()67     gfxstream::Gralloc* grallocHelper() { return m_grallocHelper.get(); }
68 #endif
syncHelper()69     gfxstream::SyncHelper* syncHelper() { return m_syncHelper.get(); }
70 
flush()71     void flush() {
72         if (m_stream) {
73             m_stream->flush();
74         }
75     }
76 
77 #ifdef __clang__
78 #pragma clang diagnostic push
79 #pragma clang diagnostic ignored "-Wthread-safety-analysis"
80 #endif
lock()81     void lock() const { m_lock.lock(); }
unlock()82     void unlock() const { m_lock.unlock(); }
83 #ifdef __clang__
84 #pragma clang diagnostic pop
85 #endif
86 
87     void setVulkanFeatureInfo(void* info);
88 
89    private:
90     // If the connection failed, |conn| is deleted.
91     // Returns NULL if connection failed.
92  static std::unique_ptr<HostConnection> connect(enum VirtGpuCapset capset);
93 
94  HostConnection();
95  static gl_client_context_t* s_getGLContext();
96  static gl2_client_context_t* s_getGL2Context();
97 
98 private:
99  HostConnectionType m_connectionType;
100 
101  // intrusively refcounted
102  gfxstream::guest::IOStream* m_stream = nullptr;
103 
104  std::unique_ptr<GLEncoder> m_glEnc;
105  std::unique_ptr<GL2Encoder> m_gl2Enc;
106 
107  // intrusively refcounted
108  std::unique_ptr<ExtendedRCEncoderContext> m_rcEnc;
109 
110  gfxstream::guest::ChecksumCalculator m_checksumHelper;
111 #if defined(ANDROID)
112  std::unique_ptr<gfxstream::ANativeWindowHelper> m_anwHelper;
113  std::unique_ptr<gfxstream::Gralloc> m_grallocHelper;
114 #endif
115  std::unique_ptr<gfxstream::SyncHelper> m_syncHelper;
116  bool m_noHostError;
117  mutable std::mutex m_lock;
118  int m_rendernodeFd;
119 };
120 
121 #endif
122