Surface is not updating

47 views
Skip to first unread message

Max Ganiyev

unread,
May 18, 2025, 9:14:00 PM5/18/25
to skia-discuss
Hey,

I'm trying to wrap an existing GLuint texture created in Unity. Everything works as expected, except it only draws the first frame, and subsequent frames do not update, the texture remains static. Unless I recreate the GL interface and rebind everything each frame.

Since I'm new to C++, it seems to me that recreating a new surface each frame is not the correct approach.

Here is my simple setup, which only draws the first frame. Further frames do not render, even though the drawing function is called each frame.

static void UNITY_INTERFACE_API OnRenderEvent(int eventId)
{
    if (!textureID || textureWidth == 0 || textureHeight == 0) {
        return;
    }

    if (!skiaGrContext) {
        // Initialize
        sk_sp<const GrGLInterface> glInterface = GrGLMakeNativeInterface();

        skiaGrContext = GrDirectContexts::MakeGL(glInterface);

        GrGLTextureInfo textureInfo = GrGLTextureInfo();
        textureInfo.fID = textureID;
        textureInfo.fTarget = GL_TEXTURE_2D;
        textureInfo.fFormat = GL_RGBA8;

        GrBackendTexture backendTexture = GrBackendTextures::MakeGL(
            textureWidth,
            textureHeight,
            skgpu::Mipmapped::kYes,
            textureInfo,
            "test"
        );

        skiaSurface = SkSurfaces::WrapBackendTexture(
            skiaGrContext.get(),
            backendTexture,
            GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
            0,
            kRGBA_8888_SkColorType,
            nullptr,
            nullptr,
            nullptr
        );
    }
    else {
        // Draw to the existing Skia surface
        skiaCanvas = skiaSurface->getCanvas();

        skiaCanvas->clear(SK_ColorBLACK);

        SkPaint paint = SkPaint();
        paint.setAntiAlias(true);
        paint.setColor(SK_ColorRED);

        skiaCanvas->drawCircle(SkPoint(), SkScalar(sin(unityTime) * 150), paint);

        skiaGrContext.get()->flushAndSubmit(GrSyncCpu::kYes);
    }
}

textureID is retrieved from Unity. 

Is there anything am I missing? Any ideas what could be an issue?
Or rewrapping the texture each frame is a common practice?

neats...@gmail.com

unread,
May 19, 2025, 3:44:49 AM5/19/25
to skia-discuss
I think your 'glInterface' gets deleted once the corresponding smart pointer ( sk_sp<const GrGLInterface> ) leaves the scope it's declared in.
Message has been deleted

Max Ganiyev

unread,
May 19, 2025, 9:09:19 AM5/19/25
to skia-discuss

Ah, my bad on posting the wrong code here, it was just a part of what I tried to initialize the interface each frame. In actual code where I'm experiencing the issue the GrGLInterface is cached into a static sk_sp<const GrGLInterface>

neats...@gmail.com

unread,
May 19, 2025, 9:44:31 AM5/19/25
to skia-discuss
It's possible that between frames Unity is changing the OpenGL context state that Skia expects to remain unchanged. You can debug this with RenderDoc or a similar tool
Reply all
Reply to author
Forward
0 new messages