Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 1 | /* |
| 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 III | e5ace3f | 2020-01-15 15:31:40 -0500 | [diff] [blame] | 21 | #include <android/data_space.h> |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 22 | #include <android/imagedecoder.h> |
Derek Sollenberger | 2173ea2 | 2020-02-19 15:37:29 -0500 | [diff] [blame] | 23 | #include <MimeType.h> |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 24 | #include <android/rect.h> |
| 25 | #include <hwui/ImageDecoder.h> |
| 26 | #include <log/log.h> |
| 27 | #include <SkAndroidCodec.h> |
Leon Scroggins III | e5ace3f | 2020-01-15 15:31:40 -0500 | [diff] [blame] | 28 | #include <utils/Color.h> |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 29 | |
| 30 | #include <fcntl.h> |
Leon Scroggins III | 2e6bedf | 2020-02-11 16:31:21 -0500 | [diff] [blame] | 31 | #include <limits> |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 32 | #include <optional> |
| 33 | #include <sys/stat.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <unistd.h> |
| 36 | |
| 37 | using namespace android; |
| 38 | |
| 39 | int 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 | |
| 64 | static 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 III | 139145b | 2020-12-17 15:43:54 -0500 | [diff] [blame^] | 68 | // 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 III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 72 | if (!androidCodec) { |
| 73 | return ResultToErrorCode(result); |
| 74 | } |
| 75 | |
Leon Scroggins III | 2e6bedf | 2020-02-11 16:31:21 -0500 | [diff] [blame] | 76 | // AImageDecoderHeaderInfo_getWidth/Height return an int32_t. Ensure that |
| 77 | // the conversion is safe. |
Leon Scroggins III | 139145b | 2020-12-17 15:43:54 -0500 | [diff] [blame^] | 78 | if (dimensions.width() > std::numeric_limits<int32_t>::max() || |
| 79 | dimensions.height() > std::numeric_limits<int32_t>::max()) { |
Leon Scroggins III | 2e6bedf | 2020-02-11 16:31:21 -0500 | [diff] [blame] | 80 | return ANDROID_IMAGE_DECODER_INVALID_INPUT; |
| 81 | } |
| 82 | |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 83 | *outDecoder = reinterpret_cast<AImageDecoder*>(new ImageDecoder(std::move(androidCodec))); |
| 84 | return ANDROID_IMAGE_DECODER_SUCCESS; |
| 85 | } |
| 86 | |
| 87 | int 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 | |
| 97 | static bool isSeekable(int descriptor) { |
| 98 | return ::lseek64(descriptor, 0, SEEK_CUR) != -1; |
| 99 | } |
| 100 | |
| 101 | int 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 | |
| 128 | int 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 | |
| 142 | static ImageDecoder* toDecoder(AImageDecoder* d) { |
| 143 | return reinterpret_cast<ImageDecoder*>(d); |
| 144 | } |
| 145 | |
Leon Scroggins III | f89de63 | 2020-01-19 21:22:18 -0500 | [diff] [blame] | 146 | static const ImageDecoder* toDecoder(const AImageDecoder* d) { |
| 147 | return reinterpret_cast<const ImageDecoder*>(d); |
| 148 | } |
| 149 | |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 150 | // 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. |
| 154 | static 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 | |
| 171 | int 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 III | e5ace3f | 2020-01-15 15:31:40 -0500 | [diff] [blame] | 180 | int 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 III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 192 | const AImageDecoderHeaderInfo* AImageDecoder_getHeaderInfo(const AImageDecoder* decoder) { |
| 193 | return reinterpret_cast<const AImageDecoderHeaderInfo*>(decoder); |
| 194 | } |
| 195 | |
| 196 | static const ImageDecoder* toDecoder(const AImageDecoderHeaderInfo* info) { |
| 197 | return reinterpret_cast<const ImageDecoder*>(info); |
| 198 | } |
| 199 | |
| 200 | int32_t AImageDecoderHeaderInfo_getWidth(const AImageDecoderHeaderInfo* info) { |
| 201 | if (!info) { |
| 202 | return 0; |
| 203 | } |
Leon Scroggins III | 139145b | 2020-12-17 15:43:54 -0500 | [diff] [blame^] | 204 | return toDecoder(info)->width(); |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | int32_t AImageDecoderHeaderInfo_getHeight(const AImageDecoderHeaderInfo* info) { |
| 208 | if (!info) { |
| 209 | return 0; |
| 210 | } |
Leon Scroggins III | 139145b | 2020-12-17 15:43:54 -0500 | [diff] [blame^] | 211 | return toDecoder(info)->height(); |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | const char* AImageDecoderHeaderInfo_getMimeType(const AImageDecoderHeaderInfo* info) { |
| 215 | if (!info) { |
| 216 | return nullptr; |
| 217 | } |
| 218 | return getMimeType(toDecoder(info)->mCodec->getEncodedFormat()); |
| 219 | } |
| 220 | |
Leon Scroggins III | e5ace3f | 2020-01-15 15:31:40 -0500 | [diff] [blame] | 221 | int32_t AImageDecoderHeaderInfo_getDataSpace(const AImageDecoderHeaderInfo* info) { |
| 222 | if (!info) { |
| 223 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 224 | } |
| 225 | |
Leon Scroggins III | 6eeca5c | 2020-01-30 13:59:50 -0500 | [diff] [blame] | 226 | // 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 III | e5ace3f | 2020-01-15 15:31:40 -0500 | [diff] [blame] | 228 | // we could store the ADataSpace in the ImageDecoder. |
| 229 | const ImageDecoder* imageDecoder = toDecoder(info); |
| 230 | SkColorType colorType = imageDecoder->mCodec->computeOutputColorType(kN32_SkColorType); |
Leon Scroggins III | 6eeca5c | 2020-01-30 13:59:50 -0500 | [diff] [blame] | 231 | sk_sp<SkColorSpace> colorSpace = imageDecoder->getDefaultColorSpace(); |
Leon Scroggins III | e5ace3f | 2020-01-15 15:31:40 -0500 | [diff] [blame] | 232 | return uirenderer::ColorSpaceToADataSpace(colorSpace.get(), colorType); |
| 233 | } |
| 234 | |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 235 | // FIXME: Share with getFormat in android_bitmap.cpp? |
| 236 | static 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 III | 64301cb | 2020-01-23 09:47:47 -0500 | [diff] [blame] | 253 | int32_t AImageDecoderHeaderInfo_getAndroidBitmapFormat(const AImageDecoderHeaderInfo* info) { |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 254 | if (!info) { |
| 255 | return ANDROID_BITMAP_FORMAT_NONE; |
| 256 | } |
| 257 | return getFormat(toDecoder(info)->mCodec->computeOutputColorType(kN32_SkColorType)); |
| 258 | } |
| 259 | |
| 260 | int AImageDecoderHeaderInfo_getAlphaFlags(const AImageDecoderHeaderInfo* info) { |
| 261 | if (!info) { |
Leon Scroggins III | d8840bd | 2020-01-15 04:09:48 -0500 | [diff] [blame] | 262 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 263 | } |
| 264 | switch (toDecoder(info)->mCodec->getInfo().alphaType()) { |
| 265 | case kUnknown_SkAlphaType: |
| 266 | LOG_ALWAYS_FATAL("Invalid alpha type"); |
Leon Scroggins III | d8840bd | 2020-01-15 04:09:48 -0500 | [diff] [blame] | 267 | return ANDROID_IMAGE_DECODER_INTERNAL_ERROR; |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 268 | 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 III | 1ade46d | 2020-01-15 05:41:06 -0500 | [diff] [blame] | 277 | int AImageDecoder_setUnpremultipliedRequired(AImageDecoder* decoder, bool required) { |
| 278 | if (!decoder) { |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 279 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 280 | } |
| 281 | |
Leon Scroggins III | 1ade46d | 2020-01-15 05:41:06 -0500 | [diff] [blame] | 282 | return toDecoder(decoder)->setUnpremultipliedRequired(required) |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 283 | ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_INVALID_CONVERSION; |
| 284 | } |
| 285 | |
Leon Scroggins III | 64301cb | 2020-01-23 09:47:47 -0500 | [diff] [blame] | 286 | int AImageDecoder_setTargetSize(AImageDecoder* decoder, int32_t width, int32_t height) { |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 287 | 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 III | f89de63 | 2020-01-19 21:22:18 -0500 | [diff] [blame] | 295 | int AImageDecoder_computeSampledSize(const AImageDecoder* decoder, int sampleSize, |
Leon Scroggins III | 64301cb | 2020-01-23 09:47:47 -0500 | [diff] [blame] | 296 | int32_t* width, int32_t* height) { |
Leon Scroggins III | f89de63 | 2020-01-19 21:22:18 -0500 | [diff] [blame] | 297 | 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 III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 307 | int 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 | |
| 320 | size_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 | |
| 329 | int 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 III | d894c59 | 2020-01-22 14:18:12 -0500 | [diff] [blame] | 338 | SkImageInfo info = imageDecoder->getOutputInfo(); |
| 339 | size_t minSize = info.computeByteSize(stride); |
| 340 | if (SkImageInfo::ByteSizeOverflowed(minSize) || size < minSize || !info.validRowBytes(stride)) { |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 341 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 342 | } |
| 343 | |
| 344 | return ResultToErrorCode(imageDecoder->decode(pixels, stride)); |
| 345 | } |
| 346 | |
| 347 | void AImageDecoder_delete(AImageDecoder* decoder) { |
| 348 | delete toDecoder(decoder); |
| 349 | } |
Leon Scroggins III | 24ae7d7 | 2020-10-09 13:14:35 -0400 | [diff] [blame] | 350 | |
| 351 | bool 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 III | 3ad12c4 | 2020-10-12 10:56:42 -0400 | [diff] [blame] | 357 | |
| 358 | int32_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 | } |