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