status_t SurfaceFlinger::readyToRun() {
LOGI( “SurfaceFlinger’s main thread ready to run. ” “Initializing graphics H/W…”);
// we only support one display currently int dpy = 0;
{
// initialize the main display
GraphicPlane& plane(graphicPlane(dpy));
DisplayHardware* const hw = new DisplayHardware(this, dpy); plane.setDisplayHardware(hw); }
// create the shared control-block
//创建共享控制块,因未指定在何处分配内存,默认的是ashmem上 mServerHeap = new MemoryHeapBase(4096,
MemoryHeapBase::READ_ONLY, “SurfaceFlinger read-only heap”);
LOGE_IF(mServerHeap==0, “can’t create shared memory dealer”);
mServerCblk = static_cast
LOGE_IF(mServerCblk==0, “can’t get to shared control block’s address”);
new(mServerCblk) surface_flinger_cblk_t;
// initialize primary screen
// (other display should be initialized in the same manner, but
// asynchronously, as they could come and go. None of this is supported yet).
const GraphicPlane& plane(graphicPlane(dpy)); const DisplayHardware& hw = plane.displayHardware(); const uint32_t w = hw.getWidth(); const uint32_t h = hw.getHeight(); const uint32_t f = hw.getFormat(); hw.makeCurrent();
// initialize the shared control block
mServerCblk->connected |= 1<
dcblk->orientation = ISurfaceComposer::eOrientationDefault; dcblk->xdpi = hw.getDpiX(); dcblk->ydpi = hw.getDpiY(); dcblk->fps = hw.getRefreshRate(); dcblk->density = hw.getDensity();
……//省略部分代码
}
它为该控制块在ashmem上分配内存并对其初始化赋值。
SharedClient控制块
在SurfaceControl的getSurface() 时,会创建一个Surface,接着调用SurfaceClient的构造函数,由于SurfaceClient是singleton,即系统中只有一个实例,因此,在其第一次创建时调用了构造函数,而在其构造函数中调用了createClientConnection,这个就是建立连接的过程,就是分配内存控制块的过程。在client侧,其代码如下:
SurfaceClient(): Singleton
sp
mClient = sf->createClientConnection();//创建client连接 if (mClient != NULL) {//创建成功的话
mControlMemory = mClient->getControlBlock();//获取内存块 if (mControlMemory != NULL) {
mControl = static_cast
mControlMemory->getBase());//得到基地址,并转换为SharedClient指针,然后赋值给mControl if (mControl) {
mStatus = NO_ERROR; } } } }
在server侧,它调用UserClient的构造函数:
UserClient::UserClient(const sp
const int pgsize = getpagesize();//获取内存页大小
const int cblksize = //控制块大小,即进行页对齐后的SharedClient大小 ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
mCblkHeap = new MemoryHeapBase(cblksize, 0,//在ashmem上分配内存 “SurfaceFlinger Client control-block”);
ctrlblk = static_cast
也就是说,建立一个clientConnection的过程,也就是在server侧:在堆上分配一块页对齐的内存块,它被用来存储SharedClient对象;之后,client侧同样也拥有该块内存,同样也被解释为SharedClient对象指针。TODO:它们是跨进程的,所以通过IMemoryHeap这个binder接口来操作。
那么SharedClient是何方神圣呢?在头文件SharedBufferStack.h中有代码注释:“
These classes manage a stack of buffers in shared memory.
SharedClient: represents a client with several stacks
SharedBufferStack: represents a stack of buffers
SharedBufferClient: manipulates the SharedBufferStack from the client side
SharedBufferServer: manipulates the SharedBufferStack from the server side
Buffers can be dequeued until there are none available, they can be locked
unless they are in use by the server, which is only the case for the last
dequeue-able buffer. When these various conditions are not met, the caller
waits until the condition is met.
”
我们先来看一下比较重要的一个类SharedBufferStack
class SharedBufferStack {
……//省略部分代码
struct SmallRect {//定义了一个矩形:左上角和右下角的坐标 uint16_t l, t, r, b; };
//定义了一个平面区域,包含了若干(不超过5个)矩形 struct FlatRegion { // 52 bytes = 4 * (1 + 2*N) static const unsigned int NUM_RECT_MAX = 5; uint32_t count;
SmallRect rects[NUM_RECT_MAX]; };
struct BufferData {//64Bytes FlatRegion dirtyRegion;//5个矩形区域 SmallRect crop;//1个剪切矩形区
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库Android的Graphic系统分析之skia(3)在线全文阅读。
相关推荐: