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