OSDN Git Service

2c8151717d3c0553aef6f0f3a998c21c3df7d588
[android-x86/device-generic-goldfish-opengl.git] / system / OpenglSystemCommon / HostConnection.h
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:
79     ExtendedRCEncoderContext(IOStream *stream, ChecksumCalculator *checksumCalculator)
80         : renderControl_encoder_context_t(stream, checksumCalculator) {
81         m_dmaCxt = NULL;
82         }
83     void setSyncImpl(SyncImpl syncImpl) { m_syncImpl = syncImpl; }
84     void setDmaImpl(DmaImpl dmaImpl) { m_dmaImpl = dmaImpl; }
85     bool hasNativeSync() const { return m_syncImpl == SYNC_IMPL_NATIVE_SYNC; }
86     DmaImpl getDmaVersion() const { return m_dmaImpl; }
87     void bindDmaContext(struct goldfish_dma_context* cxt) { m_dmaCxt = cxt; }
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     }
101     void setGLESMaxVersion(GLESMaxVersion ver) { m_glesMaxVersion = ver; }
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 class HostConnection
111 {
112 public:
113     static HostConnection *get();
114     static void exit();
115     ~HostConnection();
116
117     GLEncoder *glEncoder();
118     GL2Encoder *gl2Encoder();
119     ExtendedRCEncoderContext *rcEncoder();
120     ChecksumCalculator *checksumHelper() { return &m_checksumHelper; }
121
122     void flush() {
123         if (m_stream) {
124             m_stream->flush();
125         }
126     }
127
128     void setGrallocOnly(bool gralloc_only) {
129         m_grallocOnly = gralloc_only;
130     }
131
132     bool isGrallocOnly() const { return m_grallocOnly; }
133
134     int getPipeFd() const { return m_pipeFd; }
135
136 private:
137     HostConnection();
138     static gl_client_context_t  *s_getGLContext();
139     static gl2_client_context_t *s_getGL2Context();
140
141     const std::string& queryGLExtensions(ExtendedRCEncoderContext *rcEnc);
142     // setProtocol initilizes GL communication protocol for checksums
143     // should be called when m_rcEnc is created
144     void setChecksumHelper(ExtendedRCEncoderContext *rcEnc);
145     void queryAndSetSyncImpl(ExtendedRCEncoderContext *rcEnc);
146     void queryAndSetDmaImpl(ExtendedRCEncoderContext *rcEnc);
147     void queryAndSetGLESMaxVersion(ExtendedRCEncoderContext *rcEnc);
148
149 private:
150     IOStream *m_stream;
151     GLEncoder   *m_glEnc;
152     GL2Encoder  *m_gl2Enc;
153     ExtendedRCEncoderContext *m_rcEnc;
154     ChecksumCalculator m_checksumHelper;
155     std::string m_glExtensions;
156     bool m_grallocOnly;
157     int m_pipeFd;
158 };
159
160 #endif