blob: 1c45ea6aaecc76199716646b9d972b190bf25e1e [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
Leon Scroggins IIIf89de632020-01-19 21:22:18 -0500136static const ImageDecoder* toDecoder(const AImageDecoder* d) {
137 return reinterpret_cast<const ImageDecoder*>(d);
138}
139
Leon Scroggins III407b5442019-11-22 17:10:20 -0500140// Note: This differs from the version in android_bitmap.cpp in that this
141// version returns kGray_8_SkColorType for ANDROID_BITMAP_FORMAT_A_8. SkCodec
142// allows decoding single channel images to gray, which Android then treats
143// as A_8/ALPHA_8.
144static SkColorType getColorType(AndroidBitmapFormat format) {
145 switch (format) {
146 case ANDROID_BITMAP_FORMAT_RGBA_8888:
147 return kN32_SkColorType;
148 case ANDROID_BITMAP_FORMAT_RGB_565:
149 return kRGB_565_SkColorType;
150 case ANDROID_BITMAP_FORMAT_RGBA_4444:
151 return kARGB_4444_SkColorType;
152 case ANDROID_BITMAP_FORMAT_A_8:
153 return kGray_8_SkColorType;
154 case ANDROID_BITMAP_FORMAT_RGBA_F16:
155 return kRGBA_F16_SkColorType;
156 default:
157 return kUnknown_SkColorType;
158 }
159}
160
161int AImageDecoder_setAndroidBitmapFormat(AImageDecoder* decoder, int32_t format) {
162 if (!decoder || format < ANDROID_BITMAP_FORMAT_NONE
163 || format > ANDROID_BITMAP_FORMAT_RGBA_F16) {
164 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
165 }
166 return toDecoder(decoder)->setOutColorType(getColorType((AndroidBitmapFormat) format))
167 ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_INVALID_CONVERSION;
168}
169
Leon Scroggins IIIe5ace3f2020-01-15 15:31:40 -0500170int AImageDecoder_setDataSpace(AImageDecoder* decoder, int32_t dataspace) {
171 sk_sp<SkColorSpace> cs = uirenderer::DataSpaceToColorSpace((android_dataspace)dataspace);
172 // 0 is ADATASPACE_UNKNOWN. We need an explicit request for an ADataSpace.
173 if (!decoder || !dataspace || !cs) {
174 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
175 }
176
177 ImageDecoder* imageDecoder = toDecoder(decoder);
178 imageDecoder->setOutColorSpace(std::move(cs));
179 return ANDROID_IMAGE_DECODER_SUCCESS;
180}
181
Leon Scroggins III407b5442019-11-22 17:10:20 -0500182const AImageDecoderHeaderInfo* AImageDecoder_getHeaderInfo(const AImageDecoder* decoder) {
183 return reinterpret_cast<const AImageDecoderHeaderInfo*>(decoder);
184}
185
186static const ImageDecoder* toDecoder(const AImageDecoderHeaderInfo* info) {
187 return reinterpret_cast<const ImageDecoder*>(info);
188}
189
190int32_t AImageDecoderHeaderInfo_getWidth(const AImageDecoderHeaderInfo* info) {
191 if (!info) {
192 return 0;
193 }
194 return toDecoder(info)->mCodec->getInfo().width();
195}
196
197int32_t AImageDecoderHeaderInfo_getHeight(const AImageDecoderHeaderInfo* info) {
198 if (!info) {
199 return 0;
200 }
201 return toDecoder(info)->mCodec->getInfo().height();
202}
203
204const char* AImageDecoderHeaderInfo_getMimeType(const AImageDecoderHeaderInfo* info) {
205 if (!info) {
206 return nullptr;
207 }
208 return getMimeType(toDecoder(info)->mCodec->getEncodedFormat());
209}
210
211bool AImageDecoderHeaderInfo_isAnimated(const AImageDecoderHeaderInfo* info) {
212 if (!info) {
213 return false;
214 }
215 return toDecoder(info)->mCodec->codec()->getFrameCount() > 1;
216}
217
Leon Scroggins IIIe5ace3f2020-01-15 15:31:40 -0500218int32_t AImageDecoderHeaderInfo_getDataSpace(const AImageDecoderHeaderInfo* info) {
219 if (!info) {
220 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
221 }
222
223 // Note: This recomputes the data space because it's possible the client has
224 // changed the output color space, so we cannot rely on it. Alternatively,
225 // we could store the ADataSpace in the ImageDecoder.
226 const ImageDecoder* imageDecoder = toDecoder(info);
227 SkColorType colorType = imageDecoder->mCodec->computeOutputColorType(kN32_SkColorType);
228 sk_sp<SkColorSpace> colorSpace = imageDecoder->mCodec->computeOutputColorSpace(colorType);
229 return uirenderer::ColorSpaceToADataSpace(colorSpace.get(), colorType);
230}
231
Leon Scroggins III407b5442019-11-22 17:10:20 -0500232// FIXME: Share with getFormat in android_bitmap.cpp?
233static AndroidBitmapFormat getFormat(SkColorType colorType) {
234 switch (colorType) {
235 case kN32_SkColorType:
236 return ANDROID_BITMAP_FORMAT_RGBA_8888;
237 case kRGB_565_SkColorType:
238 return ANDROID_BITMAP_FORMAT_RGB_565;
239 case kARGB_4444_SkColorType:
240 return ANDROID_BITMAP_FORMAT_RGBA_4444;
241 case kAlpha_8_SkColorType:
242 return ANDROID_BITMAP_FORMAT_A_8;
243 case kRGBA_F16_SkColorType:
244 return ANDROID_BITMAP_FORMAT_RGBA_F16;
245 default:
246 return ANDROID_BITMAP_FORMAT_NONE;
247 }
248}
249
250AndroidBitmapFormat AImageDecoderHeaderInfo_getAndroidBitmapFormat(
251 const AImageDecoderHeaderInfo* info) {
252 if (!info) {
253 return ANDROID_BITMAP_FORMAT_NONE;
254 }
255 return getFormat(toDecoder(info)->mCodec->computeOutputColorType(kN32_SkColorType));
256}
257
258int AImageDecoderHeaderInfo_getAlphaFlags(const AImageDecoderHeaderInfo* info) {
259 if (!info) {
Leon Scroggins IIId8840bd2020-01-15 04:09:48 -0500260 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
Leon Scroggins III407b5442019-11-22 17:10:20 -0500261 }
262 switch (toDecoder(info)->mCodec->getInfo().alphaType()) {
263 case kUnknown_SkAlphaType:
264 LOG_ALWAYS_FATAL("Invalid alpha type");
Leon Scroggins IIId8840bd2020-01-15 04:09:48 -0500265 return ANDROID_IMAGE_DECODER_INTERNAL_ERROR;
Leon Scroggins III407b5442019-11-22 17:10:20 -0500266 case kUnpremul_SkAlphaType:
267 // fall through. premul is the default.
268 case kPremul_SkAlphaType:
269 return ANDROID_BITMAP_FLAGS_ALPHA_PREMUL;
270 case kOpaque_SkAlphaType:
271 return ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE;
272 }
273}
274
Leon Scroggins III1ade46d2020-01-15 05:41:06 -0500275int AImageDecoder_setUnpremultipliedRequired(AImageDecoder* decoder, bool required) {
276 if (!decoder) {
Leon Scroggins III407b5442019-11-22 17:10:20 -0500277 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
278 }
279
Leon Scroggins III1ade46d2020-01-15 05:41:06 -0500280 return toDecoder(decoder)->setUnpremultipliedRequired(required)
Leon Scroggins III407b5442019-11-22 17:10:20 -0500281 ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_INVALID_CONVERSION;
282}
283
284int AImageDecoder_setTargetSize(AImageDecoder* decoder, int width, int height) {
285 if (!decoder) {
286 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
287 }
288
289 return toDecoder(decoder)->setTargetSize(width, height)
290 ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_INVALID_SCALE;
291}
292
Leon Scroggins IIIf89de632020-01-19 21:22:18 -0500293int AImageDecoder_computeSampledSize(const AImageDecoder* decoder, int sampleSize,
294 int* width, int* height) {
295 if (!decoder || !width || !height || sampleSize < 1) {
296 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
297 }
298
299 SkISize size = toDecoder(decoder)->mCodec->getSampledDimensions(sampleSize);
300 *width = size.width();
301 *height = size.height();
302 return ANDROID_IMAGE_DECODER_SUCCESS;
303}
304
Leon Scroggins III407b5442019-11-22 17:10:20 -0500305int AImageDecoder_setCrop(AImageDecoder* decoder, ARect crop) {
306 if (!decoder) {
307 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
308 }
309
310 SkIRect cropIRect;
311 cropIRect.setLTRB(crop.left, crop.top, crop.right, crop.bottom);
312 SkIRect* cropPtr = cropIRect == SkIRect::MakeEmpty() ? nullptr : &cropIRect;
313 return toDecoder(decoder)->setCropRect(cropPtr)
314 ? ANDROID_IMAGE_DECODER_SUCCESS : ANDROID_IMAGE_DECODER_BAD_PARAMETER;
315}
316
317
318size_t AImageDecoder_getMinimumStride(AImageDecoder* decoder) {
319 if (!decoder) {
320 return 0;
321 }
322
323 SkImageInfo info = toDecoder(decoder)->getOutputInfo();
324 return info.minRowBytes();
325}
326
327int AImageDecoder_decodeImage(AImageDecoder* decoder,
328 void* pixels, size_t stride,
329 size_t size) {
330 if (!decoder || !pixels || !stride) {
331 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
332 }
333
334 ImageDecoder* imageDecoder = toDecoder(decoder);
335
336 const int height = imageDecoder->getOutputInfo().height();
337 const size_t minStride = AImageDecoder_getMinimumStride(decoder);
338 // If this calculation were to overflow, it would have been caught in
339 // setTargetSize.
340 if (stride < minStride || size < stride * (height - 1) + minStride) {
341 return ANDROID_IMAGE_DECODER_BAD_PARAMETER;
342 }
343
344 return ResultToErrorCode(imageDecoder->decode(pixels, stride));
345}
346
347void AImageDecoder_delete(AImageDecoder* decoder) {
348 delete toDecoder(decoder);
349}