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 "base/Compiler.h" 19 #include "base/MessageChannel.h" 20 #include "host-common/window_agent.h" 21 22 #include <EGL/egl.h> 23 #include <GLES/gl.h> 24 #include <GLES3/gl3.h> 25 26 #include <functional> 27 #include <vector> 28 29 #include "Hwc2.h" 30 #include "base/Compiler.h" 31 #include "base/Lock.h" 32 33 class ColorBuffer; 34 class FrameBuffer; 35 struct RenderThreadInfo; 36 37 class PostWorker { 38 public: 39 using BindSubwinCallback = std::function<bool(void)>; 40 41 PostWorker(BindSubwinCallback&& cb, 42 bool mainThreadPostingOnly, 43 EGLContext eglContext, 44 EGLSurface eglSurface); 45 ~PostWorker(); 46 47 // post: posts the next color buffer. 48 // Assumes framebuffer lock is held. 49 void post(ColorBuffer* cb); 50 51 // viewport: (re)initializes viewport dimensions. 52 // Assumes framebuffer lock is held. 53 // This is called whenever the subwindow needs a refresh 54 // (FrameBuffer::setupSubWindow). 55 void viewport(int width, int height); 56 57 // compose: compse the layers into final framebuffer 58 void compose(ComposeDevice* p, uint32_t bufferSize); 59 60 // compose: compse the layers into final framebuffer, version 2 61 void compose(ComposeDevice_v2* p, uint32_t bufferSize); 62 63 // clear: blanks out emulator display when refreshing the subwindow 64 // if there is no last posted color buffer to show yet. 65 void clear(); 66 67 void screenshot(ColorBuffer* cb, 68 int screenwidth, 69 int screenheight, 70 GLenum format, 71 GLenum type, 72 int skinRotation, 73 void* pixels); 74 75 private: 76 // Impl versions of the above, so we can run it from separate threads 77 void postImpl(ColorBuffer* cb); 78 void viewportImpl(int width, int height); 79 void composeImpl(ComposeDevice* p); 80 void composev2Impl(ComposeDevice_v2* p); 81 void clearImpl(); 82 83 // Subwindow binding 84 void bind(); 85 void unbind(); 86 87 void composeLayer(ComposeLayer* l, uint32_t w, uint32_t h); 88 void fillMultiDisplayPostStruct(ComposeLayer* l, 89 hwc_rect_t displayArea, 90 hwc_frect_t cropArea, 91 hwc_transform_t transform); 92 93 private: 94 using UiThreadRunner = std::function<void(UiUpdateFunc, void*, bool)>; 95 struct PostArgs { 96 ColorBuffer* postCb; 97 int width; 98 int height; 99 std::vector<char> composeBuffer; 100 }; 101 102 FrameBuffer* mFb; 103 104 std::function<bool(void)> mBindSubwin; 105 106 bool m_initialized = false; 107 int m_viewportWidth = 0; 108 int m_viewportHeight = 0; 109 GLuint m_composeFbo = 0; 110 111 bool m_mainThreadPostingOnly = false; 112 UiThreadRunner m_runOnUiThread = 0; 113 android::base::MessageChannel<PostArgs, 1> m_toUiThread; 114 EGLContext mContext = EGL_NO_CONTEXT; 115 116 DISALLOW_COPY_AND_ASSIGN(PostWorker); 117 }; 118