1 // Copyright (C) 2016 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 <memory> 17 #include <unordered_set> 18 #include <utility> 19 #include <vector> 20 21 #include "RenderThread.h" 22 #include "RenderWindow.h" 23 #include "aemu/base/Compiler.h" 24 #include "aemu/base/synchronization/Lock.h" 25 #include "aemu/base/synchronization/MessageChannel.h" 26 #include "aemu/base/threads/FunctorThread.h" 27 #include "gfxstream/host/Features.h" 28 #include "render-utils/Renderer.h" 29 #include "snapshot/common.h" 30 31 namespace android_studio { 32 class EmulatorGLESUsages; 33 } 34 35 namespace gfxstream { 36 37 class RendererImpl final : public Renderer { 38 public: 39 RendererImpl(); 40 ~RendererImpl(); 41 42 bool initialize(int width, int height, gfxstream::host::FeatureSet features, bool useSubWindow, 43 bool egl2egl); 44 void stop(bool wait) override; 45 void finish() override; 46 47 public: 48 RenderChannelPtr createRenderChannel( 49 android::base::Stream* loadStream, uint32_t virtioGpuContextId) final; 50 51 void* addressSpaceGraphicsConsumerCreate( 52 struct asg_context, 53 android::base::Stream* stream, 54 android::emulation::asg::ConsumerCallbacks, 55 uint32_t contextId, uint32_t capsetId, 56 std::optional<std::string> name) override final; 57 void addressSpaceGraphicsConsumerDestroy(void*) override final; 58 void addressSpaceGraphicsConsumerPreSave(void* consumer) override final; 59 void addressSpaceGraphicsConsumerSave(void* consumer, android::base::Stream* stream) override final; 60 void addressSpaceGraphicsConsumerPostSave(void* consumer) override final; 61 void addressSpaceGraphicsConsumerRegisterPostLoadRenderThread(void* consumer) override final; 62 63 HardwareStrings getHardwareStrings() final; 64 void setPostCallback(OnPostCallback onPost, 65 void* context, 66 bool useBgraReadback, 67 uint32_t displayId) final; 68 bool asyncReadbackSupported() final; 69 ReadPixelsCallback getReadPixelsCallback() final; 70 FlushReadPixelPipeline getFlushReadPixelPipeline() final; 71 bool showOpenGLSubwindow(FBNativeWindowType window, 72 int wx, 73 int wy, 74 int ww, 75 int wh, 76 int fbw, 77 int fbh, 78 float dpr, 79 float zRot, 80 bool deleteExisting, 81 bool hideWindow) final; 82 bool destroyOpenGLSubwindow() final; 83 void setOpenGLDisplayRotation(float zRot) final; 84 void setOpenGLDisplayTranslation(float px, float py) final; 85 void repaintOpenGLDisplay() final; 86 87 bool hasGuestPostedAFrame() final; 88 void resetGuestPostedAFrame() final; 89 90 void setScreenMask(int width, int height, const unsigned char* rgbaData) final; 91 void setMultiDisplay(uint32_t id, 92 int32_t x, 93 int32_t y, 94 uint32_t w, 95 uint32_t h, 96 uint32_t dpi, 97 bool add) final; 98 void setMultiDisplayColorBuffer(uint32_t id, uint32_t cb) override; 99 void onGuestGraphicsProcessCreate(uint64_t puid) final; 100 // TODO(kaiyili): rename this interface to onGuestGraphicsProcessDestroy. 101 void cleanupProcGLObjects(uint64_t puid) final; 102 void waitForProcessCleanup() final; 103 struct AndroidVirtioGpuOps* getVirtioGpuOps() final; 104 105 void pauseAllPreSave() final; 106 void resumeAll(bool waitForSave = true) final; 107 108 void save(android::base::Stream* stream, 109 const android::snapshot::ITextureSaverPtr& textureSaver) final; 110 bool load(android::base::Stream* stream, 111 const android::snapshot::ITextureLoaderPtr& textureLoader) final; 112 void fillGLESUsages(android_studio::EmulatorGLESUsages*) final; 113 int getScreenshot(unsigned int nChannels, unsigned int* width, unsigned int* height, 114 uint8_t* pixels, size_t* cPixels, int displayId, int desiredWidth, 115 int desiredHeight, int desiredRotation, Rect rect) final; 116 117 void snapshotOperationCallback( 118 int snapshotterOp, 119 int snapshotterStage) final; 120 121 void addListener(FrameBufferChangeEventListener* listener) override; 122 void removeListener(FrameBufferChangeEventListener* listener) override; 123 124 void setVsyncHz(int vsyncHz) final; 125 void setDisplayConfigs(int configId, int w, int h, int dpiX, int dpiY) override; 126 void setDisplayActiveConfig(int configId) override; 127 128 const void* getEglDispatch() override; 129 const void* getGles2Dispatch() override; 130 131 private: 132 DISALLOW_COPY_ASSIGN_AND_MOVE(RendererImpl); 133 134 private: 135 // Stop all render threads and wait until they exit, 136 // and also delete them. 137 void cleanupRenderThreads(); 138 139 std::unique_ptr<RenderWindow> mRenderWindow; 140 141 android::base::Lock mChannelsLock; 142 143 std::vector<std::shared_ptr<RenderChannelImpl>> mChannels; 144 std::vector<std::shared_ptr<RenderChannelImpl>> mStoppedChannels; 145 bool mStopped = false; 146 147 class ProcessCleanupThread; 148 std::unique_ptr<ProcessCleanupThread> mCleanupThread; 149 150 std::unique_ptr<RenderThread> mLoaderRenderThread; 151 152 std::vector<RenderThread*> mAdditionalPostLoadRenderThreads; 153 154 android::base::Lock mAddressSpaceRenderThreadLock; 155 std::unordered_set<RenderThread*> mAddressSpaceRenderThreads; 156 }; 157 158 } // namespace gfxstream 159