Skip to content

Commit

Permalink
Base context done
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Dec 7, 2024
1 parent 4ee75e4 commit 6b4fb0e
Show file tree
Hide file tree
Showing 15 changed files with 250 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Submodules/UIKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ if (APPLE)
${EXTERN_PATH}/skia/out/mac-arm64-angle/libskia.a
${EXTERN_PATH}/skia/out/mac-arm64-angle/libwindow.a
)

list(APPEND platform_sources
lib/platforms/apple/macos/SkiaCtx_macos.mm
)
# IOS
elseif (PLATFORM_IOS)
list(APPEND platform_sources
Expand Down Expand Up @@ -53,7 +57,9 @@ target_include_directories(UIKit PUBLIC
lib
)

if (PLATFORM_IOS)
if (PLATFORM_MAC)
target_include_directories(SDL2-static PUBLIC ${EXTERN_PATH}/angle/mac/Headers)
elseif (PLATFORM_IOS)
target_include_directories(SDL2-static PUBLIC ${EXTERN_PATH}/angle/ios/MetalANGLE.framework/Headers)
endif ()

Expand Down
4 changes: 4 additions & 0 deletions Submodules/UIKit/include/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#import "tools/window/WindowContext.h"
#import "SkiaCtx.h"

namespace NXKit {

class Application {
public:
Application();
Expand All @@ -23,3 +25,5 @@ class Application {
bool runLoop();
void render();
};

}
23 changes: 23 additions & 0 deletions Submodules/UIKit/include/NXSize.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
#pragma once

namespace NXKit {

typedef double NXFloat;

struct NXSize {
NXFloat width;
NXFloat height;

NXSize();

NXSize(NXFloat width, NXFloat height);

bool operator==(const NXSize &rhs) const;

NXSize operator+(const NXSize &first) const;

NXSize operator-(const NXSize &first) const;

NXSize &operator+=(const NXSize &rhs);

NXSize &operator-=(const NXSize &rhs);

NXSize operator*(const NXFloat &first) const;

NXSize operator/(const NXFloat &first) const;

NXSize &operator*=(const NXFloat &rhs);

NXSize &operator/=(const NXFloat &rhs);
};

}
4 changes: 4 additions & 0 deletions Submodules/UIKit/include/SkiaCtx.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "include/core/SkSurface.h"
#include <functional>

namespace NXKit {

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

class SkiaCtx {
Expand All @@ -23,3 +25,5 @@ class SkiaCtx {
};

std::unique_ptr<SkiaCtx> MakeSkiaCtx(SDL_Window* window);

}
14 changes: 10 additions & 4 deletions Submodules/UIKit/include/platforms/SkiaCtx_sdlBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
#include <SkiaCtx.h>
#include <include/gpu/ganesh/GrDirectContext.h>

namespace NXKit {

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

virtual void swapBuffers() override;

virtual NXSize getSize() override;

virtual float getScaleFactor() override;

protected:
SDL_Window* window = nullptr;
};
SDL_Window *window = nullptr;
};

}
11 changes: 9 additions & 2 deletions Submodules/UIKit/include/platforms/ios/SkiaCtx_ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@
#include <platforms/SkiaCtx_sdlBase.h>
#include <include/gpu/ganesh/GrDirectContext.h>

class SkiaCtx_ios: public SkiaCtx_sdlBase {
namespace NXKit {

class SkiaCtx_ios : public SkiaCtx_sdlBase {
public:
SkiaCtx_ios(SDL_Window* window);
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();
};

}
22 changes: 22 additions & 0 deletions Submodules/UIKit/include/platforms/macos/SkiaCtx_macos.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

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

namespace NXKit {

class SkiaCtx_macos: public SkiaCtx_sdlBase {
public:
SkiaCtx_macos(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();
};

}
17 changes: 8 additions & 9 deletions Submodules/UIKit/lib/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <SDL_syswm.h>

#include <include/core/SkGraphics.h>
#include <include/core/SkSurface.h>
#include <include/core/SkRRect.h>
#include <include/core/SkCanvas.h>
Expand All @@ -13,6 +12,8 @@
#include "include/effects/SkGradientShader.h"
#include "include/effects/SkImageFilters.h"

using namespace NXKit;

Application* Application::shared = nullptr;

int Application::resizingEventWatcher(void* data, SDL_Event* event) {
Expand Down Expand Up @@ -52,8 +53,6 @@ Application::Application() {
auto context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, context);

SkGraphics::Init();
// skiaWindow = skiaMakeWindow(window);
skiaCtx = MakeSkiaCtx(window);

SDL_AddEventWatch(resizingEventWatcher, window);
Expand Down Expand Up @@ -88,18 +87,18 @@ void Application::render() {
paint.setAntiAlias(true);

// Draw a rectangle with red paint
SkRect rect = SkRect::MakeXYWH(10, 10, 512, 200);
SkRect rect = SkRect::MakeXYWH(0, 0, 200, 200);
SkRRect rrect;
SkVector corners[] = {{24, 36}, {36, 24}, {24, 24}, {24, 24}};
rrect.setRectRadii(rect, corners);
canvas->drawRRect(rrect, paint);
canvas->drawRect(rect, paint);

paint.setColor(SK_ColorYELLOW);
rect = SkRect::MakeXYWH(10, 410, 2512, 200);
rect = SkRect::MakeXYWH(10, 410, 4000, 200);
canvas->drawRect(rect, paint);

paint.setColor(SK_ColorBLUE);
rect = SkRect::MakeXYWH(410, 10, 212, 2500);
rect = SkRect::MakeXYWH(410, 10, 212, 4000);
canvas->drawRect(rect, paint);

// Set up a linear gradient and draw a circle
Expand All @@ -117,12 +116,12 @@ void Application::render() {
}

// Create middle overlay rectangle for background blur
const SkRect middle = SkRect::MakeXYWH(64, 64, 128, 128);
const SkRect middle = SkRect::MakeXYWH(264, 264, 528, 528);

canvas->save();
// // Use middle rectangle as clip mask
// canvas->clipRect(middle, false);
canvas->clipRect(SkRect::MakeWH(190.5f, 110.5f), true);
canvas->clipRect(middle, true);

SkPaint redPaint;
redPaint.setAntiAlias(true);
Expand Down
48 changes: 47 additions & 1 deletion Submodules/UIKit/lib/NXSize.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,50 @@
#include <NXSize.h>

using namespace NXKit;

NXSize::NXSize(): NXSize(0, 0) {}
NXSize::NXSize(NXFloat width, NXFloat height): width(width), height(height) {}
NXSize::NXSize(NXFloat width, NXFloat height): width(width), height(height) {}

bool NXSize::operator==(const NXSize& rhs) const {
return this->width == rhs.width && this->height == rhs.height;
}

NXSize NXSize::operator+(const NXSize& first) const {
return NXSize(width + first.width, height + first.height);
}

NXSize NXSize::operator-(const NXSize& first) const {
return NXSize(width - first.width, height - first.height);
}

NXSize& NXSize::operator+=(const NXSize& rhs) {
this->width += rhs.width;
this->height += rhs.height;
return *this;
}

NXSize& NXSize::operator-=(const NXSize& rhs) {
this->width -= rhs.width;
this->height -= rhs.height;
return *this;
}

NXSize NXSize::operator*(const NXFloat& first) const {
return NXSize(width * first, height * first);
}

NXSize NXSize::operator/(const NXFloat& first) const {
return NXSize(width / first, height / first);
}

NXSize& NXSize::operator*=(const NXFloat& rhs) {
this->width *= rhs;
this->height *= rhs;
return *this;
}

NXSize& NXSize::operator/=(const NXFloat& rhs) {
this->width /= rhs;
this->height /= rhs;
return *this;
}
3 changes: 2 additions & 1 deletion Submodules/UIKit/lib/platforms/SkiaCtx.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "SkiaCtx.h"

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

using namespace NXKit;

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

using namespace NXKit;

SkiaCtx_sdlBase::SkiaCtx_sdlBase(SDL_Window *window): window(window)
{ }

Expand All @@ -9,6 +11,13 @@ void SkiaCtx_sdlBase::swapBuffers() {

NXSize SkiaCtx_sdlBase::getSize() {
int w, h;
SDL_GL_GetDrawableSize(window, &w, &h);
SDL_GetWindowSize(window, &w, &h);
return { (NXFloat)w, (NXFloat)h };
}
}

float SkiaCtx_sdlBase::getScaleFactor() {
int w, h, dw, dh;
SDL_GetWindowSize(window, &w, &h);
SDL_GL_GetDrawableSize(window, &dw, &dh);
return dw / w;
}
Loading

0 comments on commit 6b4fb0e

Please sign in to comment.