Home | History | Annotate | Download | only in OpenglSystemCommon
      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 "EmulatorFeatureInfo.h"
     20 #include "IOStream.h"
     21 #include "renderControl_enc.h"
     22 #include "ChecksumCalculator.h"
     23 #include "goldfish_dma.h"
     24 
     25 #include <cutils/native_handle.h>
     26 
     27 #ifdef GOLDFISH_VULKAN
     28 #include <mutex>
     29 #else
     30 #include <utils/threads.h>
     31 #endif
     32 
     33 #include <string>
     34 
     35 class GLEncoder;
     36 struct gl_client_context_t;
     37 class GL2Encoder;
     38 struct gl2_client_context_t;
     39 
     40 namespace goldfish_vk {
     41 class VkEncoder;
     42 }
     43 
     44 // ExtendedRCEncoderContext is an extended version of renderControl_encoder_context_t
     45 // that will be used to track available emulator features.
     46 class ExtendedRCEncoderContext : public renderControl_encoder_context_t {
     47 public:
     48     ExtendedRCEncoderContext(IOStream *stream, ChecksumCalculator *checksumCalculator)
     49         : renderControl_encoder_context_t(stream, checksumCalculator),
     50           m_dmaCxt(NULL) { }
     51     void setSyncImpl(SyncImpl syncImpl) { m_featureInfo.syncImpl = syncImpl; }
     52     void setDmaImpl(DmaImpl dmaImpl) { m_featureInfo.dmaImpl = dmaImpl; }
     53     void setHostComposition(HostComposition hostComposition) {
     54         m_featureInfo.hostComposition = hostComposition; }
     55     bool hasNativeSync() const { return m_featureInfo.syncImpl >= SYNC_IMPL_NATIVE_SYNC_V2; }
     56     bool hasNativeSyncV3() const { return m_featureInfo.syncImpl >= SYNC_IMPL_NATIVE_SYNC_V3; }
     57     bool hasHostCompositionV1() const {
     58         return m_featureInfo.hostComposition == HOST_COMPOSITION_V1; }
     59     DmaImpl getDmaVersion() const { return m_featureInfo.dmaImpl; }
     60     void bindDmaContext(struct goldfish_dma_context* cxt) { m_dmaCxt = cxt; }
     61     virtual uint64_t lockAndWriteDma(void* data, uint32_t size) {
     62         if (m_dmaCxt) {
     63             return lockAndWriteGoldfishDma(data, size, m_dmaCxt);
     64         } else {
     65             ALOGE("%s: ERROR: No DMA context bound!", __func__);
     66             return 0;
     67         }
     68     }
     69     void setGLESMaxVersion(GLESMaxVersion ver) { m_featureInfo.glesMaxVersion = ver; }
     70     GLESMaxVersion getGLESMaxVersion() const { return m_featureInfo.glesMaxVersion; }
     71 
     72     const EmulatorFeatureInfo* featureInfo_const() const { return &m_featureInfo; }
     73     EmulatorFeatureInfo* featureInfo() { return &m_featureInfo; }
     74 private:
     75     static uint64_t lockAndWriteGoldfishDma(void* data, uint32_t size,
     76                                             struct goldfish_dma_context* dmaCxt) {
     77         ALOGV("%s(data=%p, size=%u): call", __func__, data, size);
     78 
     79         goldfish_dma_write(dmaCxt, data, size);
     80         uint64_t paddr = goldfish_dma_guest_paddr(dmaCxt);
     81 
     82         ALOGV("%s: paddr=0x%llx", __func__, (unsigned long long)paddr);
     83         return paddr;
     84     }
     85 
     86     EmulatorFeatureInfo m_featureInfo;
     87     struct goldfish_dma_context* m_dmaCxt;
     88 };
     89 
     90 // Abstraction for gralloc handle conversion
     91 class Gralloc {
     92 public:
     93     virtual uint32_t getHostHandle(native_handle_t const* handle) = 0;
     94     virtual int getFormat(native_handle_t const* handle) = 0;
     95     virtual ~Gralloc() {}
     96 };
     97 
     98 // Abstraction for process pipe helper
     99 class ProcessPipe {
    100 public:
    101     virtual bool processPipeInit(renderControl_encoder_context_t *rcEnc) = 0;
    102     virtual ~ProcessPipe() {}
    103 };
    104 
    105 struct EGLThreadInfo;
    106 
    107 class HostConnection
    108 {
    109 public:
    110     static HostConnection *get();
    111     static HostConnection *getWithThreadInfo(EGLThreadInfo* tInfo);
    112     static void exit();
    113 
    114     static HostConnection *createUnique();
    115     static void teardownUnique(HostConnection* con);
    116 
    117     ~HostConnection();
    118 
    119     GLEncoder *glEncoder();
    120     GL2Encoder *gl2Encoder();
    121     goldfish_vk::VkEncoder *vkEncoder();
    122     ExtendedRCEncoderContext *rcEncoder();
    123     ChecksumCalculator *checksumHelper() { return &m_checksumHelper; }
    124     Gralloc *grallocHelper() { return m_grallocHelper; }
    125 
    126     void flush() {
    127         if (m_stream) {
    128             m_stream->flush();
    129         }
    130     }
    131 
    132     void setGrallocOnly(bool gralloc_only) {
    133         m_grallocOnly = gralloc_only;
    134     }
    135 
    136     bool isGrallocOnly() const { return m_grallocOnly; }
    137 
    138 #ifdef __clang__
    139 #pragma clang diagnostic push
    140 #pragma clang diagnostic ignored "-Wthread-safety-analysis"
    141 #endif
    142     void lock() const { m_lock.lock(); }
    143     void unlock() const { m_lock.unlock(); }
    144 #ifdef __clang__
    145 #pragma clang diagnostic pop
    146 #endif
    147 
    148 private:
    149     // If the connection failed, |conn| is deleted.
    150     // Returns NULL if connection failed.
    151     static HostConnection* connect(HostConnection* con);
    152 
    153     HostConnection();
    154     static gl_client_context_t  *s_getGLContext();
    155     static gl2_client_context_t *s_getGL2Context();
    156 
    157     const std::string& queryGLExtensions(ExtendedRCEncoderContext *rcEnc);
    158     // setProtocol initilizes GL communication protocol for checksums
    159     // should be called when m_rcEnc is created
    160     void setChecksumHelper(ExtendedRCEncoderContext *rcEnc);
    161     void queryAndSetSyncImpl(ExtendedRCEncoderContext *rcEnc);
    162     void queryAndSetDmaImpl(ExtendedRCEncoderContext *rcEnc);
    163     void queryAndSetGLESMaxVersion(ExtendedRCEncoderContext *rcEnc);
    164     void queryAndSetNoErrorState(ExtendedRCEncoderContext *rcEnc);
    165     void queryAndSetHostCompositionImpl(ExtendedRCEncoderContext *rcEnc);
    166     void queryAndSetDirectMemSupport(ExtendedRCEncoderContext *rcEnc);
    167     void queryAndSetVulkanSupport(ExtendedRCEncoderContext *rcEnc);
    168     void queryAndSetDeferredVulkanCommandsSupport(ExtendedRCEncoderContext *rcEnc);
    169 
    170 private:
    171     IOStream *m_stream;
    172     GLEncoder   *m_glEnc;
    173     GL2Encoder  *m_gl2Enc;
    174     goldfish_vk::VkEncoder  *m_vkEnc;
    175     ExtendedRCEncoderContext *m_rcEnc;
    176     ChecksumCalculator m_checksumHelper;
    177     Gralloc *m_grallocHelper;
    178     ProcessPipe *m_processPipe;
    179     std::string m_glExtensions;
    180     bool m_grallocOnly;
    181     bool m_noHostError;
    182 #ifdef GOLDFISH_VULKAN
    183     mutable std::mutex m_lock;
    184 #else
    185     mutable android::Mutex m_lock;
    186 #endif
    187 };
    188 
    189 #endif
    190