Flutter Engine
The Flutter Engine
Classes | Enumerations | Functions
SkWebpEncoder Namespace Reference

Classes

struct  Options
 

Enumerations

enum class  Compression { kLossy , kLossless }
 

Functions

SK_API bool Encode (SkWStream *dst, const SkPixmap &src, const Options &options)
 
SK_API sk_sp< SkDataEncode (GrDirectContext *ctx, const SkImage *img, const Options &options)
 
SK_API bool EncodeAnimated (SkWStream *dst, SkSpan< const SkEncoder::Frame > src, const Options &options)
 

Enumeration Type Documentation

◆ Compression

enum class SkWebpEncoder::Compression
strong
Enumerator
kLossy 
kLossless 

Definition at line 25 of file SkWebpEncoder.h.

25 {
26 kLossy,
27 kLossless,
28};

Function Documentation

◆ Encode() [1/2]

sk_sp< SkData > SkWebpEncoder::Encode ( GrDirectContext ctx,
const SkImage img,
const Options options 
)

Encode the provided image and return the resulting bytes. If the image was created as a texture-backed image on a GPU context, that |ctx| must be provided so the pixels can be read before being encoded. For raster-backed images, |ctx| can be nullptr. |options| may be used to control the encoding behavior.

Returns nullptr if the pixels could not be read or encoding otherwise fails.

Definition at line 25 of file SkWebpEncoder_none.cpp.

25 {
26 SkDEBUGFAIL("Using encoder stub");
27 return nullptr;
28}
#define SkDEBUGFAIL(message)
Definition: SkAssert.h:118

◆ Encode() [2/2]

bool SkWebpEncoder::Encode ( SkWStream dst,
const SkPixmap src,
const Options options 
)

Encode the |src| pixels to the |dst| stream. |options| may be used to control the encoding behavior.

Returns true on success. Returns false on an invalid or unsupported |src|.

Definition at line 20 of file SkWebpEncoder_none.cpp.

20 {
21 SkDEBUGFAIL("Using encoder stub");
22 return false;
23}

◆ EncodeAnimated()

bool SkWebpEncoder::EncodeAnimated ( SkWStream dst,
SkSpan< const SkEncoder::Frame src,
const Options options 
)

Encode the |src| frames to the |dst| stream. |options| may be used to control the encoding behavior.

The size of the first frame will be used as the canvas size. If any other frame does not match the canvas size, this is an error.

Returns true on success. Returns false on an invalid or unsupported |src|.

Note: libwebp API also supports set background color, loop limit and customize lossy/lossless for each frame. These could be added later as needed.

Definition at line 191 of file SkWebpEncoderImpl.cpp.

191 {
192 if (!stream || frames.empty()) {
193 return false;
194 }
195
196 const int canvasWidth = frames.front().pixmap.width();
197 const int canvasHeight = frames.front().pixmap.height();
198 int timestamp = 0;
199
200 std::unique_ptr<WebPAnimEncoder, void (*)(WebPAnimEncoder*)> enc(
201 WebPAnimEncoderNew(canvasWidth, canvasHeight, nullptr), WebPAnimEncoderDelete);
202 if (!enc) {
203 return false;
204 }
205
206 for (const auto& frame : frames) {
207 const auto& pixmap = frame.pixmap;
208
209 if (pixmap.width() != canvasWidth || pixmap.height() != canvasHeight) {
210 return false;
211 }
212
213 WebPConfig webp_config;
214 if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, opts.fQuality)) {
215 return false;
216 }
217
218 WebPPicture pic;
219 if (!WebPPictureInit(&pic)) {
220 return false;
221 }
223
224 if (!preprocess_webp_picture(&pic, &webp_config, pixmap, opts)) {
225 return false;
226 }
227
228 if (!WebPEncode(&webp_config, &pic)) {
229 return false;
230 }
231
232 if (!WebPAnimEncoderAdd(enc.get(), &pic, timestamp, &webp_config)) {
233 return false;
234 }
235
236 timestamp += frame.duration;
237 }
238
239 // Add a last fake frame to signal the last duration.
240 if (!WebPAnimEncoderAdd(enc.get(), nullptr, timestamp, nullptr)) {
241 return false;
242 }
243
244 WebPData assembled;
245 SkAutoTCallVProc<WebPData, WebPDataClear> autoWebPData(&assembled);
246 if (!WebPAnimEncoderAssemble(enc.get(), &assembled)) {
247 return false;
248 }
249
250 enc.reset();
251
252 return stream->write(assembled.bytes, assembled.size);
253}
static bool preprocess_webp_picture(WebPPicture *pic, WebPConfig *webp_config, const SkPixmap &pixmap, const SkWebpEncoder::Options &opts)
double frame
Definition: examples.cpp:31