blob: 4aeebe47f1aefe9ce7a98cc4ae1c091efe795cbd [file] [log] [blame]
Leon Scroggins III407b5442019-11-22 17:10:20 -05001/*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "aassetstreamadaptor.h"
18
19#include <android/asset_manager.h>
20#include <android/bitmap.h>
Leon Scroggins IIIe5ace3f2020-01-15 15:31:40 -050021#include <android/data_space.h>
Leon Scroggins III407b5442019-11-22 17:10:20 -050022#include <android/imagedecoder.h>
Derek Sollenberger2173ea22020-02-19 15:37:29 -050023#include <MimeType.h>
Leon Scroggins III407b5442019-11-22 17:10:20 -050024#include <android/rect.h>
25#include <hwui/ImageDecoder.h>
26#include <log/log.h>
27#include <SkAndroidCodec.h>
Leon Scroggins IIIe5ace3f2020-01-15 15:31:40 -050028#include <utils/Color.h>
Leon Scroggins III407b5442019-11-22 17:10:20 -050029
30#include <fcntl.h>
Leon Scroggins III2e6bedf2020-02-11 16:31:21 -050031#include <limits>
Leon Scroggins III407b5442019-11-22 17:10:20 -050032#include <optional>
33#include <sys/stat.h>
34#include <sys/types.h>
35#include <unistd.h>
36
37using namespace android;
38
39int ResultToErrorCode(SkCodec::Result result) {
40 switch (result) {
41 case SkCodec::kIncompleteInput:
42 return ANDROID_IMAGE_DECODER_INCOMPLETE;
43 case SkCodec::kErrorInInput:
44 return ANDROID_IMAGE_DECODER_ERROR;
45 case SkCodec::kInvalidInput:
46 return ANDROID_IMAGE_DECODER_INVALID_INPUT;
47 case SkCodec::kCouldNotRewind:
48 return ANDROID_IMAGE_DECODER_SEEK_ERROR;
49 case SkCodec::kUnimplemented:
50 return ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT;
51 case SkCodec::kInvalidConversion:
52 return ANDROID_IMAGE_DECODER_INVALID_CONVERSION;
53 case SkCodec::kInvalidParameters:
54 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
55 case SkCodec::kSuccess:
56 return ANDROID_IMAGE_DECODER_SUCCESS;
57 case SkCodec::kInvalidScale:
58 return ANDROID_IMAGE_DECODER_INVALID_SCALE;
59 case SkCodec::kInternalError:
60 return ANDROID_IMAGE_DECODER_INTERNAL_ERROR;
61 }
62}
63
64static int createFromStream(std::unique_ptr<SkStreamRewindable> stream, AImageDecoder** outDecoder) {
65 SkCodec::Result result;
66 auto codec = SkCodec::MakeFromStream(std::move(stream), &result, nullptr,
67 SkCodec::SelectionPolicy::kPreferAnimation);
Leon Scroggins III139145b2020-12-17 15:43:54 -050068 // These may be swapped due to the SkEncodedOrigin, but we're just checking
69 // them to make sure they fit in int32_t.
70 auto dimensions = codec->dimensions();
71 auto androidCodec = SkAndroidCodec::MakeFromCodec(std::move(codec));
Leon Scroggins III407b5442019-11-22 17:10:20 -050072 if (!androidCodec) {
73 return ResultToErrorCode(result);
74 }
75
Leon Scroggins III2e6bedf2020-02-11 16:31:21 -050076 // AImageDecoderHeaderInfo_getWidth/Height return an int32_t. Ensure that
77 // the conversion is safe.
Leon Scroggins III139145b2020-12-17 15:43:54 -050078 if (dimensions.width() > std::numeric_limits<int32_t>::max() ||
79 dimensions.height() > std::numeric_limits<int32_t>::max()) {
Leon Scroggins III2e6bedf2020-02-11 16:31:21 -050080 return ANDROID_IMAGE_DECODER_INVALID_INPUT;
81 }
82
Leon Scroggins III407b5442019-11-22 17:10:20 -050083 *outDecoder = reinterpret_cast<AImageDecoder*>(new ImageDecoder(std::move(androidCodec)));
84 return ANDROID_IMAGE_DECODER_SUCCESS;
85}
86
87int AImageDecoder_createFromAAsset(AAsset* asset, AImageDecoder** outDecoder) {
88 if (!asset || !outDecoder) {
89 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
90 }
91 *outDecoder = nullptr;
92
93 auto stream = std::make_unique<AAssetStreamAdaptor>(asset);
94 return createFromStream(std::move(stream), outDecoder);
95}
96
97static bool isSeekable(int descriptor) {
98 return ::lseek64(descriptor, 0, SEEK_CUR) != -1;
99}
100
101int AImageDecoder_createFromFd(int fd, AImageDecoder** outDecoder) {
102 if (fd <= 0 || !outDecoder) {
103 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
104 }
105
106 struct stat fdStat;
107 if (fstat(fd, &fdStat) == -1) {
108 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
109 }
110
111 if (!isSeekable(fd)) {
112 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
113 }
114
115 // SkFILEStream will close its descriptor. Duplicate it so the client will
116 // still be responsible for closing the original.
117 int dupDescriptor = fcntl(fd, F_DUPFD_CLOEXEC, 0);
118 FILE* file = fdopen(dupDescriptor, "r");
119 if (!file) {
120 close(dupDescriptor);
121 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
122 }
123
124 auto stream = std::unique_ptr<SkStreamRewindable>(new SkFILEStream(file));
125 return createFromStream(std::move(stream), outDecoder);
126}
127
128int AImageDecoder_createFromBuffer(const void* buffer, size_t length,
129 AImageDecoder** outDecoder) {
130 if (!buffer || !length || !outDecoder) {
131 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
132 }
133 *outDecoder = nullptr;
134
135 // The client is expected to keep the buffer alive as long as the
136 // AImageDecoder, so we do not need to copy the buffer.
137 auto stream = std::unique_ptr<SkStreamRewindable>(
138 new SkMemoryStream(buffer, length, false /* copyData */));
139 return createFromStream(std::move(stream), outDecoder);
140}
141
142static ImageDecoder* toDecoder(AImageDecoder* d) {
143 return reinterpret_cast<ImageDecoder*>(d);
144}
145
Leon Scroggins IIIf89de632020-01-19 21:22:18 -0500146static const ImageDecoder* toDecoder(const AImageDecoder* d) {
147 return reinterpret_cast<const ImageDecoder*>(d);
148}
149
Leon Scroggins III407b5442019-11-22 17:10:20 -0500150// Note: This differs from the version in android_bitmap.cpp in that this
151// version returns kGray_8_SkColorType for ANDROID_BITMAP_FORMAT_A_8. SkCodec
152// allows decoding single channel images to gray, which Android then treats
153// as A_8/ALPHA_8.
154static SkColorType getColorType(AndroidBitmapFormat format) {
155 switch (format) {
156 case ANDROID_BITMAP_FORMAT_RGBA_8888:
157 return kN32_SkColorType;
158 case ANDROID_BITMAP_FORMAT_RGB_565:
159 return kRGB_565_SkColorType;
160 case ANDROID_BITMAP_FORMAT_RGBA_4444:
161 return kARGB_4444_SkColorType;
162 case ANDROID_BITMAP_FORMAT_A_8:
163 return kGray_8_SkColorType;
164 case ANDROID_BITMAP_FORMAT_RGBA_F16:
165 return kRGBA_F16_SkColorType;
166 default:
167 return kUnknown_SkColorType;
168 }
169}
170
171int AImageDecoder_setAndroidBitmapFormat(AImageDecoder* decoder, int32_t format) {
172 if (!decoder || format < ANDROID_BITMAP_FORMAT_NONE
173 || format > ANDROID_BITMAP_FORMAT_RGBA_F16) {
174 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
175 }
176 return toDecoder(decoder)->setOutColorType(getColorType((AndroidBitmapFormat) format))
177 ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_INVALID_CONVERSION;
178}
179
Leon Scroggins IIIe5ace3f2020-01-15 15:31:40 -0500180int AImageDecoder_setDataSpace(AImageDecoder* decoder, int32_t dataspace) {
181 sk_sp<SkColorSpace> cs = uirenderer::DataSpaceToColorSpace((android_dataspace)dataspace);
182 // 0 is ADATASPACE_UNKNOWN. We need an explicit request for an ADataSpace.
183 if (!decoder || !dataspace || !cs) {
184 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
185 }
186
187 ImageDecoder* imageDecoder = toDecoder(decoder);
188 imageDecoder->setOutColorSpace(std::move(cs));
189 return ANDROID_IMAGE_DECODER_SUCCESS;
190}
191
Leon Scroggins III407b5442019-11-22 17:10:20 -0500192const AImageDecoderHeaderInfo* AImageDecoder_getHeaderInfo(const AImageDecoder* decoder) {
193 return reinterpret_cast<const AImageDecoderHeaderInfo*>(decoder);
194}
195
196static const ImageDecoder* toDecoder(const AImageDecoderHeaderInfo* info) {
197 return reinterpret_cast<const ImageDecoder*>(info);
198}
199
200int32_t AImageDecoderHeaderInfo_getWidth(const AImageDecoderHeaderInfo* info) {
201 if (!info) {
202 return 0;
203 }
Leon Scroggins III139145b2020-12-17 15:43:54 -0500204 return toDecoder(info)->width();
Leon Scroggins III407b5442019-11-22 17:10:20 -0500205}
206
207int32_t AImageDecoderHeaderInfo_getHeight(const AImageDecoderHeaderInfo* info) {
208 if (!info) {
209 return 0;
210 }
Leon Scroggins III139145b2020-12-17 15:43:54 -0500211 return toDecoder(info)->height();
Leon Scroggins III407b5442019-11-22 17:10:20 -0500212}
213
214const char* AImageDecoderHeaderInfo_getMimeType(const AImageDecoderHeaderInfo* info) {
215 if (!info) {
216 return nullptr;
217 }
218 return getMimeType(toDecoder(info)->mCodec->getEncodedFormat());
219}
220
Leon Scroggins IIIe5ace3f2020-01-15 15:31:40 -0500221int32_t AImageDecoderHeaderInfo_getDataSpace(const AImageDecoderHeaderInfo* info) {
222 if (!info) {
223 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
224 }
225
Leon Scroggins III6eeca5c2020-01-30 13:59:50 -0500226 // Note: This recomputes the color type because it's possible the client has
227 // changed the output color type, so we cannot rely on it. Alternatively,
Leon Scroggins IIIe5ace3f2020-01-15 15:31:40 -0500228 // we could store the ADataSpace in the ImageDecoder.
229 const ImageDecoder* imageDecoder = toDecoder(info);
230 SkColorType colorType = imageDecoder->mCodec->computeOutputColorType(kN32_SkColorType);
Leon Scroggins III6eeca5c2020-01-30 13:59:50 -0500231 sk_sp<SkColorSpace> colorSpace = imageDecoder->getDefaultColorSpace();
Leon Scroggins IIIe5ace3f2020-01-15 15:31:40 -0500232 return uirenderer::ColorSpaceToADataSpace(colorSpace.get(), colorType);
233}
234
Leon Scroggins III407b5442019-11-22 17:10:20 -0500235// FIXME: Share with getFormat in android_bitmap.cpp?
236static AndroidBitmapFormat getFormat(SkColorType colorType) {
237 switch (colorType) {
238 case kN32_SkColorType:
239 return ANDROID_BITMAP_FORMAT_RGBA_8888;
240 case kRGB_565_SkColorType:
241 return ANDROID_BITMAP_FORMAT_RGB_565;
242 case kARGB_4444_SkColorType:
243 return ANDROID_BITMAP_FORMAT_RGBA_4444;
244 case kAlpha_8_SkColorType:
245 return ANDROID_BITMAP_FORMAT_A_8;
246 case kRGBA_F16_SkColorType:
247 return ANDROID_BITMAP_FORMAT_RGBA_F16;
248 default:
249 return ANDROID_BITMAP_FORMAT_NONE;
250 }
251}
252
Leon Scroggins III64301cb2020-01-23 09:47:47 -0500253int32_t AImageDecoderHeaderInfo_getAndroidBitmapFormat(const AImageDecoderHeaderInfo* info) {
Leon Scroggins III407b5442019-11-22 17:10:20 -0500254 if (!info) {
255 return ANDROID_BITMAP_FORMAT_NONE;
256 }
257 return getFormat(toDecoder(info)->mCodec->computeOutputColorType(kN32_SkColorType));
258}
259
260int AImageDecoderHeaderInfo_getAlphaFlags(const AImageDecoderHeaderInfo* info) {
261 if (!info) {
Leon Scroggins IIId8840bd2020-01-15 04:09:48 -0500262 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
Leon Scroggins III407b5442019-11-22 17:10:20 -0500263 }
264 switch (toDecoder(info)->mCodec->getInfo().alphaType()) {
265 case kUnknown_SkAlphaType:
266 LOG_ALWAYS_FATAL("Invalid alpha type");
Leon Scroggins IIId8840bd2020-01-15 04:09:48 -0500267 return ANDROID_IMAGE_DECODER_INTERNAL_ERROR;
Leon Scroggins III407b5442019-11-22 17:10:20 -0500268 case kUnpremul_SkAlphaType:
269 // fall through. premul is the default.
270 case kPremul_SkAlphaType:
271 return ANDROID_BITMAP_FLAGS_ALPHA_PREMUL;
272 case kOpaque_SkAlphaType:
273 return ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE;
274 }
275}
276
Leon Scroggins III1ade46d2020-01-15 05:41:06 -0500277int AImageDecoder_setUnpremultipliedRequired(AImageDecoder* decoder, bool required) {
278 if (!decoder) {
Leon Scroggins III407b5442019-11-22 17:10:20 -0500279 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
280 }
281
Leon Scroggins III1ade46d2020-01-15 05:41:06 -0500282 return toDecoder(decoder)->setUnpremultipliedRequired(required)
Leon Scroggins III407b5442019-11-22 17:10:20 -0500283 ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_INVALID_CONVERSION;
284}
285
Leon Scroggins III64301cb2020-01-23 09:47:47 -0500286int AImageDecoder_setTargetSize(AImageDecoder* decoder, int32_t width, int32_t height) {
Leon Scroggins III407b5442019-11-22 17:10:20 -0500287 if (!decoder) {
288 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
289 }
290
291 return toDecoder(decoder)->setTargetSize(width, height)
292 ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_INVALID_SCALE;
293}
294
Leon Scroggins IIIf89de632020-01-19 21:22:18 -0500295int AImageDecoder_computeSampledSize(const AImageDecoder* decoder, int sampleSize,
Leon Scroggins III64301cb2020-01-23 09:47:47 -0500296 int32_t* width, int32_t* height) {
Leon Scroggins IIIf89de632020-01-19 21:22:18 -0500297 if (!decoder || !width || !height || sampleSize < 1) {
298 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
299 }
300
301 SkISize size = toDecoder(decoder)->mCodec->getSampledDimensions(sampleSize);
302 *width = size.width();
303 *height = size.height();
304 return ANDROID_IMAGE_DECODER_SUCCESS;
305}
306
Leon Scroggins III407b5442019-11-22 17:10:20 -0500307int AImageDecoder_setCrop(AImageDecoder* decoder, ARect crop) {
308 if (!decoder) {
309 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
310 }
311
312 SkIRect cropIRect;
313 cropIRect.setLTRB(crop.left, crop.top, crop.right, crop.bottom);
314 SkIRect* cropPtr = cropIRect == SkIRect::MakeEmpty() ? nullptr : &cropIRect;
315 return toDecoder(decoder)->setCropRect(cropPtr)
316 ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_BAD_PARAMETER;
317}
318
319
320size_t AImageDecoder_getMinimumStride(AImageDecoder* decoder) {
321 if (!decoder) {
322 return 0;
323 }
324
325 SkImageInfo info = toDecoder(decoder)->getOutputInfo();
326 return info.minRowBytes();
327}
328
329int AImageDecoder_decodeImage(AImageDecoder* decoder,
330 void* pixels, size_t stride,
331 size_t size) {
332 if (!decoder || !pixels || !stride) {
333 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
334 }
335
336 ImageDecoder* imageDecoder = toDecoder(decoder);
337
Leon Scroggins IIId894c592020-01-22 14:18:12 -0500338 SkImageInfo info = imageDecoder->getOutputInfo();
339 size_t minSize = info.computeByteSize(stride);
340 if (SkImageInfo::ByteSizeOverflowed(minSize) || size < minSize || !info.validRowBytes(stride)) {
Leon Scroggins III407b5442019-11-22 17:10:20 -0500341 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
342 }
343
344 return ResultToErrorCode(imageDecoder->decode(pixels, stride));
345}
346
347void AImageDecoder_delete(AImageDecoder* decoder) {
348 delete toDecoder(decoder);
349}
Leon Scroggins III24ae7d72020-10-09 13:14:35 -0400350
351bool AImageDecoder_isAnimated(AImageDecoder* decoder) {
352 if (!decoder) return false;
353
354 ImageDecoder* imageDecoder = toDecoder(decoder);
355 return imageDecoder->mCodec->codec()->getFrameCount() > 1;
356}
Leon Scroggins III3ad12c42020-10-12 10:56:42 -0400357
358int32_t AImageDecoder_getRepeatCount(AImageDecoder* decoder) {
359 if (!decoder) return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
360
361 ImageDecoder* imageDecoder = toDecoder(decoder);
362 const int count = imageDecoder->mCodec->codec()->getRepetitionCount();
363
364 // Skia should not report anything out of range, but defensively treat
365 // negative and too big as INFINITE.
366 if (count == SkCodec::kRepetitionCountInfinite || count < 0
367 || count > std::numeric_limits<int32_t>::max()) {
368 return ANDROID_IMAGE_DECODER_INFINITE;
369 }
370 return count;
371}