Flutter Engine
The Flutter Engine
BitmapRegionDecoder.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 */
7
13
14namespace android {
15namespace skia {
16
17std::unique_ptr<BitmapRegionDecoder> BitmapRegionDecoder::Make(sk_sp<SkData> data) {
18 auto codec = SkAndroidCodec::MakeFromData(std::move(data));
19 if (nullptr == codec) {
20 SkCodecPrintf("Error: Failed to create codec.\n");
21 return nullptr;
22 }
23
24 switch (codec->getEncodedFormat()) {
29 break;
30 default:
31 return nullptr;
32 }
33
34 return std::unique_ptr<BitmapRegionDecoder>(new BitmapRegionDecoder(std::move(codec)));
35}
36
37BitmapRegionDecoder::BitmapRegionDecoder(std::unique_ptr<SkAndroidCodec> codec)
38 : fCodec(std::move(codec))
39{}
40
42 return fCodec->getInfo().width();
43}
44
46 return fCodec->getInfo().height();
47}
48
50 const SkIRect& desiredSubset, int sampleSize, SkColorType dstColorType,
51 bool requireUnpremul, sk_sp<SkColorSpace> dstColorSpace) {
52
53 // Fix the input sampleSize if necessary.
54 if (sampleSize < 1) {
55 sampleSize = 1;
56 }
57
58 // The size of the output bitmap is determined by the size of the
59 // requested subset, not by the size of the intersection of the subset
60 // and the image dimensions.
61 // If inputX is negative, we will need to place decoded pixels into the
62 // output bitmap starting at a left offset. Call this outX.
63 // If outX is non-zero, subsetX must be zero.
64 // If inputY is negative, we will need to place decoded pixels into the
65 // output bitmap starting at a top offset. Call this outY.
66 // If outY is non-zero, subsetY must be zero.
67 int outX;
68 int outY;
69 SkIRect subset = desiredSubset;
70 SubsetType type = adjust_subset_rect(fCodec->getInfo().dimensions(), &subset, &outX, &outY);
72 return false;
73 }
74
75 // Ask the codec for a scaled subset
76 if (!fCodec->getSupportedSubset(&subset)) {
77 SkCodecPrintf("Error: Could not get subset.\n");
78 return false;
79 }
80 SkISize scaledSize = fCodec->getSampledSubsetDimensions(sampleSize, subset);
81
82 // Create the image info for the decode
83 SkAlphaType dstAlphaType = fCodec->computeOutputAlphaType(requireUnpremul);
84 SkImageInfo decodeInfo =
85 SkImageInfo::Make(scaledSize, dstColorType, dstAlphaType, std::move(dstColorSpace));
86
87 // Initialize the destination bitmap
88 int scaledOutX = 0;
89 int scaledOutY = 0;
90 int scaledOutWidth = scaledSize.width();
91 int scaledOutHeight = scaledSize.height();
93 scaledOutX = outX / sampleSize;
94 scaledOutY = outY / sampleSize;
95 // We need to be safe here because getSupportedSubset() may have modified the subset.
96 const int extraX = std::max(0, desiredSubset.width() - outX - subset.width());
97 const int extraY = std::max(0, desiredSubset.height() - outY - subset.height());
98 const int scaledExtraX = extraX / sampleSize;
99 const int scaledExtraY = extraY / sampleSize;
100 scaledOutWidth += scaledOutX + scaledExtraX;
101 scaledOutHeight += scaledOutY + scaledExtraY;
102 }
103 SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight);
104 if (kGray_8_SkColorType == dstColorType) {
105 // The legacy implementations of BitmapFactory and BitmapRegionDecoder
106 // used kAlpha8 for grayscale images (before kGray8 existed). While
107 // the codec recognizes kGray8, we need to decode into a kAlpha8
108 // bitmap in order to avoid a behavior change.
110 }
111 bitmap->setInfo(outInfo);
112 if (!bitmap->tryAllocPixels(allocator)) {
113 SkCodecPrintf("Error: Could not allocate pixels.\n");
114 return false;
115 }
116
117 // Zero the bitmap if the region is not completely within the image.
118 // TODO (msarett): Can we make this faster by implementing it to only
119 // zero parts of the image that we won't overwrite with
120 // pixels?
121 SkCodec::ZeroInitialized zeroInit = allocator ? allocator->zeroInit() :
124 SkCodec::kNo_ZeroInitialized == zeroInit) {
125 void* pixels = bitmap->getPixels();
126 size_t bytes = outInfo.computeByteSize(bitmap->rowBytes());
127 memset(pixels, 0, bytes);
128 }
129
130 // Decode into the destination bitmap
132 options.fSampleSize = sampleSize;
133 options.fSubset = &subset;
134 options.fZeroInitialized = zeroInit;
135 void* dst = bitmap->getAddr(scaledOutX, scaledOutY);
136
137 SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, bitmap->rowBytes(),
138 &options);
139 switch (result) {
143 return true;
144 default:
145 SkCodecPrintf("Error: Could not get pixels with message \"%s\".\n",
147 return false;
148 }
149}
150
151} // namespace skia
152} // namespace android
@ kOutside_SubsetType
@ kPartiallyInside_SubsetType
SubsetType adjust_subset_rect(const SkISize &imageDims, SkIRect *subset, int *outX, int *outY)
const char * options
SkAlphaType
Definition: SkAlphaType.h:26
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
#define SkCodecPrintf(...)
Definition: SkCodecPriv.h:23
SkColorType
Definition: SkColorType.h:19
@ kAlpha_8_SkColorType
pixel with alpha in 8-bit byte
Definition: SkColorType.h:21
@ kGray_8_SkColorType
pixel with grayscale level in 8-bit byte
Definition: SkColorType.h:35
GLenum type
static std::unique_ptr< SkAndroidCodec > MakeFromData(sk_sp< SkData >, SkPngChunkReader *=nullptr)
ZeroInitialized
Definition: SkCodec.h:303
@ kNo_ZeroInitialized
Definition: SkCodec.h:315
static const char * ResultToString(Result)
Definition: SkCodec.cpp:880
Result
Definition: SkCodec.h:76
@ kIncompleteInput
Definition: SkCodec.h:84
@ kSuccess
Definition: SkCodec.h:80
@ kErrorInInput
Definition: SkCodec.h:91
virtual SkCodec::ZeroInitialized zeroInit() const =0
bool decodeRegion(SkBitmap *bitmap, BRDAllocator *allocator, const SkIRect &desiredSubset, int sampleSize, SkColorType colorType, bool requireUnpremul, sk_sp< SkColorSpace > prefColorSpace)
static std::unique_ptr< BitmapRegionDecoder > Make(sk_sp< SkData > data)
GAsyncResult * result
static float max(float r, float g, float b)
Definition: hsl.cpp:49
Definition: bitmap.py:1
dst
Definition: cp.py:12
Definition: DartTypes.h:13
Definition: ref_ptr.h:256
Definition: SkRect.h:32
constexpr int32_t height() const
Definition: SkRect.h:165
constexpr int32_t width() const
Definition: SkRect.h:158
Definition: SkSize.h:16
constexpr int32_t width() const
Definition: SkSize.h:36
constexpr int32_t height() const
Definition: SkSize.h:37
SkImageInfo makeWH(int newWidth, int newHeight) const
Definition: SkImageInfo.h:444
SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const
Definition: SkImageInfo.h:466
size_t computeByteSize(size_t rowBytes) const
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
SkImageInfo makeColorType(SkColorType newColorType) const
Definition: SkImageInfo.h:475
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63