1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 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 */ 15 16 #ifndef RENDER_ENGINE_H 17 #define RENDER_ENGINE_H 18 19 #include <atomic> 20 #include <memory> 21 #include <mutex> 22 #include <thread> 23 #include <vector> 24 #include <functional> 25 #include <condition_variable> 26 27 #include <GLES2/gl2.h> 28 #include <native_image/native_image.h> 29 #include <native_window/external_window.h> 30 #include <native_vsync/native_vsync.h> 31 #include "image_render.h" 32 #include "native_render.h" 33 34 class RenderEngine { 35 public: 36 using RenderTask = std::function<void(std::shared_ptr<ImageRender> render)>; 37 38 RenderEngine(std::shared_ptr<ImageRender> imageRender, uint64_t width, uint64_t height, void *window); 39 ~RenderEngine(); 40 41 // 禁用拷贝和移动 42 RenderEngine(const RenderEngine &other) = delete; 43 RenderEngine &operator=(const RenderEngine &other) = delete; 44 RenderEngine(RenderEngine &&other) = delete; 45 RenderEngine &operator=(RenderEngine &&other) = delete; 46 47 // 启动渲染引擎 48 void Start(); 49 50 // 停止渲染引擎 51 void Stop(); 52 53 // 发布任务到渲染线程 54 void PostTask(const RenderTask &task); 55 56 // 更新 NativeWindow 57 void UpdateNativeWindow(uint64_t width, uint64_t height); 58 void OnSurfaceChanged(uint64_t width, uint64_t height); 59 void OnAppResume(); 60 void OnAppPause(); 61 private: 62 static void OnNativeImageFrameAvailable(void *data); 63 // Vsync 相关 64 bool InitNativeVsync(); 65 void DestroyNativeVsync(); 66 static void OnVsync(long long timestamp, void *data); 67 68 // 主循环 69 void MainLoop(); 70 71 // 渲染线程初始化 72 void InitializeRendering(); 73 void CleanupResources(); 74 75 // 渲染流程任务 76 void WaitForNewFrame(); 77 void WaitForVsync(); 78 void ExecuteRenderTasks(); 79 void UpdateSurfaceImage(); 80 void UpdateTextureMatrix(); 81 82 // 创建与销毁 NativeImage 83 bool CreateNativeImage(); 84 bool StartNativeRenderThread(); 85 void StopNativeRenderThread(); 86 void DestroyNativeImage(); 87 88 bool GenerateTexture(); 89 bool CreateNativeImageObject(); 90 bool AttachContextAndGetSurfaceId(); 91 bool SetFrameAvailableListener(); 92 93 // NativeRender 相关 94 std::shared_ptr<OHNativeRender> nativeRender_; 95 std::thread nativeRenderThread_; 96 97 // NativeImage 相关 98 OH_OnFrameAvailableListener nativeImageFrameAvailableListener_{}; 99 OH_NativeImage *nativeImage_ = nullptr; 100 GLuint nativeImageTexId_ = 0U; 101 std::mutex nativeImageSurfaceIdMutex_; 102 uint64_t nativeImageSurfaceId_ = 0; 103 std::atomic<int> availableFrameCnt_{0}; 104 std::condition_variable frameCond_; 105 std::mutex frameMutex_; 106 107 // 渲染引擎运行状态 108 std::atomic<bool> running_{false}; 109 std::thread thread_; 110 std::thread::id threadId_; 111 112 // 同步和任务队列 113 std::mutex taskMutex_; 114 std::vector<RenderTask> tasks_; 115 mutable std::mutex wakeUpMutex_; 116 std::condition_variable wakeUpCond_; 117 bool wakeUp_ = false; 118 std::atomic<int> vSyncCnt_{0}; 119 OH_NativeVSync *nativeVsync_ = nullptr; 120 121 // 关联的 ImageRender 实例 122 std::shared_ptr<ImageRender> imageRender_; 123 void *window_ = nullptr; 124 uint64_t width_ = 0; 125 uint64_t height_ = 0; 126 long long period = 0; 127 // 其他状态变量 128 std::atomic<bool> isPaused_{false}; 129 }; 130 131 #endif // RENDER_ENGINE_H 132