Flutter Engine
The Flutter Engine
encode_platform.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "gm/gm.h"
12#include "include/core/SkData.h"
16#include "include/core/SkRect.h"
18#include "include/core/SkSize.h"
25#include "tools/DecodeUtils.h"
26#include "tools/Resources.h"
27
28namespace {
29
30static const struct {
32 int quality;
33} gRecs[] = {
34 // We don't support GIF, BMP, or ICO. This applies to both NDK and SkEncoder.
37 { SkEncodedImageFormat::kWEBP, 100}, // Lossless
38 { SkEncodedImageFormat::kWEBP, 80}, // Lossy
40};
41
42} // anonymous namespace
43
46 if (!bitmap.peekPixels(&src)) {
47 return nullptr;
48 }
50 switch (type) {
52 bool success = SkPngEncoder::Encode(&buf, src, {});
53 return success ? buf.detachAsData() : nullptr;
54 }
57 opts.fQuality = quality;
58 bool success = SkJpegEncoder::Encode(&buf, src, opts);
59 return success ? buf.detachAsData() : nullptr;
60 }
63 opts.fQuality = quality;
64 bool success = SkWebpEncoder::Encode(&buf, src, opts);
65 return success ? buf.detachAsData() : nullptr;
66 }
67 default:
69 }
70}
71
72namespace skiagm {
73
74class EncodePlatformGM : public GM {
75public:
77
78protected:
79 SkString getName() const override { return SkString("encode-platform"); }
80
81 SkISize getISize() override { return SkISize::Make(256 * std::size(gRecs), 256 * 3); }
82
83 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
84 SkBitmap opaqueBm, premulBm, unpremulBm;
85
86 if (!ToolUtils::GetResourceAsBitmap("images/mandrill_256.png", &opaqueBm)) {
87 *errorMsg = "Could not load images/mandrill_256.png.png. "
88 "Did you forget to set the resourcePath?";
89 return DrawResult::kFail;
90 }
91 SkBitmap tmp;
92 if (!ToolUtils::GetResourceAsBitmap("images/yellow_rose.png", &tmp)) {
93 *errorMsg = "Could not load images/yellow_rose.png. "
94 "Did you forget to set the resourcePath?";
95 return DrawResult::kFail;
96 }
97 tmp.extractSubset(&premulBm, SkIRect::MakeWH(256, 256));
98 tmp.reset();
100 SkAssertResult(premulBm.readPixels(unpremulBm.pixmap()));
101
102 for (const auto& rec : gRecs) {
103 auto fmt = rec.format; int q = rec.quality;
104 auto opaqueImage = SkImages::DeferredFromEncodedData(encode_data(fmt, opaqueBm, q));
105 auto premulImage = SkImages::DeferredFromEncodedData(encode_data(fmt, premulBm, q));
106 auto unpremulImage = SkImages::DeferredFromEncodedData(encode_data(fmt, unpremulBm, q));
107
108 canvas->drawImage(opaqueImage.get(), 0.0f, 0.0f);
109 canvas->drawImage(premulImage.get(), 0.0f, 256.0f);
110 canvas->drawImage(unpremulImage.get(), 0.0f, 512.0f);
111
112 canvas->translate(256.0f, 0.0f);
113 }
114 return DrawResult::kOk;
115 }
116
117private:
118 using INHERITED = GM;
119};
120
121DEF_GM( return new EncodePlatformGM; )
122} // namespace skiagm
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
kUnpremul_SkAlphaType
#define SkUNREACHABLE
Definition: SkAssert.h:135
SkEncodedImageFormat
GLenum type
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition: SkBitmap.cpp:258
bool extractSubset(SkBitmap *dst, const SkIRect &subset) const
Definition: SkBitmap.cpp:453
const SkPixmap & pixmap() const
Definition: SkBitmap.h:133
void reset()
Definition: SkBitmap.cpp:92
const SkImageInfo & info() const
Definition: SkBitmap.h:139
bool readPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY) const
Definition: SkBitmap.cpp:488
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
sk_sp< SkData > detachAsData()
Definition: SkStream.cpp:707
DrawResult onDraw(SkCanvas *canvas, SkString *errorMsg) override
SkString getName() const override
SkISize getISize() override
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
static sk_sp< SkData > encode_data(SkEncodedImageFormat type, const SkBitmap &bitmap, int quality)
uint32_t uint32_t * format
SK_API sk_sp< SkImage > DeferredFromEncodedData(sk_sp< SkData > encoded, std::optional< SkAlphaType > alphaType=std::nullopt)
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
bool GetResourceAsBitmap(const char *resource, SkBitmap *dst)
Definition: DecodeUtils.h:21
Definition: bitmap.py:1
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
static sk_sp< SkData > encode_data(const SkBitmap &bitmap, SkEncodedImageFormat format)
Definition: encode_srgb.cpp:65
DrawResult
Definition: gm.h:104
static SkString fmt(SkColor4f c)
Definition: p3.cpp:43
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition: SkRect.h:56
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const
Definition: SkImageInfo.h:466