• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: How to draw using the SKIA C++ API on a Java Canvas object on Android?

Resolved: How to draw using the SKIA C++ API on a Java Canvas object on Android?

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I am experimenting with native drawing using the Java Canvas API on Android. As far as I can understand, Canvas is built as an abstraction on top of SKCanvas from the Skia graphics engine. I’m willing to pass the Canvas object obtained in the OnDraw() method of a view to native code and draw using C++ and the Skia API.
What would be the best way to gain access to the SkCanvas pointer after passing the java Canvas object to native code using JNI?
Here is the information I have been able to assimilate, which ultimately boils down to accessing a private member from the instance a native platform object.
Source code of Canvas.java from https://cs.android.com/android/platform/superproject/+/master:frameworks/base/graphics/java/android/graphics/Canvas.java;l=59 shows that it has private variable named mNativeCanvasWrapper (defined in frameworks/base/graphics/java/android/graphics/BaseCanvas.java) which can be accessed via reflection through the function declared below.
mNativeCanvasWrapper, among other things, is passed to all functions responsible for drawing. Following the trail of one such function (getWidth()), I can see it’s JNI registration in https://cs.android.com/android/platform/superproject/+/master:frameworks/base/libs/hwui/jni/android_graphics_Canvas.cpp;l=692
Definition of CanvasJNI::getWidth()
Definition of get_canvas(canvasHandle)
The above Canvas* pointer actually points to a class called SkiaCanvas, which inherits from the Canvas base class. Definition for the class can be found at frameworks/base/libs/hwui/SkiaCanvas.cpp. This is the class that contains the actual pointer to the SKCanvas object that I need (marked private), as can be seen at https://cs.android.com/android/platform/superproject/+/master:frameworks/base/libs/hwui/SkiaCanvas.h;l=219.
How can I obtain the SKCanvas pointer from a SkiaCanvas pointer, given that it’s marked private and the only getter is marked protected?

Answer:

The answer is in two parts.
First, getting the Canvas* out of the JNI object:
The second part goes from there:

If you have better answer, please add a comment about this, thank you!

android android-canvas java-native-interface skia
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Openlayers: Update clusters depending on filter (geometryFunction)

24/03/2023

Resolved: Getting ‘502 Bad Gateway’ while deploying Springboot app in EKS

24/03/2023

Resolved: Why is NGINX’s $request_uri empty?

24/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.