Skip to content

Commit

Permalink
Remove encodeToData from CanvasKit and documentation
Browse files Browse the repository at this point in the history
Follow-up to https://skia-review.googlesource.com/c/skia/+/667296

This adds gn arguments so clients can explicitly disable certain
encoders without having to use #ifdefs.

Change-Id: I49b9e2db3b268eaf4171b720ebfed09472af3859
Bug: skia:13983
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/669916
Reviewed-by: Leon Scroggins <[email protected]>
  • Loading branch information
kjlubick committed Apr 12, 2023
1 parent 60777d3 commit e2d3dc9
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 17 deletions.
10 changes: 10 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1548,6 +1548,16 @@ skia_component("skia") {
defines += [ "SK_BUILD_FOR_DEBUGGER" ]
}

if (skia_use_no_jpeg_encode) {
sources += skia_no_encode_jpeg_srcs
}
if (skia_use_no_png_encode) {
sources += skia_no_encode_png_srcs
}
if (skia_use_no_webp_encode) {
sources += skia_no_encode_webp_srcs
}

if (is_win) {
sources += [
"src/ports/SkDebug_win.cpp",
Expand Down
3 changes: 3 additions & 0 deletions gn/skia.gni
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ declare_args() {
skia_use_jpeg_gainmaps = is_skia_dev_build
skia_use_libjpeg_turbo_decode = true
skia_use_libjpeg_turbo_encode = true
skia_use_no_jpeg_encode = false
skia_use_libjxl_decode = false
skia_use_libpng_decode = true
skia_use_libpng_encode = true
skia_use_no_png_encode = false
skia_use_libwebp_decode = true
skia_use_libwebp_encode = !is_wasm
skia_use_no_webp_encode = false
skia_use_lua = is_skia_dev_build && !is_ios
skia_use_metal = false
skia_use_ndk_images = is_android && defined(ndk_api) && ndk_api >= 30
Expand Down
44 changes: 34 additions & 10 deletions modules/canvaskit/canvaskit_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
#include "include/effects/SkPerlinNoiseShader.h"
#include "include/effects/SkRuntimeEffect.h"
#include "include/effects/SkTrimPathEffect.h"
#include "include/encode/SkJpegEncoder.h"
#include "include/encode/SkPngEncoder.h"
#include "include/encode/SkWebpEncoder.h"
#include "include/private/SkShadowFlags.h"
#include "include/utils/SkParsePath.h"
#include "include/utils/SkShadowUtils.h"
Expand Down Expand Up @@ -917,6 +920,35 @@ sk_sp<SkImage> MakeImageFromGenerator(SimpleImageInfo ii, JSObject callbackObj)
}
#endif // CK_ENABLE_WEBGL


static Uint8Array encodeImage(GrDirectContext* dContext,
sk_sp<SkImage> img,
SkEncodedImageFormat fmt,
int quality) {
sk_sp<SkData> data = nullptr;
if (fmt == SkEncodedImageFormat::kJPEG) {
SkJpegEncoder::Options opts;
opts.fQuality = quality;
data = SkJpegEncoder::Encode(dContext, img.get(), opts);
} else if (fmt == SkEncodedImageFormat::kPNG) {
data = SkPngEncoder::Encode(dContext, img.get(), {});
} else {
SkWebpEncoder::Options opts;
if (quality >= 100) {
opts.fCompression = SkWebpEncoder::Compression::kLossless;
opts.fQuality = 75; // This is effort to compress
} else {
opts.fCompression = SkWebpEncoder::Compression::kLossy;
opts.fQuality = quality;
}
data = SkWebpEncoder::Encode(dContext, img.get(), opts);
}
if (!data) {
return emscripten::val::null();
}
return toBytes(data);
}

EMSCRIPTEN_BINDINGS(Skia) {
#ifdef ENABLE_GPU
constant("gpu", true);
Expand Down Expand Up @@ -1502,22 +1534,14 @@ EMSCRIPTEN_BINDINGS(Skia) {
.function("_encodeToBytes", optional_override([](sk_sp<SkImage> self,
SkEncodedImageFormat fmt,
int quality) -> Uint8Array {
sk_sp<SkData> data = self->encodeToData(fmt, quality);
if (!data) {
return emscripten::val::null();
}
return toBytes(data);
return encodeImage(nullptr, self, fmt, quality);
}))
#if defined(ENABLE_GPU)
.function("_encodeToBytes", optional_override([](sk_sp<SkImage> self,
SkEncodedImageFormat fmt,
int quality,
GrDirectContext* dContext) -> Uint8Array {
sk_sp<SkData> data = self->encodeToData(dContext, fmt, quality);
if (!data) {
return emscripten::val::null();
}
return toBytes(data);
return encodeImage(dContext, self, fmt, quality);
}), allow_raw_pointers())
#endif
.function("makeCopyWithDefaultMipmaps", optional_override([](sk_sp<SkImage> self)->sk_sp<SkImage> {
Expand Down
14 changes: 13 additions & 1 deletion modules/canvaskit/compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -168,24 +168,33 @@ DO_DECODE="true"
if [[ $@ == *no_codecs* ]]; then
echo "Omitting codecs"
DO_DECODE="false"
ENCODE_PNG="false"
ENCODE_JPEG="false"
ENCODE_PNG="false"
ENCODE_WEBP="false"
NO_ENCODE_JPEG="true"
NO_ENCODE_PNG="true"
NO_ENCODE_WEBP="true"
else

ENCODE_PNG="true"
NO_ENCODE_PNG="false"
if [[ $@ == *no_encode_png* ]]; then
ENCODE_PNG="false"
NO_ENCODE_PNG="true"
fi

ENCODE_JPEG="true"
NO_ENCODE_JPEG="false"
if [[ $@ == *no_encode_jpeg* ]]; then
ENCODE_JPEG="false"
NO_ENCODE_JPEG="true"
fi

ENCODE_WEBP="true"
NO_ENCODE_WEBP="false"
if [[ $@ == *no_encode_webp* ]]; then
ENCODE_WEBP="false"
NO_ENCODE_WEBP="true"
fi

fi # no_codecs
Expand Down Expand Up @@ -213,10 +222,13 @@ echo "Compiling"
skia_use_libheif=false \
skia_use_libjpeg_turbo_decode=${DO_DECODE} \
skia_use_libjpeg_turbo_encode=${ENCODE_JPEG} \
skia_use_no_jpeg_encode=${NO_ENCODE_JPEG} \
skia_use_libpng_decode=${DO_DECODE} \
skia_use_libpng_encode=${ENCODE_PNG} \
skia_use_no_png_encode=${NO_ENCODE_PNG} \
skia_use_libwebp_decode=${DO_DECODE} \
skia_use_libwebp_encode=${ENCODE_WEBP} \
skia_use_no_webp_encode=${NO_ENCODE_WEBP} \
skia_use_lua=false \
skia_use_piex=false \
skia_use_system_freetype2=false \
Expand Down
3 changes: 2 additions & 1 deletion modules/canvaskit/debugger_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "include/core/SkPicture.h"
#include "include/core/SkString.h"
#include "include/core/SkSurface.h"
#include "include/encode/SkPngEncoder.h"
#include "include/utils/SkBase64.h"
#include "src/core/SkPicturePriv.h"
#include "src/utils/SkJSONWriter.h"
Expand Down Expand Up @@ -255,7 +256,7 @@ class SkpDebugPlayer {
// filenames like "\\1" in DrawImage commands.
// Return type is the PNG data as a base64 encoded string with prepended URI.
std::string getImageResource(int index) {
sk_sp<SkData> pngData = fImages[index]->encodeToData();
sk_sp<SkData> pngData = SkPngEncoder::Encode(nullptr, fImages[index], {});
size_t len = SkBase64::Encode(pngData->data(), pngData->size(), nullptr);
SkString dst;
dst.resize(len);
Expand Down
11 changes: 6 additions & 5 deletions site/docs/user/api/skcanvas_creation.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ canvas commands are drawn.
draw(rasterCanvas);
sk_sp<SkImage> img(rasterSurface->makeImageSnapshot());
if (!img) { return; }
sk_sp<SkData> png(img->encodeToData());
sk_sp<SkData> png = SkPngEncoder::Encode(nullptr, img, {});
if (!png) { return; }
SkFILEWStream out(path);
(void)out.write(png->data(), png->size());
Expand Down Expand Up @@ -88,9 +88,9 @@ current thread when Skia calls are made.
// You've already created your OpenGL context and bound it.
sk_sp<const GrGLInterface> interface = nullptr;
// Leaving interface as null makes Skia extract pointers to OpenGL functions for the current
// context in a platform-specific way. Alternatively, you may create your own GrGLInterface and
// initialize it however you like to attach to an alternate OpenGL implementation or intercept
// Skia's OpenGL calls.
// context in a platform-specific way. Alternatively, you may create your own GrGLInterface
// and initialize it however you like to attach to an alternate OpenGL implementation or
// intercept Skia's OpenGL calls.
sk_sp<GrDirectContext> context = GrDirectContext::MakeGL(interface);
SkImageInfo info = SkImageInfo:: MakeN32Premul(width, height);
sk_sp<SkSurface> gpuSurface(
Expand All @@ -103,7 +103,8 @@ current thread when Skia calls are made.
draw(gpuCanvas);
sk_sp<SkImage> img(gpuSurface->makeImageSnapshot());
if (!img) { return; }
sk_sp<SkData> png(img->encodeToData());
// Must pass non-null context so the pixels can be read back and encoded.
sk_sp<SkData> png = SkPngEncoder::Encode(context.get(), img, {});
if (!png) { return; }
SkFILEWStream out(path);
(void)out.write(png->data(), png->size());
Expand Down

0 comments on commit e2d3dc9

Please sign in to comment.