Skip to content

Commit

Permalink
[go] update @shopify/react-native-skia to 0.1.195 (expo#23036)
Browse files Browse the repository at this point in the history
# Why

update @shopify/react-native-skia vendoring module for sdk 49

# How

- `et uvm -m @shopify/react-native-skia -c 0.1.195`

# Test Plan

- unversioned expo go + unversioned ncl skia
  • Loading branch information
Kudo authored Jun 22, 2023
1 parent 5ae19ef commit d2d1c43
Show file tree
Hide file tree
Showing 22 changed files with 227 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class RNSkAndroidView : public T, public RNSkBaseAndroidView {
void surfaceAvailable(jobject surface, int width, int height) override {
std::static_pointer_cast<RNSkOpenGLCanvasProvider>(T::getCanvasProvider())
->surfaceAvailable(surface, width, height);

// Try to render directly when the surface has been set so that
// we don't have to wait until the draw loop returns.
RNSkView::renderImmediate();
}

void surfaceDestroyed() override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ float RNSkOpenGLCanvasProvider::getScaledWidth() { return _width; }

float RNSkOpenGLCanvasProvider::getScaledHeight() { return _height; }

void RNSkOpenGLCanvasProvider::renderToCanvas(
bool RNSkOpenGLCanvasProvider::renderToCanvas(
const std::function<void(SkCanvas *)> &cb) {
if (_renderer != nullptr) {
_renderer->run(cb, _width, _height);
return _renderer->run(cb, _width, _height);
}
return false;
}

void RNSkOpenGLCanvasProvider::surfaceAvailable(jobject surface, int width,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class RNSkOpenGLCanvasProvider

float getScaledHeight() override;

void renderToCanvas(const std::function<void(SkCanvas *)> &cb) override;
bool renderToCanvas(const std::function<void(SkCanvas *)> &cb) override;

void surfaceAvailable(jobject surface, int width, int height);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <android/native_window.h>
#include <android/native_window_jni.h>

#define STENCIL_BUFFER_SIZE 8

namespace RNSkia {
/** Static members */
sk_sp<SkSurface> MakeOffscreenGLSurface(int width, int height) {
Expand Down Expand Up @@ -137,7 +139,7 @@ SkiaOpenGLRenderer::~SkiaOpenGLRenderer() {
_nativeWindow = nullptr;
}

void SkiaOpenGLRenderer::run(const std::function<void(SkCanvas *)> &cb,
bool SkiaOpenGLRenderer::run(const std::function<void(SkCanvas *)> &cb,
int width, int height) {
switch (_renderState) {
case RenderState::Initializing: {
Expand All @@ -148,31 +150,48 @@ void SkiaOpenGLRenderer::run(const std::function<void(SkCanvas *)> &cb,
case RenderState::Rendering: {
// Make sure to initialize the rendering pipeline
if (!ensureInitialised()) {
break;
}

// Ensure we have the Skia surface to draw on. We need to
// pass width and height since the surface will be recreated
// when the view is resized.
if (!ensureSkiaSurface(width, height)) {
return;
return false;
}

if (cb != nullptr) {
// Reset Skia Context since it might be modified by another Skia View
// during rendering.
// RNSkLogger::logToConsole("SKIARENDER - Render begin");

getThreadDrawingContext()->skContext->resetContext();

SkColorType colorType;
// setup surface for fbo0
GrGLFramebufferInfo fboInfo;
fboInfo.fFBOID = 0;
fboInfo.fFormat = 0x8058;
colorType = kN32_SkColorType;

GrBackendRenderTarget backendRT(width, height, 0, STENCIL_BUFFER_SIZE,
fboInfo);

SkSurfaceProps props(0, kUnknown_SkPixelGeometry);

sk_sp<SkSurface> renderTarget(SkSurface::MakeFromBackendRenderTarget(
getThreadDrawingContext()->skContext.get(), backendRT,
kBottomLeft_GrSurfaceOrigin, colorType, nullptr, &props));

auto canvas = renderTarget->getCanvas();

// Draw picture into surface
cb(_skSurface->getCanvas());
cb(canvas);

// Flush
_skSurface->flushAndSubmit();
canvas->flush();

if (!eglSwapBuffers(getThreadDrawingContext()->glDisplay, _glSurface)) {
RNSkLogger::logToConsole("eglSwapBuffers failed: %d\n", eglGetError());
return false;
}

// RNSkLogger::logToConsole("SKIARENDER - render done");
return true;
}
break;

return false;
}
case RenderState::Finishing: {
_renderState = RenderState::Done;
Expand All @@ -184,14 +203,11 @@ void SkiaOpenGLRenderer::run(const std::function<void(SkCanvas *)> &cb,
_glSurface = EGL_NO_SURFACE;
}

// Release Skia Surface
_skSurface = nullptr;

break;
return true;
}
case RenderState::Done: {
// Do nothing. We're done.
break;
return true;
}
}
}
Expand Down Expand Up @@ -326,54 +342,4 @@ bool SkiaOpenGLRenderer::initGLSurface() {

return true;
}

bool SkiaOpenGLRenderer::ensureSkiaSurface(int width, int height) {
if (getThreadDrawingContext()->skContext == nullptr) {
return false;
}

if (_skSurface == nullptr || !_skRenderTarget.isValid() ||
_prevWidth != width || _prevHeight != height) {
glViewport(0, 0, width, height);

_prevWidth = width;
_prevHeight = height;

GLint buffer;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &buffer);

GLint stencil;
glGetIntegerv(GL_STENCIL_BITS, &stencil);

GLint samples;
glGetIntegerv(GL_SAMPLES, &samples);

auto maxSamples =
getThreadDrawingContext()->skContext->maxSurfaceSampleCountForColorType(
kRGBA_8888_SkColorType);

if (samples > maxSamples)
samples = maxSamples;

GrGLFramebufferInfo fbInfo;
fbInfo.fFBOID = buffer;
fbInfo.fFormat = 0x8058;

_skRenderTarget =
GrBackendRenderTarget(width, height, samples, stencil, fbInfo);

_skSurface = SkSurface::MakeFromBackendRenderTarget(
getThreadDrawingContext()->skContext.get(), _skRenderTarget,
kBottomLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, nullptr, nullptr);

if (!_skSurface) {
RNSkLogger::logToConsole(
"JniSkiaDrawView::setupSurface - skSurface could not be created!");
return false;
}

return true;
}
return true;
}
} // namespace RNSkia
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class SkiaOpenGLRenderer {
* @param width Width of surface to render if there is a picture
* @param height Height of surface to render if there is a picture
*/
void run(const std::function<void(SkCanvas *)> &cb, int width, int height);
bool run(const std::function<void(SkCanvas *)> &cb, int width, int height);

/**
* Sets the state to finishing. Next time the renderer will be called it
Expand Down Expand Up @@ -102,15 +102,6 @@ class SkiaOpenGLRenderer {
*/
bool initGLSurface();

/**
* Ensures that we have a valid Skia surface to draw to. The surface will
* be recreated if the width/height change.
* @param width Width of the underlying view
* @param height Height of the underlying view
* @return True if initialization went well
*/
bool ensureSkiaSurface(int width, int height);

/**
* To be able to use static contexts (and avoid reloading the skia context for
* each new view, we track the OpenGL and Skia drawing context per thread.
Expand All @@ -121,8 +112,6 @@ class SkiaOpenGLRenderer {
EGLSurface _glSurface = EGL_NO_SURFACE;

ANativeWindow *_nativeWindow = nullptr;
GrBackendRenderTarget _skRenderTarget;
sk_sp<SkSurface> _skSurface;

int _prevWidth = 0;
int _prevHeight = 0;
Expand Down
Loading

0 comments on commit d2d1c43

Please sign in to comment.