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