• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "host-common/window_agent.h"
32 
33 namespace gfxstream {
34 class ColorBuffer;
35 class FrameBuffer;
36 struct RenderThreadInfo;
37 
38 class PostWorker {
39    public:
40     PostWorker(bool mainThreadPostingOnly, FrameBuffer* fb, Compositor* compositor);
41     virtual ~PostWorker();
42 
43     // post: posts the next color buffer.
44     // Assumes framebuffer lock is held.
45     void post(ColorBuffer* cb, std::unique_ptr<Post::CompletionCallback> postCallback);
46 
47     // viewport: (re)initializes viewport dimensions.
48     // Assumes framebuffer lock is held.
49     // This is called whenever the subwindow needs a refresh
50     // (FrameBuffer::setupSubWindow).
51     void viewport(int width, int height);
52 
53     // compose: compse the layers into final framebuffer. The callback will be
54     // called when the CPU side job completes. The passed in future in the
55     // callback will be completed when the GPU opereation completes.
56     void compose(std::unique_ptr<FlatComposeRequest> composeRequest,
57                  std::unique_ptr<Post::CompletionCallback> composeCallback);
58 
59     // clear: blanks out emulator display when refreshing the subwindow
60     // if there is no last posted color buffer to show yet.
61     void clear();
62 
63     virtual void screenshot(ColorBuffer* cb, int screenwidth, int screenheight, GLenum format,
64                             GLenum type, int skinRotation, void* pixels, Rect rect) = 0;
65 
66     // The block task will set the scheduledSignal promise when the task is scheduled, and wait
67     // until continueSignal is ready before completes.
68     void block(std::promise<void> scheduledSignal, std::future<void> continueSignal);
69 
70     // Exit post worker, unbind gl context if necessary.
71     void exit();
72 
73    protected:
74     void runTask(std::packaged_task<void()>);
75     // Impl versions of the above, so we can run it from separate threads
76     virtual std::shared_future<void> postImpl(ColorBuffer* cb) = 0;
77     virtual void viewportImpl(int width, int height) = 0;
78     virtual void clearImpl() = 0;
79     virtual void exitImpl() = 0;
80     virtual std::shared_future<void> composeImpl(const FlatComposeRequest& composeRequest);
81 
82    protected:
83     FrameBuffer* mFb;
84     Compositor* m_compositor = nullptr;
85 
86    protected:
87     // If m_mainThreadPostingOnly is true, schedule the task to UI thread by
88     // using m_runOnUiThread. Otherwise, execute the task on the current thread.
89     bool m_mainThreadPostingOnly = false;
90 
91    private:
92     using UiThreadRunner = std::function<void(UiUpdateFunc, void*, bool)>;
93 
94     UiThreadRunner m_runOnUiThread = 0;
95 
96     std::unordered_map<uint32_t, std::shared_future<void>> m_composeTargetToComposeFuture;
97 
98     bool isComposeTargetReady(uint32_t targetHandle);
99 
100     DISALLOW_COPY_AND_ASSIGN(PostWorker);
101 };
102 
103 }  // namespace gfxstream
104