Debian Codegraph @

src/codec/SkCodecImageGenerator.cpp

raw
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 */
7 #include "src/codec/SkCodecImageGenerator.h"
8
9 #include "include/codec/SkEncodedOrigin.h"
10 #include "include/codec/SkPixmapUtils.h"
11 #include "include/core/SkAlphaType.h"
12 #include "include/core/SkData.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkPixmap.h"
15 #include "include/core/SkStream.h"
16 #include "include/core/SkTypes.h"
17 #include "src/codec/SkPixmapUtilsPriv.h"
18 #include "src/core/SkStreamPriv.h"
19
20 #include <utility>
21
22 std::unique_ptr<SkImageGenerator> SkCodecImageGenerator::MakeFromEncodedCodec(
23 sk_sp<const SkData> data, std::optional<SkAlphaType> at) {
24 auto codec = SkCodec::MakeFromData(data);
25 if (codec == nullptr) {
26 return nullptr;
27 }
28
29 return std::unique_ptr<SkImageGenerator>(new SkCodecImageGenerator(std::move(codec), at));
30 }
31
32 std::unique_ptr<SkImageGenerator> SkCodecImageGenerator::MakeFromCodec(
33 std::unique_ptr<SkCodec> codec, std::optional<SkAlphaType> at) {
34 return codec ? std::unique_ptr<SkImageGenerator>(
35 new SkCodecImageGenerator(std::move(codec), at))
36 : nullptr;
37 }
38
39 static SkImageInfo adjust_info(SkCodec* codec, std::optional<SkAlphaType> at) {
40 SkASSERT(at != kOpaque_SkAlphaType);
41 SkImageInfo info = codec->getInfo();
42 if (at.has_value()) {
43 // If a specific alpha type was requested, use that.
44 info = info.makeAlphaType(*at);
45 } else if (kUnpremul_SkAlphaType == info.alphaType()) {
46 // Otherwise, prefer premul over unpremul (this produces better filtering in general)
47 info = info.makeAlphaType(kPremul_SkAlphaType);
48 }
49 if (SkEncodedOriginSwapsWidthHeight(codec->getOrigin())) {
50 info = SkPixmapUtils::SwapWidthHeight(info);
51 }
52 return info;
53 }
54
55 SkCodecImageGenerator::SkCodecImageGenerator(std::unique_ptr<SkCodec> codec,
56 std::optional<SkAlphaType> at)
57 : SkImageGenerator(adjust_info(codec.get(), at)), fCodec(std::move(codec)) {}
58
59 sk_sp<const SkData> SkCodecImageGenerator::onRefEncodedData() {
60 SkASSERT(fCodec);
61 if (!fCachedData) {
62 std::unique_ptr<SkStream> stream = fCodec->getEncodedData();
63 fCachedData = stream->getData();
64 if (!fCachedData) {
65 // stream should already be a copy of the underlying stream.
66 fCachedData = SkData::MakeFromStream(stream.get(), stream->getLength());
67 }
68 }
69 return fCachedData;
70 }
71
72 bool SkCodecImageGenerator::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, const SkCodec::Options* options) {
73 SkPixmap dst(info, pixels, rowBytes);
74
75 auto decode = [this, options](const SkPixmap& pm) {
76 SkCodec::Result result = fCodec->getPixels(pm, options);
77 switch (result) {
78 case SkCodec::kSuccess:
79 case SkCodec::kIncompleteInput:
80 case SkCodec::kErrorInInput:
81 return true;
82 default:
83 return false;
84 }
85 };
86
87 return SkPixmapUtils::Orient(dst, fCodec->getOrigin(), decode);
88 }
89
90 bool SkCodecImageGenerator::onGetPixels(const SkImageInfo& requestInfo, void* requestPixels,
91 size_t requestRowBytes, const Options& options) {
92 return this->getPixels(requestInfo, requestPixels, requestRowBytes, nullptr);
93 }
94
95 bool SkCodecImageGenerator::onQueryYUVAInfo(
96 const SkYUVAPixmapInfo::SupportedDataTypes& supportedDataTypes,
97 SkYUVAPixmapInfo* yuvaPixmapInfo) const {
98 return fCodec->queryYUVAInfo(supportedDataTypes, yuvaPixmapInfo);
99 }
100
101 bool SkCodecImageGenerator::onGetYUVAPlanes(const SkYUVAPixmaps& yuvaPixmaps) {
102 switch (fCodec->getYUVAPlanes(yuvaPixmaps)) {
103 case SkCodec::kSuccess:
104 case SkCodec::kIncompleteInput:
105 case SkCodec::kErrorInInput:
106 return true;
107 default:
108 return false;
109 }
110 }
111
112 SkISize SkCodecImageGenerator::getScaledDimensions(float desiredScale) const {
113 SkISize size = fCodec->getScaledDimensions(desiredScale);
114 if (SkEncodedOriginSwapsWidthHeight(fCodec->getOrigin())) {
115 std::swap(size.fWidth, size.fHeight);
116 }
117 return size;
118 }