Skip to content

Commit

Permalink
WIP iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Dec 7, 2024
1 parent edcfd6f commit 4ee75e4
Show file tree
Hide file tree
Showing 17 changed files with 210 additions and 73 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 12 additions & 2 deletions Submodules/UIKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ add_definitions(
# APPLE
if (APPLE)
list(APPEND platform_sources
lib/platforms/apple/SkiaInit.mm
lib/platforms/SkiaCtx_sdlBase.cpp
)

# MAC
Expand All @@ -21,18 +21,28 @@ 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
SDL2-static SDL2main
${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}
)

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#pragma once

#include <SDL.h>
#include "include/gpu/ganesh/GrDirectContext.h"
#import "tools/window/WindowContext.h"
#import "SkiaCtx.h"

class Application {
public:
Expand All @@ -11,8 +14,8 @@ class Application {
SDL_Window* window;
SDL_Renderer* renderer;

sk_sp<GrDirectContext> ctx;
std::unique_ptr<skwindow::WindowContext> skiaWindow;
// std::unique_ptr<skwindow::WindowContext> skiaWindow;
std::unique_ptr<SkiaCtx> skiaCtx;

static Application* shared;
static int resizingEventWatcher(void* data, SDL_Event* event);
Expand Down
11 changes: 11 additions & 0 deletions Submodules/UIKit/include/NXSize.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

typedef double NXFloat;

struct NXSize {
NXFloat width;
NXFloat height;

NXSize();
NXSize(NXFloat width, NXFloat height);
};
25 changes: 25 additions & 0 deletions Submodules/UIKit/include/SkiaCtx.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <NXSize.h>

#include <SDL.h>
#include <memory>
#include "include/core/SkSurface.h"
#include <functional>

bool platformRunLoop(std::function<bool()> loop);

class SkiaCtx {
public:
virtual sk_sp<SkSurface> getBackbufferSurface() = 0;
virtual sk_sp<GrDirectContext> directContext() = 0;
virtual void flushAndSubmit(sk_sp<SkSurface> surface);
virtual NXSize getSize() = 0;
virtual float getScaleFactor() { return 1; }
virtual void swapBuffers() = 0;

protected:
NXSize _size;
};

std::unique_ptr<SkiaCtx> MakeSkiaCtx(SDL_Window* window);
5 changes: 1 addition & 4 deletions Submodules/UIKit/include/UIKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@
// Created by Даниил Виноградов on 17.11.2024.
//

#ifndef THORVGAPP_UIKIT_H
#define THORVGAPP_UIKIT_H

#endif //THORVGAPP_UIKIT_H
#pragma once
16 changes: 16 additions & 0 deletions Submodules/UIKit/include/platforms/SkiaCtx_sdlBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <SkiaCtx.h>
#include <include/gpu/ganesh/GrDirectContext.h>


class SkiaCtx_sdlBase: public SkiaCtx {
public:
SkiaCtx_sdlBase(SDL_Window* window);

virtual void swapBuffers() override;
virtual NXSize getSize() override;

protected:
SDL_Window* window = nullptr;
};
8 changes: 0 additions & 8 deletions Submodules/UIKit/include/platforms/SkiaInit.h

This file was deleted.

18 changes: 18 additions & 0 deletions Submodules/UIKit/include/platforms/ios/SkiaCtx_ios.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <SkiaCtx.h>
#include <platforms/SkiaCtx_sdlBase.h>
#include <include/gpu/ganesh/GrDirectContext.h>

class SkiaCtx_ios: public SkiaCtx_sdlBase {
public:
SkiaCtx_ios(SDL_Window* window);

sk_sp<SkSurface> getBackbufferSurface() override;
float getScaleFactor() override;
sk_sp<GrDirectContext> directContext() override { return context; }
private:
sk_sp<GrDirectContext> context;

void initContext();
};
46 changes: 35 additions & 11 deletions Submodules/UIKit/lib/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -31,18 +30,39 @@ 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);

while(platformRunLoop([this]() { return runLoop(); }));
}

Application::~Application() {
skiaWindow = nullptr;
skiaCtx = nullptr;
}

bool Application::runLoop() {
Expand All @@ -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);
Expand All @@ -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
{
Expand Down Expand Up @@ -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();
}
4 changes: 4 additions & 0 deletions Submodules/UIKit/lib/NXSize.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include <NXSize.h>

NXSize::NXSize(): NXSize(0, 0) {}
NXSize::NXSize(NXFloat width, NXFloat height): width(width), height(height) {}
9 changes: 9 additions & 0 deletions Submodules/UIKit/lib/platforms/SkiaCtx.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "SkiaCtx.h"

#include "include/gpu/ganesh/GrDirectContext.h"

void SkiaCtx::flushAndSubmit(sk_sp<SkSurface> surface) {
if (auto dContext = directContext()) {
dContext->flushAndSubmit(surface.get(), GrSyncCpu::kNo);
}
}
14 changes: 14 additions & 0 deletions Submodules/UIKit/lib/platforms/SkiaCtx_sdlBase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <platforms/SkiaCtx_sdlBase.h>

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 };
}
39 changes: 0 additions & 39 deletions Submodules/UIKit/lib/platforms/apple/SkiaInit.mm

This file was deleted.

56 changes: 56 additions & 0 deletions Submodules/UIKit/lib/platforms/apple/ios/SkiaCtx_ios.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <platforms/ios/SkiaCtx_ios.h>

#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 <GLES3/gl3.h>
#include <UIKit/UIKit.h>

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<SkSurface> 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<SkiaCtx> MakeSkiaCtx(SDL_Window* window) {
return std::make_unique<SkiaCtx_ios>(window);
}

bool platformRunLoop(std::function<bool()> loop) {
@autoreleasepool {
return loop();
}
}
Loading

0 comments on commit 4ee75e4

Please sign in to comment.