segmentation fault while taking snapshot of image using Skia

129 views
Skip to first unread message

Ninad Dafale

unread,
May 19, 2023, 7:48:37 AM5/19/23
to skia-discuss
I have built a static SKIA library for capturing a snapshot of the provided image.

The build process followed was as follows -:
   git clone https://skia.googlesource.com/skia.git
    bin/gn gen out/Static --args='is_official_build=false cc="clang" cxx="clang++"'
    ninja -C out/Static

code I am using (snapshot.cpp) -:
#include <filesystem>
#include <iostream>
#include "include/codec/SkCodec.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkData.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkStream.h"
#include "include/core/SkString.h"
#include "include/core/SkSurface.h"
// #include "include/core/SkImageEncoder.h"
#include "include/core/SkEncodedImageFormat.h"
#include "include/core/SkImageGenerator.h"
#include "include/core/SkRect.h"
#include "include/encode/SkWebpEncoder.h"
#include "include/core/SkGraphics.h"
#include "include/core/SkImage.h"
#include "include/core/SkPaint.h"
#include "include/core/SkSurfaceProps.h"
#include "tools/Resources.h"
#include "include/core/SkColorSpace.h"
#include "include/core/SkData.h"
#include "include/core/SkImage.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkSize.h"
#include "include/core/SkString.h"
#include "gm/gm.h"
#include "include/codec/SkEncodedImageFormat.h"

using namespace std;
int main() {
     SkString path = SkStringPrintf("path_to_image");
     sk_sp<SkImage> image =
             SkImages::DeferredFromEncodedData(SkData::MakeFromFileName(path.c_str()));
     int width = image->width();
     int height = image->height();

     // Create a surface with the same dimensions as the input image
     SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
     sk_sp<SkSurface> surface = SkSurface::MakeRaster(info);

     // Get the canvas from the surface
     SkCanvas* canvas = surface->getCanvas();

     // Draw the input image onto the canvas
     canvas->drawImage(image, 0, 0);

     // Save the snapshot to a file in WebP format
     const char* snapshot_path = "path_to_store_webp";
     sk_sp<SkImage> snapshot_image = surface->makeImageSnapshot();

     SkPixmap pixmap2;
     if (!image->peekPixels(&pixmap2)) {
        // return 1;
     }

     // Encode the SkPixmap to a WebP file
     SkFILEWStream file_stream(snapshot_path);
     SkWebpEncoder::Encode(&file_stream, pixmap2, SkWebpEncoder::Options());

     return 0;
}

compiled the code using the command -:

 g++ -g -std=c++17 snapshot.cpp -o snapshot -I/home/maint/code/skia -L/home/maint/code/skia/out/Static -lskia -pthread -lfreetype -lfontconfig -lGL

it is giving me a seg fault which I have backtrace using gdb as shown in the screenshot -:
skia.PNG

pls, can anyone tell us where it is going wrong.

thanks in advance...!!

John Stiles

unread,
May 19, 2023, 7:53:18 AM5/19/23
to skia-discuss
This won't solve the problem, but try setting `is_debug = true` in your GN args and set -O0 when building your program. Having assertions enabled and optimizations off will often make the problem a lot easier to find.

K. Moon

unread,
May 19, 2023, 12:32:58 PM5/19/23
to skia-d...@googlegroups.com
Can you post the gdb output as text, rather than a screenshot?

--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/0c43af8e-0837-4652-be03-fd1378079106n%40googlegroups.com.

Ninad Dafale

unread,
May 22, 2023, 8:48:18 AM5/22/23
to skia-d...@googlegroups.com
thanks for the reply but no luck by changing GN args setting `is_debug = true`

--
You received this message because you are subscribed to a topic in the Google Groups "skia-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/skia-discuss/tf68tGiZH-A/unsubscribe.
To unsubscribe from this group and all its topics, send an email to skia-discuss...@googlegroups.com.

Ninad Dafale

unread,
May 22, 2023, 8:48:27 AM5/22/23
to skia-d...@googlegroups.com
thanks for reply @k.Moon

gdb output 

(gdb) r
Starting program: /home/maint/code/skia/snapshot
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
*********** main start ****************

Program received signal SIGSEGV, Segmentation fault.
__memcpy_ssse3 () at ../sysdeps/x86_64/multiarch/memcpy-ssse3.S:1799
1799    ../sysdeps/x86_64/multiarch/memcpy-ssse3.S: No such file or directory.
(gdb) bt
#0  __memcpy_ssse3 () at ../sysdeps/x86_64/multiarch/memcpy-ssse3.S:1799
#1  0x00005555555ccb88 in SkMemoryStream::read (this=0x5555568deab0, buffer=0x7fffffffe270, size=32) at ../../src/core/SkStream.cpp:349
#2  0x00005555555ccc12 in SkMemoryStream::peek (this=0x5555568deab0, buffer=0x7fffffffe270, size=32) at ../../src/core/SkStream.cpp:360
#3  0x00005555561d6565 in SkCodec::MakeFromStream (stream=std::unique_ptr<SkStream> = {...}, outResult=0x7fffffffe2a0, chunkReader=0x0,
    selectionPolicy=SkCodec::SelectionPolicy::kPreferStillImage) at ../../src/codec/SkCodec.cpp:128
#4  0x00005555561d6946 in SkCodec::MakeFromData (data=..., reader=0x0) at ../../src/codec/SkCodec.cpp:193
#5  0x0000555555f82245 in SkCodecImageGenerator::MakeFromEncodedCodec (data=..., at=std::optional<SkAlphaType> = {...})
    at ../../src/codec/SkCodecImageGenerator.cpp:23
#6  0x000055555581e853 in SkImageGenerators::MakeFromEncodedImpl (data=..., at=std::optional<SkAlphaType> = {...})
    at ../../src/ports/SkImageGenerator_skia.cpp:16
#7  0x0000555555649c0b in SkImageGenerators::MakeFromEncoded (data=..., at=std::optional<SkAlphaType> = {...}) at ../../src/core/SkImageGenerator.cpp:101
#8  0x00005555555d65ad in SkImages::DeferredFromEncodedData (encoded=..., alphaType=std::optional<SkAlphaType> = {...})
    at ../../src/image/SkImage_LazyFactories.cpp:32
#9  0x00005555555c9a66 in main () at snapshot.cpp:36
(gdb) Quit
(gdb)




K. Moon

unread,
May 22, 2023, 1:40:09 PM5/22/23
to skia-d...@googlegroups.com
Is the stack trace the same with is_debug=true? I don't have any bright ideas, but it looks like SkMemoryStream::read() is trying to copy memory that either doesn't exist, or isn't writable.

Reply all
Reply to author
Forward
0 new messages