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 "Renderer.h" 17 18 #include "RenderWindow.h" 19 20 #include "base/Compiler.h" 21 #include "base/Lock.h" 22 #include "base/MessageChannel.h" 23 #include "base/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 emugl { 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) override final; 54 void addressSpaceGraphicsConsumerDestroy(void*) override final; 55 void addressSpaceGraphicsConsumerPreSave(void* consumer) override final; 56 void addressSpaceGraphicsConsumerSave(void* consumer, android::base::Stream* stream) override final; 57 void addressSpaceGraphicsConsumerPostSave(void* consumer) override final; 58 void addressSpaceGraphicsConsumerRegisterPostLoadRenderThread(void* consumer) override final; 59 60 HardwareStrings getHardwareStrings() final; 61 void setPostCallback(OnPostCallback onPost, 62 void* context, 63 bool useBgraReadback, 64 uint32_t displayId) final; 65 bool asyncReadbackSupported() final; 66 ReadPixelsCallback getReadPixelsCallback() final; 67 FlushReadPixelPipeline getFlushReadPixelPipeline() final; 68 bool showOpenGLSubwindow(FBNativeWindowType window, 69 int wx, 70 int wy, 71 int ww, 72 int wh, 73 int fbw, 74 int fbh, 75 float dpr, 76 float zRot, 77 bool deleteExisting, 78 bool hideWindow) final; 79 bool destroyOpenGLSubwindow() final; 80 void setOpenGLDisplayRotation(float zRot) final; 81 void setOpenGLDisplayTranslation(float px, float py) final; 82 void repaintOpenGLDisplay() final; 83 84 bool hasGuestPostedAFrame() final; 85 void resetGuestPostedAFrame() final; 86 87 void setScreenMask(int width, int height, const unsigned char* rgbaData) final; 88 void setMultiDisplay(uint32_t id, 89 int32_t x, 90 int32_t y, 91 uint32_t w, 92 uint32_t h, 93 uint32_t dpi, 94 bool add) final; 95 void setMultiDisplayColorBuffer(uint32_t id, uint32_t cb) override; 96 void cleanupProcGLObjects(uint64_t puid) final; 97 void waitForProcessCleanup() final; 98 struct AndroidVirtioGpuOps* getVirtioGpuOps() final; 99 100 void pauseAllPreSave() final; 101 void resumeAll() final; 102 103 void save(android::base::Stream* stream, 104 const android::snapshot::ITextureSaverPtr& textureSaver) final; 105 bool load(android::base::Stream* stream, 106 const android::snapshot::ITextureLoaderPtr& textureLoader) final; 107 void fillGLESUsages(android_studio::EmulatorGLESUsages*) final; 108 void getScreenshot(unsigned int nChannels, unsigned int* width, 109 unsigned int* height, std::vector<unsigned char>& pixels, 110 int displayId, int desiredWidth, int desiredHeight, 111 int desiredRotation) final; 112 void snapshotOperationCallback( 113 int snapshotterOp, 114 int snapshotterStage) final; 115 116 private: 117 DISALLOW_COPY_ASSIGN_AND_MOVE(RendererImpl); 118 119 private: 120 // Stop all render threads and wait until they exit, 121 // and also delete them. 122 void cleanupRenderThreads(); 123 124 std::unique_ptr<RenderWindow> mRenderWindow; 125 126 android::base::Lock mChannelsLock; 127 128 std::vector<std::shared_ptr<RenderChannelImpl>> mChannels; 129 std::vector<std::shared_ptr<RenderChannelImpl>> mStoppedChannels; 130 bool mStopped = false; 131 132 class ProcessCleanupThread; 133 std::unique_ptr<ProcessCleanupThread> mCleanupThread; 134 135 std::unique_ptr<RenderThread> mLoaderRenderThread; 136 137 std::vector<RenderThread*> mAdditionalPostLoadRenderThreads; 138 }; 139 140 } // namespace emugl 141