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> |
| 21 | #include <android/imagedecoder.h> |
| 22 | #include <android/graphics/MimeType.h> |
| 23 | #include <android/rect.h> |
| 24 | #include <hwui/ImageDecoder.h> |
| 25 | #include <log/log.h> |
| 26 | #include <SkAndroidCodec.h> |
| 27 | |
| 28 | #include <fcntl.h> |
| 29 | #include <optional> |
| 30 | #include <sys/stat.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <unistd.h> |
| 33 | |
| 34 | using namespace android; |
| 35 | |
| 36 | int ResultToErrorCode(SkCodec::Result result) { |
| 37 | switch (result) { |
| 38 | case SkCodec::kIncompleteInput: |
| 39 | return ANDROID_IMAGE_DECODER_INCOMPLETE; |
| 40 | case SkCodec::kErrorInInput: |
| 41 | return ANDROID_IMAGE_DECODER_ERROR; |
| 42 | case SkCodec::kInvalidInput: |
| 43 | return ANDROID_IMAGE_DECODER_INVALID_INPUT; |
| 44 | case SkCodec::kCouldNotRewind: |
| 45 | return ANDROID_IMAGE_DECODER_SEEK_ERROR; |
| 46 | case SkCodec::kUnimplemented: |
| 47 | return ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT; |
| 48 | case SkCodec::kInvalidConversion: |
| 49 | return ANDROID_IMAGE_DECODER_INVALID_CONVERSION; |
| 50 | case SkCodec::kInvalidParameters: |
| 51 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 52 | case SkCodec::kSuccess: |
| 53 | return ANDROID_IMAGE_DECODER_SUCCESS; |
| 54 | case SkCodec::kInvalidScale: |
| 55 | return ANDROID_IMAGE_DECODER_INVALID_SCALE; |
| 56 | case SkCodec::kInternalError: |
| 57 | return ANDROID_IMAGE_DECODER_INTERNAL_ERROR; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | static int createFromStream(std::unique_ptr<SkStreamRewindable> stream, AImageDecoder** outDecoder) { |
| 62 | SkCodec::Result result; |
| 63 | auto codec = SkCodec::MakeFromStream(std::move(stream), &result, nullptr, |
| 64 | SkCodec::SelectionPolicy::kPreferAnimation); |
| 65 | auto androidCodec = SkAndroidCodec::MakeFromCodec(std::move(codec), |
| 66 | SkAndroidCodec::ExifOrientationBehavior::kRespect); |
| 67 | if (!androidCodec) { |
| 68 | return ResultToErrorCode(result); |
| 69 | } |
| 70 | |
| 71 | *outDecoder = reinterpret_cast<AImageDecoder*>(new ImageDecoder(std::move(androidCodec))); |
| 72 | return ANDROID_IMAGE_DECODER_SUCCESS; |
| 73 | } |
| 74 | |
| 75 | int AImageDecoder_createFromAAsset(AAsset* asset, AImageDecoder** outDecoder) { |
| 76 | if (!asset || !outDecoder) { |
| 77 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 78 | } |
| 79 | *outDecoder = nullptr; |
| 80 | |
| 81 | auto stream = std::make_unique<AAssetStreamAdaptor>(asset); |
| 82 | return createFromStream(std::move(stream), outDecoder); |
| 83 | } |
| 84 | |
| 85 | static bool isSeekable(int descriptor) { |
| 86 | return ::lseek64(descriptor, 0, SEEK_CUR) != -1; |
| 87 | } |
| 88 | |
| 89 | int AImageDecoder_createFromFd(int fd, AImageDecoder** outDecoder) { |
| 90 | if (fd <= 0 || !outDecoder) { |
| 91 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 92 | } |
| 93 | |
| 94 | struct stat fdStat; |
| 95 | if (fstat(fd, &fdStat) == -1) { |
| 96 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 97 | } |
| 98 | |
| 99 | if (!isSeekable(fd)) { |
| 100 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 101 | } |
| 102 | |
| 103 | // SkFILEStream will close its descriptor. Duplicate it so the client will |
| 104 | // still be responsible for closing the original. |
| 105 | int dupDescriptor = fcntl(fd, F_DUPFD_CLOEXEC, 0); |
| 106 | FILE* file = fdopen(dupDescriptor, "r"); |
| 107 | if (!file) { |
| 108 | close(dupDescriptor); |
| 109 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 110 | } |
| 111 | |
| 112 | auto stream = std::unique_ptr<SkStreamRewindable>(new SkFILEStream(file)); |
| 113 | return createFromStream(std::move(stream), outDecoder); |
| 114 | } |
| 115 | |
| 116 | int AImageDecoder_createFromBuffer(const void* buffer, size_t length, |
| 117 | AImageDecoder** outDecoder) { |
| 118 | if (!buffer || !length || !outDecoder) { |
| 119 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 120 | } |
| 121 | *outDecoder = nullptr; |
| 122 | |
| 123 | // The client is expected to keep the buffer alive as long as the |
| 124 | // AImageDecoder, so we do not need to copy the buffer. |
| 125 | auto stream = std::unique_ptr<SkStreamRewindable>( |
| 126 | new SkMemoryStream(buffer, length, false /* copyData */)); |
| 127 | return createFromStream(std::move(stream), outDecoder); |
| 128 | } |
| 129 | |
| 130 | static ImageDecoder* toDecoder(AImageDecoder* d) { |
| 131 | return reinterpret_cast<ImageDecoder*>(d); |
| 132 | } |
| 133 | |
| 134 | // Note: This differs from the version in android_bitmap.cpp in that this |
| 135 | // version returns kGray_8_SkColorType for ANDROID_BITMAP_FORMAT_A_8. SkCodec |
| 136 | // allows decoding single channel images to gray, which Android then treats |
| 137 | // as A_8/ALPHA_8. |
| 138 | static SkColorType getColorType(AndroidBitmapFormat format) { |
| 139 | switch (format) { |
| 140 | case ANDROID_BITMAP_FORMAT_RGBA_8888: |
| 141 | return kN32_SkColorType; |
| 142 | case ANDROID_BITMAP_FORMAT_RGB_565: |
| 143 | return kRGB_565_SkColorType; |
| 144 | case ANDROID_BITMAP_FORMAT_RGBA_4444: |
| 145 | return kARGB_4444_SkColorType; |
| 146 | case ANDROID_BITMAP_FORMAT_A_8: |
| 147 | return kGray_8_SkColorType; |
| 148 | case ANDROID_BITMAP_FORMAT_RGBA_F16: |
| 149 | return kRGBA_F16_SkColorType; |
| 150 | default: |
| 151 | return kUnknown_SkColorType; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | int AImageDecoder_setAndroidBitmapFormat(AImageDecoder* decoder, int32_t format) { |
| 156 | if (!decoder || format < ANDROID_BITMAP_FORMAT_NONE |
| 157 | || format > ANDROID_BITMAP_FORMAT_RGBA_F16) { |
| 158 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 159 | } |
| 160 | return toDecoder(decoder)->setOutColorType(getColorType((AndroidBitmapFormat) format)) |
| 161 | ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_INVALID_CONVERSION; |
| 162 | } |
| 163 | |
| 164 | const AImageDecoderHeaderInfo* AImageDecoder_getHeaderInfo(const AImageDecoder* decoder) { |
| 165 | return reinterpret_cast<const AImageDecoderHeaderInfo*>(decoder); |
| 166 | } |
| 167 | |
| 168 | static const ImageDecoder* toDecoder(const AImageDecoderHeaderInfo* info) { |
| 169 | return reinterpret_cast<const ImageDecoder*>(info); |
| 170 | } |
| 171 | |
| 172 | int32_t AImageDecoderHeaderInfo_getWidth(const AImageDecoderHeaderInfo* info) { |
| 173 | if (!info) { |
| 174 | return 0; |
| 175 | } |
| 176 | return toDecoder(info)->mCodec->getInfo().width(); |
| 177 | } |
| 178 | |
| 179 | int32_t AImageDecoderHeaderInfo_getHeight(const AImageDecoderHeaderInfo* info) { |
| 180 | if (!info) { |
| 181 | return 0; |
| 182 | } |
| 183 | return toDecoder(info)->mCodec->getInfo().height(); |
| 184 | } |
| 185 | |
| 186 | const char* AImageDecoderHeaderInfo_getMimeType(const AImageDecoderHeaderInfo* info) { |
| 187 | if (!info) { |
| 188 | return nullptr; |
| 189 | } |
| 190 | return getMimeType(toDecoder(info)->mCodec->getEncodedFormat()); |
| 191 | } |
| 192 | |
| 193 | bool AImageDecoderHeaderInfo_isAnimated(const AImageDecoderHeaderInfo* info) { |
| 194 | if (!info) { |
| 195 | return false; |
| 196 | } |
| 197 | return toDecoder(info)->mCodec->codec()->getFrameCount() > 1; |
| 198 | } |
| 199 | |
| 200 | // FIXME: Share with getFormat in android_bitmap.cpp? |
| 201 | static AndroidBitmapFormat getFormat(SkColorType colorType) { |
| 202 | switch (colorType) { |
| 203 | case kN32_SkColorType: |
| 204 | return ANDROID_BITMAP_FORMAT_RGBA_8888; |
| 205 | case kRGB_565_SkColorType: |
| 206 | return ANDROID_BITMAP_FORMAT_RGB_565; |
| 207 | case kARGB_4444_SkColorType: |
| 208 | return ANDROID_BITMAP_FORMAT_RGBA_4444; |
| 209 | case kAlpha_8_SkColorType: |
| 210 | return ANDROID_BITMAP_FORMAT_A_8; |
| 211 | case kRGBA_F16_SkColorType: |
| 212 | return ANDROID_BITMAP_FORMAT_RGBA_F16; |
| 213 | default: |
| 214 | return ANDROID_BITMAP_FORMAT_NONE; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | AndroidBitmapFormat AImageDecoderHeaderInfo_getAndroidBitmapFormat( |
| 219 | const AImageDecoderHeaderInfo* info) { |
| 220 | if (!info) { |
| 221 | return ANDROID_BITMAP_FORMAT_NONE; |
| 222 | } |
| 223 | return getFormat(toDecoder(info)->mCodec->computeOutputColorType(kN32_SkColorType)); |
| 224 | } |
| 225 | |
| 226 | int AImageDecoderHeaderInfo_getAlphaFlags(const AImageDecoderHeaderInfo* info) { |
| 227 | if (!info) { |
| 228 | // FIXME: Better invalid? |
| 229 | return -1; |
| 230 | } |
| 231 | switch (toDecoder(info)->mCodec->getInfo().alphaType()) { |
| 232 | case kUnknown_SkAlphaType: |
| 233 | LOG_ALWAYS_FATAL("Invalid alpha type"); |
| 234 | return -1; |
| 235 | case kUnpremul_SkAlphaType: |
| 236 | // fall through. premul is the default. |
| 237 | case kPremul_SkAlphaType: |
| 238 | return ANDROID_BITMAP_FLAGS_ALPHA_PREMUL; |
| 239 | case kOpaque_SkAlphaType: |
| 240 | return ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | SkAlphaType toAlphaType(int androidBitmapFlags) { |
| 245 | switch (androidBitmapFlags) { |
| 246 | case ANDROID_BITMAP_FLAGS_ALPHA_PREMUL: |
| 247 | return kPremul_SkAlphaType; |
| 248 | case ANDROID_BITMAP_FLAGS_ALPHA_UNPREMUL: |
| 249 | return kUnpremul_SkAlphaType; |
| 250 | case ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE: |
| 251 | return kOpaque_SkAlphaType; |
| 252 | default: |
| 253 | return kUnknown_SkAlphaType; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | int AImageDecoder_setAlphaFlags(AImageDecoder* decoder, int alphaFlag) { |
| 258 | if (!decoder || alphaFlag < ANDROID_BITMAP_FLAGS_ALPHA_PREMUL |
| 259 | || alphaFlag > ANDROID_BITMAP_FLAGS_ALPHA_UNPREMUL) { |
| 260 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 261 | } |
| 262 | |
| 263 | return toDecoder(decoder)->setOutAlphaType(toAlphaType(alphaFlag)) |
| 264 | ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_INVALID_CONVERSION; |
| 265 | } |
| 266 | |
| 267 | int AImageDecoder_setTargetSize(AImageDecoder* decoder, int width, int height) { |
| 268 | if (!decoder) { |
| 269 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 270 | } |
| 271 | |
| 272 | return toDecoder(decoder)->setTargetSize(width, height) |
| 273 | ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_INVALID_SCALE; |
| 274 | } |
| 275 | |
| 276 | int AImageDecoder_setCrop(AImageDecoder* decoder, ARect crop) { |
| 277 | if (!decoder) { |
| 278 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 279 | } |
| 280 | |
| 281 | SkIRect cropIRect; |
| 282 | cropIRect.setLTRB(crop.left, crop.top, crop.right, crop.bottom); |
| 283 | SkIRect* cropPtr = cropIRect == SkIRect::MakeEmpty() ? nullptr : &cropIRect; |
| 284 | return toDecoder(decoder)->setCropRect(cropPtr) |
| 285 | ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 286 | } |
| 287 | |
| 288 | |
| 289 | size_t AImageDecoder_getMinimumStride(AImageDecoder* decoder) { |
| 290 | if (!decoder) { |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | SkImageInfo info = toDecoder(decoder)->getOutputInfo(); |
| 295 | return info.minRowBytes(); |
| 296 | } |
| 297 | |
| 298 | int AImageDecoder_decodeImage(AImageDecoder* decoder, |
| 299 | void* pixels, size_t stride, |
| 300 | size_t size) { |
| 301 | if (!decoder || !pixels || !stride) { |
| 302 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 303 | } |
| 304 | |
| 305 | ImageDecoder* imageDecoder = toDecoder(decoder); |
| 306 | |
| 307 | const int height = imageDecoder->getOutputInfo().height(); |
| 308 | const size_t minStride = AImageDecoder_getMinimumStride(decoder); |
| 309 | // If this calculation were to overflow, it would have been caught in |
| 310 | // setTargetSize. |
| 311 | if (stride < minStride || size < stride * (height - 1) + minStride) { |
| 312 | return ANDROID_IMAGE_DECODER_BAD_PARAMETER; |
| 313 | } |
| 314 | |
| 315 | return ResultToErrorCode(imageDecoder->decode(pixels, stride)); |
| 316 | } |
| 317 | |
| 318 | void AImageDecoder_delete(AImageDecoder* decoder) { |
| 319 | delete toDecoder(decoder); |
| 320 | } |