blob: c442a7b1d17cfe623fd4714e307b10852321c76b [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 Sollenberger5368eda2019-10-25 11:20:03 -040017#undef LOG_TAG
Derek Sollenberger6c41ab12019-11-08 08:50:58 -050018#define LOG_TAG "Bitmap"
19#include <log/log.h>
20
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040021#include "android/graphics/bitmap.h"
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040022#include "TypeCast.h"
Derek Sollenberger213daca2019-10-25 14:17:32 -040023#include "GraphicsJNI.h"
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040024
Derek Sollenberger6c41ab12019-11-08 08:50:58 -050025#include <GraphicsJNI.h>
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040026#include <hwui/Bitmap.h>
Kevin Lubick1175dc02022-02-28 12:41:27 -050027#include <SkBitmap.h>
28#include <SkColorSpace.h>
29#include <SkImageInfo.h>
30#include <SkRefCnt.h>
31#include <SkStream.h>
Leon Scroggins III9010e8b2019-08-20 11:27:17 -040032#include <utils/Color.h>
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040033
34using namespace android;
35
36ABitmap* ABitmap_acquireBitmapFromJava(JNIEnv* env, jobject bitmapObj) {
Derek Sollenberger6c41ab12019-11-08 08:50:58 -050037 Bitmap* bitmap = GraphicsJNI::getNativeBitmap(env, bitmapObj);
38 if (bitmap) {
39 bitmap->ref();
40 return TypeCast::toABitmap(bitmap);
41 }
42 return nullptr;
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040043}
44
45void ABitmap_acquireRef(ABitmap* bitmap) {
46 SkSafeRef(TypeCast::toBitmap(bitmap));
47}
48
49void ABitmap_releaseRef(ABitmap* bitmap) {
50 SkSafeUnref(TypeCast::toBitmap(bitmap));
51}
52
Derek Sollenberger6c41ab12019-11-08 08:50:58 -050053static AndroidBitmapFormat getFormat(const SkImageInfo& info) {
54 switch (info.colorType()) {
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040055 case kN32_SkColorType:
56 return ANDROID_BITMAP_FORMAT_RGBA_8888;
57 case kRGB_565_SkColorType:
58 return ANDROID_BITMAP_FORMAT_RGB_565;
59 case kARGB_4444_SkColorType:
60 return ANDROID_BITMAP_FORMAT_RGBA_4444;
61 case kAlpha_8_SkColorType:
62 return ANDROID_BITMAP_FORMAT_A_8;
63 case kRGBA_F16_SkColorType:
64 return ANDROID_BITMAP_FORMAT_RGBA_F16;
Alec Mouri1efd0a52022-01-20 13:58:23 -080065 case kRGBA_1010102_SkColorType:
66 return ANDROID_BITMAP_FORMAT_RGBA_1010102;
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040067 default:
68 return ANDROID_BITMAP_FORMAT_NONE;
69 }
70}
71
72static SkColorType getColorType(AndroidBitmapFormat format) {
73 switch (format) {
74 case ANDROID_BITMAP_FORMAT_RGBA_8888:
75 return kN32_SkColorType;
76 case ANDROID_BITMAP_FORMAT_RGB_565:
77 return kRGB_565_SkColorType;
78 case ANDROID_BITMAP_FORMAT_RGBA_4444:
79 return kARGB_4444_SkColorType;
80 case ANDROID_BITMAP_FORMAT_A_8:
81 return kAlpha_8_SkColorType;
82 case ANDROID_BITMAP_FORMAT_RGBA_F16:
83 return kRGBA_F16_SkColorType;
Alec Mouri1efd0a52022-01-20 13:58:23 -080084 case ANDROID_BITMAP_FORMAT_RGBA_1010102:
85 return kRGBA_1010102_SkColorType;
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040086 default:
87 return kUnknown_SkColorType;
88 }
89}
90
Leon Scroggins III84a2afc2020-01-19 19:27:16 -050091static uint32_t getAlphaFlags(const SkImageInfo& info) {
Derek Sollenberger6c41ab12019-11-08 08:50:58 -050092 switch (info.alphaType()) {
93 case kUnknown_SkAlphaType:
94 LOG_ALWAYS_FATAL("Bitmap has no alpha type");
95 break;
96 case kOpaque_SkAlphaType:
97 return ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE;
98 case kPremul_SkAlphaType:
99 return ANDROID_BITMAP_FLAGS_ALPHA_PREMUL;
100 case kUnpremul_SkAlphaType:
101 return ANDROID_BITMAP_FLAGS_ALPHA_UNPREMUL;
102 }
103}
104
Leon Scroggins III84a2afc2020-01-19 19:27:16 -0500105static uint32_t getInfoFlags(const SkImageInfo& info, bool isHardware) {
106 uint32_t flags = getAlphaFlags(info);
107 if (isHardware) {
108 flags |= ANDROID_BITMAP_FLAGS_IS_HARDWARE;
109 }
110 return flags;
111}
112
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -0400113ABitmap* ABitmap_copy(ABitmap* srcBitmapHandle, AndroidBitmapFormat dstFormat) {
114 SkColorType dstColorType = getColorType(dstFormat);
115 if (srcBitmapHandle && dstColorType != kUnknown_SkColorType) {
116 SkBitmap srcBitmap;
117 TypeCast::toBitmap(srcBitmapHandle)->getSkBitmap(&srcBitmap);
118
119 sk_sp<Bitmap> dstBitmap =
120 Bitmap::allocateHeapBitmap(srcBitmap.info().makeColorType(dstColorType));
121 if (dstBitmap && srcBitmap.readPixels(dstBitmap->info(), dstBitmap->pixels(),
122 dstBitmap->rowBytes(), 0, 0)) {
123 return TypeCast::toABitmap(dstBitmap.release());
124 }
125 }
126 return nullptr;
127}
128
Leon Scroggins III84a2afc2020-01-19 19:27:16 -0500129static AndroidBitmapInfo getInfo(const SkImageInfo& imageInfo, uint32_t rowBytes, bool isHardware) {
Derek Sollenberger6c41ab12019-11-08 08:50:58 -0500130 AndroidBitmapInfo info;
131 info.width = imageInfo.width();
132 info.height = imageInfo.height();
133 info.stride = rowBytes;
134 info.format = getFormat(imageInfo);
Leon Scroggins III84a2afc2020-01-19 19:27:16 -0500135 info.flags = getInfoFlags(imageInfo, isHardware);
Derek Sollenberger6c41ab12019-11-08 08:50:58 -0500136 return info;
137}
138
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -0400139AndroidBitmapInfo ABitmap_getInfo(ABitmap* bitmapHandle) {
140 Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
Leon Scroggins III84a2afc2020-01-19 19:27:16 -0500141 return getInfo(bitmap->info(), bitmap->rowBytes(), bitmap->isHardware());
Derek Sollenberger6c41ab12019-11-08 08:50:58 -0500142}
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -0400143
Leon Scroggins III1994fcb2020-01-10 13:40:07 -0500144ADataSpace ABitmap_getDataSpace(ABitmap* bitmapHandle) {
145 Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
146 const SkImageInfo& info = bitmap->info();
Leon Scroggins IIId256790d12020-01-16 15:01:52 -0500147 return (ADataSpace)uirenderer::ColorSpaceToADataSpace(info.colorSpace(), info.colorType());
Leon Scroggins III1994fcb2020-01-10 13:40:07 -0500148}
149
Derek Sollenberger6c41ab12019-11-08 08:50:58 -0500150AndroidBitmapInfo ABitmap_getInfoFromJava(JNIEnv* env, jobject bitmapObj) {
151 uint32_t rowBytes = 0;
Leon Scroggins III84a2afc2020-01-19 19:27:16 -0500152 bool isHardware = false;
153 SkImageInfo imageInfo = GraphicsJNI::getBitmapInfo(env, bitmapObj, &rowBytes, &isHardware);
154 return getInfo(imageInfo, rowBytes, isHardware);
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -0400155}
156
157void* ABitmap_getPixels(ABitmap* bitmapHandle) {
158 Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
159 if (bitmap->isHardware()) {
160 return nullptr;
161 }
162 return bitmap->pixels();
163}
Derek Sollenberger213daca2019-10-25 14:17:32 -0400164
165AndroidBitmapFormat ABitmapConfig_getFormatFromConfig(JNIEnv* env, jobject bitmapConfigObj) {
Derek Sollenberger6c41ab12019-11-08 08:50:58 -0500166 return GraphicsJNI::getFormatFromConfig(env, bitmapConfigObj);
Derek Sollenberger213daca2019-10-25 14:17:32 -0400167}
168
169jobject ABitmapConfig_getConfigFromFormat(JNIEnv* env, AndroidBitmapFormat format) {
Derek Sollenberger6c41ab12019-11-08 08:50:58 -0500170 return GraphicsJNI::getConfigFromFormat(env, format);
171}
172
173void ABitmap_notifyPixelsChanged(ABitmap* bitmapHandle) {
174 Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
Derek Sollenbergerd3e9eec2020-04-06 11:43:59 -0400175 if (!bitmap->isImmutable()) {
176 bitmap->notifyPixelsChanged();
Derek Sollenberger6c41ab12019-11-08 08:50:58 -0500177 }
Derek Sollenberger213daca2019-10-25 14:17:32 -0400178}
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400179
180namespace {
181SkAlphaType getAlphaType(const AndroidBitmapInfo* info) {
182 switch (info->flags & ANDROID_BITMAP_FLAGS_ALPHA_MASK) {
183 case ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE:
184 return kOpaque_SkAlphaType;
185 case ANDROID_BITMAP_FLAGS_ALPHA_PREMUL:
186 return kPremul_SkAlphaType;
187 case ANDROID_BITMAP_FLAGS_ALPHA_UNPREMUL:
188 return kUnpremul_SkAlphaType;
189 default:
190 return kUnknown_SkAlphaType;
191 }
192}
193
194class CompressWriter : public SkWStream {
195public:
Leon Scroggins III7871f492020-01-22 11:57:19 -0500196 CompressWriter(void* userContext, AndroidBitmap_CompressWriteFunc fn)
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400197 : mUserContext(userContext), mFn(fn), mBytesWritten(0) {}
198
199 bool write(const void* buffer, size_t size) override {
200 if (mFn(mUserContext, buffer, size)) {
201 mBytesWritten += size;
202 return true;
203 }
204 return false;
205 }
206
207 size_t bytesWritten() const override { return mBytesWritten; }
208
209private:
210 void* mUserContext;
Leon Scroggins III7871f492020-01-22 11:57:19 -0500211 AndroidBitmap_CompressWriteFunc mFn;
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400212 size_t mBytesWritten;
213};
214
215} // anonymous namespace
216
217int ABitmap_compress(const AndroidBitmapInfo* info, ADataSpace dataSpace, const void* pixels,
218 AndroidBitmapCompressFormat inFormat, int32_t quality, void* userContext,
Leon Scroggins III7871f492020-01-22 11:57:19 -0500219 AndroidBitmap_CompressWriteFunc fn) {
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400220 Bitmap::JavaCompressFormat format;
221 switch (inFormat) {
222 case ANDROID_BITMAP_COMPRESS_FORMAT_JPEG:
223 format = Bitmap::JavaCompressFormat::Jpeg;
224 break;
225 case ANDROID_BITMAP_COMPRESS_FORMAT_PNG:
226 format = Bitmap::JavaCompressFormat::Png;
227 break;
228 case ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSY:
229 format = Bitmap::JavaCompressFormat::WebpLossy;
230 break;
231 case ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSLESS:
232 format = Bitmap::JavaCompressFormat::WebpLossless;
233 break;
234 default:
235 // kWEBP_JavaEncodeFormat is a valid parameter for Bitmap::compress,
236 // for the deprecated Bitmap.CompressFormat.WEBP, but it should not
237 // be provided via the NDK. Other integers are likewise invalid.
238 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
239 }
240
241 SkColorType colorType;
242 switch (info->format) {
243 case ANDROID_BITMAP_FORMAT_RGBA_8888:
244 colorType = kN32_SkColorType;
245 break;
246 case ANDROID_BITMAP_FORMAT_RGB_565:
247 colorType = kRGB_565_SkColorType;
248 break;
249 case ANDROID_BITMAP_FORMAT_A_8:
250 // FIXME b/146637821: Should this encode as grayscale? We should
251 // make the same decision as for encoding an android.graphics.Bitmap.
252 // Note that encoding kAlpha_8 as WebP or JPEG will fail. Encoding
253 // it to PNG encodes as GRAY+ALPHA with a secret handshake that we
254 // only care about the alpha. I'm not sure whether Android decoding
255 // APIs respect that handshake.
256 colorType = kAlpha_8_SkColorType;
257 break;
258 case ANDROID_BITMAP_FORMAT_RGBA_F16:
259 colorType = kRGBA_F16_SkColorType;
260 break;
Alec Mouri1efd0a52022-01-20 13:58:23 -0800261 case ANDROID_BITMAP_FORMAT_RGBA_1010102:
262 colorType = kRGBA_1010102_SkColorType;
263 break;
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400264 default:
265 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
266 }
267
268 auto alphaType = getAlphaType(info);
269 if (alphaType == kUnknown_SkAlphaType) {
270 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
271 }
272
273 sk_sp<SkColorSpace> cs;
274 if (info->format == ANDROID_BITMAP_FORMAT_A_8) {
275 // FIXME: A Java Bitmap with ALPHA_8 never has a ColorSpace. So should
276 // we force that here (as I'm doing now) or should we treat anything
277 // besides ADATASPACE_UNKNOWN as an error?
278 cs = nullptr;
279 } else {
280 cs = uirenderer::DataSpaceToColorSpace((android_dataspace) dataSpace);
281 // DataSpaceToColorSpace treats UNKNOWN as SRGB, but compress forces the
282 // client to specify SRGB if that is what they want.
283 if (!cs || dataSpace == ADATASPACE_UNKNOWN) {
284 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
285 }
286 }
287
288 {
289 size_t size;
290 if (!Bitmap::computeAllocationSize(info->stride, info->height, &size)) {
291 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
292 }
293 }
294
295 auto imageInfo =
296 SkImageInfo::Make(info->width, info->height, colorType, alphaType, std::move(cs));
297 SkBitmap bitmap;
298 // We are not going to modify the pixels, but installPixels expects them to
299 // not be const, since for all it knows we might want to draw to the SkBitmap.
300 if (!bitmap.installPixels(imageInfo, const_cast<void*>(pixels), info->stride)) {
301 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
302 }
303
304 CompressWriter stream(userContext, fn);
Leon Scroggins III949c6002020-01-23 16:20:39 -0500305 return Bitmap::compress(bitmap, format, quality, &stream) ? ANDROID_BITMAP_RESULT_SUCCESS
306 : ANDROID_BITMAP_RESULT_JNI_EXCEPTION;
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400307}
Leon Scroggins III84a2afc2020-01-19 19:27:16 -0500308
309AHardwareBuffer* ABitmap_getHardwareBuffer(ABitmap* bitmapHandle) {
310 Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
311 AHardwareBuffer* buffer = bitmap->hardwareBuffer();
312 if (buffer) {
313 AHardwareBuffer_acquire(buffer);
314 }
315 return buffer;
316}