• 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_CANVAS_H
17 #define ANDROID_GRAPHICS_CANVAS_H
18 
19 #include <android/graphics/bitmap.h>
20 #include <android/graphics/paint.h>
21 #include <android/native_window.h>
22 #include <android/rect.h>
23 #include <cutils/compiler.h>
24 #include <jni.h>
25 
26 __BEGIN_DECLS
27 
28 /**
29  * Opaque handle for a native graphics canvas.
30  */
31 typedef struct ACanvas ACanvas;
32 
33 //  One of AHardwareBuffer_Format.
34 ANDROID_API bool ACanvas_isSupportedPixelFormat(int32_t bufferFormat);
35 
36 /**
37  * Returns a native handle to a Java android.graphics.Canvas
38  *
39  * @return ACanvas* that is only valid for the life of the jobject.
40  */
41 ANDROID_API ACanvas* ACanvas_getNativeHandleFromJava(JNIEnv* env, jobject canvas);
42 
43 /**
44  * Creates a canvas that wraps the buffer
45  *
46  * @param buffer is a required param.  If no buffer is provided a nullptr will be returned.
47  */
48 ANDROID_API ACanvas* ACanvas_createCanvas(const ANativeWindow_Buffer* buffer,
49                               int32_t /*android_dataspace_t*/ dataspace);
50 
51 ANDROID_API void ACanvas_destroyCanvas(ACanvas* canvas);
52 
53 /**
54  * Updates the canvas to render into the pixels in the provided buffer
55  *
56  * @param buffer The buffer that will provide the backing store for this canvas.  The buffer must
57  *               remain valid until the this method is called again with either another active
58  *               buffer or nullptr.  If nullptr is given the canvas will release the previous buffer
59  *               and set an empty backing store.
60  * @return A boolean value indicating whether or not the buffer was successfully set. If false the
61  *         method will behave as if nullptr were passed as the input buffer and the previous buffer
62  *         will still be released.
63  */
64 ANDROID_API bool ACanvas_setBuffer(ACanvas* canvas, const ANativeWindow_Buffer* buffer,
65                        int32_t /*android_dataspace_t*/ dataspace);
66 
67 /**
68  * Clips operations on the canvas to the intersection of the current clip and the provided clipRect.
69  *
70  * @param clipRect required
71  */
72 ANDROID_API void ACanvas_clipRect(ACanvas* canvas, const ARect* clipRect, bool doAntiAlias = false);
73 
74 /**
75  * Clips operations on the canvas to the difference of the current clip and the provided clipRect.
76  *
77  * @param clipRect required
78  */
79 ANDROID_API void ACanvas_clipOutRect(ACanvas* canvas, const ARect* clipRect, bool doAntiAlias = false);
80 
81 /**
82  *
83  * @param rect required
84  * @param paint required
85  */
86 ANDROID_API void ACanvas_drawRect(ACanvas* canvas, const ARect* rect, const APaint* paint);
87 
88 /**
89  *
90  * @param bitmap required
91  * @param left
92  * @param top
93  * @param paint
94  */
95 ANDROID_API void ACanvas_drawBitmap(ACanvas* canvas, const ABitmap* bitmap, float left, float top,
96                         const APaint* paint);
97 
98 __END_DECLS
99 
100 #ifdef	__cplusplus
101 namespace android {
102 namespace graphics {
103     class Canvas {
104     public:
Canvas(JNIEnv * env,jobject canvasObj)105         Canvas(JNIEnv* env, jobject canvasObj) :
106                 mCanvas(ACanvas_getNativeHandleFromJava(env, canvasObj)),
107                 mOwnedPtr(false) {}
Canvas(const ANativeWindow_Buffer & buffer,int32_t dataspace)108         Canvas(const ANativeWindow_Buffer& buffer, int32_t /*android_dataspace_t*/ dataspace) :
109                 mCanvas(ACanvas_createCanvas(&buffer, dataspace)),
110                 mOwnedPtr(true) {}
~Canvas()111         ~Canvas() {
112             if (mOwnedPtr) {
113                 ACanvas_destroyCanvas(mCanvas);
114             }
115         }
116 
setBuffer(const ANativeWindow_Buffer * buffer,int32_t dataspace)117         bool setBuffer(const ANativeWindow_Buffer* buffer,
118                        int32_t /*android_dataspace_t*/ dataspace) {
119             return ACanvas_setBuffer(mCanvas, buffer, dataspace);
120         }
121 
122         void clipRect(const ARect& clipRect, bool doAntiAlias = false) {
123             ACanvas_clipRect(mCanvas, &clipRect, doAntiAlias);
124         }
125 
drawRect(const ARect & rect,const Paint & paint)126         void drawRect(const ARect& rect, const Paint& paint) {
127             ACanvas_drawRect(mCanvas, &rect, &paint.get());
128         }
drawBitmap(const Bitmap & bitmap,float left,float top,const Paint * paint)129         void drawBitmap(const Bitmap& bitmap, float left, float top, const Paint* paint) {
130             const APaint* aPaint = (paint) ? &paint->get() : nullptr;
131             ACanvas_drawBitmap(mCanvas, bitmap.get(), left, top, aPaint);
132         }
133 
134     private:
135         ACanvas* mCanvas;
136         const bool mOwnedPtr;
137     };
138 }; // namespace graphics
139 }; // namespace android
140 #endif // __cplusplus
141 
142 #endif // ANDROID_GRAPHICS_CANVAS_H