Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkCodecImageGenerator.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2015 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 */
8
12#include "include/core/SkData.h"
18
19#include <utility>
20
21std::unique_ptr<SkImageGenerator> SkCodecImageGenerator::MakeFromEncodedCodec(
22 sk_sp<SkData> data, std::optional<SkAlphaType> at) {
23 auto codec = SkCodec::MakeFromData(data);
24 if (codec == nullptr) {
25 return nullptr;
26 }
27
28 return std::unique_ptr<SkImageGenerator>(new SkCodecImageGenerator(std::move(codec), at));
29}
30
31std::unique_ptr<SkImageGenerator> SkCodecImageGenerator::MakeFromCodec(
32 std::unique_ptr<SkCodec> codec, std::optional<SkAlphaType> at) {
33 return codec ? std::unique_ptr<SkImageGenerator>(
34 new SkCodecImageGenerator(std::move(codec), at))
35 : nullptr;
36}
37
38static SkImageInfo adjust_info(SkCodec* codec, std::optional<SkAlphaType> at) {
40 SkImageInfo info = codec->getInfo();
41 if (at.has_value()) {
42 // If a specific alpha type was requested, use that.
43 info = info.makeAlphaType(*at);
44 } else if (kUnpremul_SkAlphaType == info.alphaType()) {
45 // Otherwise, prefer premul over unpremul (this produces better filtering in general)
47 }
50 }
51 return info;
52}
53
54SkCodecImageGenerator::SkCodecImageGenerator(std::unique_ptr<SkCodec> codec,
55 std::optional<SkAlphaType> at)
56 : SkImageGenerator(adjust_info(codec.get(), at)), fCodec(std::move(codec)) {}
57
59 SkASSERT(fCodec);
60 if (!fCachedData) {
61 std::unique_ptr<SkStream> stream = fCodec->getEncodedData();
62 fCachedData = stream->getData();
63 if (!fCachedData) {
64 // stream should already be a copy of the underlying stream.
65 fCachedData = SkData::MakeFromStream(stream.get(), stream->getLength());
66 }
67 }
68 return fCachedData;
69}
70
71bool SkCodecImageGenerator::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, const SkCodec::Options* options) {
72 SkPixmap dst(info, pixels, rowBytes);
73
74 auto decode = [this, options](const SkPixmap& pm) {
75 SkCodec::Result result = fCodec->getPixels(pm, options);
76 switch (result) {
80 return true;
81 default:
82 return false;
83 }
84 };
85
86 return SkPixmapUtils::Orient(dst, fCodec->getOrigin(), decode);
87}
88
89bool SkCodecImageGenerator::onGetPixels(const SkImageInfo& requestInfo, void* requestPixels,
90 size_t requestRowBytes, const Options& options) {
91 return this->getPixels(requestInfo, requestPixels, requestRowBytes, nullptr);
92}
93
95 const SkYUVAPixmapInfo::SupportedDataTypes& supportedDataTypes,
96 SkYUVAPixmapInfo* yuvaPixmapInfo) const {
97 return fCodec->queryYUVAInfo(supportedDataTypes, yuvaPixmapInfo);
98}
99
101 switch (fCodec->getYUVAPlanes(yuvaPixmaps)) {
105 return true;
106 default:
107 return false;
108 }
109}
110
112 SkISize size = fCodec->getScaledDimensions(desiredScale);
113 if (SkEncodedOriginSwapsWidthHeight(fCodec->getOrigin())) {
114 std::swap(size.fWidth, size.fHeight);
115 }
116 return size;
117}
const char * options
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
kUnpremul_SkAlphaType
@ kOpaque_SkAlphaType
pixel is opaque
Definition SkAlphaType.h:28
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
#define SkASSERT(cond)
Definition SkAssert.h:116
static SkImageInfo adjust_info(SkCodec *codec, std::optional< SkAlphaType > at)
static bool SkEncodedOriginSwapsWidthHeight(SkEncodedOrigin origin)
SkISize getScaledDimensions(float desiredScale) const
bool onGetYUVAPlanes(const SkYUVAPixmaps &yuvaPixmaps) override
bool onQueryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes &, SkYUVAPixmapInfo *) const override
static std::unique_ptr< SkImageGenerator > MakeFromEncodedCodec(sk_sp< SkData >, std::optional< SkAlphaType >=std::nullopt)
sk_sp< SkData > onRefEncodedData() override
bool getPixels(const SkImageInfo &info, void *pixels, size_t rowBytes, const SkCodec::Options *options=nullptr)
bool onGetPixels(const SkImageInfo &info, void *pixels, size_t rowBytes, const Options &opts) override
static std::unique_ptr< SkImageGenerator > MakeFromCodec(std::unique_ptr< SkCodec >, std::optional< SkAlphaType >=std::nullopt)
static std::unique_ptr< SkCodec > MakeFromData(sk_sp< SkData >, SkSpan< const SkCodecs::Decoder > decoders, SkPngChunkReader *=nullptr)
Definition SkCodec.cpp:241
@ kIncompleteInput
Definition SkCodec.h:84
@ kSuccess
Definition SkCodec.h:80
@ kErrorInInput
Definition SkCodec.h:91
SkEncodedOrigin getOrigin() const
Definition SkCodec.h:246
SkImageInfo getInfo() const
Definition SkCodec.h:228
static sk_sp< SkData > MakeFromStream(SkStream *, size_t size)
Definition SkData.cpp:208
GAsyncResult * result
SK_API bool Orient(const SkPixmap &dst, const SkPixmap &src, SkEncodedOrigin origin)
SK_API SkImageInfo SwapWidthHeight(const SkImageInfo &info)
const myers::Point & get(const myers::Segment &)
Definition ref_ptr.h:256
static DecodeResult decode(std::string path)
SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const