From 4ee75e411a0d0b38e29c192bc0e02e307d0e820f Mon Sep 17 00:00:00 2001 From: Daniil Vinogradov Date: Sat, 7 Dec 2024 13:16:20 +0100 Subject: [PATCH] WIP iOS --- CMakeLists.txt | 8 +-- Submodules/UIKit/CMakeLists.txt | 14 ++++- .../UIKit/{lib => include}/Application.h | 7 ++- Submodules/UIKit/include/NXSize.h | 11 ++++ Submodules/UIKit/include/SkiaCtx.h | 25 +++++++++ Submodules/UIKit/include/UIKit.h | 5 +- .../UIKit/include/platforms/SkiaCtx_sdlBase.h | 16 ++++++ Submodules/UIKit/include/platforms/SkiaInit.h | 8 --- .../UIKit/include/platforms/ios/SkiaCtx_ios.h | 18 ++++++ Submodules/UIKit/lib/Application.cpp | 46 +++++++++++---- Submodules/UIKit/lib/NXSize.cpp | 4 ++ Submodules/UIKit/lib/platforms/SkiaCtx.cpp | 9 +++ .../UIKit/lib/platforms/SkiaCtx_sdlBase.cpp | 14 +++++ .../UIKit/lib/platforms/apple/SkiaInit.mm | 39 ------------- .../lib/platforms/apple/ios/SkiaCtx_ios.mm | 56 +++++++++++++++++++ app/main.cpp | 1 - app/platforms/ios/iOSBundleInfo.plist.in | 2 - 17 files changed, 210 insertions(+), 73 deletions(-) rename Submodules/UIKit/{lib => include}/Application.h (74%) create mode 100644 Submodules/UIKit/include/NXSize.h create mode 100644 Submodules/UIKit/include/SkiaCtx.h create mode 100644 Submodules/UIKit/include/platforms/SkiaCtx_sdlBase.h delete mode 100644 Submodules/UIKit/include/platforms/SkiaInit.h create mode 100644 Submodules/UIKit/include/platforms/ios/SkiaCtx_ios.h create mode 100644 Submodules/UIKit/lib/NXSize.cpp create mode 100644 Submodules/UIKit/lib/platforms/SkiaCtx.cpp create mode 100644 Submodules/UIKit/lib/platforms/SkiaCtx_sdlBase.cpp delete mode 100644 Submodules/UIKit/lib/platforms/apple/SkiaInit.mm create mode 100644 Submodules/UIKit/lib/platforms/apple/ios/SkiaCtx_ios.mm diff --git a/CMakeLists.txt b/CMakeLists.txt index 1adff3d..566448f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,11 +5,11 @@ set(EXTERN_PATH ${CMAKE_CURRENT_SOURCE_DIR}/Submodules) include(${EXTERN_PATH}/cmake/toolchain.cmake) project(ThorVGApp) -set(VERSION_MAJOR "1") -set(VERSION_MINOR "2") -set(VERSION_ALTER "2") +set(VERSION_MAJOR "0") +set(VERSION_MINOR "0") +set(VERSION_ALTER "1") set(VERSION_BUILD "1") -set(PACKAGE_NAME "ru.xitrix.Moonlight") +set(PACKAGE_NAME "ru.xitrix.skiatest") set(PSN_VERSION "01.00") set(PROJECT_AUTHOR "XITRIX") set(PROJECT_ICON ${CMAKE_CURRENT_SOURCE_DIR}/resources/img/moonlight_icon.jpg) diff --git a/Submodules/UIKit/CMakeLists.txt b/Submodules/UIKit/CMakeLists.txt index b839820..9532872 100644 --- a/Submodules/UIKit/CMakeLists.txt +++ b/Submodules/UIKit/CMakeLists.txt @@ -7,7 +7,7 @@ add_definitions( # APPLE if (APPLE) list(APPEND platform_sources - lib/platforms/apple/SkiaInit.mm + lib/platforms/SkiaCtx_sdlBase.cpp ) # MAC @@ -21,6 +21,10 @@ if (APPLE) ) # IOS elseif (PLATFORM_IOS) + list(APPEND platform_sources + lib/platforms/apple/ios/SkiaCtx_ios.mm + ) + find_library(SDL2 NAMES SDL2-static SDL2main) list(APPEND platform_libs @@ -28,11 +32,17 @@ if (APPLE) ${EXTERN_PATH}/skia/out/ios-arm64-angle/libskia.a ${EXTERN_PATH}/skia/out/ios-arm64-angle/libwindow.a ) + + list(APPEND platform_sources + lib/platforms/apple/ios/SkiaCtx_ios.mm + ) endif () endif () add_library(UIKit + lib/platforms/SkiaCtx.cpp lib/Application.cpp + lib/NXSize.cpp ${platform_sources} ) @@ -44,7 +54,7 @@ target_include_directories(UIKit PUBLIC ) if (PLATFORM_IOS) - target_include_directories(SDL2-static PRIVATE ${EXTERN_PATH}/angle/ios/MetalANGLE.framework/Headers) + target_include_directories(SDL2-static PUBLIC ${EXTERN_PATH}/angle/ios/MetalANGLE.framework/Headers) endif () if (APPLE) diff --git a/Submodules/UIKit/lib/Application.h b/Submodules/UIKit/include/Application.h similarity index 74% rename from Submodules/UIKit/lib/Application.h rename to Submodules/UIKit/include/Application.h index 692f357..6b3ab55 100644 --- a/Submodules/UIKit/lib/Application.h +++ b/Submodules/UIKit/include/Application.h @@ -1,6 +1,9 @@ +#pragma once + #include #include "include/gpu/ganesh/GrDirectContext.h" #import "tools/window/WindowContext.h" +#import "SkiaCtx.h" class Application { public: @@ -11,8 +14,8 @@ class Application { SDL_Window* window; SDL_Renderer* renderer; - sk_sp ctx; - std::unique_ptr skiaWindow; +// std::unique_ptr skiaWindow; + std::unique_ptr skiaCtx; static Application* shared; static int resizingEventWatcher(void* data, SDL_Event* event); diff --git a/Submodules/UIKit/include/NXSize.h b/Submodules/UIKit/include/NXSize.h new file mode 100644 index 0000000..b39df88 --- /dev/null +++ b/Submodules/UIKit/include/NXSize.h @@ -0,0 +1,11 @@ +#pragma once + +typedef double NXFloat; + +struct NXSize { + NXFloat width; + NXFloat height; + + NXSize(); + NXSize(NXFloat width, NXFloat height); +}; diff --git a/Submodules/UIKit/include/SkiaCtx.h b/Submodules/UIKit/include/SkiaCtx.h new file mode 100644 index 0000000..4aefea4 --- /dev/null +++ b/Submodules/UIKit/include/SkiaCtx.h @@ -0,0 +1,25 @@ +#pragma once + +#include + +#include +#include +#include "include/core/SkSurface.h" +#include + +bool platformRunLoop(std::function loop); + +class SkiaCtx { +public: + virtual sk_sp getBackbufferSurface() = 0; + virtual sk_sp directContext() = 0; + virtual void flushAndSubmit(sk_sp surface); + virtual NXSize getSize() = 0; + virtual float getScaleFactor() { return 1; } + virtual void swapBuffers() = 0; + +protected: + NXSize _size; +}; + +std::unique_ptr MakeSkiaCtx(SDL_Window* window); diff --git a/Submodules/UIKit/include/UIKit.h b/Submodules/UIKit/include/UIKit.h index a7540a1..ac499c4 100644 --- a/Submodules/UIKit/include/UIKit.h +++ b/Submodules/UIKit/include/UIKit.h @@ -2,7 +2,4 @@ // Created by Даниил Виноградов on 17.11.2024. // -#ifndef THORVGAPP_UIKIT_H -#define THORVGAPP_UIKIT_H - -#endif //THORVGAPP_UIKIT_H +#pragma once \ No newline at end of file diff --git a/Submodules/UIKit/include/platforms/SkiaCtx_sdlBase.h b/Submodules/UIKit/include/platforms/SkiaCtx_sdlBase.h new file mode 100644 index 0000000..5e24796 --- /dev/null +++ b/Submodules/UIKit/include/platforms/SkiaCtx_sdlBase.h @@ -0,0 +1,16 @@ +#pragma once + +#include +#include + + +class SkiaCtx_sdlBase: public SkiaCtx { +public: + SkiaCtx_sdlBase(SDL_Window* window); + + virtual void swapBuffers() override; + virtual NXSize getSize() override; + +protected: + SDL_Window* window = nullptr; +}; \ No newline at end of file diff --git a/Submodules/UIKit/include/platforms/SkiaInit.h b/Submodules/UIKit/include/platforms/SkiaInit.h deleted file mode 100644 index 0f4b16c..0000000 --- a/Submodules/UIKit/include/platforms/SkiaInit.h +++ /dev/null @@ -1,8 +0,0 @@ -#include "tools/window/WindowContext.h" - -#include -#include - -std::unique_ptr skiaMakeWindow(SDL_Window* window); - -bool platformRunLoop(const std::function& runLoop); diff --git a/Submodules/UIKit/include/platforms/ios/SkiaCtx_ios.h b/Submodules/UIKit/include/platforms/ios/SkiaCtx_ios.h new file mode 100644 index 0000000..ea8165c --- /dev/null +++ b/Submodules/UIKit/include/platforms/ios/SkiaCtx_ios.h @@ -0,0 +1,18 @@ +#pragma once + +#include +#include +#include + +class SkiaCtx_ios: public SkiaCtx_sdlBase { +public: + SkiaCtx_ios(SDL_Window* window); + + sk_sp getBackbufferSurface() override; + float getScaleFactor() override; + sk_sp directContext() override { return context; } +private: + sk_sp context; + + void initContext(); +}; diff --git a/Submodules/UIKit/lib/Application.cpp b/Submodules/UIKit/lib/Application.cpp index a520b61..916cacb 100644 --- a/Submodules/UIKit/lib/Application.cpp +++ b/Submodules/UIKit/lib/Application.cpp @@ -10,7 +10,6 @@ // SKIA METAL GPU #include "include/gpu/ganesh/GrBackendSurface.h" -#include "platforms/SkiaInit.h" #include "include/effects/SkGradientShader.h" #include "include/effects/SkImageFilters.h" @@ -31,10 +30,31 @@ Application::Application() { shared = this; SDL_Init(SDL_INIT_EVERYTHING); - window = SDL_CreateWindow("Window", 12, 12, 1280, 720, SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_RESIZABLE); + + Uint32 flags = SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_RESIZABLE; +#ifdef PLATFORM_IOS + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); + SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0); + SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 0); + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); + + flags |= SDL_WINDOW_OPENGL; +#endif + + window = SDL_CreateWindow("Window", 12, 12, 1280, 720, flags); + auto context = SDL_GL_CreateContext(window); + SDL_GL_MakeCurrent(window, context); SkGraphics::Init(); - skiaWindow = skiaMakeWindow(window); +// skiaWindow = skiaMakeWindow(window); + skiaCtx = MakeSkiaCtx(window); SDL_AddEventWatch(resizingEventWatcher, window); @@ -42,7 +62,7 @@ Application::Application() { } Application::~Application() { - skiaWindow = nullptr; + skiaCtx = nullptr; } bool Application::runLoop() { @@ -57,11 +77,11 @@ bool Application::runLoop() { } void Application::render() { - auto surface = skiaWindow->getBackbufferSurface(); + auto surface = skiaCtx->getBackbufferSurface(); if (!surface) return; auto canvas = surface->getCanvas(); - canvas->clear(SK_ColorWHITE); + canvas->clear(SK_ColorCYAN); SkPaint paint; paint.setColor(SK_ColorRED); @@ -74,6 +94,13 @@ void Application::render() { rrect.setRectRadii(rect, corners); canvas->drawRRect(rrect, paint); + paint.setColor(SK_ColorYELLOW); + rect = SkRect::MakeXYWH(10, 410, 2512, 200); + canvas->drawRect(rect, paint); + + paint.setColor(SK_ColorBLUE); + rect = SkRect::MakeXYWH(410, 10, 212, 2500); + canvas->drawRect(rect, paint); // Set up a linear gradient and draw a circle { @@ -160,9 +187,6 @@ void Application::render() { // ----------------------- - if (auto dContext = skiaWindow->directContext()) { - dContext->flushAndSubmit(surface.get(), GrSyncCpu::kNo); - } - - skiaWindow->swapBuffers(); + skiaCtx->flushAndSubmit(surface); + skiaCtx->swapBuffers(); } diff --git a/Submodules/UIKit/lib/NXSize.cpp b/Submodules/UIKit/lib/NXSize.cpp new file mode 100644 index 0000000..6a96ab8 --- /dev/null +++ b/Submodules/UIKit/lib/NXSize.cpp @@ -0,0 +1,4 @@ +#include + +NXSize::NXSize(): NXSize(0, 0) {} +NXSize::NXSize(NXFloat width, NXFloat height): width(width), height(height) {} \ No newline at end of file diff --git a/Submodules/UIKit/lib/platforms/SkiaCtx.cpp b/Submodules/UIKit/lib/platforms/SkiaCtx.cpp new file mode 100644 index 0000000..e1d9ead --- /dev/null +++ b/Submodules/UIKit/lib/platforms/SkiaCtx.cpp @@ -0,0 +1,9 @@ +#include "SkiaCtx.h" + +#include "include/gpu/ganesh/GrDirectContext.h" + +void SkiaCtx::flushAndSubmit(sk_sp surface) { + if (auto dContext = directContext()) { + dContext->flushAndSubmit(surface.get(), GrSyncCpu::kNo); + } +} \ No newline at end of file diff --git a/Submodules/UIKit/lib/platforms/SkiaCtx_sdlBase.cpp b/Submodules/UIKit/lib/platforms/SkiaCtx_sdlBase.cpp new file mode 100644 index 0000000..85b5f6f --- /dev/null +++ b/Submodules/UIKit/lib/platforms/SkiaCtx_sdlBase.cpp @@ -0,0 +1,14 @@ +#include + +SkiaCtx_sdlBase::SkiaCtx_sdlBase(SDL_Window *window): window(window) +{ } + +void SkiaCtx_sdlBase::swapBuffers() { + SDL_GL_SwapWindow(window); +} + +NXSize SkiaCtx_sdlBase::getSize() { + int w, h; + SDL_GL_GetDrawableSize(window, &w, &h); + return { (NXFloat)w, (NXFloat)h }; +} \ No newline at end of file diff --git a/Submodules/UIKit/lib/platforms/apple/SkiaInit.mm b/Submodules/UIKit/lib/platforms/apple/SkiaInit.mm deleted file mode 100644 index 58f0ac1..0000000 --- a/Submodules/UIKit/lib/platforms/apple/SkiaInit.mm +++ /dev/null @@ -1,39 +0,0 @@ -#include - -#import "include/core/SkSurface.h" -#import "include/gpu/ganesh/GrBackendSurface.h" - -#ifdef PLATFORM_DESKTOP -#import "tools/window/mac/MacWindowInfo.h" -#import "tools/window/mac/GaneshANGLEWindowContext_mac.h" - -std::unique_ptr skiaMakeWindow(SDL_Window* window) { - auto metalView = SDL_Metal_CreateView(window); - - skwindow::MacWindowInfo info{}; - info.fMainView = (__bridge NSView *) metalView; - return skwindow::MakeGaneshANGLEForMac(info, std::make_unique()); -} -#endif - -#ifdef PLATFORM_IOS -#include "tools/window/ios/WindowContextFactory_ios.h" -#import "UIKit/UIKit.h" - -std::unique_ptr skiaMakeWindow(SDL_Window* window) { - auto metalView = SDL_Metal_CreateView(window); - - auto appWindow = UIApplication.sharedApplication.keyWindow; - skwindow::IOSWindowInfo info{}; - info.fWindow = appWindow; - info.fViewController = appWindow.rootViewController; - return skwindow::MakeMetalForIOS(info, std::make_unique()); -} - -#endif - -bool platformRunLoop(const std::function& runLoop) { - @autoreleasepool { - return runLoop(); - } -} diff --git a/Submodules/UIKit/lib/platforms/apple/ios/SkiaCtx_ios.mm b/Submodules/UIKit/lib/platforms/apple/ios/SkiaCtx_ios.mm new file mode 100644 index 0000000..3cdee2e --- /dev/null +++ b/Submodules/UIKit/lib/platforms/apple/ios/SkiaCtx_ios.mm @@ -0,0 +1,56 @@ +#include + +#include "include/core/SkColorSpace.h" +#include "include/gpu/ganesh/gl/GrGLDirectContext.h" +#include "include/gpu/ganesh/gl/GrGLInterface.h" +#include "include/gpu/ganesh/gl/GrGLBackendSurface.h" +#include "include/gpu/ganesh/gl/ios/GrGLMakeIOSInterface.h" +#include "include/gpu/ganesh/SkSurfaceGanesh.h" +#include "include/gpu/ganesh/GrDirectContext.h" +#include "include/gpu/ganesh/GrBackendSurface.h" + +#include +#include + +SkiaCtx_ios::SkiaCtx_ios(SDL_Window* window): SkiaCtx_sdlBase(window) { + initContext(); +} + +void SkiaCtx_ios::initContext() { + auto interface = GrGLInterfaces::MakeIOS(); + _size = getSize(); + interface->fFunctions.fViewport(0, 0, _size.width, _size.height); + context = GrDirectContexts::MakeGL(interface); +} + +float SkiaCtx_ios::getScaleFactor() { + return UIApplication.sharedApplication.keyWindow.traitCollection.displayScale; +} + +sk_sp SkiaCtx_ios::getBackbufferSurface() { + auto size = getSize(); + if (_size.width != size.width || _size.height != size.width) + initContext(); + + GrGLFramebufferInfo framebuffer_info; + framebuffer_info.fFormat = GL_RGBA8; + framebuffer_info.fFBOID = 0; + auto frame = UIApplication.sharedApplication.keyWindow.frame; + GrBackendRenderTarget target = GrBackendRenderTargets::MakeGL(frame.size.width, frame.size.height, 0, 8, framebuffer_info); + + SkSurfaceProps props; + + return SkSurfaces::WrapBackendRenderTarget(context.get(), target, + kBottomLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, + nullptr, &props); +} + +std::unique_ptr MakeSkiaCtx(SDL_Window* window) { + return std::make_unique(window); +} + +bool platformRunLoop(std::function loop) { + @autoreleasepool { + return loop(); + } +} diff --git a/app/main.cpp b/app/main.cpp index 9bcf408..3fb0964 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -1,5 +1,4 @@ #include -#include #include int main(int argc, char *argv[]) { diff --git a/app/platforms/ios/iOSBundleInfo.plist.in b/app/platforms/ios/iOSBundleInfo.plist.in index 1ea4be2..5d64d1a 100644 --- a/app/platforms/ios/iOSBundleInfo.plist.in +++ b/app/platforms/ios/iOSBundleInfo.plist.in @@ -63,8 +63,6 @@ UISupportedInterfaceOrientations - UIInterfaceOrientationLandscapeLeft - UIInterfaceOrientationLandscapeRight GCSupportedGameControllers