1 /* 2 * Copyright (C) 2017 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 #pragma once 17 18 #include <functional> 19 #include <future> 20 #include <optional> 21 #include <unordered_map> 22 #include <vector> 23 24 #include "Compositor.h" 25 #include "ContextHelper.h" 26 #include "Hwc2.h" 27 #include "PostCommands.h" 28 #include "aemu/base/Compiler.h" 29 #include "aemu/base/synchronization/Lock.h" 30 #include "aemu/base/synchronization/MessageChannel.h" 31 #include "gl/DisplayGl.h" 32 #include "host-common/window_agent.h" 33 34 namespace gfxstream { 35 namespace gl { 36 class DisplayGl; 37 } // namespace gl 38 } // namespace gfxstream 39 40 namespace gfxstream { 41 namespace vk { 42 class DisplayVk; 43 } // namespace vk 44 } // namespace gfxstream 45 46 namespace gfxstream { 47 class ColorBuffer; 48 class FrameBuffer; 49 struct RenderThreadInfo; 50 51 class PostWorker { 52 public: 53 PostWorker(bool mainThreadPostingOnly, Compositor* compositor, gl::DisplayGl* displayGl, 54 vk::DisplayVk* displayVk); 55 ~PostWorker(); 56 57 // post: posts the next color buffer. 58 // Assumes framebuffer lock is held. 59 void post(ColorBuffer* cb, std::unique_ptr<Post::CompletionCallback> postCallback); 60 61 // viewport: (re)initializes viewport dimensions. 62 // Assumes framebuffer lock is held. 63 // This is called whenever the subwindow needs a refresh 64 // (FrameBuffer::setupSubWindow). 65 void viewport(int width, int height); 66 67 // compose: compse the layers into final framebuffer. The callback will be 68 // called when the CPU side job completes. The passed in future in the 69 // callback will be completed when the GPU opereation completes. 70 void compose(std::unique_ptr<FlatComposeRequest> composeRequest, 71 std::unique_ptr<Post::CompletionCallback> composeCallback); 72 73 // clear: blanks out emulator display when refreshing the subwindow 74 // if there is no last posted color buffer to show yet. 75 void clear(); 76 77 void screenshot(ColorBuffer* cb, int screenwidth, int screenheight, GLenum format, GLenum type, 78 int skinRotation, void* pixels, Rect rect); 79 80 // The block task will set the scheduledSignal promise when the task is scheduled, and wait 81 // until continueSignal is ready before completes. 82 void block(std::promise<void> scheduledSignal, std::future<void> continueSignal); 83 84 private: 85 // Impl versions of the above, so we can run it from separate threads 86 std::shared_future<void> postImpl(ColorBuffer* cb); 87 gl::DisplayGl::PostLayer postWithOverlay(ColorBuffer* cb); 88 void viewportImpl(int width, int height); 89 std::shared_future<void> composeImpl(const FlatComposeRequest& composeRequest); 90 void clearImpl(); 91 92 // If m_mainThreadPostingOnly is true, schedule the task to UI thread by 93 // using m_runOnUiThread. Otherwise, execute the task on the current thread. 94 void runTask(std::packaged_task<void()>); 95 96 private: 97 using UiThreadRunner = std::function<void(UiUpdateFunc, void*, bool)>; 98 99 FrameBuffer* mFb; 100 101 Compositor* m_compositor = nullptr; 102 103 int m_viewportWidth = 0; 104 int m_viewportHeight = 0; 105 106 bool m_mainThreadPostingOnly = false; 107 UiThreadRunner m_runOnUiThread = 0; 108 109 // TODO(b/233939967): conslidate DisplayGl and DisplayVk into 110 // `Display* const m_display`. 111 gl::DisplayGl* const m_displayGl; 112 // The implementation for Vulkan native swapchain. Only initialized when 113 // useVulkan is set when calling FrameBuffer::initialize(). PostWorker 114 // doesn't take the ownership of this DisplayVk object. 115 vk::DisplayVk* const m_displayVk; 116 std::unordered_map<uint32_t, std::shared_future<void>> m_composeTargetToComposeFuture; 117 118 bool isComposeTargetReady(uint32_t targetHandle); 119 120 DISALLOW_COPY_AND_ASSIGN(PostWorker); 121 }; 122 123 } // namespace gfxstream 124