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 #include "IOStream.h"
20 #include "renderControl_enc.h"
21 #include "ChecksumCalculator.h"
22 #include "goldfish_dma.h"
23 
24 #include <string>
25 
26 class GLEncoder;
27 struct gl_client_context_t;
28 class GL2Encoder;
29 struct gl2_client_context_t;
30 
31 // SyncImpl determines the presence of host/guest OpenGL fence sync
32 // capabilities. It corresponds exactly to EGL_ANDROID_native_fence_sync
33 // capability, but for the emulator, we need to make sure that
34 // OpenGL pipe protocols match, so we use a special extension name
35 // here.
36 // SYNC_IMPL_NONE means that the native fence sync capability is
37 // not present, and we will end up using the equivalent of glFinish
38 // in order to preserve buffer swapping order.
39 // SYNC_IMPL_NATIVE_SYNC means that we do have native fence sync
40 // capability, and we will use a fence fd to synchronize buffer swaps.
41 enum SyncImpl {
42     SYNC_IMPL_NONE = 0,
43     SYNC_IMPL_NATIVE_SYNC = 1
44 };
45 
46 // Interface:
47 // If this GL extension string shows up, we use
48 // SYNC_IMPL_NATIVE_SYNC, otherwise we use SYNC_IMPL_NONE.
49 // This string is always updated to require the _latest_
50 // version of Android emulator native sync in this system image;
51 // otherwise, we do not use the feature.
52 static const char kRCNativeSync[] = "ANDROID_EMU_native_sync_v2";
53 
54 // DMA for OpenGL
55 enum DmaImpl {
56     DMA_IMPL_NONE = 0,
57     DMA_IMPL_v1 = 1,
58 };
59 
60 static const char kDmaExtStr_v1[] = "ANDROID_EMU_dma_v1";
61 
62 // OpenGL ES max supported version
63 enum GLESMaxVersion {
64     GLES_MAX_VERSION_2 = 0,
65     GLES_MAX_VERSION_3_0 = 1,
66     GLES_MAX_VERSION_3_1 = 2,
67     GLES_MAX_VERSION_3_2 = 3,
68 };
69 
70 static const char kGLESMaxVersion_2[] = "ANDROID_EMU_gles_max_version_2";
71 static const char kGLESMaxVersion_3_0[] = "ANDROID_EMU_gles_max_version_3_0";
72 static const char kGLESMaxVersion_3_1[] = "ANDROID_EMU_gles_max_version_3_1";
73 static const char kGLESMaxVersion_3_2[] = "ANDROID_EMU_gles_max_version_3_2";
74 
75 // ExtendedRCEncoderContext is an extended version of renderControl_encoder_context_t
76 // that will be used to track SyncImpl.
77 class ExtendedRCEncoderContext : public renderControl_encoder_context_t {
78 public:
ExtendedRCEncoderContext(IOStream * stream,ChecksumCalculator * checksumCalculator)79     ExtendedRCEncoderContext(IOStream *stream, ChecksumCalculator *checksumCalculator)
80         : renderControl_encoder_context_t(stream, checksumCalculator) {
81         m_dmaCxt = NULL;
82         }
setSyncImpl(SyncImpl syncImpl)83     void setSyncImpl(SyncImpl syncImpl) { m_syncImpl = syncImpl; }
setDmaImpl(DmaImpl dmaImpl)84     void setDmaImpl(DmaImpl dmaImpl) { m_dmaImpl = dmaImpl; }
hasNativeSync()85     bool hasNativeSync() const { return m_syncImpl == SYNC_IMPL_NATIVE_SYNC; }
getDmaVersion()86     DmaImpl getDmaVersion() const { return m_dmaImpl; }
bindDmaContext(struct goldfish_dma_context * cxt)87     void bindDmaContext(struct goldfish_dma_context* cxt) { m_dmaCxt = cxt; }
lockAndWriteDma(void * data,uint32_t size)88     virtual uint64_t lockAndWriteDma(void* data, uint32_t size) {
89         ALOGV("%s: call", __FUNCTION__);
90         if (!m_dmaCxt) {
91             ALOGE("%s: ERROR: No DMA context bound!",
92                   __FUNCTION__);
93             return 0;
94         }
95         goldfish_dma_lock(m_dmaCxt);
96         goldfish_dma_write(m_dmaCxt, data, size);
97         uint64_t paddr = goldfish_dma_guest_paddr(m_dmaCxt);
98         ALOGV("%s: paddr=0x%llx", __FUNCTION__, paddr);
99         return paddr;
100     }
setGLESMaxVersion(GLESMaxVersion ver)101     void setGLESMaxVersion(GLESMaxVersion ver) { m_glesMaxVersion = ver; }
getGLESMaxVersion()102     GLESMaxVersion getGLESMaxVersion() const { return m_glesMaxVersion; }
103 private:
104     SyncImpl m_syncImpl;
105     DmaImpl m_dmaImpl;
106     struct goldfish_dma_context* m_dmaCxt;
107     GLESMaxVersion m_glesMaxVersion;
108 };
109 
110 struct EGLThreadInfo;
111 
112 class HostConnection
113 {
114 public:
115     static HostConnection *get();
116     static HostConnection *getWithThreadInfo(EGLThreadInfo* tInfo);
117     static void exit();
118     ~HostConnection();
119 
120     GLEncoder *glEncoder();
121     GL2Encoder *gl2Encoder();
122     ExtendedRCEncoderContext *rcEncoder();
checksumHelper()123     ChecksumCalculator *checksumHelper() { return &m_checksumHelper; }
124 
flush()125     void flush() {
126         if (m_stream) {
127             m_stream->flush();
128         }
129     }
130 
setGrallocOnly(bool gralloc_only)131     void setGrallocOnly(bool gralloc_only) {
132         m_grallocOnly = gralloc_only;
133     }
134 
isGrallocOnly()135     bool isGrallocOnly() const { return m_grallocOnly; }
136 
getPipeFd()137     int getPipeFd() const { return m_pipeFd; }
138 
139 private:
140     HostConnection();
141     static gl_client_context_t  *s_getGLContext();
142     static gl2_client_context_t *s_getGL2Context();
143 
144     const std::string& queryGLExtensions(ExtendedRCEncoderContext *rcEnc);
145     // setProtocol initilizes GL communication protocol for checksums
146     // should be called when m_rcEnc is created
147     void setChecksumHelper(ExtendedRCEncoderContext *rcEnc);
148     void queryAndSetSyncImpl(ExtendedRCEncoderContext *rcEnc);
149     void queryAndSetDmaImpl(ExtendedRCEncoderContext *rcEnc);
150     void queryAndSetGLESMaxVersion(ExtendedRCEncoderContext *rcEnc);
151 
152 private:
153     IOStream *m_stream;
154     GLEncoder   *m_glEnc;
155     GL2Encoder  *m_gl2Enc;
156     ExtendedRCEncoderContext *m_rcEnc;
157     ChecksumCalculator m_checksumHelper;
158     std::string m_glExtensions;
159     bool m_grallocOnly;
160     int m_pipeFd;
161 };
162 
163 #endif
164