blob: 000f1092eb1734e53382e420bd111e8bb781c7af [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
Alec Mouri6f6679b2024-07-15 22:43:54 +000017#include <Gainmap.h>
Derek Sollenberger6c41ab12019-11-08 08:50:58 -050018#include <GraphicsJNI.h>
Kevin Lubick1175dc02022-02-28 12:41:27 -050019#include <SkBitmap.h>
20#include <SkColorSpace.h>
21#include <SkImageInfo.h>
22#include <SkRefCnt.h>
23#include <SkStream.h>
Alec Mouri6f6679b2024-07-15 22:43:54 +000024#include <hwui/Bitmap.h>
25#include <log/log.h>
Leon Scroggins III9010e8b2019-08-20 11:27:17 -040026#include <utils/Color.h>
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040027
Alec Mouri6f6679b2024-07-15 22:43:54 +000028#include "GraphicsJNI.h"
29#include "TypeCast.h"
30#include "android/graphics/bitmap.h"
31
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040032using 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) {
Alec Mouri6f6679b2024-07-15 22:43:54 +0000218 return ABitmap_compressWithGainmap(info, dataSpace, pixels, nullptr, -1.f, inFormat, quality,
219 userContext, fn);
220}
221
222int ABitmap_compressWithGainmap(const AndroidBitmapInfo* info, ADataSpace dataSpace,
223 const void* pixels, const void* gainmapPixels, float hdrSdrRatio,
224 AndroidBitmapCompressFormat inFormat, int32_t quality,
225 void* userContext, AndroidBitmap_CompressWriteFunc fn) {
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400226 Bitmap::JavaCompressFormat format;
227 switch (inFormat) {
228 case ANDROID_BITMAP_COMPRESS_FORMAT_JPEG:
229 format = Bitmap::JavaCompressFormat::Jpeg;
230 break;
231 case ANDROID_BITMAP_COMPRESS_FORMAT_PNG:
232 format = Bitmap::JavaCompressFormat::Png;
233 break;
234 case ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSY:
235 format = Bitmap::JavaCompressFormat::WebpLossy;
236 break;
237 case ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSLESS:
238 format = Bitmap::JavaCompressFormat::WebpLossless;
239 break;
240 default:
241 // kWEBP_JavaEncodeFormat is a valid parameter for Bitmap::compress,
242 // for the deprecated Bitmap.CompressFormat.WEBP, but it should not
243 // be provided via the NDK. Other integers are likewise invalid.
244 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
245 }
246
247 SkColorType colorType;
248 switch (info->format) {
249 case ANDROID_BITMAP_FORMAT_RGBA_8888:
250 colorType = kN32_SkColorType;
251 break;
252 case ANDROID_BITMAP_FORMAT_RGB_565:
253 colorType = kRGB_565_SkColorType;
254 break;
255 case ANDROID_BITMAP_FORMAT_A_8:
256 // FIXME b/146637821: Should this encode as grayscale? We should
257 // make the same decision as for encoding an android.graphics.Bitmap.
258 // Note that encoding kAlpha_8 as WebP or JPEG will fail. Encoding
259 // it to PNG encodes as GRAY+ALPHA with a secret handshake that we
260 // only care about the alpha. I'm not sure whether Android decoding
261 // APIs respect that handshake.
262 colorType = kAlpha_8_SkColorType;
263 break;
264 case ANDROID_BITMAP_FORMAT_RGBA_F16:
265 colorType = kRGBA_F16_SkColorType;
266 break;
Alec Mouri1efd0a52022-01-20 13:58:23 -0800267 case ANDROID_BITMAP_FORMAT_RGBA_1010102:
268 colorType = kRGBA_1010102_SkColorType;
269 break;
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400270 default:
271 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
272 }
273
274 auto alphaType = getAlphaType(info);
275 if (alphaType == kUnknown_SkAlphaType) {
276 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
277 }
278
279 sk_sp<SkColorSpace> cs;
280 if (info->format == ANDROID_BITMAP_FORMAT_A_8) {
281 // FIXME: A Java Bitmap with ALPHA_8 never has a ColorSpace. So should
282 // we force that here (as I'm doing now) or should we treat anything
283 // besides ADATASPACE_UNKNOWN as an error?
284 cs = nullptr;
285 } else {
Alec Mouri6f6679b2024-07-15 22:43:54 +0000286 cs = uirenderer::DataSpaceToColorSpace((android_dataspace)dataSpace);
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400287 // DataSpaceToColorSpace treats UNKNOWN as SRGB, but compress forces the
288 // client to specify SRGB if that is what they want.
289 if (!cs || dataSpace == ADATASPACE_UNKNOWN) {
290 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
291 }
292 }
293
294 {
295 size_t size;
296 if (!Bitmap::computeAllocationSize(info->stride, info->height, &size)) {
297 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
298 }
299 }
300
301 auto imageInfo =
302 SkImageInfo::Make(info->width, info->height, colorType, alphaType, std::move(cs));
Alec Mouri6f6679b2024-07-15 22:43:54 +0000303
304 // Validate the image info
305 {
306 SkBitmap tempBitmap;
307 if (!tempBitmap.installPixels(imageInfo, const_cast<void*>(pixels), info->stride)) {
308 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
309 }
310 }
311
312 SkPixelRef pixelRef =
313 SkPixelRef(info->width, info->height, const_cast<void*>(pixels), info->stride);
314
315 auto bitmap = Bitmap::createFrom(imageInfo, pixelRef);
316
317 if (gainmapPixels) {
318 auto gainmap = sp<uirenderer::Gainmap>::make();
319 gainmap->info.fGainmapRatioMin = {
320 1.f,
321 1.f,
322 1.f,
323 1.f,
324 };
325 gainmap->info.fGainmapRatioMax = {
326 hdrSdrRatio,
327 hdrSdrRatio,
328 hdrSdrRatio,
329 1.f,
330 };
331 gainmap->info.fGainmapGamma = {
332 1.f,
333 1.f,
334 1.f,
335 1.f,
336 };
337
338 static constexpr auto kDefaultEpsilon = 1.f / 64.f;
339 gainmap->info.fEpsilonSdr = {
340 kDefaultEpsilon,
341 kDefaultEpsilon,
342 kDefaultEpsilon,
343 1.f,
344 };
345 gainmap->info.fEpsilonHdr = {
346 kDefaultEpsilon,
347 kDefaultEpsilon,
348 kDefaultEpsilon,
349 1.f,
350 };
351 gainmap->info.fDisplayRatioSdr = 1.f;
352 gainmap->info.fDisplayRatioHdr = hdrSdrRatio;
353
354 SkPixelRef gainmapPixelRef = SkPixelRef(info->width, info->height,
355 const_cast<void*>(gainmapPixels), info->stride);
356 auto gainmapBitmap = Bitmap::createFrom(imageInfo, gainmapPixelRef);
357 gainmap->bitmap = std::move(gainmapBitmap);
358 bitmap->setGainmap(std::move(gainmap));
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400359 }
360
361 CompressWriter stream(userContext, fn);
Alec Mouri6f6679b2024-07-15 22:43:54 +0000362
363 return bitmap->compress(format, quality, &stream)
364
365 ? ANDROID_BITMAP_RESULT_SUCCESS
366 : ANDROID_BITMAP_RESULT_JNI_EXCEPTION;
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400367}
Leon Scroggins III84a2afc2020-01-19 19:27:16 -0500368
369AHardwareBuffer* ABitmap_getHardwareBuffer(ABitmap* bitmapHandle) {
370 Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
371 AHardwareBuffer* buffer = bitmap->hardwareBuffer();
372 if (buffer) {
373 AHardwareBuffer_acquire(buffer);
374 }
375 return buffer;
376}