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 | |
Leon Scroggins III | 946f8d4 | 2020-12-02 14:05:44 -0500 | [diff] [blame] | 64 | const 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 III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 95 | static 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 III | 139145b | 2020-12-17 15:43:54 -0500 | [diff] [blame] | 99 | // 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 III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 103 | if (!androidCodec) { |
| 104 | return ResultToErrorCode(result); |
| 105 | } |
| 106 | |
Leon Scroggins III | 2e6bedf | 2020-02-11 16:31:21 -0500 | [diff] [blame] | 107 | // AImageDecoderHeaderInfo_getWidth/Height return an int32_t. Ensure that |
| 108 | // the conversion is safe. |
Leon Scroggins III | 139145b | 2020-12-17 15:43:54 -0500 | [diff] [blame] | 109 | if (dimensions.width() > std::numeric_limits<int32_t>::max() || |
| 110 | dimensions.height() > std::numeric_limits<int32_t>::max()) { |
Leon Scroggins III | 2e6bedf | 2020-02-11 16:31:21 -0500 | [diff] [blame] | 111 | return ANDROID_IMAGE_DECODER_INVALID_INPUT; |
| 112 | } |
| 113 | |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 114 | *outDecoder = reinterpret_cast<AImageDecoder*>(new ImageDecoder(std::move(androidCodec))); |
| 115 | return ANDROID_IMAGE_DECODER_SUCCESS; |
| 116 | } |
| 117 | |
| 118 | int AImageDecoder_createFromAAsset(AAsset* asset, AImageDecoder** outDecoder) { |
| 119 | if (!asset || !outDecoder) { |
| 120 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 121 | } |
| 122 | *outDecoder = nullptr; |
| 123 | |
Leon Scroggins III | c72d0fb | 2020-12-30 16:38:23 -0500 | [diff] [blame^] | 124 | #ifdef __ANDROID__ |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 125 | auto stream = std::make_unique<AAssetStreamAdaptor>(asset); |
| 126 | return createFromStream(std::move(stream), outDecoder); |
Leon Scroggins III | c72d0fb | 2020-12-30 16:38:23 -0500 | [diff] [blame^] | 127 | #else |
| 128 | return ANDROID_IMAGE_DECODER_INTERNAL_ERROR; |
| 129 | #endif |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static bool isSeekable(int descriptor) { |
| 133 | return ::lseek64(descriptor, 0, SEEK_CUR) != -1; |
| 134 | } |
| 135 | |
| 136 | int 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 | |
| 163 | int 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 | |
| 177 | static ImageDecoder* toDecoder(AImageDecoder* d) { |
| 178 | return reinterpret_cast<ImageDecoder*>(d); |
| 179 | } |
| 180 | |
Leon Scroggins III | f89de63 | 2020-01-19 21:22:18 -0500 | [diff] [blame] | 181 | static const ImageDecoder* toDecoder(const AImageDecoder* d) { |
| 182 | return reinterpret_cast<const ImageDecoder*>(d); |
| 183 | } |
| 184 | |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 185 | // 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. |
| 189 | static 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; |
| 201 | default: |
| 202 | return kUnknown_SkColorType; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | int AImageDecoder_setAndroidBitmapFormat(AImageDecoder* decoder, int32_t format) { |
| 207 | if (!decoder || format < ANDROID_BITMAP_FORMAT_NONE |
| 208 | || format > ANDROID_BITMAP_FORMAT_RGBA_F16) { |
| 209 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 210 | } |
Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 211 | |
| 212 | auto* imageDecoder = toDecoder(decoder); |
| 213 | if (imageDecoder->currentFrame() != 0) { |
| 214 | return ANDROID_IMAGE_DECODER_INVALID_STATE; |
| 215 | } |
| 216 | |
| 217 | return imageDecoder->setOutColorType(getColorType((AndroidBitmapFormat) format)) |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 218 | ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_INVALID_CONVERSION; |
| 219 | } |
| 220 | |
Leon Scroggins III | e5ace3f | 2020-01-15 15:31:40 -0500 | [diff] [blame] | 221 | int AImageDecoder_setDataSpace(AImageDecoder* decoder, int32_t dataspace) { |
| 222 | sk_sp<SkColorSpace> cs = uirenderer::DataSpaceToColorSpace((android_dataspace)dataspace); |
| 223 | // 0 is ADATASPACE_UNKNOWN. We need an explicit request for an ADataSpace. |
| 224 | if (!decoder || !dataspace || !cs) { |
| 225 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 226 | } |
| 227 | |
| 228 | ImageDecoder* imageDecoder = toDecoder(decoder); |
Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 229 | if (imageDecoder->currentFrame() != 0) { |
| 230 | return ANDROID_IMAGE_DECODER_INVALID_STATE; |
| 231 | } |
| 232 | |
Leon Scroggins III | e5ace3f | 2020-01-15 15:31:40 -0500 | [diff] [blame] | 233 | imageDecoder->setOutColorSpace(std::move(cs)); |
| 234 | return ANDROID_IMAGE_DECODER_SUCCESS; |
| 235 | } |
| 236 | |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 237 | const AImageDecoderHeaderInfo* AImageDecoder_getHeaderInfo(const AImageDecoder* decoder) { |
| 238 | return reinterpret_cast<const AImageDecoderHeaderInfo*>(decoder); |
| 239 | } |
| 240 | |
| 241 | static const ImageDecoder* toDecoder(const AImageDecoderHeaderInfo* info) { |
| 242 | return reinterpret_cast<const ImageDecoder*>(info); |
| 243 | } |
| 244 | |
| 245 | int32_t AImageDecoderHeaderInfo_getWidth(const AImageDecoderHeaderInfo* info) { |
| 246 | if (!info) { |
| 247 | return 0; |
| 248 | } |
Leon Scroggins III | 139145b | 2020-12-17 15:43:54 -0500 | [diff] [blame] | 249 | return toDecoder(info)->width(); |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | int32_t AImageDecoderHeaderInfo_getHeight(const AImageDecoderHeaderInfo* info) { |
| 253 | if (!info) { |
| 254 | return 0; |
| 255 | } |
Leon Scroggins III | 139145b | 2020-12-17 15:43:54 -0500 | [diff] [blame] | 256 | return toDecoder(info)->height(); |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | const char* AImageDecoderHeaderInfo_getMimeType(const AImageDecoderHeaderInfo* info) { |
| 260 | if (!info) { |
| 261 | return nullptr; |
| 262 | } |
| 263 | return getMimeType(toDecoder(info)->mCodec->getEncodedFormat()); |
| 264 | } |
| 265 | |
Leon Scroggins III | e5ace3f | 2020-01-15 15:31:40 -0500 | [diff] [blame] | 266 | int32_t AImageDecoderHeaderInfo_getDataSpace(const AImageDecoderHeaderInfo* info) { |
| 267 | if (!info) { |
| 268 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 269 | } |
| 270 | |
Leon Scroggins III | 6eeca5c | 2020-01-30 13:59:50 -0500 | [diff] [blame] | 271 | // Note: This recomputes the color type because it's possible the client has |
| 272 | // 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] | 273 | // we could store the ADataSpace in the ImageDecoder. |
| 274 | const ImageDecoder* imageDecoder = toDecoder(info); |
| 275 | SkColorType colorType = imageDecoder->mCodec->computeOutputColorType(kN32_SkColorType); |
Leon Scroggins III | 6eeca5c | 2020-01-30 13:59:50 -0500 | [diff] [blame] | 276 | sk_sp<SkColorSpace> colorSpace = imageDecoder->getDefaultColorSpace(); |
Leon Scroggins III | e5ace3f | 2020-01-15 15:31:40 -0500 | [diff] [blame] | 277 | return uirenderer::ColorSpaceToADataSpace(colorSpace.get(), colorType); |
| 278 | } |
| 279 | |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 280 | // FIXME: Share with getFormat in android_bitmap.cpp? |
| 281 | static AndroidBitmapFormat getFormat(SkColorType colorType) { |
| 282 | switch (colorType) { |
| 283 | case kN32_SkColorType: |
| 284 | return ANDROID_BITMAP_FORMAT_RGBA_8888; |
| 285 | case kRGB_565_SkColorType: |
| 286 | return ANDROID_BITMAP_FORMAT_RGB_565; |
| 287 | case kARGB_4444_SkColorType: |
| 288 | return ANDROID_BITMAP_FORMAT_RGBA_4444; |
| 289 | case kAlpha_8_SkColorType: |
| 290 | return ANDROID_BITMAP_FORMAT_A_8; |
| 291 | case kRGBA_F16_SkColorType: |
| 292 | return ANDROID_BITMAP_FORMAT_RGBA_F16; |
| 293 | default: |
| 294 | return ANDROID_BITMAP_FORMAT_NONE; |
| 295 | } |
| 296 | } |
| 297 | |
Leon Scroggins III | 64301cb | 2020-01-23 09:47:47 -0500 | [diff] [blame] | 298 | int32_t AImageDecoderHeaderInfo_getAndroidBitmapFormat(const AImageDecoderHeaderInfo* info) { |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 299 | if (!info) { |
| 300 | return ANDROID_BITMAP_FORMAT_NONE; |
| 301 | } |
| 302 | return getFormat(toDecoder(info)->mCodec->computeOutputColorType(kN32_SkColorType)); |
| 303 | } |
| 304 | |
| 305 | int AImageDecoderHeaderInfo_getAlphaFlags(const AImageDecoderHeaderInfo* info) { |
| 306 | if (!info) { |
Leon Scroggins III | d8840bd | 2020-01-15 04:09:48 -0500 | [diff] [blame] | 307 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 308 | } |
| 309 | switch (toDecoder(info)->mCodec->getInfo().alphaType()) { |
| 310 | case kUnknown_SkAlphaType: |
| 311 | LOG_ALWAYS_FATAL("Invalid alpha type"); |
Leon Scroggins III | d8840bd | 2020-01-15 04:09:48 -0500 | [diff] [blame] | 312 | return ANDROID_IMAGE_DECODER_INTERNAL_ERROR; |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 313 | case kUnpremul_SkAlphaType: |
| 314 | // fall through. premul is the default. |
| 315 | case kPremul_SkAlphaType: |
| 316 | return ANDROID_BITMAP_FLAGS_ALPHA_PREMUL; |
| 317 | case kOpaque_SkAlphaType: |
| 318 | return ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE; |
| 319 | } |
| 320 | } |
| 321 | |
Leon Scroggins III | 1ade46d | 2020-01-15 05:41:06 -0500 | [diff] [blame] | 322 | int AImageDecoder_setUnpremultipliedRequired(AImageDecoder* decoder, bool required) { |
| 323 | if (!decoder) { |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 324 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 325 | } |
| 326 | |
Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 327 | auto* imageDecoder = toDecoder(decoder); |
| 328 | if (imageDecoder->currentFrame() != 0) { |
| 329 | return ANDROID_IMAGE_DECODER_INVALID_STATE; |
| 330 | } |
| 331 | |
| 332 | return imageDecoder->setUnpremultipliedRequired(required) |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 333 | ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_INVALID_CONVERSION; |
| 334 | } |
| 335 | |
Leon Scroggins III | 64301cb | 2020-01-23 09:47:47 -0500 | [diff] [blame] | 336 | int AImageDecoder_setTargetSize(AImageDecoder* decoder, int32_t width, int32_t height) { |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 337 | if (!decoder) { |
| 338 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 339 | } |
| 340 | |
Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 341 | auto* imageDecoder = toDecoder(decoder); |
| 342 | if (imageDecoder->currentFrame() != 0) { |
| 343 | return ANDROID_IMAGE_DECODER_INVALID_STATE; |
| 344 | } |
| 345 | |
| 346 | return imageDecoder->setTargetSize(width, height) |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 347 | ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_INVALID_SCALE; |
| 348 | } |
| 349 | |
Leon Scroggins III | f89de63 | 2020-01-19 21:22:18 -0500 | [diff] [blame] | 350 | int AImageDecoder_computeSampledSize(const AImageDecoder* decoder, int sampleSize, |
Leon Scroggins III | 64301cb | 2020-01-23 09:47:47 -0500 | [diff] [blame] | 351 | int32_t* width, int32_t* height) { |
Leon Scroggins III | f89de63 | 2020-01-19 21:22:18 -0500 | [diff] [blame] | 352 | if (!decoder || !width || !height || sampleSize < 1) { |
| 353 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 354 | } |
| 355 | |
| 356 | SkISize size = toDecoder(decoder)->mCodec->getSampledDimensions(sampleSize); |
| 357 | *width = size.width(); |
| 358 | *height = size.height(); |
| 359 | return ANDROID_IMAGE_DECODER_SUCCESS; |
| 360 | } |
| 361 | |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 362 | int AImageDecoder_setCrop(AImageDecoder* decoder, ARect crop) { |
| 363 | if (!decoder) { |
| 364 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 365 | } |
| 366 | |
Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 367 | auto* imageDecoder = toDecoder(decoder); |
| 368 | if (imageDecoder->currentFrame() != 0) { |
| 369 | return ANDROID_IMAGE_DECODER_INVALID_STATE; |
| 370 | } |
| 371 | |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 372 | SkIRect cropIRect; |
| 373 | cropIRect.setLTRB(crop.left, crop.top, crop.right, crop.bottom); |
| 374 | SkIRect* cropPtr = cropIRect == SkIRect::MakeEmpty() ? nullptr : &cropIRect; |
Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 375 | return imageDecoder->setCropRect(cropPtr) |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 376 | ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 377 | } |
| 378 | |
| 379 | |
| 380 | size_t AImageDecoder_getMinimumStride(AImageDecoder* decoder) { |
| 381 | if (!decoder) { |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | SkImageInfo info = toDecoder(decoder)->getOutputInfo(); |
| 386 | return info.minRowBytes(); |
| 387 | } |
| 388 | |
| 389 | int AImageDecoder_decodeImage(AImageDecoder* decoder, |
| 390 | void* pixels, size_t stride, |
| 391 | size_t size) { |
| 392 | if (!decoder || !pixels || !stride) { |
| 393 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 394 | } |
| 395 | |
| 396 | ImageDecoder* imageDecoder = toDecoder(decoder); |
| 397 | |
Leon Scroggins III | d894c59 | 2020-01-22 14:18:12 -0500 | [diff] [blame] | 398 | SkImageInfo info = imageDecoder->getOutputInfo(); |
| 399 | size_t minSize = info.computeByteSize(stride); |
| 400 | if (SkImageInfo::ByteSizeOverflowed(minSize) || size < minSize || !info.validRowBytes(stride)) { |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 401 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 402 | } |
| 403 | |
Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 404 | if (imageDecoder->finished()) { |
| 405 | return ANDROID_IMAGE_DECODER_FINISHED; |
| 406 | } |
| 407 | |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 408 | return ResultToErrorCode(imageDecoder->decode(pixels, stride)); |
| 409 | } |
| 410 | |
| 411 | void AImageDecoder_delete(AImageDecoder* decoder) { |
| 412 | delete toDecoder(decoder); |
| 413 | } |
Leon Scroggins III | 24ae7d7 | 2020-10-09 13:14:35 -0400 | [diff] [blame] | 414 | |
| 415 | bool AImageDecoder_isAnimated(AImageDecoder* decoder) { |
| 416 | if (!decoder) return false; |
| 417 | |
| 418 | ImageDecoder* imageDecoder = toDecoder(decoder); |
Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 419 | return imageDecoder->isAnimated(); |
Leon Scroggins III | 24ae7d7 | 2020-10-09 13:14:35 -0400 | [diff] [blame] | 420 | } |
Leon Scroggins III | 3ad12c4 | 2020-10-12 10:56:42 -0400 | [diff] [blame] | 421 | |
| 422 | int32_t AImageDecoder_getRepeatCount(AImageDecoder* decoder) { |
| 423 | if (!decoder) return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 424 | |
| 425 | ImageDecoder* imageDecoder = toDecoder(decoder); |
| 426 | const int count = imageDecoder->mCodec->codec()->getRepetitionCount(); |
| 427 | |
| 428 | // Skia should not report anything out of range, but defensively treat |
| 429 | // negative and too big as INFINITE. |
| 430 | if (count == SkCodec::kRepetitionCountInfinite || count < 0 |
| 431 | || count > std::numeric_limits<int32_t>::max()) { |
| 432 | return ANDROID_IMAGE_DECODER_INFINITE; |
| 433 | } |
| 434 | return count; |
| 435 | } |
Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 436 | |
| 437 | int AImageDecoder_advanceFrame(AImageDecoder* decoder) { |
| 438 | if (!decoder) return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 439 | |
| 440 | ImageDecoder* imageDecoder = toDecoder(decoder); |
| 441 | if (!imageDecoder->isAnimated()) { |
| 442 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 443 | } |
| 444 | |
| 445 | if (imageDecoder->advanceFrame()) { |
| 446 | return ANDROID_IMAGE_DECODER_SUCCESS; |
| 447 | } |
| 448 | |
| 449 | if (imageDecoder->finished()) { |
| 450 | return ANDROID_IMAGE_DECODER_FINISHED; |
| 451 | } |
| 452 | |
| 453 | return ANDROID_IMAGE_DECODER_INCOMPLETE; |
| 454 | } |
| 455 | |
| 456 | int AImageDecoder_rewind(AImageDecoder* decoder) { |
| 457 | if (!decoder) return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 458 | |
| 459 | ImageDecoder* imageDecoder = toDecoder(decoder); |
| 460 | if (!imageDecoder->isAnimated()) { |
| 461 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 462 | } |
| 463 | |
| 464 | return imageDecoder->rewind() ? ANDROID_IMAGE_DECODER_SUCCESS |
| 465 | : ANDROID_IMAGE_DECODER_SEEK_ERROR; |
| 466 | } |
Leon Scroggins III | 0621313 | 2020-12-28 15:31:48 -0500 | [diff] [blame] | 467 | |
| 468 | AImageDecoderFrameInfo* AImageDecoderFrameInfo_create() { |
| 469 | return reinterpret_cast<AImageDecoderFrameInfo*>(new SkCodec::FrameInfo); |
| 470 | } |
| 471 | |
| 472 | static SkCodec::FrameInfo* toFrameInfo(AImageDecoderFrameInfo* info) { |
| 473 | return reinterpret_cast<SkCodec::FrameInfo*>(info); |
| 474 | } |
| 475 | |
| 476 | static const SkCodec::FrameInfo* toFrameInfo(const AImageDecoderFrameInfo* info) { |
| 477 | return reinterpret_cast<const SkCodec::FrameInfo*>(info); |
| 478 | } |
| 479 | |
| 480 | void AImageDecoderFrameInfo_delete(AImageDecoderFrameInfo* info) { |
| 481 | delete toFrameInfo(info); |
| 482 | } |
| 483 | |
| 484 | int AImageDecoder_getFrameInfo(AImageDecoder* decoder, |
| 485 | AImageDecoderFrameInfo* info) { |
| 486 | if (!decoder || !info) { |
| 487 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 488 | } |
| 489 | |
| 490 | auto* imageDecoder = toDecoder(decoder); |
| 491 | if (imageDecoder->finished()) { |
| 492 | return ANDROID_IMAGE_DECODER_FINISHED; |
| 493 | } |
| 494 | |
| 495 | *toFrameInfo(info) = imageDecoder->getCurrentFrameInfo(); |
| 496 | return ANDROID_IMAGE_DECODER_SUCCESS; |
| 497 | } |
| 498 | |
| 499 | int64_t AImageDecoderFrameInfo_getDuration(const AImageDecoderFrameInfo* info) { |
| 500 | if (!info) return 0; |
| 501 | |
| 502 | return toFrameInfo(info)->fDuration * 1'000'000; |
| 503 | } |
| 504 | |
| 505 | ARect AImageDecoderFrameInfo_getFrameRect(const AImageDecoderFrameInfo* info) { |
| 506 | if (!info) { |
| 507 | return { 0, 0, 0, 0}; |
| 508 | } |
| 509 | |
| 510 | const SkIRect& r = toFrameInfo(info)->fFrameRect; |
| 511 | return { r.left(), r.top(), r.right(), r.bottom() }; |
| 512 | } |
| 513 | |
| 514 | bool AImageDecoderFrameInfo_hasAlphaWithinBounds(const AImageDecoderFrameInfo* info) { |
| 515 | if (!info) return false; |
| 516 | |
| 517 | return toFrameInfo(info)->fHasAlphaWithinBounds; |
| 518 | } |
| 519 | |
| 520 | int32_t AImageDecoderFrameInfo_getDisposeOp(const AImageDecoderFrameInfo* info) { |
| 521 | if (!info) return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 522 | |
| 523 | static_assert(static_cast<int>(SkCodecAnimation::DisposalMethod::kKeep) |
| 524 | == ANDROID_IMAGE_DECODER_DISPOSE_OP_NONE); |
| 525 | static_assert(static_cast<int>(SkCodecAnimation::DisposalMethod::kRestoreBGColor) |
| 526 | == ANDROID_IMAGE_DECODER_DISPOSE_OP_BACKGROUND); |
| 527 | static_assert(static_cast<int>(SkCodecAnimation::DisposalMethod::kRestorePrevious) |
| 528 | == ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS); |
| 529 | return static_cast<int>(toFrameInfo(info)->fDisposalMethod); |
| 530 | } |
| 531 | |
| 532 | int32_t AImageDecoderFrameInfo_getBlendOp(const AImageDecoderFrameInfo* info) { |
| 533 | if (!info) return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 534 | |
| 535 | switch (toFrameInfo(info)->fBlend) { |
| 536 | case SkCodecAnimation::Blend::kSrc: |
| 537 | return ANDROID_IMAGE_DECODER_BLEND_OP_SRC; |
| 538 | case SkCodecAnimation::Blend::kSrcOver: |
| 539 | return ANDROID_IMAGE_DECODER_BLEND_OP_SRC_OVER; |
| 540 | } |
| 541 | } |
Leon Scroggins III | c2ebc2b | 2021-01-13 09:46:06 -0500 | [diff] [blame] | 542 | |
| 543 | void AImageDecoder_setInternallyHandleDisposePrevious(AImageDecoder* decoder, bool handle) { |
| 544 | if (decoder) { |
| 545 | toDecoder(decoder)->setHandleRestorePrevious(handle); |
| 546 | } |
| 547 | } |