Skip to content

Commit

Permalink
Minor improvement to test Skia API (#1154)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon authored Nov 28, 2022
1 parent 9aa784d commit bee9011
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 33 deletions.
17 changes: 11 additions & 6 deletions example/src/Tests/Tests.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { SkiaDomView } from "@shopify/react-native-skia";
import { Canvas } from "@shopify/react-native-skia";
import { Group, Canvas, Skia } from "@shopify/react-native-skia";
import React, { useEffect, useRef, useState } from "react";
import { Text, View } from "react-native";
import { PixelRatio, Text, View } from "react-native";

import type { SerializedNode } from "./deserialize";
import { parseProps, parseNode } from "./deserialize";
import { parseNode } from "./deserialize";
import { useClient } from "./useClient";

const scale = 3 / PixelRatio.get();
const size = 256 * scale;

export const Tests = () => {
const ref = useRef<SkiaDomView>(null);
const client = useClient();
Expand All @@ -20,7 +23,9 @@ export const Tests = () => {
client.send(
JSON.stringify(
// eslint-disable-next-line no-eval
eval(`(function Main(){${tree.code}})`).call(parseProps(tree.ctx))
eval(`(function Main(){const {Skia} = this;${tree.code}})`).call({
Skia,
})
)
);
} else {
Expand Down Expand Up @@ -52,8 +57,8 @@ export const Tests = () => {
return (
<View style={{ flex: 1 }}>
<Text>💚 Waiting for the server to send tests</Text>
<Canvas style={{ width: 256, height: 256 }} ref={ref}>
{drawing}
<Canvas style={{ width: size, height: size }} ref={ref}>
<Group transform={[{ scale }]}>{drawing}</Group>
</Canvas>
</View>
);
Expand Down
2 changes: 1 addition & 1 deletion example/src/Tests/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const parseNode = (serializedNode: SerializedNode): any => {
);
};

export const parseProps = (props: SerializedProps) => {
const parseProps = (props: SerializedProps) => {
const newProps: SerializedProps = {};
Object.keys(props).forEach((key) => {
newProps[key] = parseProp(props[key]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ class RNSkAndroidPlatformContext : public RNSkPlatformContext {
[this]() { notifyDrawLoop(false); });
}

~RNSkAndroidPlatformContext() {
stopDrawLoop();
}
~RNSkAndroidPlatformContext() { stopDrawLoop(); }

void performStreamOperation(
const std::string &sourceUri,
Expand Down
4 changes: 2 additions & 2 deletions package/android/cpp/rnskia-android/SkiaOpenGLRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"

#include "include/gpu/GrDirectContext.h"
#include "include/gpu/gl/GrGLInterface.h"
#include "SkCanvas.h"
#include "SkColorSpace.h"
#include "SkPicture.h"
#include "SkSurface.h"
#include "include/gpu/GrDirectContext.h"
#include "include/gpu/gl/GrGLInterface.h"

#pragma clang diagnostic pop

Expand Down
4 changes: 2 additions & 2 deletions package/cpp/api/JsiSkPathEffectFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"

#include "include/effects/Sk1DPathEffect.h"
#include "include/effects/Sk2DPathEffect.h"
#include "SkCornerPathEffect.h"
#include "SkDashPathEffect.h"
#include "SkDiscretePathEffect.h"
#include "SkPathEffect.h"
#include "include/effects/Sk1DPathEffect.h"
#include "include/effects/Sk2DPathEffect.h"

#pragma clang diagnostic pop

Expand Down
2 changes: 1 addition & 1 deletion package/cpp/api/JsiSkPathFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"

#include <RNSkLog.h>
#include "SkPath.h"
#include "SkPathOps.h"
#include <RNSkLog.h>

#pragma clang diagnostic pop

Expand Down
3 changes: 2 additions & 1 deletion package/cpp/rnskia/RNSkPlatformContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ class RNSkPlatformContext {
}
}

// default implementation does nothing, so it can be called from virtual destructor.
// default implementation does nothing, so it can be called from virtual
// destructor.
virtual void startDrawLoop() {}
virtual void stopDrawLoop() {}

Expand Down
9 changes: 4 additions & 5 deletions package/src/renderer/__tests__/e2e/Drawings.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,12 @@ describe("Drawings", () => {
});

it("Should do rect marshalling properly", async () => {
const { Skia } = importSkia();
const result = await surface.eval(
`
this.path.addRect({ x: 0, y: 0, width: 100, height: 100 });
return this.path.getBounds().width;
`,
{ path: Skia.Path.Make() }
const path = Skia.Path.Make();
path.addRect({ x: 0, y: 0, width: 100, height: 100 });
return path.getBounds().width;
`
);
expect(result).toBe(100);
});
Expand Down
22 changes: 10 additions & 12 deletions package/src/renderer/__tests__/setup.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/ban-types */
/* eslint-disable @typescript-eslint/no-explicit-any */
import fs from "fs";
import path from "path";
Expand Down Expand Up @@ -305,7 +304,7 @@ type Assets = Map<SkFont, number[]>;

interface TestingSurface {
init(): Promise<void>;
eval(code: string, ctx: {}): Promise<string>;
eval(code: string): Promise<string>;
draw(node: ReactNode, assets?: Assets): Promise<SkImage>;
dispose(): void;
width: number;
Expand All @@ -326,9 +325,13 @@ class LocalSurface implements TestingSurface {

dispose(): void {}

eval(code: string, ctx: {}): Promise<string> {
// eslint-disable-next-line no-eval
return Promise.resolve(eval(`(function Main(){${code}})`).call(ctx));
eval(code: string): Promise<string> {
return Promise.resolve(
// eslint-disable-next-line no-eval
eval(`(function Main(){const {Skia} = this;${code}})`).call({
Skia: global.SkiaApi,
})
);
}

draw(node: ReactNode): Promise<SkImage> {
Expand Down Expand Up @@ -373,19 +376,14 @@ class RemoteSurface implements TestingSurface {
}
}

eval(code: string, ctx: { [key: string]: any }): Promise<string> {
eval(code: string): Promise<string> {
this.assertInit();
return new Promise((resolve) => {
const client = this.client!;
client!.once("message", (raw: Buffer) => {
resolve(JSON.parse(raw.toString()));
});

const newCtx: { [key: string]: any } = {};
Object.keys(ctx).forEach((key) => {
newCtx[key] = serializeSkOjects(ctx[key], new Map());
});
client!.send(JSON.stringify({ code, ctx: newCtx }));
client!.send(JSON.stringify({ code }));
});
}

Expand Down

0 comments on commit bee9011

Please sign in to comment.