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