| 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> | 
| Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 28 | #include <SkCodec.h> | 
|  | 29 | #include <SkColorSpace.h> | 
|  | 30 | #include <SkImageInfo.h> | 
|  | 31 | #include <SkRect.h> | 
|  | 32 | #include <SkSize.h> | 
|  | 33 | #include <SkStream.h> | 
| Leon Scroggins III | e5ace3f | 2020-01-15 15:31:40 -0500 | [diff] [blame] | 34 | #include <utils/Color.h> | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 35 |  | 
|  | 36 | #include <fcntl.h> | 
| Leon Scroggins III | 2e6bedf | 2020-02-11 16:31:21 -0500 | [diff] [blame] | 37 | #include <limits> | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 38 | #include <optional> | 
|  | 39 | #include <sys/stat.h> | 
|  | 40 | #include <sys/types.h> | 
|  | 41 | #include <unistd.h> | 
|  | 42 |  | 
|  | 43 | using namespace android; | 
|  | 44 |  | 
|  | 45 | int ResultToErrorCode(SkCodec::Result result) { | 
|  | 46 | switch (result) { | 
|  | 47 | case SkCodec::kIncompleteInput: | 
|  | 48 | return ANDROID_IMAGE_DECODER_INCOMPLETE; | 
|  | 49 | case SkCodec::kErrorInInput: | 
|  | 50 | return ANDROID_IMAGE_DECODER_ERROR; | 
|  | 51 | case SkCodec::kInvalidInput: | 
|  | 52 | return ANDROID_IMAGE_DECODER_INVALID_INPUT; | 
|  | 53 | case SkCodec::kCouldNotRewind: | 
|  | 54 | return ANDROID_IMAGE_DECODER_SEEK_ERROR; | 
|  | 55 | case SkCodec::kUnimplemented: | 
|  | 56 | return ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT; | 
|  | 57 | case SkCodec::kInvalidConversion: | 
|  | 58 | return ANDROID_IMAGE_DECODER_INVALID_CONVERSION; | 
|  | 59 | case SkCodec::kInvalidParameters: | 
|  | 60 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 61 | case SkCodec::kSuccess: | 
|  | 62 | return ANDROID_IMAGE_DECODER_SUCCESS; | 
|  | 63 | case SkCodec::kInvalidScale: | 
|  | 64 | return ANDROID_IMAGE_DECODER_INVALID_SCALE; | 
|  | 65 | case SkCodec::kInternalError: | 
|  | 66 | return ANDROID_IMAGE_DECODER_INTERNAL_ERROR; | 
|  | 67 | } | 
|  | 68 | } | 
|  | 69 |  | 
| Leon Scroggins III | 946f8d4 | 2020-12-02 14:05:44 -0500 | [diff] [blame] | 70 | const char* AImageDecoder_resultToString(int result) { | 
|  | 71 | switch (result) { | 
|  | 72 | case        ANDROID_IMAGE_DECODER_SUCCESS: | 
|  | 73 | return "ANDROID_IMAGE_DECODER_SUCCESS"; | 
|  | 74 | case        ANDROID_IMAGE_DECODER_INCOMPLETE: | 
|  | 75 | return "ANDROID_IMAGE_DECODER_INCOMPLETE"; | 
|  | 76 | case        ANDROID_IMAGE_DECODER_ERROR: | 
|  | 77 | return "ANDROID_IMAGE_DECODER_ERROR"; | 
|  | 78 | case        ANDROID_IMAGE_DECODER_INVALID_CONVERSION: | 
|  | 79 | return "ANDROID_IMAGE_DECODER_INVALID_CONVERSION"; | 
|  | 80 | case        ANDROID_IMAGE_DECODER_INVALID_SCALE: | 
|  | 81 | return "ANDROID_IMAGE_DECODER_INVALID_SCALE"; | 
|  | 82 | case        ANDROID_IMAGE_DECODER_BAD_PARAMETER: | 
|  | 83 | return "ANDROID_IMAGE_DECODER_BAD_PARAMETER"; | 
|  | 84 | case        ANDROID_IMAGE_DECODER_INVALID_INPUT: | 
|  | 85 | return "ANDROID_IMAGE_DECODER_INVALID_INPUT"; | 
|  | 86 | case        ANDROID_IMAGE_DECODER_SEEK_ERROR: | 
|  | 87 | return "ANDROID_IMAGE_DECODER_SEEK_ERROR"; | 
|  | 88 | case        ANDROID_IMAGE_DECODER_INTERNAL_ERROR: | 
|  | 89 | return "ANDROID_IMAGE_DECODER_INTERNAL_ERROR"; | 
|  | 90 | case        ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT: | 
|  | 91 | return "ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT"; | 
|  | 92 | case        ANDROID_IMAGE_DECODER_FINISHED: | 
|  | 93 | return "ANDROID_IMAGE_DECODER_FINISHED"; | 
|  | 94 | case        ANDROID_IMAGE_DECODER_INVALID_STATE: | 
|  | 95 | return "ANDROID_IMAGE_DECODER_INVALID_STATE"; | 
|  | 96 | default: | 
|  | 97 | return nullptr; | 
|  | 98 | } | 
|  | 99 | } | 
|  | 100 |  | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 101 | static int createFromStream(std::unique_ptr<SkStreamRewindable> stream, AImageDecoder** outDecoder) { | 
|  | 102 | SkCodec::Result result; | 
|  | 103 | auto codec = SkCodec::MakeFromStream(std::move(stream), &result, nullptr, | 
|  | 104 | SkCodec::SelectionPolicy::kPreferAnimation); | 
| Leon Scroggins III | 139145b | 2020-12-17 15:43:54 -0500 | [diff] [blame] | 105 | // These may be swapped due to the SkEncodedOrigin, but we're just checking | 
|  | 106 | // them to make sure they fit in int32_t. | 
|  | 107 | auto dimensions = codec->dimensions(); | 
|  | 108 | auto androidCodec = SkAndroidCodec::MakeFromCodec(std::move(codec)); | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 109 | if (!androidCodec) { | 
|  | 110 | return ResultToErrorCode(result); | 
|  | 111 | } | 
|  | 112 |  | 
| Leon Scroggins III | 2e6bedf | 2020-02-11 16:31:21 -0500 | [diff] [blame] | 113 | // AImageDecoderHeaderInfo_getWidth/Height return an int32_t. Ensure that | 
|  | 114 | // the conversion is safe. | 
| Leon Scroggins III | 139145b | 2020-12-17 15:43:54 -0500 | [diff] [blame] | 115 | if (dimensions.width() > std::numeric_limits<int32_t>::max() || | 
|  | 116 | dimensions.height() > std::numeric_limits<int32_t>::max()) { | 
| Leon Scroggins III | 2e6bedf | 2020-02-11 16:31:21 -0500 | [diff] [blame] | 117 | return ANDROID_IMAGE_DECODER_INVALID_INPUT; | 
|  | 118 | } | 
|  | 119 |  | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 120 | *outDecoder = reinterpret_cast<AImageDecoder*>(new ImageDecoder(std::move(androidCodec))); | 
|  | 121 | return ANDROID_IMAGE_DECODER_SUCCESS; | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | int AImageDecoder_createFromAAsset(AAsset* asset, AImageDecoder** outDecoder) { | 
|  | 125 | if (!asset || !outDecoder) { | 
|  | 126 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 127 | } | 
|  | 128 | *outDecoder = nullptr; | 
|  | 129 |  | 
| Leon Scroggins III | c72d0fb | 2020-12-30 16:38:23 -0500 | [diff] [blame] | 130 | #ifdef __ANDROID__ | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 131 | auto stream = std::make_unique<AAssetStreamAdaptor>(asset); | 
|  | 132 | return createFromStream(std::move(stream), outDecoder); | 
| Leon Scroggins III | c72d0fb | 2020-12-30 16:38:23 -0500 | [diff] [blame] | 133 | #else | 
|  | 134 | return ANDROID_IMAGE_DECODER_INTERNAL_ERROR; | 
|  | 135 | #endif | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 136 | } | 
|  | 137 |  | 
|  | 138 | static bool isSeekable(int descriptor) { | 
|  | 139 | return ::lseek64(descriptor, 0, SEEK_CUR) != -1; | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | int AImageDecoder_createFromFd(int fd, AImageDecoder** outDecoder) { | 
|  | 143 | if (fd <= 0 || !outDecoder) { | 
|  | 144 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | struct stat fdStat; | 
|  | 148 | if (fstat(fd, &fdStat) == -1) { | 
|  | 149 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | if (!isSeekable(fd)) { | 
|  | 153 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | // SkFILEStream will close its descriptor. Duplicate it so the client will | 
|  | 157 | // still be responsible for closing the original. | 
|  | 158 | int dupDescriptor = fcntl(fd, F_DUPFD_CLOEXEC, 0); | 
|  | 159 | FILE* file = fdopen(dupDescriptor, "r"); | 
|  | 160 | if (!file) { | 
|  | 161 | close(dupDescriptor); | 
|  | 162 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | auto stream = std::unique_ptr<SkStreamRewindable>(new SkFILEStream(file)); | 
|  | 166 | return createFromStream(std::move(stream), outDecoder); | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | int AImageDecoder_createFromBuffer(const void* buffer, size_t length, | 
|  | 170 | AImageDecoder** outDecoder) { | 
|  | 171 | if (!buffer || !length  || !outDecoder) { | 
|  | 172 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 173 | } | 
|  | 174 | *outDecoder = nullptr; | 
|  | 175 |  | 
|  | 176 | // The client is expected to keep the buffer alive as long as the | 
|  | 177 | // AImageDecoder, so we do not need to copy the buffer. | 
|  | 178 | auto stream = std::unique_ptr<SkStreamRewindable>( | 
|  | 179 | new SkMemoryStream(buffer, length, false /* copyData */)); | 
|  | 180 | return createFromStream(std::move(stream), outDecoder); | 
|  | 181 | } | 
|  | 182 |  | 
|  | 183 | static ImageDecoder* toDecoder(AImageDecoder* d) { | 
|  | 184 | return reinterpret_cast<ImageDecoder*>(d); | 
|  | 185 | } | 
|  | 186 |  | 
| Leon Scroggins III | f89de63 | 2020-01-19 21:22:18 -0500 | [diff] [blame] | 187 | static const ImageDecoder* toDecoder(const AImageDecoder* d) { | 
|  | 188 | return reinterpret_cast<const ImageDecoder*>(d); | 
|  | 189 | } | 
|  | 190 |  | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 191 | // Note: This differs from the version in android_bitmap.cpp in that this | 
|  | 192 | // version returns kGray_8_SkColorType for ANDROID_BITMAP_FORMAT_A_8. SkCodec | 
|  | 193 | // allows decoding single channel images to gray, which Android then treats | 
|  | 194 | // as A_8/ALPHA_8. | 
|  | 195 | static SkColorType getColorType(AndroidBitmapFormat format) { | 
|  | 196 | switch (format) { | 
|  | 197 | case ANDROID_BITMAP_FORMAT_RGBA_8888: | 
|  | 198 | return kN32_SkColorType; | 
|  | 199 | case ANDROID_BITMAP_FORMAT_RGB_565: | 
|  | 200 | return kRGB_565_SkColorType; | 
|  | 201 | case ANDROID_BITMAP_FORMAT_RGBA_4444: | 
|  | 202 | return kARGB_4444_SkColorType; | 
|  | 203 | case ANDROID_BITMAP_FORMAT_A_8: | 
|  | 204 | return kGray_8_SkColorType; | 
|  | 205 | case ANDROID_BITMAP_FORMAT_RGBA_F16: | 
|  | 206 | return kRGBA_F16_SkColorType; | 
| Alec Mouri | 1efd0a5 | 2022-01-20 13:58:23 -0800 | [diff] [blame] | 207 | case ANDROID_BITMAP_FORMAT_RGBA_1010102: | 
|  | 208 | return kRGBA_1010102_SkColorType; | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 209 | default: | 
|  | 210 | return kUnknown_SkColorType; | 
|  | 211 | } | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | int AImageDecoder_setAndroidBitmapFormat(AImageDecoder* decoder, int32_t format) { | 
| Alec Mouri | 1efd0a5 | 2022-01-20 13:58:23 -0800 | [diff] [blame] | 215 | if (!decoder || format < ANDROID_BITMAP_FORMAT_NONE || | 
|  | 216 | format > ANDROID_BITMAP_FORMAT_RGBA_1010102) { | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 217 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 218 | } | 
| Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 219 |  | 
|  | 220 | auto* imageDecoder = toDecoder(decoder); | 
|  | 221 | if (imageDecoder->currentFrame() != 0) { | 
|  | 222 | return ANDROID_IMAGE_DECODER_INVALID_STATE; | 
|  | 223 | } | 
|  | 224 |  | 
|  | 225 | return imageDecoder->setOutColorType(getColorType((AndroidBitmapFormat) format)) | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 226 | ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_INVALID_CONVERSION; | 
|  | 227 | } | 
|  | 228 |  | 
| Leon Scroggins III | e5ace3f | 2020-01-15 15:31:40 -0500 | [diff] [blame] | 229 | int AImageDecoder_setDataSpace(AImageDecoder* decoder, int32_t dataspace) { | 
|  | 230 | sk_sp<SkColorSpace> cs = uirenderer::DataSpaceToColorSpace((android_dataspace)dataspace); | 
|  | 231 | // 0 is ADATASPACE_UNKNOWN. We need an explicit request for an ADataSpace. | 
|  | 232 | if (!decoder || !dataspace || !cs) { | 
|  | 233 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 234 | } | 
|  | 235 |  | 
|  | 236 | ImageDecoder* imageDecoder = toDecoder(decoder); | 
| Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 237 | if (imageDecoder->currentFrame() != 0) { | 
|  | 238 | return ANDROID_IMAGE_DECODER_INVALID_STATE; | 
|  | 239 | } | 
|  | 240 |  | 
| Leon Scroggins III | e5ace3f | 2020-01-15 15:31:40 -0500 | [diff] [blame] | 241 | imageDecoder->setOutColorSpace(std::move(cs)); | 
|  | 242 | return ANDROID_IMAGE_DECODER_SUCCESS; | 
|  | 243 | } | 
|  | 244 |  | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 245 | const AImageDecoderHeaderInfo* AImageDecoder_getHeaderInfo(const AImageDecoder* decoder) { | 
|  | 246 | return reinterpret_cast<const AImageDecoderHeaderInfo*>(decoder); | 
|  | 247 | } | 
|  | 248 |  | 
|  | 249 | static const ImageDecoder* toDecoder(const AImageDecoderHeaderInfo* info) { | 
|  | 250 | return reinterpret_cast<const ImageDecoder*>(info); | 
|  | 251 | } | 
|  | 252 |  | 
|  | 253 | int32_t AImageDecoderHeaderInfo_getWidth(const AImageDecoderHeaderInfo* info) { | 
|  | 254 | if (!info) { | 
|  | 255 | return 0; | 
|  | 256 | } | 
| Leon Scroggins III | 139145b | 2020-12-17 15:43:54 -0500 | [diff] [blame] | 257 | return toDecoder(info)->width(); | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 258 | } | 
|  | 259 |  | 
|  | 260 | int32_t AImageDecoderHeaderInfo_getHeight(const AImageDecoderHeaderInfo* info) { | 
|  | 261 | if (!info) { | 
|  | 262 | return 0; | 
|  | 263 | } | 
| Leon Scroggins III | 139145b | 2020-12-17 15:43:54 -0500 | [diff] [blame] | 264 | return toDecoder(info)->height(); | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 265 | } | 
|  | 266 |  | 
|  | 267 | const char* AImageDecoderHeaderInfo_getMimeType(const AImageDecoderHeaderInfo* info) { | 
|  | 268 | if (!info) { | 
|  | 269 | return nullptr; | 
|  | 270 | } | 
|  | 271 | return getMimeType(toDecoder(info)->mCodec->getEncodedFormat()); | 
|  | 272 | } | 
|  | 273 |  | 
| Leon Scroggins III | e5ace3f | 2020-01-15 15:31:40 -0500 | [diff] [blame] | 274 | int32_t AImageDecoderHeaderInfo_getDataSpace(const AImageDecoderHeaderInfo* info) { | 
|  | 275 | if (!info) { | 
|  | 276 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 277 | } | 
|  | 278 |  | 
| Leon Scroggins III | 6eeca5c | 2020-01-30 13:59:50 -0500 | [diff] [blame] | 279 | // Note: This recomputes the color type because it's possible the client has | 
|  | 280 | // 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] | 281 | // we could store the ADataSpace in the ImageDecoder. | 
|  | 282 | const ImageDecoder* imageDecoder = toDecoder(info); | 
|  | 283 | SkColorType colorType = imageDecoder->mCodec->computeOutputColorType(kN32_SkColorType); | 
| Leon Scroggins III | 6eeca5c | 2020-01-30 13:59:50 -0500 | [diff] [blame] | 284 | sk_sp<SkColorSpace> colorSpace = imageDecoder->getDefaultColorSpace(); | 
| Leon Scroggins III | e5ace3f | 2020-01-15 15:31:40 -0500 | [diff] [blame] | 285 | return uirenderer::ColorSpaceToADataSpace(colorSpace.get(), colorType); | 
|  | 286 | } | 
|  | 287 |  | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 288 | // FIXME: Share with getFormat in android_bitmap.cpp? | 
|  | 289 | static AndroidBitmapFormat getFormat(SkColorType colorType) { | 
|  | 290 | switch (colorType) { | 
|  | 291 | case kN32_SkColorType: | 
|  | 292 | return ANDROID_BITMAP_FORMAT_RGBA_8888; | 
|  | 293 | case kRGB_565_SkColorType: | 
|  | 294 | return ANDROID_BITMAP_FORMAT_RGB_565; | 
|  | 295 | case kARGB_4444_SkColorType: | 
|  | 296 | return ANDROID_BITMAP_FORMAT_RGBA_4444; | 
|  | 297 | case kAlpha_8_SkColorType: | 
|  | 298 | return ANDROID_BITMAP_FORMAT_A_8; | 
|  | 299 | case kRGBA_F16_SkColorType: | 
|  | 300 | return ANDROID_BITMAP_FORMAT_RGBA_F16; | 
| Alec Mouri | 1efd0a5 | 2022-01-20 13:58:23 -0800 | [diff] [blame] | 301 | case kRGBA_1010102_SkColorType: | 
|  | 302 | return ANDROID_BITMAP_FORMAT_RGBA_1010102; | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 303 | default: | 
|  | 304 | return ANDROID_BITMAP_FORMAT_NONE; | 
|  | 305 | } | 
|  | 306 | } | 
|  | 307 |  | 
| Leon Scroggins III | 64301cb | 2020-01-23 09:47:47 -0500 | [diff] [blame] | 308 | int32_t AImageDecoderHeaderInfo_getAndroidBitmapFormat(const AImageDecoderHeaderInfo* info) { | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 309 | if (!info) { | 
|  | 310 | return ANDROID_BITMAP_FORMAT_NONE; | 
|  | 311 | } | 
|  | 312 | return getFormat(toDecoder(info)->mCodec->computeOutputColorType(kN32_SkColorType)); | 
|  | 313 | } | 
|  | 314 |  | 
|  | 315 | int AImageDecoderHeaderInfo_getAlphaFlags(const AImageDecoderHeaderInfo* info) { | 
|  | 316 | if (!info) { | 
| Leon Scroggins III | d8840bd | 2020-01-15 04:09:48 -0500 | [diff] [blame] | 317 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 318 | } | 
|  | 319 | switch (toDecoder(info)->mCodec->getInfo().alphaType()) { | 
|  | 320 | case kUnknown_SkAlphaType: | 
|  | 321 | LOG_ALWAYS_FATAL("Invalid alpha type"); | 
| Leon Scroggins III | d8840bd | 2020-01-15 04:09:48 -0500 | [diff] [blame] | 322 | return ANDROID_IMAGE_DECODER_INTERNAL_ERROR; | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 323 | case kUnpremul_SkAlphaType: | 
|  | 324 | // fall through. premul is the default. | 
|  | 325 | case kPremul_SkAlphaType: | 
|  | 326 | return ANDROID_BITMAP_FLAGS_ALPHA_PREMUL; | 
|  | 327 | case kOpaque_SkAlphaType: | 
|  | 328 | return ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE; | 
|  | 329 | } | 
|  | 330 | } | 
|  | 331 |  | 
| Leon Scroggins III | 1ade46d | 2020-01-15 05:41:06 -0500 | [diff] [blame] | 332 | int AImageDecoder_setUnpremultipliedRequired(AImageDecoder* decoder, bool required) { | 
|  | 333 | if (!decoder) { | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 334 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 335 | } | 
|  | 336 |  | 
| Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 337 | auto* imageDecoder = toDecoder(decoder); | 
|  | 338 | if (imageDecoder->currentFrame() != 0) { | 
|  | 339 | return ANDROID_IMAGE_DECODER_INVALID_STATE; | 
|  | 340 | } | 
|  | 341 |  | 
|  | 342 | return imageDecoder->setUnpremultipliedRequired(required) | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 343 | ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_INVALID_CONVERSION; | 
|  | 344 | } | 
|  | 345 |  | 
| Leon Scroggins III | 64301cb | 2020-01-23 09:47:47 -0500 | [diff] [blame] | 346 | int AImageDecoder_setTargetSize(AImageDecoder* decoder, int32_t width, int32_t height) { | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 347 | if (!decoder) { | 
|  | 348 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 349 | } | 
|  | 350 |  | 
| Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 351 | auto* imageDecoder = toDecoder(decoder); | 
|  | 352 | if (imageDecoder->currentFrame() != 0) { | 
|  | 353 | return ANDROID_IMAGE_DECODER_INVALID_STATE; | 
|  | 354 | } | 
|  | 355 |  | 
|  | 356 | return imageDecoder->setTargetSize(width, height) | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 357 | ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_INVALID_SCALE; | 
|  | 358 | } | 
|  | 359 |  | 
| Leon Scroggins III | f89de63 | 2020-01-19 21:22:18 -0500 | [diff] [blame] | 360 | int AImageDecoder_computeSampledSize(const AImageDecoder* decoder, int sampleSize, | 
| Leon Scroggins III | 64301cb | 2020-01-23 09:47:47 -0500 | [diff] [blame] | 361 | int32_t* width, int32_t* height) { | 
| Leon Scroggins III | f89de63 | 2020-01-19 21:22:18 -0500 | [diff] [blame] | 362 | if (!decoder || !width || !height || sampleSize < 1) { | 
|  | 363 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 364 | } | 
|  | 365 |  | 
| Leon Scroggins III | 5a5c2ce | 2021-01-15 14:09:13 -0500 | [diff] [blame] | 366 | SkISize size = toDecoder(decoder)->getSampledDimensions(sampleSize); | 
| Leon Scroggins III | f89de63 | 2020-01-19 21:22:18 -0500 | [diff] [blame] | 367 | *width = size.width(); | 
|  | 368 | *height = size.height(); | 
|  | 369 | return ANDROID_IMAGE_DECODER_SUCCESS; | 
|  | 370 | } | 
|  | 371 |  | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 372 | int AImageDecoder_setCrop(AImageDecoder* decoder, ARect crop) { | 
|  | 373 | if (!decoder) { | 
|  | 374 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 375 | } | 
|  | 376 |  | 
| Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 377 | auto* imageDecoder = toDecoder(decoder); | 
|  | 378 | if (imageDecoder->currentFrame() != 0) { | 
|  | 379 | return ANDROID_IMAGE_DECODER_INVALID_STATE; | 
|  | 380 | } | 
|  | 381 |  | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 382 | SkIRect cropIRect; | 
|  | 383 | cropIRect.setLTRB(crop.left, crop.top, crop.right, crop.bottom); | 
|  | 384 | SkIRect* cropPtr = cropIRect == SkIRect::MakeEmpty() ? nullptr : &cropIRect; | 
| Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 385 | return imageDecoder->setCropRect(cropPtr) | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 386 | ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 387 | } | 
|  | 388 |  | 
|  | 389 |  | 
|  | 390 | size_t AImageDecoder_getMinimumStride(AImageDecoder* decoder) { | 
|  | 391 | if (!decoder) { | 
|  | 392 | return 0; | 
|  | 393 | } | 
|  | 394 |  | 
|  | 395 | SkImageInfo info = toDecoder(decoder)->getOutputInfo(); | 
|  | 396 | return info.minRowBytes(); | 
|  | 397 | } | 
|  | 398 |  | 
|  | 399 | int AImageDecoder_decodeImage(AImageDecoder* decoder, | 
|  | 400 | void* pixels, size_t stride, | 
|  | 401 | size_t size) { | 
|  | 402 | if (!decoder || !pixels || !stride) { | 
|  | 403 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 404 | } | 
|  | 405 |  | 
|  | 406 | ImageDecoder* imageDecoder = toDecoder(decoder); | 
|  | 407 |  | 
| Leon Scroggins III | d894c59 | 2020-01-22 14:18:12 -0500 | [diff] [blame] | 408 | SkImageInfo info = imageDecoder->getOutputInfo(); | 
|  | 409 | size_t minSize = info.computeByteSize(stride); | 
|  | 410 | if (SkImageInfo::ByteSizeOverflowed(minSize) || size < minSize || !info.validRowBytes(stride)) { | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 411 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 412 | } | 
|  | 413 |  | 
| Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 414 | if (imageDecoder->finished()) { | 
|  | 415 | return ANDROID_IMAGE_DECODER_FINISHED; | 
|  | 416 | } | 
|  | 417 |  | 
| Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 418 | return ResultToErrorCode(imageDecoder->decode(pixels, stride)); | 
|  | 419 | } | 
|  | 420 |  | 
|  | 421 | void AImageDecoder_delete(AImageDecoder* decoder) { | 
|  | 422 | delete toDecoder(decoder); | 
|  | 423 | } | 
| Leon Scroggins III | 24ae7d7 | 2020-10-09 13:14:35 -0400 | [diff] [blame] | 424 |  | 
|  | 425 | bool AImageDecoder_isAnimated(AImageDecoder* decoder) { | 
|  | 426 | if (!decoder) return false; | 
|  | 427 |  | 
|  | 428 | ImageDecoder* imageDecoder = toDecoder(decoder); | 
| Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 429 | return imageDecoder->isAnimated(); | 
| Leon Scroggins III | 24ae7d7 | 2020-10-09 13:14:35 -0400 | [diff] [blame] | 430 | } | 
| Leon Scroggins III | 3ad12c4 | 2020-10-12 10:56:42 -0400 | [diff] [blame] | 431 |  | 
|  | 432 | int32_t AImageDecoder_getRepeatCount(AImageDecoder* decoder) { | 
|  | 433 | if (!decoder) return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 434 |  | 
|  | 435 | ImageDecoder* imageDecoder = toDecoder(decoder); | 
|  | 436 | const int count = imageDecoder->mCodec->codec()->getRepetitionCount(); | 
|  | 437 |  | 
|  | 438 | // Skia should not report anything out of range, but defensively treat | 
|  | 439 | // negative and too big as INFINITE. | 
|  | 440 | if (count == SkCodec::kRepetitionCountInfinite || count < 0 | 
|  | 441 | || count > std::numeric_limits<int32_t>::max()) { | 
|  | 442 | return ANDROID_IMAGE_DECODER_INFINITE; | 
|  | 443 | } | 
|  | 444 | return count; | 
|  | 445 | } | 
| Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 446 |  | 
|  | 447 | int AImageDecoder_advanceFrame(AImageDecoder* decoder) { | 
|  | 448 | if (!decoder) return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 449 |  | 
|  | 450 | ImageDecoder* imageDecoder = toDecoder(decoder); | 
|  | 451 | if (!imageDecoder->isAnimated()) { | 
|  | 452 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 453 | } | 
|  | 454 |  | 
| Leon Scroggins III | df33b95 | 2021-04-26 15:35:56 -0400 | [diff] [blame] | 455 | const auto colorType = imageDecoder->getOutputInfo().colorType(); | 
|  | 456 | switch (colorType) { | 
|  | 457 | case kN32_SkColorType: | 
|  | 458 | case kRGBA_F16_SkColorType: | 
|  | 459 | break; | 
|  | 460 | default: | 
|  | 461 | return ANDROID_IMAGE_DECODER_INVALID_STATE; | 
|  | 462 | } | 
|  | 463 |  | 
| Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 464 | if (imageDecoder->advanceFrame()) { | 
|  | 465 | return ANDROID_IMAGE_DECODER_SUCCESS; | 
|  | 466 | } | 
|  | 467 |  | 
|  | 468 | if (imageDecoder->finished()) { | 
|  | 469 | return ANDROID_IMAGE_DECODER_FINISHED; | 
|  | 470 | } | 
|  | 471 |  | 
|  | 472 | return ANDROID_IMAGE_DECODER_INCOMPLETE; | 
|  | 473 | } | 
|  | 474 |  | 
|  | 475 | int AImageDecoder_rewind(AImageDecoder* decoder) { | 
|  | 476 | if (!decoder) return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 477 |  | 
|  | 478 | ImageDecoder* imageDecoder = toDecoder(decoder); | 
|  | 479 | if (!imageDecoder->isAnimated()) { | 
|  | 480 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 481 | } | 
|  | 482 |  | 
|  | 483 | return imageDecoder->rewind() ? ANDROID_IMAGE_DECODER_SUCCESS | 
|  | 484 | : ANDROID_IMAGE_DECODER_SEEK_ERROR; | 
|  | 485 | } | 
| Leon Scroggins III | 0621313 | 2020-12-28 15:31:48 -0500 | [diff] [blame] | 486 |  | 
|  | 487 | AImageDecoderFrameInfo* AImageDecoderFrameInfo_create() { | 
|  | 488 | return reinterpret_cast<AImageDecoderFrameInfo*>(new SkCodec::FrameInfo); | 
|  | 489 | } | 
|  | 490 |  | 
|  | 491 | static SkCodec::FrameInfo* toFrameInfo(AImageDecoderFrameInfo* info) { | 
|  | 492 | return reinterpret_cast<SkCodec::FrameInfo*>(info); | 
|  | 493 | } | 
|  | 494 |  | 
|  | 495 | static const SkCodec::FrameInfo* toFrameInfo(const AImageDecoderFrameInfo* info) { | 
|  | 496 | return reinterpret_cast<const SkCodec::FrameInfo*>(info); | 
|  | 497 | } | 
|  | 498 |  | 
|  | 499 | void AImageDecoderFrameInfo_delete(AImageDecoderFrameInfo* info) { | 
|  | 500 | delete toFrameInfo(info); | 
|  | 501 | } | 
|  | 502 |  | 
|  | 503 | int AImageDecoder_getFrameInfo(AImageDecoder* decoder, | 
|  | 504 | AImageDecoderFrameInfo* info) { | 
|  | 505 | if (!decoder || !info) { | 
|  | 506 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 507 | } | 
|  | 508 |  | 
|  | 509 | auto* imageDecoder = toDecoder(decoder); | 
|  | 510 | if (imageDecoder->finished()) { | 
|  | 511 | return ANDROID_IMAGE_DECODER_FINISHED; | 
|  | 512 | } | 
|  | 513 |  | 
|  | 514 | *toFrameInfo(info) = imageDecoder->getCurrentFrameInfo(); | 
|  | 515 | return ANDROID_IMAGE_DECODER_SUCCESS; | 
|  | 516 | } | 
|  | 517 |  | 
|  | 518 | int64_t AImageDecoderFrameInfo_getDuration(const AImageDecoderFrameInfo* info) { | 
| Leon Scroggins III | 6116e5c | 2021-05-03 11:34:34 -0400 | [diff] [blame] | 519 | if (!info) return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
| Leon Scroggins III | 0621313 | 2020-12-28 15:31:48 -0500 | [diff] [blame] | 520 |  | 
|  | 521 | return toFrameInfo(info)->fDuration * 1'000'000; | 
|  | 522 | } | 
|  | 523 |  | 
|  | 524 | ARect AImageDecoderFrameInfo_getFrameRect(const AImageDecoderFrameInfo* info) { | 
|  | 525 | if (!info) { | 
|  | 526 | return { 0, 0, 0, 0}; | 
|  | 527 | } | 
|  | 528 |  | 
|  | 529 | const SkIRect& r = toFrameInfo(info)->fFrameRect; | 
|  | 530 | return { r.left(), r.top(), r.right(), r.bottom() }; | 
|  | 531 | } | 
|  | 532 |  | 
|  | 533 | bool AImageDecoderFrameInfo_hasAlphaWithinBounds(const AImageDecoderFrameInfo* info) { | 
|  | 534 | if (!info) return false; | 
|  | 535 |  | 
|  | 536 | return toFrameInfo(info)->fHasAlphaWithinBounds; | 
|  | 537 | } | 
|  | 538 |  | 
|  | 539 | int32_t AImageDecoderFrameInfo_getDisposeOp(const AImageDecoderFrameInfo* info) { | 
|  | 540 | if (!info) return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 541 |  | 
|  | 542 | static_assert(static_cast<int>(SkCodecAnimation::DisposalMethod::kKeep) | 
|  | 543 | == ANDROID_IMAGE_DECODER_DISPOSE_OP_NONE); | 
|  | 544 | static_assert(static_cast<int>(SkCodecAnimation::DisposalMethod::kRestoreBGColor) | 
|  | 545 | == ANDROID_IMAGE_DECODER_DISPOSE_OP_BACKGROUND); | 
|  | 546 | static_assert(static_cast<int>(SkCodecAnimation::DisposalMethod::kRestorePrevious) | 
|  | 547 | == ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS); | 
|  | 548 | return static_cast<int>(toFrameInfo(info)->fDisposalMethod); | 
|  | 549 | } | 
|  | 550 |  | 
|  | 551 | int32_t AImageDecoderFrameInfo_getBlendOp(const AImageDecoderFrameInfo* info) { | 
|  | 552 | if (!info) return ANDROID_IMAGE_DECODER_BAD_PARAMETER; | 
|  | 553 |  | 
|  | 554 | switch (toFrameInfo(info)->fBlend) { | 
|  | 555 | case SkCodecAnimation::Blend::kSrc: | 
|  | 556 | return ANDROID_IMAGE_DECODER_BLEND_OP_SRC; | 
|  | 557 | case SkCodecAnimation::Blend::kSrcOver: | 
|  | 558 | return ANDROID_IMAGE_DECODER_BLEND_OP_SRC_OVER; | 
|  | 559 | } | 
|  | 560 | } | 
| Leon Scroggins III | c2ebc2b | 2021-01-13 09:46:06 -0500 | [diff] [blame] | 561 |  | 
|  | 562 | void AImageDecoder_setInternallyHandleDisposePrevious(AImageDecoder* decoder, bool handle) { | 
|  | 563 | if (decoder) { | 
|  | 564 | toDecoder(decoder)->setHandleRestorePrevious(handle); | 
|  | 565 | } | 
|  | 566 | } |