xref: /aosp_15_r20/frameworks/base/libs/hwui/apex/include/android/graphics/bitmap.h (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
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 // If gainmapPixels is null, then no gainmap is encoded, and hdrSdrRatio is
69 // unused
70 ANDROID_API int ABitmap_compressWithGainmap(const AndroidBitmapInfo* info, ADataSpace dataSpace,
71                                             const void* pixels, const void* gainmapPixels,
72                                             float hdrSdrRatio, AndroidBitmapCompressFormat format,
73                                             int32_t quality, void* userContext,
74                                             AndroidBitmap_CompressWriteFunc);
75 /**
76  *  Retrieve the native object associated with a HARDWARE Bitmap.
77  *
78  *  Client must not modify it while a Bitmap is wrapping it.
79  *
80  *  @param bitmap Handle to an android.graphics.Bitmap.
81  *  @return on success, a pointer to the
82  *         AHardwareBuffer associated with bitmap. This acquires
83  *         a reference on the buffer, and the client must call
84  *         AHardwareBuffer_release when finished with it.
85  */
86 ANDROID_API AHardwareBuffer* ABitmap_getHardwareBuffer(ABitmap* bitmap);
87 
88 __END_DECLS
89 
90 #ifdef	__cplusplus
91 namespace android {
92 namespace graphics {
93     class Bitmap {
94     public:
Bitmap()95         Bitmap() : mBitmap(nullptr) {}
Bitmap(JNIEnv * env,jobject bitmapObj)96         Bitmap(JNIEnv* env, jobject bitmapObj) :
97                 mBitmap(ABitmap_acquireBitmapFromJava(env, bitmapObj)) {}
Bitmap(const Bitmap & src)98         Bitmap(const Bitmap& src) : mBitmap(src.mBitmap) { ABitmap_acquireRef(src.mBitmap); }
~Bitmap()99         ~Bitmap() { ABitmap_releaseRef(mBitmap); }
100 
101         // copy operator
102         Bitmap& operator=(const Bitmap& other) {
103             if (&other != this) {
104                 ABitmap_releaseRef(mBitmap);
105                 mBitmap = other.mBitmap;
106                 ABitmap_acquireRef(mBitmap);
107             }
108             return *this;
109         }
110 
111         // move operator
112         Bitmap& operator=(Bitmap&& other) {
113             if (&other != this) {
114                 ABitmap_releaseRef(mBitmap);
115                 mBitmap = other.mBitmap;
116                 other.mBitmap = nullptr;
117             }
118             return *this;
119         }
120 
copy(AndroidBitmapFormat dstFormat)121         Bitmap copy(AndroidBitmapFormat dstFormat) const {
122             return Bitmap(ABitmap_copy(mBitmap, dstFormat));
123         }
124 
isValid()125         bool isValid() const { return mBitmap != nullptr; }
isEmpty()126         bool isEmpty() const {
127             AndroidBitmapInfo info = getInfo();
128             return info.width <= 0 || info.height <= 0;
129         }
reset()130         void reset() {
131             ABitmap_releaseRef(mBitmap);
132             mBitmap = nullptr;
133         }
134 
get()135         ABitmap* get() const { return mBitmap; }
136 
getInfo()137         AndroidBitmapInfo getInfo() const { return ABitmap_getInfo(mBitmap); }
getDataSpace()138         ADataSpace getDataSpace() const { return ABitmap_getDataSpace(mBitmap); }
getPixels()139         void* getPixels() const { return ABitmap_getPixels(mBitmap); }
notifyPixelsChanged()140         void notifyPixelsChanged() const { ABitmap_notifyPixelsChanged(mBitmap); }
getHardwareBuffer()141         AHardwareBuffer* getHardwareBuffer() const { return ABitmap_getHardwareBuffer(mBitmap); }
142 
143     private:
144         // takes ownership of the provided ABitmap
Bitmap(ABitmap * bitmap)145         Bitmap(ABitmap* bitmap) : mBitmap(bitmap) {}
146 
147         ABitmap* mBitmap;
148     };
149 }; // namespace graphics
150 }; // namespace android
151 #endif // __cplusplus
152 
153 #endif // ANDROID_GRAPHICS_BITMAP_H
154