Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

SkSurface::getCanvas() always gets nullptr.

182 views
Skip to first unread message

Fledge Xu

unread,
Jan 6, 2024, 4:20:43 PM1/6/24
to skia-discuss
Hi I'm a new comer for Skia, I was trying to build a example of skia, but I met a wired error.
There is my code:

#include "include/core/SkImageInfo.h"
#include "include/core/SkSurface.h"

int main(int argc, char *argv[]) {
  sk_sp<SkSurface> surface =
      SkSurfaces::Raster(SkImageInfo::MakeN32Premul(500, 500));
  auto canvas = surface->getCanvas();
  return 0;
}

This piece of code should create a canvas, but it yields a segment fault, I try to debug it and find SkSurface_Base::fCachedCanvas is a nullptr for some reasons.

I also tried to assign the ColorType like this:

#include "include/core/SkColorType.h"
#include "include/core/SkAlphaType.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkSurface.h"

int main(int argc, char *argv[]) {
  sk_sp<SkSurface> surface =
      SkSurfaces::Raster(SkImageInfo::Make(500, 500, kBGRA_8888_SkColorType, kPremul_SkAlphaType));
  auto canvas = surface->getCanvas();
  return 0;
}

However, it's also not working.

Here is my skia buid commands:
bin/gn gen out/Debug --args='is_official_build=false is_debug=true target_cpu="arm64"'
my system is: Apple M2 Max, macOS: 14.2.1 (23C71)
Skia Version: canvaskit/0.38.2

Peng-Yu Chen

unread,
Jan 7, 2024, 2:45:06 PM1/7/24
to skia-discuss
(Sorry in advance that I've got little experience with Skia.)

1.
Your sample code (1st piece) seems to work fine (no segfault) on my machine (GNU/Linux x86_64) with the same release of Skia (canvaskit/0.38.2, or 57f648e943).
Hypothesis is that it's relevant to your system environment. Sorry I don't have an ARM-based Mac to further check with.

2.
> but it yields a segment fault
Could you share the stack trace?

3.
> I try to debug it and find SkSurface_Base::fCachedCanvas is a nullptr for some reasons.
That alone doesn't seem unexpected. A canvas may not be initialized until the code calls SkSurface::getCanvas.
My best guess would be that it crashes during canvas creation and a stack trace shall greatly help narrow the scope.

goola bool

unread,
Mar 18, 2025, 12:45:19 PMMar 18
to skia-discuss
Hello, I encountered a similar issue as well. By adding the argument is_trivial_abi=false in the --args parameter in GN, I was able to resolve it.
(I also try add -O0 in extra_cflags_cc, but it does not work for me.)

   gn gen out/Shared --args='is_trivial_abi=false ......'

The root cause of the problem was related to optimization. it in in include/core/SkSurface.h

inline sk_sp<SkSurface> Raster(const SkImageInfo& imageInfo,
                               const SkSurfaceProps* props = nullptr) {
    return Raster(imageInfo, 0, props);
}


Use gdb, by inspecting the parameter addresses, I identified an offset 8. U  can  input: print imageInfo -> step -> print info, the imageInfo and info address is diffrent, they should be same.

(gdb) print imageInfo
$1 = (const SkImageInfo &) @0x7fffffffd0d0: {fColorInfo = {fColorSpace = {fPtr = 0x0}, fColorType = kBGRA_8888_SkColorType, fAlphaType = kPremul_SkAlphaType}, fDimensions = {fWidth = 800, fHeight = 563}}
[Thread 0x7fffdbfff6c0 (LWP 136331) exited]
(gdb) step
SkSurfaces::Raster (info=..., rowBytes=0, props=0x0) at ../../src/image/SkSurface_Raster.cpp:187
187     if (!SkSurfaceValidateRasterInfo(info)) {
(gdb) print info
$2 = (const SkImageInfo &) @0x7fffffffd0c8: {fColorInfo = {fColorSpace = {fPtr = 0x7ffff7be0562}, fColorType = kUnknown_SkColorType, fAlphaType = kUnknown_SkAlphaType}, fDimensions = {fWidth = 6, fHeight = 2}}
Reply all
Reply to author
Forward
0 new messages