Android ndk provides the surface class, which can be used for screen plotting. However, this class has many functions,
If you do not pay attention, the code will not work normally.
The following are the problems we encountered and solutions.
1. To use the surface class in C ++ code, you must add permissions in Android. mk of the APK project file.
Uses-Permission
Android: Name = "android. Permission. access_surface_flinger"
If this statement is not found, the surface creation fails.
2. Create a surface
// SP client;
Client = new surfacecomposerclient ();
// Apply for a surface from surfaceflinger. The surface type is pushbuffers.
Displayinfo dispinfo;
Client-> getdisplayinfo (0, & dispinfo );
Int OK = client-> initcheck ();
// Create a surface with a 32-bit color. epushbuffers indicates that buffer is provided by us.
Surfacecontrol = client-> createsurface (getpid (), 0,800,
600,
Pixel_format_rgba_8888, isurfacecomposer: epushbuffers );
If (surfacecontrol = NULL ){
Return jni_false;
}
// Set the position, size, and Z-order
Client-> opentransaction ();
Surfacecontrol-> setposition (0, 0 );
Surfacecontrol-> setsize (800,600 );
Surfacecontrol-> setlayer (99999 );
Surfacecontrol-> show ();
Client-> closetransaction ();
3. Get the isurface object
// This sentence requires permissions. You need to add the frends class to surface. h.
Isurface = test: getisurface (surfacecontrol );
Here we use a technique. For more information, see "How to trick the compiler into getting an isurface object"
4. register the buffer
Static const char * pmem = "/dev/pmem ";
// This sentence requires the root permission
Heap = new memoryheapbase (pmem, 800*600*4 );
If (heap-> heapid () <0 ){
Return jni_false;
}
SP pmemheap = new memoryheappmem (heap, 0 );
If (pmemheap-> getheapid ()> = 0 ){
// Pmemheap-> slap ();
// Heap. Clear ();
// Pmemheap. Clear ();
Return jni_false;
}
Mbufferheap = isurface: bufferheap (800,600,
800,600, pixel_format_bgra_8888, pmemheap );
Int ret = isurface-> registerbuffers (mbufferheap );
Here, to open the/dev/pmem device file, you need to have the permission. The method is simple, that is, the root mobile phone,
Then use the command
Chmod A + RW/dev/pmem
To add permissions.
Remember to recover after use, otherwise there may be security risks.
For details about how to modify permissions in the code, see "Java code for modifying device file permissions by calling the su command ".
5. Obtain the buffer pointer.
Char * BP = static_cast (mbufferheap. Heap-> base ());
6. Write bitmap data to the buffer.
For specific code, see "How to Write bitmap data to surface"
7. Submit the modification and notify the surface flinger to update the screen.
Isurface-> postbuffer (0 );
In this way, you can see the display on the screen.
Http://blog.sina.com.cn/s/blog_53d1e55b0100ne4x.html