|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2023 Project CHIP Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +#include "ui.h" |
| 18 | + |
| 19 | +#include <SDL.h> |
| 20 | +#include <SDL_opengl.h> |
| 21 | +#include <imgui.h> |
| 22 | +#include <imgui_impl_opengl3.h> |
| 23 | +#include <imgui_impl_sdl2.h> |
| 24 | + |
| 25 | +#include <lib/support/logging/CHIPLogging.h> |
| 26 | + |
| 27 | +#include <atomic> |
| 28 | +#include <thread> |
| 29 | + |
| 30 | +namespace example { |
| 31 | +namespace Ui { |
| 32 | +namespace { |
| 33 | + |
| 34 | +// Controls running the UI event loop |
| 35 | +std::atomic<bool> gUiRunning{ false }; |
| 36 | + |
| 37 | +void UiInit(SDL_GLContext * gl_context, SDL_Window ** window) |
| 38 | +{ |
| 39 | + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) |
| 40 | + { |
| 41 | + ChipLogError(AppServer, "SDL Init Error: %s\n", SDL_GetError()); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | +#if defined(__APPLE__) |
| 46 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); // Always required on Mac |
| 47 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); |
| 48 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); |
| 49 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); |
| 50 | +#else |
| 51 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0); |
| 52 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); |
| 53 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); |
| 54 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); |
| 55 | +#endif |
| 56 | + |
| 57 | +#ifdef SDL_HINT_IME_SHOW_UI |
| 58 | + SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1"); |
| 59 | +#endif |
| 60 | + |
| 61 | + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
| 62 | + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); |
| 63 | + SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); |
| 64 | + SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI); |
| 65 | + *window = SDL_CreateWindow("Light UI", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags); |
| 66 | + *gl_context = SDL_GL_CreateContext(*window); |
| 67 | + SDL_GL_MakeCurrent(*window, *gl_context); |
| 68 | + SDL_GL_SetSwapInterval(1); // Enable vsync |
| 69 | + |
| 70 | + // Setup Dear ImGui context |
| 71 | + IMGUI_CHECKVERSION(); |
| 72 | + ImGui::CreateContext(); |
| 73 | + ImGuiIO & io = ImGui::GetIO(); |
| 74 | + (void) io; |
| 75 | + // io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls |
| 76 | + // io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls |
| 77 | + |
| 78 | + ImGui::StyleColorsDark(); |
| 79 | + |
| 80 | + // Setup Platform/Renderer backends |
| 81 | + ImGui_ImplSDL2_InitForOpenGL(*window, *gl_context); |
| 82 | + ImGui_ImplOpenGL3_Init(); |
| 83 | +} |
| 84 | + |
| 85 | +void UiShutdown(SDL_GLContext * gl_context, SDL_Window ** window) |
| 86 | +{ |
| 87 | + ImGui_ImplOpenGL3_Shutdown(); |
| 88 | + ImGui_ImplSDL2_Shutdown(); |
| 89 | + ImGui::DestroyContext(); |
| 90 | + |
| 91 | + SDL_GL_DeleteContext(*gl_context); |
| 92 | + SDL_DestroyWindow(*window); |
| 93 | + SDL_Quit(); |
| 94 | +} |
| 95 | + |
| 96 | +void EventLoop(ImguiUi * ui) |
| 97 | +{ |
| 98 | + gUiRunning = true; |
| 99 | + SDL_GLContext gl_context; |
| 100 | + SDL_Window * window = nullptr; |
| 101 | + |
| 102 | + UiInit(&gl_context, &window); |
| 103 | + |
| 104 | + ImGuiIO & io = ImGui::GetIO(); |
| 105 | + |
| 106 | + while (gUiRunning.load()) |
| 107 | + { |
| 108 | + SDL_Event event; |
| 109 | + while (SDL_PollEvent(&event)) |
| 110 | + { |
| 111 | + ImGui_ImplSDL2_ProcessEvent(&event); |
| 112 | + if ((event.type == SDL_QUIT) || |
| 113 | + (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && |
| 114 | + event.window.windowID == SDL_GetWindowID(window))) |
| 115 | + { |
| 116 | + gUiRunning = false; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + ImGui_ImplOpenGL3_NewFrame(); |
| 121 | + ImGui_ImplSDL2_NewFrame(); |
| 122 | + ImGui::NewFrame(); |
| 123 | + |
| 124 | + ui->UpdateState(); |
| 125 | + ui->Render(); |
| 126 | + |
| 127 | + // rendering |
| 128 | + ImGui::Render(); |
| 129 | + glViewport(0, 0, (int) io.DisplaySize.x, (int) io.DisplaySize.y); |
| 130 | + glClearColor(0, 0, 0, 0); |
| 131 | + glClear(GL_COLOR_BUFFER_BIT); |
| 132 | + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); |
| 133 | + SDL_GL_SwapWindow(window); |
| 134 | + } |
| 135 | + |
| 136 | + UiShutdown(&gl_context, &window); |
| 137 | + |
| 138 | + ChipLogProgress(AppServer, "UI thread Stopped..."); |
| 139 | +} |
| 140 | + |
| 141 | +} // namespace |
| 142 | + |
| 143 | +void ImguiUi::RunMainLoop() |
| 144 | +{ |
| 145 | + // Guaranteed to be on the main task (no chip event loop started yet) |
| 146 | + ChipLoopLoadInitialState(); |
| 147 | + |
| 148 | + // Platform event loop will be on a separate thread, |
| 149 | + // while the event UI loop will be on the main thread. |
| 150 | + chip::DeviceLayer::PlatformMgr().StartEventLoopTask(); |
| 151 | + |
| 152 | + // SignalSafeStopMainLoop will stop this loop below |
| 153 | + // or the loop exits by itself when processing a SDL |
| 154 | + // exit (generally by clicking the window close icon). |
| 155 | + EventLoop(this); |
| 156 | + |
| 157 | + // ensure shutdown events are generated (generally basic cluster |
| 158 | + // will send a shutdown event to subscribers). |
| 159 | + // |
| 160 | + // We attempt to wait for finish as the event will be sent sync. |
| 161 | + // Since the Main loop is stopped, there will be no MRP, however at least |
| 162 | + // one event is attempted to be sent. |
| 163 | + chip::DeviceLayer::PlatformMgr().ScheduleWork( |
| 164 | + [](intptr_t arg) { |
| 165 | + chip::DeviceLayer::PlatformMgr().HandleServerShuttingDown(); |
| 166 | + sem_t * semaphore = reinterpret_cast<sem_t *>(arg); |
| 167 | + sem_post(semaphore); // notify complete |
| 168 | + }, |
| 169 | + reinterpret_cast<intptr_t>(&mChipLoopWaitSemaphore)); |
| 170 | + sem_wait(&mChipLoopWaitSemaphore); |
| 171 | + |
| 172 | + // Stop the chip main loop as well. This is expected to |
| 173 | + // wait for the task to finish. |
| 174 | + chip::DeviceLayer::PlatformMgr().StopEventLoopTask(); |
| 175 | +} |
| 176 | + |
| 177 | +void ImguiUi::SignalSafeStopMainLoop() |
| 178 | +{ |
| 179 | + gUiRunning = false; |
| 180 | +} |
| 181 | + |
| 182 | +void ImguiUi::ChipLoopStateUpdate() |
| 183 | +{ |
| 184 | + assertChipStackLockedByCurrentThread(); |
| 185 | + for (auto it = mWindows.begin(); it != mWindows.end(); it++) |
| 186 | + { |
| 187 | + (*it)->UpdateState(); |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +void ImguiUi::ChipLoopLoadInitialState() |
| 192 | +{ |
| 193 | + assertChipStackLockedByCurrentThread(); |
| 194 | + for (auto it = mWindows.begin(); it != mWindows.end(); it++) |
| 195 | + { |
| 196 | + (*it)->LoadInitialState(); |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +void ImguiUi::Render() |
| 201 | +{ |
| 202 | + for (auto it = mWindows.begin(); it != mWindows.end(); it++) |
| 203 | + { |
| 204 | + (*it)->Render(); |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +void ImguiUi::ChipLoopUpdateCallback(intptr_t self) |
| 209 | +{ |
| 210 | + ImguiUi * _this = reinterpret_cast<ImguiUi *>(self); |
| 211 | + _this->ChipLoopStateUpdate(); |
| 212 | + sem_post(&_this->mChipLoopWaitSemaphore); // notify complete |
| 213 | +} |
| 214 | + |
| 215 | +void ImguiUi::UpdateState() |
| 216 | +{ |
| 217 | + CHIP_ERROR err = chip::DeviceLayer::PlatformMgr().ScheduleWork(&ChipLoopUpdateCallback, reinterpret_cast<intptr_t>(this)); |
| 218 | + |
| 219 | + if (err != CHIP_NO_ERROR) |
| 220 | + { |
| 221 | + ChipLogError(AppServer, "Failed to schedule state update: %" CHIP_ERROR_FORMAT, err.Format()); |
| 222 | + return; |
| 223 | + } |
| 224 | + |
| 225 | + // ensure update is done when exiting |
| 226 | + sem_wait(&mChipLoopWaitSemaphore); |
| 227 | +} |
| 228 | + |
| 229 | +} // namespace Ui |
| 230 | +} // namespace example |
0 commit comments