blob: bb25274e3136c6ede1ea241febb79dd44b8faae0 [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
Leon Scroggins III946f8d42020-12-02 14:05:44 -050064const char* AImageDecoder_resultToString(int result) {
65 switch (result) {
66 case ANDROID_IMAGE_DECODER_SUCCESS:
67 return "ANDROID_IMAGE_DECODER_SUCCESS";
68 case ANDROID_IMAGE_DECODER_INCOMPLETE:
69 return "ANDROID_IMAGE_DECODER_INCOMPLETE";
70 case ANDROID_IMAGE_DECODER_ERROR:
71 return "ANDROID_IMAGE_DECODER_ERROR";
72 case ANDROID_IMAGE_DECODER_INVALID_CONVERSION:
73 return "ANDROID_IMAGE_DECODER_INVALID_CONVERSION";
74 case ANDROID_IMAGE_DECODER_INVALID_SCALE:
75 return "ANDROID_IMAGE_DECODER_INVALID_SCALE";
76 case ANDROID_IMAGE_DECODER_BAD_PARAMETER:
77 return "ANDROID_IMAGE_DECODER_BAD_PARAMETER";
78 case ANDROID_IMAGE_DECODER_INVALID_INPUT:
79 return "ANDROID_IMAGE_DECODER_INVALID_INPUT";
80 case ANDROID_IMAGE_DECODER_SEEK_ERROR:
81 return "ANDROID_IMAGE_DECODER_SEEK_ERROR";
82 case ANDROID_IMAGE_DECODER_INTERNAL_ERROR:
83 return "ANDROID_IMAGE_DECODER_INTERNAL_ERROR";
84 case ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT:
85 return "ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT";
86 case ANDROID_IMAGE_DECODER_FINISHED:
87 return "ANDROID_IMAGE_DECODER_FINISHED";
88 case ANDROID_IMAGE_DECODER_INVALID_STATE:
89 return "ANDROID_IMAGE_DECODER_INVALID_STATE";
90 default:
91 return nullptr;
92 }
93}
94
Leon Scroggins III407b5442019-11-22 17:10:20 -050095static int createFromStream(std::unique_ptr<SkStreamRewindable> stream, AImageDecoder** outDecoder) {
96 SkCodec::Result result;
97 auto codec = SkCodec::MakeFromStream(std::move(stream), &result, nullptr,
98 SkCodec::SelectionPolicy::kPreferAnimation);
Leon Scroggins III139145b2020-12-17 15:43:54 -050099 // These may be swapped due to the SkEncodedOrigin, but we're just checking
100 // them to make sure they fit in int32_t.
101 auto dimensions = codec->dimensions();
102 auto androidCodec = SkAndroidCodec::MakeFromCodec(std::move(codec));
Leon Scroggins III407b5442019-11-22 17:10:20 -0500103 if (!androidCodec) {
104 return ResultToErrorCode(result);
105 }
106
Leon Scroggins III2e6bedf2020-02-11 16:31:21 -0500107 // AImageDecoderHeaderInfo_getWidth/Height return an int32_t. Ensure that
108 // the conversion is safe.
Leon Scroggins III139145b2020-12-17 15:43:54 -0500109 if (dimensions.width() > std::numeric_limits<int32_t>::max() ||
110 dimensions.height() > std::numeric_limits<int32_t>::max()) {
Leon Scroggins III2e6bedf2020-02-11 16:31:21 -0500111 return ANDROID_IMAGE_DECODER_INVALID_INPUT;
112 }
113
Leon Scroggins III407b5442019-11-22 17:10:20 -0500114 *outDecoder = reinterpret_cast<AImageDecoder*>(new ImageDecoder(std::move(androidCodec)));
115 return ANDROID_IMAGE_DECODER_SUCCESS;
116}
117
118int AImageDecoder_createFromAAsset(AAsset* asset, AImageDecoder** outDecoder) {
119 if (!asset || !outDecoder) {
120 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
121 }
122 *outDecoder = nullptr;
123
Leon Scroggins IIIc72d0fb2020-12-30 16:38:23 -0500124#ifdef __ANDROID__
Leon Scroggins III407b5442019-11-22 17:10:20 -0500125 auto stream = std::make_unique<AAssetStreamAdaptor>(asset);
126 return createFromStream(std::move(stream), outDecoder);
Leon Scroggins IIIc72d0fb2020-12-30 16:38:23 -0500127#else
128 return ANDROID_IMAGE_DECODER_INTERNAL_ERROR;
129#endif
Leon Scroggins III407b5442019-11-22 17:10:20 -0500130}
131
132static bool isSeekable(int descriptor) {
133 return ::lseek64(descriptor, 0, SEEK_CUR) != -1;
134}
135
136int AImageDecoder_createFromFd(int fd, AImageDecoder** outDecoder) {
137 if (fd <= 0 || !outDecoder) {
138 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
139 }
140
141 struct stat fdStat;
142 if (fstat(fd, &fdStat) == -1) {
143 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
144 }
145
146 if (!isSeekable(fd)) {
147 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
148 }
149
150 // SkFILEStream will close its descriptor. Duplicate it so the client will
151 // still be responsible for closing the original.
152 int dupDescriptor = fcntl(fd, F_DUPFD_CLOEXEC, 0);
153 FILE* file = fdopen(dupDescriptor, "r");
154 if (!file) {
155 close(dupDescriptor);
156 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
157 }
158
159 auto stream = std::unique_ptr<SkStreamRewindable>(new SkFILEStream(file));
160 return createFromStream(std::move(stream), outDecoder);
161}
162
163int AImageDecoder_createFromBuffer(const void* buffer, size_t length,
164 AImageDecoder** outDecoder) {
165 if (!buffer || !length || !outDecoder) {
166 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
167 }
168 *outDecoder = nullptr;
169
170 // The client is expected to keep the buffer alive as long as the
171 // AImageDecoder, so we do not need to copy the buffer.
172 auto stream = std::unique_ptr<SkStreamRewindable>(
173 new SkMemoryStream(buffer, length, false /* copyData */));
174 return createFromStream(std::move(stream), outDecoder);
175}
176
177static ImageDecoder* toDecoder(AImageDecoder* d) {
178 return reinterpret_cast<ImageDecoder*>(d);
179}
180
Leon Scroggins IIIf89de632020-01-19 21:22:18 -0500181static const ImageDecoder* toDecoder(const AImageDecoder* d) {
182 return reinterpret_cast<const ImageDecoder*>(d);
183}
184
Leon Scroggins III407b5442019-11-22 17:10:20 -0500185// Note: This differs from the version in android_bitmap.cpp in that this
186// version returns kGray_8_SkColorType for ANDROID_BITMAP_FORMAT_A_8. SkCodec
187// allows decoding single channel images to gray, which Android then treats
188// as A_8/ALPHA_8.
189static SkColorType getColorType(AndroidBitmapFormat format) {
190 switch (format) {
191 case ANDROID_BITMAP_FORMAT_RGBA_8888:
192 return kN32_SkColorType;
193 case ANDROID_BITMAP_FORMAT_RGB_565:
194 return kRGB_565_SkColorType;
195 case ANDROID_BITMAP_FORMAT_RGBA_4444:
196 return kARGB_4444_SkColorType;
197 case ANDROID_BITMAP_FORMAT_A_8:
198 return kGray_8_SkColorType;
199 case ANDROID_BITMAP_FORMAT_RGBA_F16:
200 return kRGBA_F16_SkColorType;
Alec Mouri1efd0a52022-01-20 13:58:23 -0800201 case ANDROID_BITMAP_FORMAT_RGBA_1010102:
202 return kRGBA_1010102_SkColorType;
Leon Scroggins III407b5442019-11-22 17:10:20 -0500203 default:
204 return kUnknown_SkColorType;
205 }
206}
207
208int AImageDecoder_setAndroidBitmapFormat(AImageDecoder* decoder, int32_t format) {
Alec Mouri1efd0a52022-01-20 13:58:23 -0800209 if (!decoder || format < ANDROID_BITMAP_FORMAT_NONE ||
210 format > ANDROID_BITMAP_FORMAT_RGBA_1010102) {
Leon Scroggins III407b5442019-11-22 17:10:20 -0500211 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
212 }
Leon Scroggins IIIb26aebc2020-12-21 14:55:22 -0500213
214 auto* imageDecoder = toDecoder(decoder);
215 if (imageDecoder->currentFrame() != 0) {
216 return ANDROID_IMAGE_DECODER_INVALID_STATE;
217 }
218
219 return imageDecoder->setOutColorType(getColorType((AndroidBitmapFormat) format))
Leon Scroggins III407b5442019-11-22 17:10:20 -0500220 ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_INVALID_CONVERSION;
221}
222
Leon Scroggins IIIe5ace3f2020-01-15 15:31:40 -0500223int AImageDecoder_setDataSpace(AImageDecoder* decoder, int32_t dataspace) {
224 sk_sp<SkColorSpace> cs = uirenderer::DataSpaceToColorSpace((android_dataspace)dataspace);
225 // 0 is ADATASPACE_UNKNOWN. We need an explicit request for an ADataSpace.
226 if (!decoder || !dataspace || !cs) {
227 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
228 }
229
230 ImageDecoder* imageDecoder = toDecoder(decoder);
Leon Scroggins IIIb26aebc2020-12-21 14:55:22 -0500231 if (imageDecoder->currentFrame() != 0) {
232 return ANDROID_IMAGE_DECODER_INVALID_STATE;
233 }
234
Leon Scroggins IIIe5ace3f2020-01-15 15:31:40 -0500235 imageDecoder->setOutColorSpace(std::move(cs));
236 return ANDROID_IMAGE_DECODER_SUCCESS;
237}
238
Leon Scroggins III407b5442019-11-22 17:10:20 -0500239const AImageDecoderHeaderInfo* AImageDecoder_getHeaderInfo(const AImageDecoder* decoder) {
240 return reinterpret_cast<const AImageDecoderHeaderInfo*>(decoder);
241}
242
243static const ImageDecoder* toDecoder(const AImageDecoderHeaderInfo* info) {
244 return reinterpret_cast<const ImageDecoder*>(info);
245}
246
247int32_t AImageDecoderHeaderInfo_getWidth(const AImageDecoderHeaderInfo* info) {
248 if (!info) {
249 return 0;
250 }
Leon Scroggins III139145b2020-12-17 15:43:54 -0500251 return toDecoder(info)->width();
Leon Scroggins III407b5442019-11-22 17:10:20 -0500252}
253
254int32_t AImageDecoderHeaderInfo_getHeight(const AImageDecoderHeaderInfo* info) {
255 if (!info) {
256 return 0;
257 }
Leon Scroggins III139145b2020-12-17 15:43:54 -0500258 return toDecoder(info)->height();
Leon Scroggins III407b5442019-11-22 17:10:20 -0500259}
260
261const char* AImageDecoderHeaderInfo_getMimeType(const AImageDecoderHeaderInfo* info) {
262 if (!info) {
263 return nullptr;
264 }
265 return getMimeType(toDecoder(info)->mCodec->getEncodedFormat());
266}
267
Leon Scroggins IIIe5ace3f2020-01-15 15:31:40 -0500268int32_t AImageDecoderHeaderInfo_getDataSpace(const AImageDecoderHeaderInfo* info) {
269 if (!info) {
270 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
271 }
272
Leon Scroggins III6eeca5c2020-01-30 13:59:50 -0500273 // Note: This recomputes the color type because it's possible the client has
274 // changed the output color type, so we cannot rely on it. Alternatively,
Leon Scroggins IIIe5ace3f2020-01-15 15:31:40 -0500275 // we could store the ADataSpace in the ImageDecoder.
276 const ImageDecoder* imageDecoder = toDecoder(info);
277 SkColorType colorType = imageDecoder->mCodec->computeOutputColorType(kN32_SkColorType);
Leon Scroggins III6eeca5c2020-01-30 13:59:50 -0500278 sk_sp<SkColorSpace> colorSpace = imageDecoder->getDefaultColorSpace();
Leon Scroggins IIIe5ace3f2020-01-15 15:31:40 -0500279 return uirenderer::ColorSpaceToADataSpace(colorSpace.get(), colorType);
280}
281
Leon Scroggins III407b5442019-11-22 17:10:20 -0500282// FIXME: Share with getFormat in android_bitmap.cpp?
283static AndroidBitmapFormat getFormat(SkColorType colorType) {
284 switch (colorType) {
285 case kN32_SkColorType:
286 return ANDROID_BITMAP_FORMAT_RGBA_8888;
287 case kRGB_565_SkColorType:
288 return ANDROID_BITMAP_FORMAT_RGB_565;
289 case kARGB_4444_SkColorType:
290 return ANDROID_BITMAP_FORMAT_RGBA_4444;
291 case kAlpha_8_SkColorType:
292 return ANDROID_BITMAP_FORMAT_A_8;
293 case kRGBA_F16_SkColorType:
294 return ANDROID_BITMAP_FORMAT_RGBA_F16;
Alec Mouri1efd0a52022-01-20 13:58:23 -0800295 case kRGBA_1010102_SkColorType:
296 return ANDROID_BITMAP_FORMAT_RGBA_1010102;
Leon Scroggins III407b5442019-11-22 17:10:20 -0500297 default:
298 return ANDROID_BITMAP_FORMAT_NONE;
299 }
300}
301
Leon Scroggins III64301cb2020-01-23 09:47:47 -0500302int32_t AImageDecoderHeaderInfo_getAndroidBitmapFormat(const AImageDecoderHeaderInfo* info) {
Leon Scroggins III407b5442019-11-22 17:10:20 -0500303 if (!info) {
304 return ANDROID_BITMAP_FORMAT_NONE;
305 }
306 return getFormat(toDecoder(info)->mCodec->computeOutputColorType(kN32_SkColorType));
307}
308
309int AImageDecoderHeaderInfo_getAlphaFlags(const AImageDecoderHeaderInfo* info) {
310 if (!info) {
Leon Scroggins IIId8840bd2020-01-15 04:09:48 -0500311 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
Leon Scroggins III407b5442019-11-22 17:10:20 -0500312 }
313 switch (toDecoder(info)->mCodec->getInfo().alphaType()) {
314 case kUnknown_SkAlphaType:
315 LOG_ALWAYS_FATAL("Invalid alpha type");
Leon Scroggins IIId8840bd2020-01-15 04:09:48 -0500316 return ANDROID_IMAGE_DECODER_INTERNAL_ERROR;
Leon Scroggins III407b5442019-11-22 17:10:20 -0500317 case kUnpremul_SkAlphaType:
318 // fall through. premul is the default.
319 case kPremul_SkAlphaType:
320 return ANDROID_BITMAP_FLAGS_ALPHA_PREMUL;
321 case kOpaque_SkAlphaType:
322 return ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE;
323 }
324}
325
Leon Scroggins III1ade46d2020-01-15 05:41:06 -0500326int AImageDecoder_setUnpremultipliedRequired(AImageDecoder* decoder, bool required) {
327 if (!decoder) {
Leon Scroggins III407b5442019-11-22 17:10:20 -0500328 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
329 }
330
Leon Scroggins IIIb26aebc2020-12-21 14:55:22 -0500331 auto* imageDecoder = toDecoder(decoder);
332 if (imageDecoder->currentFrame() != 0) {
333 return ANDROID_IMAGE_DECODER_INVALID_STATE;
334 }
335
336 return imageDecoder->setUnpremultipliedRequired(required)
Leon Scroggins III407b5442019-11-22 17:10:20 -0500337 ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_INVALID_CONVERSION;
338}
339
Leon Scroggins III64301cb2020-01-23 09:47:47 -0500340int AImageDecoder_setTargetSize(AImageDecoder* decoder, int32_t width, int32_t height) {
Leon Scroggins III407b5442019-11-22 17:10:20 -0500341 if (!decoder) {
342 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
343 }
344
Leon Scroggins IIIb26aebc2020-12-21 14:55:22 -0500345 auto* imageDecoder = toDecoder(decoder);
346 if (imageDecoder->currentFrame() != 0) {
347 return ANDROID_IMAGE_DECODER_INVALID_STATE;
348 }
349
350 return imageDecoder->setTargetSize(width, height)
Leon Scroggins III407b5442019-11-22 17:10:20 -0500351 ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_INVALID_SCALE;
352}
353
Leon Scroggins IIIf89de632020-01-19 21:22:18 -0500354int AImageDecoder_computeSampledSize(const AImageDecoder* decoder, int sampleSize,
Leon Scroggins III64301cb2020-01-23 09:47:47 -0500355 int32_t* width, int32_t* height) {
Leon Scroggins IIIf89de632020-01-19 21:22:18 -0500356 if (!decoder || !width || !height || sampleSize < 1) {
357 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
358 }
359
Leon Scroggins III5a5c2ce2021-01-15 14:09:13 -0500360 SkISize size = toDecoder(decoder)->getSampledDimensions(sampleSize);
Leon Scroggins IIIf89de632020-01-19 21:22:18 -0500361 *width = size.width();
362 *height = size.height();
363 return ANDROID_IMAGE_DECODER_SUCCESS;
364}
365
Leon Scroggins III407b5442019-11-22 17:10:20 -0500366int AImageDecoder_setCrop(AImageDecoder* decoder, ARect crop) {
367 if (!decoder) {
368 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
369 }
370
Leon Scroggins IIIb26aebc2020-12-21 14:55:22 -0500371 auto* imageDecoder = toDecoder(decoder);
372 if (imageDecoder->currentFrame() != 0) {
373 return ANDROID_IMAGE_DECODER_INVALID_STATE;
374 }
375
Leon Scroggins III407b5442019-11-22 17:10:20 -0500376 SkIRect cropIRect;
377 cropIRect.setLTRB(crop.left, crop.top, crop.right, crop.bottom);
378 SkIRect* cropPtr = cropIRect == SkIRect::MakeEmpty() ? nullptr : &cropIRect;
Leon Scroggins IIIb26aebc2020-12-21 14:55:22 -0500379 return imageDecoder->setCropRect(cropPtr)
Leon Scroggins III407b5442019-11-22 17:10:20 -0500380 ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_BAD_PARAMETER;
381}
382
383
384size_t AImageDecoder_getMinimumStride(AImageDecoder* decoder) {
385 if (!decoder) {
386 return 0;
387 }
388
389 SkImageInfo info = toDecoder(decoder)->getOutputInfo();
390 return info.minRowBytes();
391}
392
393int AImageDecoder_decodeImage(AImageDecoder* decoder,
394 void* pixels, size_t stride,
395 size_t size) {
396 if (!decoder || !pixels || !stride) {
397 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
398 }
399
400 ImageDecoder* imageDecoder = toDecoder(decoder);
401
Leon Scroggins IIId894c592020-01-22 14:18:12 -0500402 SkImageInfo info = imageDecoder->getOutputInfo();
403 size_t minSize = info.computeByteSize(stride);
404 if (SkImageInfo::ByteSizeOverflowed(minSize) || size < minSize || !info.validRowBytes(stride)) {
Leon Scroggins III407b5442019-11-22 17:10:20 -0500405 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
406 }
407
Leon Scroggins IIIb26aebc2020-12-21 14:55:22 -0500408 if (imageDecoder->finished()) {
409 return ANDROID_IMAGE_DECODER_FINISHED;
410 }
411
Leon Scroggins III407b5442019-11-22 17:10:20 -0500412 return ResultToErrorCode(imageDecoder->decode(pixels, stride));
413}
414
415void AImageDecoder_delete(AImageDecoder* decoder) {
416 delete toDecoder(decoder);
417}
Leon Scroggins III24ae7d72020-10-09 13:14:35 -0400418
419bool AImageDecoder_isAnimated(AImageDecoder* decoder) {
420 if (!decoder) return false;
421
422 ImageDecoder* imageDecoder = toDecoder(decoder);
Leon Scroggins IIIb26aebc2020-12-21 14:55:22 -0500423 return imageDecoder->isAnimated();
Leon Scroggins III24ae7d72020-10-09 13:14:35 -0400424}
Leon Scroggins III3ad12c42020-10-12 10:56:42 -0400425
426int32_t AImageDecoder_getRepeatCount(AImageDecoder* decoder) {
427 if (!decoder) return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
428
429 ImageDecoder* imageDecoder = toDecoder(decoder);
430 const int count = imageDecoder->mCodec->codec()->getRepetitionCount();
431
432 // Skia should not report anything out of range, but defensively treat
433 // negative and too big as INFINITE.
434 if (count == SkCodec::kRepetitionCountInfinite || count < 0
435 || count > std::numeric_limits<int32_t>::max()) {
436 return ANDROID_IMAGE_DECODER_INFINITE;
437 }
438 return count;
439}
Leon Scroggins IIIb26aebc2020-12-21 14:55:22 -0500440
441int AImageDecoder_advanceFrame(AImageDecoder* decoder) {
442 if (!decoder) return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
443
444 ImageDecoder* imageDecoder = toDecoder(decoder);
445 if (!imageDecoder->isAnimated()) {
446 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
447 }
448
Leon Scroggins IIIdf33b952021-04-26 15:35:56 -0400449 const auto colorType = imageDecoder->getOutputInfo().colorType();
450 switch (colorType) {
451 case kN32_SkColorType:
452 case kRGBA_F16_SkColorType:
453 break;
454 default:
455 return ANDROID_IMAGE_DECODER_INVALID_STATE;
456 }
457
Leon Scroggins IIIb26aebc2020-12-21 14:55:22 -0500458 if (imageDecoder->advanceFrame()) {
459 return ANDROID_IMAGE_DECODER_SUCCESS;
460 }
461
462 if (imageDecoder->finished()) {
463 return ANDROID_IMAGE_DECODER_FINISHED;
464 }
465
466 return ANDROID_IMAGE_DECODER_INCOMPLETE;
467}
468
469int AImageDecoder_rewind(AImageDecoder* decoder) {
470 if (!decoder) return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
471
472 ImageDecoder* imageDecoder = toDecoder(decoder);
473 if (!imageDecoder->isAnimated()) {
474 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
475 }
476
477 return imageDecoder->rewind() ? ANDROID_IMAGE_DECODER_SUCCESS
478 : ANDROID_IMAGE_DECODER_SEEK_ERROR;
479}
Leon Scroggins III06213132020-12-28 15:31:48 -0500480
481AImageDecoderFrameInfo* AImageDecoderFrameInfo_create() {
482 return reinterpret_cast<AImageDecoderFrameInfo*>(new SkCodec::FrameInfo);
483}
484
485static SkCodec::FrameInfo* toFrameInfo(AImageDecoderFrameInfo* info) {
486 return reinterpret_cast<SkCodec::FrameInfo*>(info);
487}
488
489static const SkCodec::FrameInfo* toFrameInfo(const AImageDecoderFrameInfo* info) {
490 return reinterpret_cast<const SkCodec::FrameInfo*>(info);
491}
492
493void AImageDecoderFrameInfo_delete(AImageDecoderFrameInfo* info) {
494 delete toFrameInfo(info);
495}
496
497int AImageDecoder_getFrameInfo(AImageDecoder* decoder,
498 AImageDecoderFrameInfo* info) {
499 if (!decoder || !info) {
500 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
501 }
502
503 auto* imageDecoder = toDecoder(decoder);
504 if (imageDecoder->finished()) {
505 return ANDROID_IMAGE_DECODER_FINISHED;
506 }
507
508 *toFrameInfo(info) = imageDecoder->getCurrentFrameInfo();
509 return ANDROID_IMAGE_DECODER_SUCCESS;
510}
511
512int64_t AImageDecoderFrameInfo_getDuration(const AImageDecoderFrameInfo* info) {
Leon Scroggins III6116e5c2021-05-03 11:34:34 -0400513 if (!info) return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
Leon Scroggins III06213132020-12-28 15:31:48 -0500514
515 return toFrameInfo(info)->fDuration * 1'000'000;
516}
517
518ARect AImageDecoderFrameInfo_getFrameRect(const AImageDecoderFrameInfo* info) {
519 if (!info) {
520 return { 0, 0, 0, 0};
521 }
522
523 const SkIRect& r = toFrameInfo(info)->fFrameRect;
524 return { r.left(), r.top(), r.right(), r.bottom() };
525}
526
527bool AImageDecoderFrameInfo_hasAlphaWithinBounds(const AImageDecoderFrameInfo* info) {
528 if (!info) return false;
529
530 return toFrameInfo(info)->fHasAlphaWithinBounds;
531}
532
533int32_t AImageDecoderFrameInfo_getDisposeOp(const AImageDecoderFrameInfo* info) {
534 if (!info) return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
535
536 static_assert(static_cast<int>(SkCodecAnimation::DisposalMethod::kKeep)
537 == ANDROID_IMAGE_DECODER_DISPOSE_OP_NONE);
538 static_assert(static_cast<int>(SkCodecAnimation::DisposalMethod::kRestoreBGColor)
539 == ANDROID_IMAGE_DECODER_DISPOSE_OP_BACKGROUND);
540 static_assert(static_cast<int>(SkCodecAnimation::DisposalMethod::kRestorePrevious)
541 == ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS);
542 return static_cast<int>(toFrameInfo(info)->fDisposalMethod);
543}
544
545int32_t AImageDecoderFrameInfo_getBlendOp(const AImageDecoderFrameInfo* info) {
546 if (!info) return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
547
548 switch (toFrameInfo(info)->fBlend) {
549 case SkCodecAnimation::Blend::kSrc:
550 return ANDROID_IMAGE_DECODER_BLEND_OP_SRC;
551 case SkCodecAnimation::Blend::kSrcOver:
552 return ANDROID_IMAGE_DECODER_BLEND_OP_SRC_OVER;
553 }
554}
Leon Scroggins IIIc2ebc2b2021-01-13 09:46:06 -0500555
556void AImageDecoder_setInternallyHandleDisposePrevious(AImageDecoder* decoder, bool handle) {
557 if (decoder) {
558 toDecoder(decoder)->setHandleRestorePrevious(handle);
559 }
560}