• 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 #ifndef ANDROID_GRAPHICS_BITMAP_H
17 #define ANDROID_GRAPHICS_BITMAP_H
18 
19 #include <android/bitmap.h>
20 #include <android/data_space.h>
21 #include <cutils/compiler.h>
22 #include <jni.h>
23 #include <sys/cdefs.h>
24 
25 struct AHardwareBuffer;
26 
27 __BEGIN_DECLS
28 
29 /**
30  * Opaque handle for a native graphics bitmap.
31  */
32 typedef struct ABitmap ABitmap;
33 
34 /**
35  * Retrieve bitmapInfo for the provided java bitmap even if it has been recycled.  In the case of a
36  * recycled bitmap the values contained in the bitmap before it was recycled are returned.
37  *
38  * NOTE: This API does not need to remain as an APEX API if/when we pull libjnigraphics into the
39  *       UI module.
40  */
41 ANDROID_API AndroidBitmapInfo ABitmap_getInfoFromJava(JNIEnv* env, jobject bitmapObj);
42 
43 /**
44  *
45  * @return ptr to an opaque handle to the native bitmap or null if the java bitmap has been recycled
46  *         or does not exist.
47  */
48 ANDROID_API ABitmap* ABitmap_acquireBitmapFromJava(JNIEnv* env, jobject bitmapObj);
49 
50 ANDROID_API ABitmap* ABitmap_copy(ABitmap* srcBitmap, AndroidBitmapFormat dstFormat);
51 
52 ANDROID_API void ABitmap_acquireRef(ABitmap* bitmap);
53 ANDROID_API void ABitmap_releaseRef(ABitmap* bitmap);
54 
55 ANDROID_API AndroidBitmapInfo ABitmap_getInfo(ABitmap* bitmap);
56 ANDROID_API ADataSpace ABitmap_getDataSpace(ABitmap* bitmap);
57 
58 ANDROID_API void* ABitmap_getPixels(ABitmap* bitmap);
59 ANDROID_API void ABitmap_notifyPixelsChanged(ABitmap* bitmap);
60 
61 ANDROID_API AndroidBitmapFormat ABitmapConfig_getFormatFromConfig(JNIEnv* env, jobject bitmapConfigObj);
62 ANDROID_API jobject ABitmapConfig_getConfigFromFormat(JNIEnv* env, AndroidBitmapFormat format);
63 
64 // NDK access
65 ANDROID_API int ABitmap_compress(const AndroidBitmapInfo* info, ADataSpace dataSpace, const void* pixels,
66                      AndroidBitmapCompressFormat format, int32_t quality, void* userContext,
67                      AndroidBitmap_CompressWriteFunc);
68 /**
69  *  Retrieve the native object associated with a HARDWARE Bitmap.
70  *
71  *  Client must not modify it while a Bitmap is wrapping it.
72  *
73  *  @param bitmap Handle to an android.graphics.Bitmap.
74  *  @return on success, a pointer to the
75  *         AHardwareBuffer associated with bitmap. This acquires
76  *         a reference on the buffer, and the client must call
77  *         AHardwareBuffer_release when finished with it.
78  */
79 ANDROID_API AHardwareBuffer* ABitmap_getHardwareBuffer(ABitmap* bitmap);
80 
81 __END_DECLS
82 
83 #ifdef	__cplusplus
84 namespace android {
85 namespace graphics {
86     class Bitmap {
87     public:
Bitmap()88         Bitmap() : mBitmap(nullptr) {}
Bitmap(JNIEnv * env,jobject bitmapObj)89         Bitmap(JNIEnv* env, jobject bitmapObj) :
90                 mBitmap(ABitmap_acquireBitmapFromJava(env, bitmapObj)) {}
Bitmap(const Bitmap & src)91         Bitmap(const Bitmap& src) : mBitmap(src.mBitmap) { ABitmap_acquireRef(src.mBitmap); }
~Bitmap()92         ~Bitmap() { ABitmap_releaseRef(mBitmap); }
93 
94         // copy operator
95         Bitmap& operator=(const Bitmap& other) {
96             if (&other != this) {
97                 ABitmap_releaseRef(mBitmap);
98                 mBitmap = other.mBitmap;
99                 ABitmap_acquireRef(mBitmap);
100             }
101             return *this;
102         }
103 
104         // move operator
105         Bitmap& operator=(Bitmap&& other) {
106             if (&other != this) {
107                 ABitmap_releaseRef(mBitmap);
108                 mBitmap = other.mBitmap;
109                 other.mBitmap = nullptr;
110             }
111             return *this;
112         }
113 
copy(AndroidBitmapFormat dstFormat)114         Bitmap copy(AndroidBitmapFormat dstFormat) const {
115             return Bitmap(ABitmap_copy(mBitmap, dstFormat));
116         }
117 
isValid()118         bool isValid() const { return mBitmap != nullptr; }
isEmpty()119         bool isEmpty() const {
120             AndroidBitmapInfo info = getInfo();
121             return info.width <= 0 || info.height <= 0;
122         }
reset()123         void reset() {
124             ABitmap_releaseRef(mBitmap);
125             mBitmap = nullptr;
126         }
127 
get()128         ABitmap* get() const { return mBitmap; }
129 
getInfo()130         AndroidBitmapInfo getInfo() const { return ABitmap_getInfo(mBitmap); }
getDataSpace()131         ADataSpace getDataSpace() const { return ABitmap_getDataSpace(mBitmap); }
getPixels()132         void* getPixels() const { return ABitmap_getPixels(mBitmap); }
notifyPixelsChanged()133         void notifyPixelsChanged() const { ABitmap_notifyPixelsChanged(mBitmap); }
getHardwareBuffer()134         AHardwareBuffer* getHardwareBuffer() const { return ABitmap_getHardwareBuffer(mBitmap); }
135 
136     private:
137         // takes ownership of the provided ABitmap
Bitmap(ABitmap * bitmap)138         Bitmap(ABitmap* bitmap) : mBitmap(bitmap) {}
139 
140         ABitmap* mBitmap;
141     };
142 }; // namespace graphics
143 }; // namespace android
144 #endif // __cplusplus
145 
146 #endif // ANDROID_GRAPHICS_BITMAP_H
147