Android Graphics
Android Graphics
Jim Huang ( )
Developer & Co-Founder, 0xlab
jserv@0xlab.org Sep 24, 2011 / Study-Area
Rights to copy
Copyright 2011 0xlab http://0xlab.org/
Attribution ShareAlike 3.0 Corrections, suggestions, contributions and You are free translations are welcome! to copy, distribute, display, and perform the work to make derivative works Latest update: Nov 20, 2011 to make commercial use of the work Under the following conditions Attribution. You must give the original author credit. Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. For any reuse or distribution, you must make clear to others the license terms of this work. Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above. License text: http://creativecommons.org/licenses/by-sa/3.0/legalcode
contact@0xlab.org
Agenda
(1) Binder IPC (2) Android Graphics (3) 2D and Accelerations (4) OpenGL|ES
Notice: Before looking into Android Graphics, you should be aware of the design of Binder IPC, otherwise you would get lost!
Binder IPC
Kernel
IPC Abstraction
Intent AIDL
More abstract
Intent The highest level abstraction Inter process method invocation AIDL: Android Interface Definition Language binder: kernel driver ashmem: shared memory
Binder
Method invocation
caller
callee
caller
interface
How?
callee
interface
callee
10
caller
interface
Proxy
Binder in kernel
Binder Thread
callee
Stub callee
interface
11
android.os.Parcel
flatten
unflatten
transmit
12
UML Representation
<<interface>> implements
Proxy
Stub
13
UML Representation
caller
<<interface>> implements
calls
Proxy
AIDL
Auto generated from .aidl file
caller
<<interface>>
Proxy
Stub
callee
15
Activity
3
Looper
Main Thread
Kernel
16
Have internal status per thead Compare to UNIX socket: sockets have internal status per file descriptor (FD)
17
Binder
A pool of threads is associated to each service application to process incoming IPC (Inter-Process Communication). Binder performs mapping of object between two processes. Binder uses an object reference as an address in a processs memory space. Synchronous call, reference couting
Binder
binder
associated to PID
(FD can be shared among threads in the same process)
20
Transaction of Binder
binder_write_read write_size write_consumed write_buffer read_size read_consumed read_buffer read buffer write buffer
Transaction of Binder
Process A and B have different memory space. They can not see each other. Kernel Binder Process B Process A
Copy memory by copy_from _user Then, wake up process B Kernel Binder Process B
Internally, Android uses Binder for graphics data transaction across processes. It is fairly efficient.
22
Real Case
Binder IPC is used for communicating between Graphics client and server. Taken from http://www.cnblogs.com/xl19862005/archive/2011/11/17/2215363.html
23
Ashmem
a named memory block shared between processes that the kernel is allowed to free. This is notable as the kernel is not allowed to free standard shared memory. Similar to weak reference of Java. Useful to implement cache. Used in android.os.MemoryFile (Java), 2D memory allocator, etc.
24
Android Graphics
Surface
Source: frameworks/base/core/java/android/view/Surface.java
/* Handle on to a raw buffer that is being managed by the screen compositor */ public class Surface implements Parcelable { public Surface() {
mCanvas = new CompatibleCanvas();
flatten
unflatten
transmit
Properties
Android SurfaceFlinger
Can combine 2D/3D surfaces and surfaces from multiple applications Surfaces passed as buffers via Binder IPC calls Can use OpenGL ES and 2D hardware accelerator for its compositions
2D and Accelerator
Skia
SkBitmap SkBitmap::Allocator GPU integration in Android 4.0
Set clip region Acquire font cache Show glyphs BatchBlit() Allocate font cache Surface composite Blit() TileBlit() Prepare / finish composite Clear() Blit(), memcpy()
2D Compositing
SRC
1 -asrc
DST
Compositing
SRC & NO-DST
Enable 2D Accelerator
libui
LibGLES (libagl)
libpixelflinger
Copybit could improve the performance of page flipping Copybit could improve the performance of page flipping
Copybit operations
Copybit: 2D blitter Copybit: 2D blitter
PMEM:
Hardware graphics/bitblt operations (blitter) needs physical continuous memory to manipulate. Android use libgralloc to allocate pmem (physical continuous memory) for android native buffer. pmem driver can support up to 12 devices, we have only one for copybit (Android, android native buffer) While running 0xbench, the peak size of the pmem allocated (mapped) is 25194496 bytes.
Take Qualcomm MSM7x25 for example: Take Qualcomm MSM7x25 for example: /dev/pmem /dev/pmem /dev/pmem_adsp /dev/pmem_adsp For multimedia codec, audio, video, camera For multimedia codec, audio, video, camera
gralloc_alloc
Offset = Alloc(size), PMEM_CONNECT, PMEM_MAP, memset If success, new private_handle_t(fd, size, flags); gralloc module
gralloc_free
PMEM_UNMAP, deallocate(offset)
pmem memory
(mmaped virtual address)base 0x49422336, c06c4000, 6c4000 buffer base = base + offset Buffer 2 master_fd (mmap size: 47MB) Buffer 1 fd when being freed, this will be closed
Buffer 3
Quantum
pmem pmem can have up to 12 devices..., we have only one for copybit (Android native buffer)
gralloc/copybit structure
android_native_buffer_t buffer_handle_t handle; copybit_image_t native_handle_t* handle;
native_handle_t
private_handle_t
Fd (pmem dev) Offset Size Base Flags lockState
buffer_handle_t
libgralloc
virtual address
private_handle_t::PRIV_FLAGS_U SES_PMEM
private_handle_t::LOCK_STATE_MAPPED
OpenGL|ES
Key Concepts
Android is moving to OpenGL|ES accelerated rendering since version 2.x Window systems already comprehend z-order 3D != 2D with depth OpenGL is object based
Describes a scene using its components and properties. Output quality is dependent on the renderer, not source
OpenGL Terminology
OpenGL
An API from Khronos (from SGI), for constructing a 3D object, doing operations on the object, and displaying it
Primitives
Triangles, Lines, Points, that can be specified through vertices to define an arbitrary shape
Texture
Small (!) bitmap to make objects more realistic
EGL
The EGL API defines a portable mechanism for creating GL contexts and windows for rendering into, which may be used in conjunction with different native platform window systems using the WSEGL layer
EGL
EGL (Embedded-System Graphics Library) is an interface between Khronos rendering APIs (such as OpenGL ES or OpenVG) and the underlying native platform window system. EGL handles graphics context management, surface/buffer binding, and rendering synchronization and enables high-performance, accelerated, mixedmode 2D and 3D rendering using other Khronos APIs. EGL Surfaces windows - on-screen rendering pbuffers - off-screen rendering pixmaps - off-screen rendering
agl = android software agl = android software OpenGL|ES renderer OpenGL|ES renderer
http://0xlab.org