1 /*
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 
17 #undef LOG_TAG
18 #define LOG_TAG "Bitmap"
19 #include <log/log.h>
20 
21 #include "android/graphics/bitmap.h"
22 #include "TypeCast.h"
23 #include "GraphicsJNI.h"
24 
25 #include <GraphicsJNI.h>
26 #include <hwui/Bitmap.h>
27 #include <SkBitmap.h>
28 #include <SkColorSpace.h>
29 #include <SkImageInfo.h>
30 #include <SkRefCnt.h>
31 #include <SkStream.h>
32 #include <utils/Color.h>
33 
34 using namespace android;
35 
ABitmap_acquireBitmapFromJava(JNIEnv * env,jobject bitmapObj)36 ABitmap* ABitmap_acquireBitmapFromJava(JNIEnv* env, jobject bitmapObj) {
37     Bitmap* bitmap = GraphicsJNI::getNativeBitmap(env, bitmapObj);
38     if (bitmap) {
39         bitmap->ref();
40         return TypeCast::toABitmap(bitmap);
41     }
42     return nullptr;
43 }
44 
ABitmap_acquireRef(ABitmap * bitmap)45 void ABitmap_acquireRef(ABitmap* bitmap) {
46     SkSafeRef(TypeCast::toBitmap(bitmap));
47 }
48 
ABitmap_releaseRef(ABitmap * bitmap)49 void ABitmap_releaseRef(ABitmap* bitmap) {
50     SkSafeUnref(TypeCast::toBitmap(bitmap));
51 }
52 
getFormat(const SkImageInfo & info)53 static AndroidBitmapFormat getFormat(const SkImageInfo& info) {
54     switch (info.colorType()) {
55         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;
65         case kRGBA_1010102_SkColorType:
66             return ANDROID_BITMAP_FORMAT_RGBA_1010102;
67         default:
68             return ANDROID_BITMAP_FORMAT_NONE;
69     }
70 }
71 
getColorType(AndroidBitmapFormat format)72 static 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;
84         case ANDROID_BITMAP_FORMAT_RGBA_1010102:
85             return kRGBA_1010102_SkColorType;
86         default:
87             return kUnknown_SkColorType;
88     }
89 }
90 
getAlphaFlags(const SkImageInfo & info)91 static uint32_t getAlphaFlags(const SkImageInfo& info) {
92     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 
getInfoFlags(const SkImageInfo & info,bool isHardware)105 static 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 
ABitmap_copy(ABitmap * srcBitmapHandle,AndroidBitmapFormat dstFormat)113 ABitmap* 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 
getInfo(const SkImageInfo & imageInfo,uint32_t rowBytes,bool isHardware)129 static AndroidBitmapInfo getInfo(const SkImageInfo& imageInfo, uint32_t rowBytes, bool isHardware) {
130     AndroidBitmapInfo info;
131     info.width = imageInfo.width();
132     info.height = imageInfo.height();
133     info.stride = rowBytes;
134     info.format = getFormat(imageInfo);
135     info.flags = getInfoFlags(imageInfo, isHardware);
136     return info;
137 }
138 
ABitmap_getInfo(ABitmap * bitmapHandle)139 AndroidBitmapInfo ABitmap_getInfo(ABitmap* bitmapHandle) {
140     Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
141     return getInfo(bitmap->info(), bitmap->rowBytes(), bitmap->isHardware());
142 }
143 
ABitmap_getDataSpace(ABitmap * bitmapHandle)144 ADataSpace ABitmap_getDataSpace(ABitmap* bitmapHandle) {
145     Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
146     const SkImageInfo& info = bitmap->info();
147     return (ADataSpace)uirenderer::ColorSpaceToADataSpace(info.colorSpace(), info.colorType());
148 }
149 
ABitmap_getInfoFromJava(JNIEnv * env,jobject bitmapObj)150 AndroidBitmapInfo ABitmap_getInfoFromJava(JNIEnv* env, jobject bitmapObj) {
151     uint32_t rowBytes = 0;
152     bool isHardware = false;
153     SkImageInfo imageInfo = GraphicsJNI::getBitmapInfo(env, bitmapObj, &rowBytes, &isHardware);
154     return getInfo(imageInfo, rowBytes, isHardware);
155 }
156 
ABitmap_getPixels(ABitmap * bitmapHandle)157 void* ABitmap_getPixels(ABitmap* bitmapHandle) {
158     Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
159     if (bitmap->isHardware()) {
160         return nullptr;
161     }
162     return bitmap->pixels();
163 }
164 
ABitmapConfig_getFormatFromConfig(JNIEnv * env,jobject bitmapConfigObj)165 AndroidBitmapFormat ABitmapConfig_getFormatFromConfig(JNIEnv* env, jobject bitmapConfigObj) {
166     return GraphicsJNI::getFormatFromConfig(env, bitmapConfigObj);
167 }
168 
ABitmapConfig_getConfigFromFormat(JNIEnv * env,AndroidBitmapFormat format)169 jobject ABitmapConfig_getConfigFromFormat(JNIEnv* env, AndroidBitmapFormat format) {
170     return GraphicsJNI::getConfigFromFormat(env, format);
171 }
172 
ABitmap_notifyPixelsChanged(ABitmap * bitmapHandle)173 void ABitmap_notifyPixelsChanged(ABitmap* bitmapHandle) {
174     Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
175     if (!bitmap->isImmutable()) {
176         bitmap->notifyPixelsChanged();
177     }
178 }
179 
180 namespace {
getAlphaType(const AndroidBitmapInfo * info)181 SkAlphaType 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 
194 class CompressWriter : public SkWStream {
195 public:
CompressWriter(void * userContext,AndroidBitmap_CompressWriteFunc fn)196     CompressWriter(void* userContext, AndroidBitmap_CompressWriteFunc fn)
197           : mUserContext(userContext), mFn(fn), mBytesWritten(0) {}
198 
write(const void * buffer,size_t size)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 
bytesWritten() const207     size_t bytesWritten() const override { return mBytesWritten; }
208 
209 private:
210     void* mUserContext;
211     AndroidBitmap_CompressWriteFunc mFn;
212     size_t mBytesWritten;
213 };
214 
215 } // anonymous namespace
216 
ABitmap_compress(const AndroidBitmapInfo * info,ADataSpace dataSpace,const void * pixels,AndroidBitmapCompressFormat inFormat,int32_t quality,void * userContext,AndroidBitmap_CompressWriteFunc fn)217 int ABitmap_compress(const AndroidBitmapInfo* info, ADataSpace dataSpace, const void* pixels,
218                      AndroidBitmapCompressFormat inFormat, int32_t quality, void* userContext,
219                      AndroidBitmap_CompressWriteFunc fn) {
220     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;
261         case ANDROID_BITMAP_FORMAT_RGBA_1010102:
262             colorType = kRGBA_1010102_SkColorType;
263             break;
264         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);
305     return Bitmap::compress(bitmap, format, quality, &stream) ? ANDROID_BITMAP_RESULT_SUCCESS
306                                                               : ANDROID_BITMAP_RESULT_JNI_EXCEPTION;
307 }
308 
ABitmap_getHardwareBuffer(ABitmap * bitmapHandle)309 AHardwareBuffer* 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 }
317