blob: c80a9b4ae97ff0b20b3990d672d46b7b035d5696 [file] [log] [blame]
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -04001/*
2 * Copyright (C) 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
Derek Sollenberger6c41ab12019-11-08 08:50:58 -050017#include <log/log.h>
18
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040019#include "android/graphics/bitmap.h"
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040020#include "TypeCast.h"
Derek Sollenberger213daca2019-10-25 14:17:32 -040021#include "GraphicsJNI.h"
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040022
Derek Sollenberger6c41ab12019-11-08 08:50:58 -050023#include <GraphicsJNI.h>
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040024#include <hwui/Bitmap.h>
Kevin Lubick1175dc02022-02-28 12:41:27 -050025#include <SkBitmap.h>
26#include <SkColorSpace.h>
27#include <SkImageInfo.h>
28#include <SkRefCnt.h>
29#include <SkStream.h>
Leon Scroggins III9010e8b2019-08-20 11:27:17 -040030#include <utils/Color.h>
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040031
32using namespace android;
33
34ABitmap* ABitmap_acquireBitmapFromJava(JNIEnv* env, jobject bitmapObj) {
Derek Sollenberger6c41ab12019-11-08 08:50:58 -050035 Bitmap* bitmap = GraphicsJNI::getNativeBitmap(env, bitmapObj);
36 if (bitmap) {
37 bitmap->ref();
38 return TypeCast::toABitmap(bitmap);
39 }
40 return nullptr;
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040041}
42
43void ABitmap_acquireRef(ABitmap* bitmap) {
44 SkSafeRef(TypeCast::toBitmap(bitmap));
45}
46
47void ABitmap_releaseRef(ABitmap* bitmap) {
48 SkSafeUnref(TypeCast::toBitmap(bitmap));
49}
50
Derek Sollenberger6c41ab12019-11-08 08:50:58 -050051static AndroidBitmapFormat getFormat(const SkImageInfo& info) {
52 switch (info.colorType()) {
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040053 case kN32_SkColorType:
54 return ANDROID_BITMAP_FORMAT_RGBA_8888;
55 case kRGB_565_SkColorType:
56 return ANDROID_BITMAP_FORMAT_RGB_565;
57 case kARGB_4444_SkColorType:
58 return ANDROID_BITMAP_FORMAT_RGBA_4444;
59 case kAlpha_8_SkColorType:
60 return ANDROID_BITMAP_FORMAT_A_8;
61 case kRGBA_F16_SkColorType:
62 return ANDROID_BITMAP_FORMAT_RGBA_F16;
Alec Mouri1efd0a52022-01-20 13:58:23 -080063 case kRGBA_1010102_SkColorType:
64 return ANDROID_BITMAP_FORMAT_RGBA_1010102;
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040065 default:
66 return ANDROID_BITMAP_FORMAT_NONE;
67 }
68}
69
70static SkColorType getColorType(AndroidBitmapFormat format) {
71 switch (format) {
72 case ANDROID_BITMAP_FORMAT_RGBA_8888:
73 return kN32_SkColorType;
74 case ANDROID_BITMAP_FORMAT_RGB_565:
75 return kRGB_565_SkColorType;
76 case ANDROID_BITMAP_FORMAT_RGBA_4444:
77 return kARGB_4444_SkColorType;
78 case ANDROID_BITMAP_FORMAT_A_8:
79 return kAlpha_8_SkColorType;
80 case ANDROID_BITMAP_FORMAT_RGBA_F16:
81 return kRGBA_F16_SkColorType;
Alec Mouri1efd0a52022-01-20 13:58:23 -080082 case ANDROID_BITMAP_FORMAT_RGBA_1010102:
83 return kRGBA_1010102_SkColorType;
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040084 default:
85 return kUnknown_SkColorType;
86 }
87}
88
Leon Scroggins III84a2afc2020-01-19 19:27:16 -050089static uint32_t getAlphaFlags(const SkImageInfo& info) {
Derek Sollenberger6c41ab12019-11-08 08:50:58 -050090 switch (info.alphaType()) {
91 case kUnknown_SkAlphaType:
92 LOG_ALWAYS_FATAL("Bitmap has no alpha type");
93 break;
94 case kOpaque_SkAlphaType:
95 return ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE;
96 case kPremul_SkAlphaType:
97 return ANDROID_BITMAP_FLAGS_ALPHA_PREMUL;
98 case kUnpremul_SkAlphaType:
99 return ANDROID_BITMAP_FLAGS_ALPHA_UNPREMUL;
100 }
101}
102
Leon Scroggins III84a2afc2020-01-19 19:27:16 -0500103static uint32_t getInfoFlags(const SkImageInfo& info, bool isHardware) {
104 uint32_t flags = getAlphaFlags(info);
105 if (isHardware) {
106 flags |= ANDROID_BITMAP_FLAGS_IS_HARDWARE;
107 }
108 return flags;
109}
110
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -0400111ABitmap* ABitmap_copy(ABitmap* srcBitmapHandle, AndroidBitmapFormat dstFormat) {
112 SkColorType dstColorType = getColorType(dstFormat);
113 if (srcBitmapHandle && dstColorType != kUnknown_SkColorType) {
114 SkBitmap srcBitmap;
115 TypeCast::toBitmap(srcBitmapHandle)->getSkBitmap(&srcBitmap);
116
117 sk_sp<Bitmap> dstBitmap =
118 Bitmap::allocateHeapBitmap(srcBitmap.info().makeColorType(dstColorType));
119 if (dstBitmap && srcBitmap.readPixels(dstBitmap->info(), dstBitmap->pixels(),
120 dstBitmap->rowBytes(), 0, 0)) {
121 return TypeCast::toABitmap(dstBitmap.release());
122 }
123 }
124 return nullptr;
125}
126
Leon Scroggins III84a2afc2020-01-19 19:27:16 -0500127static AndroidBitmapInfo getInfo(const SkImageInfo& imageInfo, uint32_t rowBytes, bool isHardware) {
Derek Sollenberger6c41ab12019-11-08 08:50:58 -0500128 AndroidBitmapInfo info;
129 info.width = imageInfo.width();
130 info.height = imageInfo.height();
131 info.stride = rowBytes;
132 info.format = getFormat(imageInfo);
Leon Scroggins III84a2afc2020-01-19 19:27:16 -0500133 info.flags = getInfoFlags(imageInfo, isHardware);
Derek Sollenberger6c41ab12019-11-08 08:50:58 -0500134 return info;
135}
136
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -0400137AndroidBitmapInfo ABitmap_getInfo(ABitmap* bitmapHandle) {
138 Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
Leon Scroggins III84a2afc2020-01-19 19:27:16 -0500139 return getInfo(bitmap->info(), bitmap->rowBytes(), bitmap->isHardware());
Derek Sollenberger6c41ab12019-11-08 08:50:58 -0500140}
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -0400141
Leon Scroggins III1994fcb2020-01-10 13:40:07 -0500142ADataSpace ABitmap_getDataSpace(ABitmap* bitmapHandle) {
143 Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
144 const SkImageInfo& info = bitmap->info();
Leon Scroggins IIId256790d12020-01-16 15:01:52 -0500145 return (ADataSpace)uirenderer::ColorSpaceToADataSpace(info.colorSpace(), info.colorType());
Leon Scroggins III1994fcb2020-01-10 13:40:07 -0500146}
147
Derek Sollenberger6c41ab12019-11-08 08:50:58 -0500148AndroidBitmapInfo ABitmap_getInfoFromJava(JNIEnv* env, jobject bitmapObj) {
149 uint32_t rowBytes = 0;
Leon Scroggins III84a2afc2020-01-19 19:27:16 -0500150 bool isHardware = false;
151 SkImageInfo imageInfo = GraphicsJNI::getBitmapInfo(env, bitmapObj, &rowBytes, &isHardware);
152 return getInfo(imageInfo, rowBytes, isHardware);
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -0400153}
154
155void* ABitmap_getPixels(ABitmap* bitmapHandle) {
156 Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
157 if (bitmap->isHardware()) {
158 return nullptr;
159 }
160 return bitmap->pixels();
161}
Derek Sollenberger213daca2019-10-25 14:17:32 -0400162
163AndroidBitmapFormat ABitmapConfig_getFormatFromConfig(JNIEnv* env, jobject bitmapConfigObj) {
Derek Sollenberger6c41ab12019-11-08 08:50:58 -0500164 return GraphicsJNI::getFormatFromConfig(env, bitmapConfigObj);
Derek Sollenberger213daca2019-10-25 14:17:32 -0400165}
166
167jobject ABitmapConfig_getConfigFromFormat(JNIEnv* env, AndroidBitmapFormat format) {
Derek Sollenberger6c41ab12019-11-08 08:50:58 -0500168 return GraphicsJNI::getConfigFromFormat(env, format);
169}
170
171void ABitmap_notifyPixelsChanged(ABitmap* bitmapHandle) {
172 Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
Derek Sollenbergerd3e9eec2020-04-06 11:43:59 -0400173 if (!bitmap->isImmutable()) {
174 bitmap->notifyPixelsChanged();
Derek Sollenberger6c41ab12019-11-08 08:50:58 -0500175 }
Derek Sollenberger213daca2019-10-25 14:17:32 -0400176}
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400177
178namespace {
179SkAlphaType getAlphaType(const AndroidBitmapInfo* info) {
180 switch (info->flags & ANDROID_BITMAP_FLAGS_ALPHA_MASK) {
181 case ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE:
182 return kOpaque_SkAlphaType;
183 case ANDROID_BITMAP_FLAGS_ALPHA_PREMUL:
184 return kPremul_SkAlphaType;
185 case ANDROID_BITMAP_FLAGS_ALPHA_UNPREMUL:
186 return kUnpremul_SkAlphaType;
187 default:
188 return kUnknown_SkAlphaType;
189 }
190}
191
192class CompressWriter : public SkWStream {
193public:
Leon Scroggins III7871f492020-01-22 11:57:19 -0500194 CompressWriter(void* userContext, AndroidBitmap_CompressWriteFunc fn)
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400195 : mUserContext(userContext), mFn(fn), mBytesWritten(0) {}
196
197 bool write(const void* buffer, size_t size) override {
198 if (mFn(mUserContext, buffer, size)) {
199 mBytesWritten += size;
200 return true;
201 }
202 return false;
203 }
204
205 size_t bytesWritten() const override { return mBytesWritten; }
206
207private:
208 void* mUserContext;
Leon Scroggins III7871f492020-01-22 11:57:19 -0500209 AndroidBitmap_CompressWriteFunc mFn;
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400210 size_t mBytesWritten;
211};
212
213} // anonymous namespace
214
215int ABitmap_compress(const AndroidBitmapInfo* info, ADataSpace dataSpace, const void* pixels,
216 AndroidBitmapCompressFormat inFormat, int32_t quality, void* userContext,
Leon Scroggins III7871f492020-01-22 11:57:19 -0500217 AndroidBitmap_CompressWriteFunc fn) {
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400218 Bitmap::JavaCompressFormat format;
219 switch (inFormat) {
220 case ANDROID_BITMAP_COMPRESS_FORMAT_JPEG:
221 format = Bitmap::JavaCompressFormat::Jpeg;
222 break;
223 case ANDROID_BITMAP_COMPRESS_FORMAT_PNG:
224 format = Bitmap::JavaCompressFormat::Png;
225 break;
226 case ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSY:
227 format = Bitmap::JavaCompressFormat::WebpLossy;
228 break;
229 case ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSLESS:
230 format = Bitmap::JavaCompressFormat::WebpLossless;
231 break;
232 default:
233 // kWEBP_JavaEncodeFormat is a valid parameter for Bitmap::compress,
234 // for the deprecated Bitmap.CompressFormat.WEBP, but it should not
235 // be provided via the NDK. Other integers are likewise invalid.
236 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
237 }
238
239 SkColorType colorType;
240 switch (info->format) {
241 case ANDROID_BITMAP_FORMAT_RGBA_8888:
242 colorType = kN32_SkColorType;
243 break;
244 case ANDROID_BITMAP_FORMAT_RGB_565:
245 colorType = kRGB_565_SkColorType;
246 break;
247 case ANDROID_BITMAP_FORMAT_A_8:
248 // FIXME b/146637821: Should this encode as grayscale? We should
249 // make the same decision as for encoding an android.graphics.Bitmap.
250 // Note that encoding kAlpha_8 as WebP or JPEG will fail. Encoding
251 // it to PNG encodes as GRAY+ALPHA with a secret handshake that we
252 // only care about the alpha. I'm not sure whether Android decoding
253 // APIs respect that handshake.
254 colorType = kAlpha_8_SkColorType;
255 break;
256 case ANDROID_BITMAP_FORMAT_RGBA_F16:
257 colorType = kRGBA_F16_SkColorType;
258 break;
Alec Mouri1efd0a52022-01-20 13:58:23 -0800259 case ANDROID_BITMAP_FORMAT_RGBA_1010102:
260 colorType = kRGBA_1010102_SkColorType;
261 break;
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400262 default:
263 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
264 }
265
266 auto alphaType = getAlphaType(info);
267 if (alphaType == kUnknown_SkAlphaType) {
268 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
269 }
270
271 sk_sp<SkColorSpace> cs;
272 if (info->format == ANDROID_BITMAP_FORMAT_A_8) {
273 // FIXME: A Java Bitmap with ALPHA_8 never has a ColorSpace. So should
274 // we force that here (as I'm doing now) or should we treat anything
275 // besides ADATASPACE_UNKNOWN as an error?
276 cs = nullptr;
277 } else {
278 cs = uirenderer::DataSpaceToColorSpace((android_dataspace) dataSpace);
279 // DataSpaceToColorSpace treats UNKNOWN as SRGB, but compress forces the
280 // client to specify SRGB if that is what they want.
281 if (!cs || dataSpace == ADATASPACE_UNKNOWN) {
282 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
283 }
284 }
285
286 {
287 size_t size;
288 if (!Bitmap::computeAllocationSize(info->stride, info->height, &size)) {
289 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
290 }
291 }
292
293 auto imageInfo =
294 SkImageInfo::Make(info->width, info->height, colorType, alphaType, std::move(cs));
295 SkBitmap bitmap;
296 // We are not going to modify the pixels, but installPixels expects them to
297 // not be const, since for all it knows we might want to draw to the SkBitmap.
298 if (!bitmap.installPixels(imageInfo, const_cast<void*>(pixels), info->stride)) {
299 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
300 }
301
302 CompressWriter stream(userContext, fn);
Leon Scroggins III949c6002020-01-23 16:20:39 -0500303 return Bitmap::compress(bitmap, format, quality, &stream) ? ANDROID_BITMAP_RESULT_SUCCESS
304 : ANDROID_BITMAP_RESULT_JNI_EXCEPTION;
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400305}
Leon Scroggins III84a2afc2020-01-19 19:27:16 -0500306
307AHardwareBuffer* ABitmap_getHardwareBuffer(ABitmap* bitmapHandle) {
308 Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
309 AHardwareBuffer* buffer = bitmap->hardwareBuffer();
310 if (buffer) {
311 AHardwareBuffer_acquire(buffer);
312 }
313 return buffer;
314}