blob: b56a619b00aa3b78297735180136b83f63505ba1 [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>
Leon Scroggins III9010e8b2019-08-20 11:27:17 -040027#include <utils/Color.h>
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040028
29using namespace android;
30
31ABitmap* ABitmap_acquireBitmapFromJava(JNIEnv* env, jobject bitmapObj) {
Derek Sollenberger6c41ab12019-11-08 08:50:58 -050032 Bitmap* bitmap = GraphicsJNI::getNativeBitmap(env, bitmapObj);
33 if (bitmap) {
34 bitmap->ref();
35 return TypeCast::toABitmap(bitmap);
36 }
37 return nullptr;
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040038}
39
40void ABitmap_acquireRef(ABitmap* bitmap) {
41 SkSafeRef(TypeCast::toBitmap(bitmap));
42}
43
44void ABitmap_releaseRef(ABitmap* bitmap) {
45 SkSafeUnref(TypeCast::toBitmap(bitmap));
46}
47
Derek Sollenberger6c41ab12019-11-08 08:50:58 -050048static AndroidBitmapFormat getFormat(const SkImageInfo& info) {
49 switch (info.colorType()) {
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040050 case kN32_SkColorType:
51 return ANDROID_BITMAP_FORMAT_RGBA_8888;
52 case kRGB_565_SkColorType:
53 return ANDROID_BITMAP_FORMAT_RGB_565;
54 case kARGB_4444_SkColorType:
55 return ANDROID_BITMAP_FORMAT_RGBA_4444;
56 case kAlpha_8_SkColorType:
57 return ANDROID_BITMAP_FORMAT_A_8;
58 case kRGBA_F16_SkColorType:
59 return ANDROID_BITMAP_FORMAT_RGBA_F16;
60 default:
61 return ANDROID_BITMAP_FORMAT_NONE;
62 }
63}
64
65static SkColorType getColorType(AndroidBitmapFormat format) {
66 switch (format) {
67 case ANDROID_BITMAP_FORMAT_RGBA_8888:
68 return kN32_SkColorType;
69 case ANDROID_BITMAP_FORMAT_RGB_565:
70 return kRGB_565_SkColorType;
71 case ANDROID_BITMAP_FORMAT_RGBA_4444:
72 return kARGB_4444_SkColorType;
73 case ANDROID_BITMAP_FORMAT_A_8:
74 return kAlpha_8_SkColorType;
75 case ANDROID_BITMAP_FORMAT_RGBA_F16:
76 return kRGBA_F16_SkColorType;
77 default:
78 return kUnknown_SkColorType;
79 }
80}
81
Leon Scroggins III84a2afc2020-01-19 19:27:16 -050082static uint32_t getAlphaFlags(const SkImageInfo& info) {
Derek Sollenberger6c41ab12019-11-08 08:50:58 -050083 switch (info.alphaType()) {
84 case kUnknown_SkAlphaType:
85 LOG_ALWAYS_FATAL("Bitmap has no alpha type");
86 break;
87 case kOpaque_SkAlphaType:
88 return ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE;
89 case kPremul_SkAlphaType:
90 return ANDROID_BITMAP_FLAGS_ALPHA_PREMUL;
91 case kUnpremul_SkAlphaType:
92 return ANDROID_BITMAP_FLAGS_ALPHA_UNPREMUL;
93 }
94}
95
Leon Scroggins III84a2afc2020-01-19 19:27:16 -050096static uint32_t getInfoFlags(const SkImageInfo& info, bool isHardware) {
97 uint32_t flags = getAlphaFlags(info);
98 if (isHardware) {
99 flags |= ANDROID_BITMAP_FLAGS_IS_HARDWARE;
100 }
101 return flags;
102}
103
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -0400104ABitmap* ABitmap_copy(ABitmap* srcBitmapHandle, AndroidBitmapFormat dstFormat) {
105 SkColorType dstColorType = getColorType(dstFormat);
106 if (srcBitmapHandle && dstColorType != kUnknown_SkColorType) {
107 SkBitmap srcBitmap;
108 TypeCast::toBitmap(srcBitmapHandle)->getSkBitmap(&srcBitmap);
109
110 sk_sp<Bitmap> dstBitmap =
111 Bitmap::allocateHeapBitmap(srcBitmap.info().makeColorType(dstColorType));
112 if (dstBitmap && srcBitmap.readPixels(dstBitmap->info(), dstBitmap->pixels(),
113 dstBitmap->rowBytes(), 0, 0)) {
114 return TypeCast::toABitmap(dstBitmap.release());
115 }
116 }
117 return nullptr;
118}
119
Leon Scroggins III84a2afc2020-01-19 19:27:16 -0500120static AndroidBitmapInfo getInfo(const SkImageInfo& imageInfo, uint32_t rowBytes, bool isHardware) {
Derek Sollenberger6c41ab12019-11-08 08:50:58 -0500121 AndroidBitmapInfo info;
122 info.width = imageInfo.width();
123 info.height = imageInfo.height();
124 info.stride = rowBytes;
125 info.format = getFormat(imageInfo);
Leon Scroggins III84a2afc2020-01-19 19:27:16 -0500126 info.flags = getInfoFlags(imageInfo, isHardware);
Derek Sollenberger6c41ab12019-11-08 08:50:58 -0500127 return info;
128}
129
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -0400130AndroidBitmapInfo ABitmap_getInfo(ABitmap* bitmapHandle) {
131 Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
Leon Scroggins III84a2afc2020-01-19 19:27:16 -0500132 return getInfo(bitmap->info(), bitmap->rowBytes(), bitmap->isHardware());
Derek Sollenberger6c41ab12019-11-08 08:50:58 -0500133}
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -0400134
Leon Scroggins III1994fcb2020-01-10 13:40:07 -0500135ADataSpace ABitmap_getDataSpace(ABitmap* bitmapHandle) {
136 Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
137 const SkImageInfo& info = bitmap->info();
Leon Scroggins IIId256790d12020-01-16 15:01:52 -0500138 return (ADataSpace)uirenderer::ColorSpaceToADataSpace(info.colorSpace(), info.colorType());
Leon Scroggins III1994fcb2020-01-10 13:40:07 -0500139}
140
Derek Sollenberger6c41ab12019-11-08 08:50:58 -0500141AndroidBitmapInfo ABitmap_getInfoFromJava(JNIEnv* env, jobject bitmapObj) {
142 uint32_t rowBytes = 0;
Leon Scroggins III84a2afc2020-01-19 19:27:16 -0500143 bool isHardware = false;
144 SkImageInfo imageInfo = GraphicsJNI::getBitmapInfo(env, bitmapObj, &rowBytes, &isHardware);
145 return getInfo(imageInfo, rowBytes, isHardware);
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -0400146}
147
148void* ABitmap_getPixels(ABitmap* bitmapHandle) {
149 Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
150 if (bitmap->isHardware()) {
151 return nullptr;
152 }
153 return bitmap->pixels();
154}
Derek Sollenberger213daca2019-10-25 14:17:32 -0400155
156AndroidBitmapFormat ABitmapConfig_getFormatFromConfig(JNIEnv* env, jobject bitmapConfigObj) {
Derek Sollenberger6c41ab12019-11-08 08:50:58 -0500157 return GraphicsJNI::getFormatFromConfig(env, bitmapConfigObj);
Derek Sollenberger213daca2019-10-25 14:17:32 -0400158}
159
160jobject ABitmapConfig_getConfigFromFormat(JNIEnv* env, AndroidBitmapFormat format) {
Derek Sollenberger6c41ab12019-11-08 08:50:58 -0500161 return GraphicsJNI::getConfigFromFormat(env, format);
162}
163
164void ABitmap_notifyPixelsChanged(ABitmap* bitmapHandle) {
165 Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
166 if (bitmap->isImmutable()) {
167 ALOGE("Attempting to modify an immutable Bitmap!");
168 }
169 return bitmap->notifyPixelsChanged();
Derek Sollenberger213daca2019-10-25 14:17:32 -0400170}
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400171
172namespace {
173SkAlphaType getAlphaType(const AndroidBitmapInfo* info) {
174 switch (info->flags & ANDROID_BITMAP_FLAGS_ALPHA_MASK) {
175 case ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE:
176 return kOpaque_SkAlphaType;
177 case ANDROID_BITMAP_FLAGS_ALPHA_PREMUL:
178 return kPremul_SkAlphaType;
179 case ANDROID_BITMAP_FLAGS_ALPHA_UNPREMUL:
180 return kUnpremul_SkAlphaType;
181 default:
182 return kUnknown_SkAlphaType;
183 }
184}
185
186class CompressWriter : public SkWStream {
187public:
Leon Scroggins III7871f492020-01-22 11:57:19 -0500188 CompressWriter(void* userContext, AndroidBitmap_CompressWriteFunc fn)
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400189 : mUserContext(userContext), mFn(fn), mBytesWritten(0) {}
190
191 bool write(const void* buffer, size_t size) override {
192 if (mFn(mUserContext, buffer, size)) {
193 mBytesWritten += size;
194 return true;
195 }
196 return false;
197 }
198
199 size_t bytesWritten() const override { return mBytesWritten; }
200
201private:
202 void* mUserContext;
Leon Scroggins III7871f492020-01-22 11:57:19 -0500203 AndroidBitmap_CompressWriteFunc mFn;
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400204 size_t mBytesWritten;
205};
206
207} // anonymous namespace
208
209int ABitmap_compress(const AndroidBitmapInfo* info, ADataSpace dataSpace, const void* pixels,
210 AndroidBitmapCompressFormat inFormat, int32_t quality, void* userContext,
Leon Scroggins III7871f492020-01-22 11:57:19 -0500211 AndroidBitmap_CompressWriteFunc fn) {
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400212 Bitmap::JavaCompressFormat format;
213 switch (inFormat) {
214 case ANDROID_BITMAP_COMPRESS_FORMAT_JPEG:
215 format = Bitmap::JavaCompressFormat::Jpeg;
216 break;
217 case ANDROID_BITMAP_COMPRESS_FORMAT_PNG:
218 format = Bitmap::JavaCompressFormat::Png;
219 break;
220 case ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSY:
221 format = Bitmap::JavaCompressFormat::WebpLossy;
222 break;
223 case ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSLESS:
224 format = Bitmap::JavaCompressFormat::WebpLossless;
225 break;
226 default:
227 // kWEBP_JavaEncodeFormat is a valid parameter for Bitmap::compress,
228 // for the deprecated Bitmap.CompressFormat.WEBP, but it should not
229 // be provided via the NDK. Other integers are likewise invalid.
230 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
231 }
232
233 SkColorType colorType;
234 switch (info->format) {
235 case ANDROID_BITMAP_FORMAT_RGBA_8888:
236 colorType = kN32_SkColorType;
237 break;
238 case ANDROID_BITMAP_FORMAT_RGB_565:
239 colorType = kRGB_565_SkColorType;
240 break;
241 case ANDROID_BITMAP_FORMAT_A_8:
242 // FIXME b/146637821: Should this encode as grayscale? We should
243 // make the same decision as for encoding an android.graphics.Bitmap.
244 // Note that encoding kAlpha_8 as WebP or JPEG will fail. Encoding
245 // it to PNG encodes as GRAY+ALPHA with a secret handshake that we
246 // only care about the alpha. I'm not sure whether Android decoding
247 // APIs respect that handshake.
248 colorType = kAlpha_8_SkColorType;
249 break;
250 case ANDROID_BITMAP_FORMAT_RGBA_F16:
251 colorType = kRGBA_F16_SkColorType;
252 break;
253 default:
254 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
255 }
256
257 auto alphaType = getAlphaType(info);
258 if (alphaType == kUnknown_SkAlphaType) {
259 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
260 }
261
262 sk_sp<SkColorSpace> cs;
263 if (info->format == ANDROID_BITMAP_FORMAT_A_8) {
264 // FIXME: A Java Bitmap with ALPHA_8 never has a ColorSpace. So should
265 // we force that here (as I'm doing now) or should we treat anything
266 // besides ADATASPACE_UNKNOWN as an error?
267 cs = nullptr;
268 } else {
269 cs = uirenderer::DataSpaceToColorSpace((android_dataspace) dataSpace);
270 // DataSpaceToColorSpace treats UNKNOWN as SRGB, but compress forces the
271 // client to specify SRGB if that is what they want.
272 if (!cs || dataSpace == ADATASPACE_UNKNOWN) {
273 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
274 }
275 }
276
277 {
278 size_t size;
279 if (!Bitmap::computeAllocationSize(info->stride, info->height, &size)) {
280 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
281 }
282 }
283
284 auto imageInfo =
285 SkImageInfo::Make(info->width, info->height, colorType, alphaType, std::move(cs));
286 SkBitmap bitmap;
287 // We are not going to modify the pixels, but installPixels expects them to
288 // not be const, since for all it knows we might want to draw to the SkBitmap.
289 if (!bitmap.installPixels(imageInfo, const_cast<void*>(pixels), info->stride)) {
290 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
291 }
292
293 CompressWriter stream(userContext, fn);
Leon Scroggins III949c6002020-01-23 16:20:39 -0500294 return Bitmap::compress(bitmap, format, quality, &stream) ? ANDROID_BITMAP_RESULT_SUCCESS
295 : ANDROID_BITMAP_RESULT_JNI_EXCEPTION;
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400296}
Leon Scroggins III84a2afc2020-01-19 19:27:16 -0500297
298AHardwareBuffer* ABitmap_getHardwareBuffer(ABitmap* bitmapHandle) {
299 Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
300 AHardwareBuffer* buffer = bitmap->hardwareBuffer();
301 if (buffer) {
302 AHardwareBuffer_acquire(buffer);
303 }
304 return buffer;
305}