Skip to content
This repository was archived by the owner on Nov 27, 2025. It is now read-only.

Commit 43a3dff

Browse files
committed
feat: 'flush' grContext after every frame
in order to tell Skia to paint stuff.
1 parent 496d083 commit 43a3dff

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/ISkiaGLWindow.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
#include "ISkiaGLWindow.h"
22

33
#include "include/gpu/gl/GrGLInterface.h"
4-
#include "include/gpu/GrContext.h"
54
#include "include/core/SkImageInfo.h"
65
#include "include/core/SkGraphics.h"
76

87
// The Skia Surface is the object we use to talk to the GPU
98
// In order to render to an OpenGL context, we need to obtain the
109
// SkSurface from the GL context directly.
1110
// Ref: https://skia.org/docs/user/api/skcanvas_creation/
12-
sk_sp<SkSurface> obtainSkiaSurfaceFromCurrentGLContext(GLWindow *glWindow) {
13-
auto glContext = glWindow->glContext();
11+
sk_sp<SkSurface> ISkiaGLWindow::obtainSkiaSurfaceFromCurrentGLContext() {
12+
auto glContext = m_glWindow->glContext();
1413

1514
// You've already created your OpenGL context and bound it.
1615
sk_sp<const GrGLInterface> interface = nullptr;
1716

1817
const GrContextOptions &grOptions = GrContextOptions();
19-
auto grContext = GrContext::MakeGL(interface, grOptions);
18+
m_graphicsContext = GrContext::MakeGL(interface, grOptions);
2019

21-
int width = glWindow->width();
22-
int height = glWindow->height();
20+
int width = m_glWindow->width();
21+
int height = m_glWindow->height();
2322
SkImageInfo imageInformation = SkImageInfo::MakeN32Premul(width, height);
2423

2524

2625
SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
2726

2827
// Create the Skia Surface from the GL context
2928
return SkSurface::MakeRenderTarget(
30-
grContext.get(),
29+
m_graphicsContext.get(),
3130
SkBudgeted::kNo,
3231
imageInformation,
3332
kTopLeft_GrSurfaceOrigin,
@@ -39,7 +38,7 @@ sk_sp<SkSurface> obtainSkiaSurfaceFromCurrentGLContext(GLWindow *glWindow) {
3938
SkCanvas* ISkiaGLWindow::obtainSkCanvas() {
4039
// NOTE: We put SkiaSurface in a class field to keep it in memory
4140
// So we can keep drawing (either it crashes)
42-
m_skiaSurface = obtainSkiaSurfaceFromCurrentGLContext(m_glWindow);
41+
m_skiaSurface = obtainSkiaSurfaceFromCurrentGLContext();
4342
if (!m_skiaSurface) {
4443
return nullptr;
4544
}
@@ -56,6 +55,8 @@ void ISkiaGLWindow::requestPaint() {
5655
if (!m_skiaContext || !m_skiaSurface) return;
5756

5857
paintThisFrame(m_skiaContext);
58+
// Tell Skia to render stuff to GPU
59+
m_graphicsContext->flush();
5960
};
6061

6162
ISkiaGLWindow::ISkiaGLWindow(GLWindow *glWindow)

src/ISkiaGLWindow.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "GLWindow.h"
44
#include "include/core/SkCanvas.h"
55
#include "include/core/SkSurface.h"
6+
#include "include/gpu/GrContext.h"
67

78
// This object connects the Skia library to the OpenGL window,
89
// and provides a method to draw.
@@ -20,9 +21,12 @@ class ISkiaGLWindow {
2021

2122
// Skia Canvas is the object we use to draw shapes
2223
SkCanvas* m_skiaContext = nullptr;
24+
sk_sp<SkSurface> obtainSkiaSurfaceFromCurrentGLContext();
2325
SkCanvas* obtainSkCanvas();
2426
// we store skia surface here to keep it in memory
2527
sk_sp<SkSurface> m_skiaSurface;
28+
// portal between software and the gpu
29+
sk_sp<GrContext> m_graphicsContext = nullptr;
2630

2731
// Call this function when something happens on the canvas and needs to be redrawn.
2832
// (An object changes, a new keyframe is selected, zoom in/out...)

0 commit comments

Comments
 (0)