creating a skimage (gpu-backend), and throwing it into a non-gl thread would cause program crash

37 views
Skip to first unread message

han “mor” wang

unread,
Jul 22, 2025, 3:57:51 PMJul 22
to skia-discuss
hello everyone,
    when I was using skia, I confront a issue.
   First I create skimage (gpu-backend ,using makeImageSnapshot) in gl-contex thread, and post into anothe non-gl thread.
   As a result, program crased becuase of invoking gl-function in a non-gl thread. I wonder if skia should keep gl-resource from releasing in a non-gl thread. I upload a patch to try to solve it. (gerrit)I would be very thankful if you could review the uploaded code  in your spare time.

I post source code in below 
static void run() {
sk_sp<SkImage> img;
int i = 0;
int j = 0;
std::vector<sk_sp<SkImage>> vc;
while (1) {
std::unique_lock<std::mutex> lck(mtx);
while (!run_loop.load()) {
std::cout<< "i am waiting" <<std::endl;
cv.wait(lck);
}
{
std::lock_guard<std::mutex> lck(task_mutex);
if (!Tasks.empty()) {
auto tmp = Tasks.front()();
img = tmp;
Tasks.pop();
}
}
if (img) {
int width = img->width();
i++;
j++;
//std::cout <<" width is" <<width << " i: "<<i++ <<std::endl;
}
}
}

int main() {
initGLFWWindow(window_width, window_height);
init_skia(window_width, window_height);
std::thread t(run);
std::cout << " skia init ok " << std::endl;

SkCanvas *canvas = g_surface->getCanvas();
int i = 0;
bool first = true;
while (!glfwWindowShouldClose(g_window)) {
canvas->drawColor(SK_ColorWHITE);
SkRect rect = SkRect::MakeXYWH(0,0,250,250);
SkPaint paint;
paint.setColor(SK_ColorRED);
canvas->drawRect(rect,paint);
if (i == 0) {
//i++;
{
auto tmpImage = g_surface->makeImageSnapshot();
SkBitmap bitmap;
// if (!tmpImage->asLegacyBitmap(&bitmap)) {
// return 0;
// }
std::lock_guard<std::mutex> lock(task_mutex);
Tasks.push([tmpImage]() {
return tmpImage;
});
}
if (first) {
run_loop.store(true);
cv.notify_all();
first = false;
}
//t.join();
}
g_context->flush();
g_context->submit();

glfwSwapBuffers(g_window);
glfwPollEvents();

}
}
Reply all
Reply to author
Forward
0 new messages