diff --git a/src/engine/engine.cc b/src/engine/engine.cc index f14cf21..6d41bc4 100644 --- a/src/engine/engine.cc +++ b/src/engine/engine.cc @@ -84,14 +84,12 @@ void Engine::Run() { // Subdivide the frame time using fixed time steps. while (accumulator >= time_step_) { TaskRunner::GetThreadLocalTaskRunner()->SingleConsumerRun(); - platform_->Update(); Update(time_step_); - - if (platform_->should_exit()) { - return; - } accumulator -= time_step_; + + if (platform_->should_exit()) + return; }; // Calculate frame fraction from remainder of the frame time. @@ -100,6 +98,8 @@ void Engine::Run() { } void Engine::Initialize() { + LOG << "Initializing engine."; + thread_pool_.Initialize(); CreateRenderer(true); @@ -203,19 +203,18 @@ void Engine::CreateRenderer(bool vulkan) { return; if (vulkan) - renderer_ = std::make_unique(); + renderer_ = + std::make_unique(std::bind(&Engine::ContextLost, this)); else - renderer_ = std::make_unique(); - renderer_->SetContextLostCB(std::bind(&Engine::ContextLost, this)); + renderer_ = + std::make_unique(std::bind(&Engine::ContextLost, this)); - bool result = InitializeRenderer(); - LOG_IF(!result) << "Failed to initialize " << renderer_->GetDebugName() - << " renderer."; + bool result = renderer_->Initialize(platform_); if (!result && vulkan) { + LOG << "Failed to initialize " << renderer_->GetDebugName() << " renderer."; LOG << "Fallback to OpenGL renderer."; - renderer_ = std::make_unique(); - renderer_->SetContextLostCB(std::bind(&Engine::ContextLost, this)); - result = InitializeRenderer(); + CreateRenderer(false); + return; } CHECK(result) << "Failed to initialize " << renderer_->GetDebugName() << " renderer."; @@ -480,7 +479,7 @@ bool Engine::IsMobile() const { } void Engine::OnWindowCreated() { - InitializeRenderer(); + renderer_->Initialize(platform_); } void Engine::OnWindowDestroyed() { @@ -491,7 +490,7 @@ void Engine::OnWindowResized(int width, int height) { if (width != renderer_->screen_width() || height != renderer_->screen_height()) { renderer_->Shutdown(); - InitializeRenderer(); + renderer_->Initialize(platform_); } } @@ -546,14 +545,6 @@ void Engine::AddInputEvent(std::unique_ptr event) { input_queue_.push_back(std::move(event)); } -bool Engine::InitializeRenderer() { -#if defined(__ANDROID__) - return renderer_->Initialize(platform_->GetWindow()); -#elif defined(__linux__) - return renderer_->Initialize(platform_->GetDisplay(), platform_->GetWindow()); -#endif -} - void Engine::CreateTextureCompressors() { tex_comp_alpha_.reset(); tex_comp_opaque_.reset(); diff --git a/src/engine/engine.h b/src/engine/engine.h index 3f9ec7b..ecb53a8 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -231,8 +231,6 @@ class Engine : public PlatformObserver { void GainedFocus(bool from_interstitial_ad) final; void AddInputEvent(std::unique_ptr event) final; - bool InitializeRenderer(); - void CreateTextureCompressors(); void ContextLost(); diff --git a/src/engine/platform/platform_android.cc b/src/engine/platform/platform_android.cc index 8adf003..00c2020 100644 --- a/src/engine/platform/platform_android.cc +++ b/src/engine/platform/platform_android.cc @@ -338,6 +338,8 @@ void Platform::HandleCmd(android_app* app, int32_t cmd) { } Platform::Platform(android_app* app) { + LOG << "Initializing platform."; + app_ = app; mobile_device_ = true; diff --git a/src/engine/platform/platform_linux.cc b/src/engine/platform/platform_linux.cc index 30b83d8..d911257 100644 --- a/src/engine/platform/platform_linux.cc +++ b/src/engine/platform/platform_linux.cc @@ -14,6 +14,8 @@ namespace eng { void KaliberMain(Platform* platform); Platform::Platform() { + LOG << "Initializing platform."; + root_path_ = "../../"; LOG << "Root path: " << root_path_.c_str(); diff --git a/src/engine/renderer/opengl/renderer_opengl.cc b/src/engine/renderer/opengl/renderer_opengl.cc index 3a5fd44..8f3c2ef 100644 --- a/src/engine/renderer/opengl/renderer_opengl.cc +++ b/src/engine/renderer/opengl/renderer_opengl.cc @@ -40,8 +40,9 @@ const std::string kAttributeNames[eng::kAttribType_Max] = { namespace eng { #ifdef THREADED_RENDERING -RendererOpenGL::RendererOpenGL() - : main_thread_task_runner_(TaskRunner::GetThreadLocalTaskRunner()) {} +RendererOpenGL::RendererOpenGL(base::Closure context_lost_cb) + : Renderer(context_lost_cb), + main_thread_task_runner_(TaskRunner::GetThreadLocalTaskRunner()) {} #else RendererOpenGL::RendererOpenGL() = default; #endif // THREADED_RENDERING diff --git a/src/engine/renderer/opengl/renderer_opengl.h b/src/engine/renderer/opengl/renderer_opengl.h index aa7c9f4..e5c02e6 100644 --- a/src/engine/renderer/opengl/renderer_opengl.h +++ b/src/engine/renderer/opengl/renderer_opengl.h @@ -37,15 +37,10 @@ struct RenderCommand; class RendererOpenGL final : public Renderer { public: - RendererOpenGL(); + RendererOpenGL(base::Closure context_lost_cb); ~RendererOpenGL() final; -#if defined(__ANDROID__) - bool Initialize(ANativeWindow* window) final; -#elif defined(__linux__) - bool Initialize(Display* display, Window window) final; -#endif - + virtual bool Initialize(Platform* platform) final; void Shutdown() final; uint64_t CreateGeometry(std::unique_ptr mesh) final; diff --git a/src/engine/renderer/opengl/renderer_opengl_android.cc b/src/engine/renderer/opengl/renderer_opengl_android.cc index 2c36c71..e8db219 100644 --- a/src/engine/renderer/opengl/renderer_opengl_android.cc +++ b/src/engine/renderer/opengl/renderer_opengl_android.cc @@ -3,14 +3,15 @@ #include #include "base/log.h" +#include "engine/platform/platform.h" #include "third_party/android/GLContext.h" namespace eng { -bool RendererOpenGL::Initialize(ANativeWindow* window) { +bool RendererOpenGL::Initialize(Platform* platform) { LOG << "Initializing renderer."; - window_ = window; + window_ = platform->GetWindow(); return StartRenderThread(); } diff --git a/src/engine/renderer/opengl/renderer_opengl_linux.cc b/src/engine/renderer/opengl/renderer_opengl_linux.cc index 267a5bf..9d5cd00 100644 --- a/src/engine/renderer/opengl/renderer_opengl_linux.cc +++ b/src/engine/renderer/opengl/renderer_opengl_linux.cc @@ -1,14 +1,15 @@ #include "engine/renderer/opengl/renderer_opengl.h" #include "base/log.h" +#include "engine/platform/platform.h" namespace eng { -bool RendererOpenGL::Initialize(Display* display, Window window) { +bool RendererOpenGL::Initialize(Platform* platform) { LOG << "Initializing renderer."; - display_ = display; - window_ = window; + display_ = platform->GetDisplay(); + window_ = platform->GetWindow(); XWindowAttributes xwa; XGetWindowAttributes(display_, window_, &xwa); @@ -24,8 +25,7 @@ bool RendererOpenGL::InitInternal() { GLint glx_attributes[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None}; XVisualInfo* visual_info = glXChooseVisual(display_, 0, glx_attributes); - glx_context_ = - glXCreateContext(display_, visual_info, NULL, GL_TRUE); + glx_context_ = glXCreateContext(display_, visual_info, NULL, GL_TRUE); if (!glx_context_) { LOG << "Couldn't create the glx context."; return false; diff --git a/src/engine/renderer/renderer.h b/src/engine/renderer/renderer.h index 2ce496b..564b7b5 100644 --- a/src/engine/renderer/renderer.h +++ b/src/engine/renderer/renderer.h @@ -4,40 +4,26 @@ #include #include -#if defined(__linux__) && !defined(__ANDROID__) -#include -#include -#endif - #include "base/closure.h" #include "base/vecmath.h" #include "engine/renderer/renderer_types.h" -#if defined(__ANDROID__) -struct ANativeWindow; -#endif - namespace eng { class Image; class ShaderSource; class Mesh; +class Platform; class Renderer { public: const unsigned kInvalidId = 0; - Renderer() = default; + Renderer(base::Closure context_lost_cb) + : context_lost_cb_{std::move(context_lost_cb)} {} virtual ~Renderer() = default; - void SetContextLostCB(base::Closure cb) { context_lost_cb_ = std::move(cb); } - -#if defined(__ANDROID__) - virtual bool Initialize(ANativeWindow* window) = 0; -#elif defined(__linux__) - virtual bool Initialize(Display* display, Window window) = 0; -#endif - + virtual bool Initialize(Platform* platform) = 0; virtual void Shutdown() = 0; virtual uint64_t CreateGeometry(std::unique_ptr mesh) = 0; diff --git a/src/engine/renderer/vulkan/renderer_vulkan.cc b/src/engine/renderer/vulkan/renderer_vulkan.cc index 290189f..7affd05 100644 --- a/src/engine/renderer/vulkan/renderer_vulkan.cc +++ b/src/engine/renderer/vulkan/renderer_vulkan.cc @@ -370,7 +370,8 @@ std::pair GetNumBlocksForImageFormat(VkFormat format, namespace eng { -RendererVulkan::RendererVulkan() = default; +RendererVulkan::RendererVulkan(base::Closure context_lost_cb) + : Renderer(context_lost_cb) {} RendererVulkan::~RendererVulkan() { Shutdown(); diff --git a/src/engine/renderer/vulkan/renderer_vulkan.h b/src/engine/renderer/vulkan/renderer_vulkan.h index 5e54c98..0bc09e8 100644 --- a/src/engine/renderer/vulkan/renderer_vulkan.h +++ b/src/engine/renderer/vulkan/renderer_vulkan.h @@ -18,19 +18,12 @@ namespace eng { -class Image; - class RendererVulkan final : public Renderer { public: - RendererVulkan(); + RendererVulkan(base::Closure context_lost_cb); ~RendererVulkan() final; -#if defined(__ANDROID__) - bool Initialize(ANativeWindow* window) final; -#elif defined(__linux__) - bool Initialize(Display* display, Window window) final; -#endif - + virtual bool Initialize(Platform* platform) final; void Shutdown() final; uint64_t CreateGeometry(std::unique_ptr mesh) final; diff --git a/src/engine/renderer/vulkan/renderer_vulkan_android.cc b/src/engine/renderer/vulkan/renderer_vulkan_android.cc index 8106fb4..2f1bf02 100644 --- a/src/engine/renderer/vulkan/renderer_vulkan_android.cc +++ b/src/engine/renderer/vulkan/renderer_vulkan_android.cc @@ -3,20 +3,22 @@ #include #include "base/log.h" +#include "engine/platform/platform.h" namespace eng { -bool RendererVulkan::Initialize(ANativeWindow* window) { +bool RendererVulkan::Initialize(Platform* platform) { LOG << "Initializing renderer."; - screen_width_ = ANativeWindow_getWidth(window); - screen_height_ = ANativeWindow_getHeight(window); + screen_width_ = ANativeWindow_getWidth(platform->GetWindow()); + screen_height_ = ANativeWindow_getHeight(platform->GetWindow()); if (!context_.Initialize()) { LOG << "Failed to initialize Vulkan context."; return false; } - if (!context_.CreateWindow(window, screen_width_, screen_height_)) { + if (!context_.CreateWindow(platform->GetWindow(), screen_width_, + screen_height_)) { LOG << "Vulkan context failed to create window."; return false; } diff --git a/src/engine/renderer/vulkan/renderer_vulkan_linux.cc b/src/engine/renderer/vulkan/renderer_vulkan_linux.cc index 35eea3d..3de0b91 100644 --- a/src/engine/renderer/vulkan/renderer_vulkan_linux.cc +++ b/src/engine/renderer/vulkan/renderer_vulkan_linux.cc @@ -1,14 +1,15 @@ #include "engine/renderer/vulkan/renderer_vulkan.h" #include "base/log.h" +#include "engine/platform/platform.h" namespace eng { -bool RendererVulkan::Initialize(Display* display, Window window) { +bool RendererVulkan::Initialize(Platform* platform) { LOG << "Initializing renderer."; XWindowAttributes xwa; - XGetWindowAttributes(display, window, &xwa); + XGetWindowAttributes(platform->GetDisplay(), platform->GetWindow(), &xwa); screen_width_ = xwa.width; screen_height_ = xwa.height; @@ -16,7 +17,8 @@ bool RendererVulkan::Initialize(Display* display, Window window) { LOG << "Failed to initialize Vulkan context."; return false; } - if (!context_.CreateWindow(display, window, screen_width_, screen_height_)) { + if (!context_.CreateWindow(platform->GetDisplay(), platform->GetWindow(), + screen_width_, screen_height_)) { LOG << "Vulkan context failed to create window."; return false; }