• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <utils/Color.h>
28 
29 using namespace android;
30 
ABitmap_acquireBitmapFromJava(JNIEnv * env,jobject bitmapObj)31 ABitmap* ABitmap_acquireBitmapFromJava(JNIEnv* env, jobject bitmapObj) {
32     Bitmap* bitmap = GraphicsJNI::getNativeBitmap(env, bitmapObj);
33     if (bitmap) {
34         bitmap->ref();
35         return TypeCast::toABitmap(bitmap);
36     }
37     return nullptr;
38 }
39 
ABitmap_acquireRef(ABitmap * bitmap)40 void ABitmap_acquireRef(ABitmap* bitmap) {
41     SkSafeRef(TypeCast::toBitmap(bitmap));
42 }
43 
ABitmap_releaseRef(ABitmap * bitmap)44 void ABitmap_releaseRef(ABitmap* bitmap) {
45     SkSafeUnref(TypeCast::toBitmap(bitmap));
46 }
47 
getFormat(const SkImageInfo & info)48 static AndroidBitmapFormat getFormat(const SkImageInfo& info) {
49     switch (info.colorType()) {
50         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         case kRGBA_1010102_SkColorType:
61             return ANDROID_BITMAP_FORMAT_RGBA_1010102;
62         default:
63             return ANDROID_BITMAP_FORMAT_NONE;
64     }
65 }
66 
getColorType(AndroidBitmapFormat format)67 static SkColorType getColorType(AndroidBitmapFormat format) {
68     switch (format) {
69         case ANDROID_BITMAP_FORMAT_RGBA_8888:
70             return kN32_SkColorType;
71         case ANDROID_BITMAP_FORMAT_RGB_565:
72             return kRGB_565_SkColorType;
73         case ANDROID_BITMAP_FORMAT_RGBA_4444:
74             return kARGB_4444_SkColorType;
75         case ANDROID_BITMAP_FORMAT_A_8:
76             return kAlpha_8_SkColorType;
77         case ANDROID_BITMAP_FORMAT_RGBA_F16:
78             return kRGBA_F16_SkColorType;
79         case ANDROID_BITMAP_FORMAT_RGBA_1010102:
80             return kRGBA_1010102_SkColorType;
81         default:
82             return kUnknown_SkColorType;
83     }
84 }
85 
getAlphaFlags(const SkImageInfo & info)86 static uint32_t getAlphaFlags(const SkImageInfo& info) {
87     switch (info.alphaType()) {
88         case kUnknown_SkAlphaType:
89             LOG_ALWAYS_FATAL("Bitmap has no alpha type");
90             break;
91         case kOpaque_SkAlphaType:
92             return ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE;
93         case kPremul_SkAlphaType:
94             return ANDROID_BITMAP_FLAGS_ALPHA_PREMUL;
95         case kUnpremul_SkAlphaType:
96             return ANDROID_BITMAP_FLAGS_ALPHA_UNPREMUL;
97     }
98 }
99 
getInfoFlags(const SkImageInfo & info,bool isHardware)100 static uint32_t getInfoFlags(const SkImageInfo& info, bool isHardware) {
101     uint32_t flags = getAlphaFlags(info);
102     if (isHardware) {
103         flags |= ANDROID_BITMAP_FLAGS_IS_HARDWARE;
104     }
105     return flags;
106 }
107 
ABitmap_copy(ABitmap * srcBitmapHandle,AndroidBitmapFormat dstFormat)108 ABitmap* ABitmap_copy(ABitmap* srcBitmapHandle, AndroidBitmapFormat dstFormat) {
109     SkColorType dstColorType = getColorType(dstFormat);
110     if (srcBitmapHandle && dstColorType != kUnknown_SkColorType) {
111         SkBitmap srcBitmap;
112         TypeCast::toBitmap(srcBitmapHandle)->getSkBitmap(&srcBitmap);
113 
114         sk_sp<Bitmap> dstBitmap =
115                 Bitmap::allocateHeapBitmap(srcBitmap.info().makeColorType(dstColorType));
116         if (dstBitmap && srcBitmap.readPixels(dstBitmap->info(), dstBitmap->pixels(),
117                                               dstBitmap->rowBytes(), 0, 0)) {
118             return TypeCast::toABitmap(dstBitmap.release());
119         }
120     }
121     return nullptr;
122 }
123 
getInfo(const SkImageInfo & imageInfo,uint32_t rowBytes,bool isHardware)124 static AndroidBitmapInfo getInfo(const SkImageInfo& imageInfo, uint32_t rowBytes, bool isHardware) {
125     AndroidBitmapInfo info;
126     info.width = imageInfo.width();
127     info.height = imageInfo.height();
128     info.stride = rowBytes;
129     info.format = getFormat(imageInfo);
130     info.flags = getInfoFlags(imageInfo, isHardware);
131     return info;
132 }
133 
ABitmap_getInfo(ABitmap * bitmapHandle)134 AndroidBitmapInfo ABitmap_getInfo(ABitmap* bitmapHandle) {
135     Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
136     return getInfo(bitmap->info(), bitmap->rowBytes(), bitmap->isHardware());
137 }
138 
ABitmap_getDataSpace(ABitmap * bitmapHandle)139 ADataSpace ABitmap_getDataSpace(ABitmap* bitmapHandle) {
140     Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
141     const SkImageInfo& info = bitmap->info();
142     return (ADataSpace)uirenderer::ColorSpaceToADataSpace(info.colorSpace(), info.colorType());
143 }
144 
ABitmap_getInfoFromJava(JNIEnv * env,jobject bitmapObj)145 AndroidBitmapInfo ABitmap_getInfoFromJava(JNIEnv* env, jobject bitmapObj) {
146     uint32_t rowBytes = 0;
147     bool isHardware = false;
148     SkImageInfo imageInfo = GraphicsJNI::getBitmapInfo(env, bitmapObj, &rowBytes, &isHardware);
149     return getInfo(imageInfo, rowBytes, isHardware);
150 }
151 
ABitmap_getPixels(ABitmap * bitmapHandle)152 void* ABitmap_getPixels(ABitmap* bitmapHandle) {
153     Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
154     if (bitmap->isHardware()) {
155         return nullptr;
156     }
157     return bitmap->pixels();
158 }
159 
ABitmapConfig_getFormatFromConfig(JNIEnv * env,jobject bitmapConfigObj)160 AndroidBitmapFormat ABitmapConfig_getFormatFromConfig(JNIEnv* env, jobject bitmapConfigObj) {
161     return GraphicsJNI::getFormatFromConfig(env, bitmapConfigObj);
162 }
163 
ABitmapConfig_getConfigFromFormat(JNIEnv * env,AndroidBitmapFormat format)164 jobject ABitmapConfig_getConfigFromFormat(JNIEnv* env, AndroidBitmapFormat format) {
165     return GraphicsJNI::getConfigFromFormat(env, format);
166 }
167 
ABitmap_notifyPixelsChanged(ABitmap * bitmapHandle)168 void ABitmap_notifyPixelsChanged(ABitmap* bitmapHandle) {
169     Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
170     if (!bitmap->isImmutable()) {
171         bitmap->notifyPixelsChanged();
172     }
173 }
174 
175 namespace {
getAlphaType(const AndroidBitmapInfo * info)176 SkAlphaType getAlphaType(const AndroidBitmapInfo* info) {
177     switch (info->flags & ANDROID_BITMAP_FLAGS_ALPHA_MASK) {
178         case ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE:
179             return kOpaque_SkAlphaType;
180         case ANDROID_BITMAP_FLAGS_ALPHA_PREMUL:
181             return kPremul_SkAlphaType;
182         case ANDROID_BITMAP_FLAGS_ALPHA_UNPREMUL:
183             return kUnpremul_SkAlphaType;
184         default:
185             return kUnknown_SkAlphaType;
186     }
187 }
188 
189 class CompressWriter : public SkWStream {
190 public:
CompressWriter(void * userContext,AndroidBitmap_CompressWriteFunc fn)191     CompressWriter(void* userContext, AndroidBitmap_CompressWriteFunc fn)
192           : mUserContext(userContext), mFn(fn), mBytesWritten(0) {}
193 
write(const void * buffer,size_t size)194     bool write(const void* buffer, size_t size) override {
195         if (mFn(mUserContext, buffer, size)) {
196             mBytesWritten += size;
197             return true;
198         }
199         return false;
200     }
201 
bytesWritten() const202     size_t bytesWritten() const override { return mBytesWritten; }
203 
204 private:
205     void* mUserContext;
206     AndroidBitmap_CompressWriteFunc mFn;
207     size_t mBytesWritten;
208 };
209 
210 } // anonymous namespace
211 
ABitmap_compress(const AndroidBitmapInfo * info,ADataSpace dataSpace,const void * pixels,AndroidBitmapCompressFormat inFormat,int32_t quality,void * userContext,AndroidBitmap_CompressWriteFunc fn)212 int ABitmap_compress(const AndroidBitmapInfo* info, ADataSpace dataSpace, const void* pixels,
213                      AndroidBitmapCompressFormat inFormat, int32_t quality, void* userContext,
214                      AndroidBitmap_CompressWriteFunc fn) {
215     Bitmap::JavaCompressFormat format;
216     switch (inFormat) {
217         case ANDROID_BITMAP_COMPRESS_FORMAT_JPEG:
218             format = Bitmap::JavaCompressFormat::Jpeg;
219             break;
220         case ANDROID_BITMAP_COMPRESS_FORMAT_PNG:
221             format = Bitmap::JavaCompressFormat::Png;
222             break;
223         case ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSY:
224             format = Bitmap::JavaCompressFormat::WebpLossy;
225             break;
226         case ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSLESS:
227             format = Bitmap::JavaCompressFormat::WebpLossless;
228             break;
229         default:
230             // kWEBP_JavaEncodeFormat is a valid parameter for Bitmap::compress,
231             // for the deprecated Bitmap.CompressFormat.WEBP, but it should not
232             // be provided via the NDK. Other integers are likewise invalid.
233             return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
234     }
235 
236     SkColorType colorType;
237     switch (info->format) {
238         case ANDROID_BITMAP_FORMAT_RGBA_8888:
239             colorType = kN32_SkColorType;
240             break;
241         case ANDROID_BITMAP_FORMAT_RGB_565:
242             colorType = kRGB_565_SkColorType;
243             break;
244         case ANDROID_BITMAP_FORMAT_A_8:
245             // FIXME b/146637821: Should this encode as grayscale? We should
246             // make the same decision as for encoding an android.graphics.Bitmap.
247             // Note that encoding kAlpha_8 as WebP or JPEG will fail. Encoding
248             // it to PNG encodes as GRAY+ALPHA with a secret handshake that we
249             // only care about the alpha. I'm not sure whether Android decoding
250             // APIs respect that handshake.
251             colorType = kAlpha_8_SkColorType;
252             break;
253         case ANDROID_BITMAP_FORMAT_RGBA_F16:
254             colorType = kRGBA_F16_SkColorType;
255             break;
256         case ANDROID_BITMAP_FORMAT_RGBA_1010102:
257             colorType = kRGBA_1010102_SkColorType;
258             break;
259         default:
260             return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
261     }
262 
263     auto alphaType = getAlphaType(info);
264     if (alphaType == kUnknown_SkAlphaType) {
265         return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
266     }
267 
268     sk_sp<SkColorSpace> cs;
269     if (info->format == ANDROID_BITMAP_FORMAT_A_8) {
270         // FIXME: A Java Bitmap with ALPHA_8 never has a ColorSpace. So should
271         // we force that here (as I'm doing now) or should we treat anything
272         // besides ADATASPACE_UNKNOWN as an error?
273         cs = nullptr;
274     } else {
275         cs = uirenderer::DataSpaceToColorSpace((android_dataspace) dataSpace);
276         // DataSpaceToColorSpace treats UNKNOWN as SRGB, but compress forces the
277         // client to specify SRGB if that is what they want.
278         if (!cs || dataSpace == ADATASPACE_UNKNOWN) {
279             return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
280         }
281     }
282 
283     {
284         size_t size;
285         if (!Bitmap::computeAllocationSize(info->stride, info->height, &size)) {
286             return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
287         }
288     }
289 
290     auto imageInfo =
291             SkImageInfo::Make(info->width, info->height, colorType, alphaType, std::move(cs));
292     SkBitmap bitmap;
293     // We are not going to modify the pixels, but installPixels expects them to
294     // not be const, since for all it knows we might want to draw to the SkBitmap.
295     if (!bitmap.installPixels(imageInfo, const_cast<void*>(pixels), info->stride)) {
296         return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
297     }
298 
299     CompressWriter stream(userContext, fn);
300     return Bitmap::compress(bitmap, format, quality, &stream) ? ANDROID_BITMAP_RESULT_SUCCESS
301                                                               : ANDROID_BITMAP_RESULT_JNI_EXCEPTION;
302 }
303 
ABitmap_getHardwareBuffer(ABitmap * bitmapHandle)304 AHardwareBuffer* ABitmap_getHardwareBuffer(ABitmap* bitmapHandle) {
305     Bitmap* bitmap = TypeCast::toBitmap(bitmapHandle);
306     AHardwareBuffer* buffer = bitmap->hardwareBuffer();
307     if (buffer) {
308         AHardwareBuffer_acquire(buffer);
309     }
310     return buffer;
311 }
312