在讲解该小节之前,我们来回顾之前编写的一个程序SurfaceTest.cpp:
#include
#include
#include
#include
#include
#include
#include
#include
using namespace android;
int main()
{
// set up the thread-pool
sp proc(ProcessState::self());
ProcessState::self()->startThreadPool();
// create a client to surfaceflinger
/*获得SurfaceComposerClient服务*/
sp client = new SurfaceComposerClient();
/*创建Surface*/
sp surfaceControl = client->createSurface(String8("resize"),
160, 240, PIXEL_FORMAT_RGB_565, 0);
sp surface = surfaceControl->getSurface();
SurfaceComposerClient::openGlobalTransaction();
/*设置Z轴高度*/
surfaceControl->setLayer(100000);
SurfaceComposerClient::closeGlobalTransaction();
ANativeWindow_Buffer outBuffer;
/*获得这个surface的一个buf*/
surface->lock(&outBuffer, NULL);
ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format);
/*填充buf*/
android_memset16((uint16_t*)outBuffer.bits, 0xF800, bpr*outBuffer.height);
/*提交buf给surfaceflinger,让其显示*/
surface->unlockAndPost();
/*增加一个休眠时间*/
sleep(3);
surface->lock(&outBuffer,NULL);
android_memset16((uint16_t*)outBuffer.bits, 0x07E0, bpr*outBuffer.height);
surface->unlockAndPost();
sleep(3);
surface->lock(&outBuffer,NULL);
android_memset16((uint16_t*)outBuffer.bits, 0x001F, bpr*outBuffer.height);
surface->unlockAndPost();
sleep(3);
for(int i = 0; i lock(&outBuffer,NULL);
printf("%03d buff addr = 0x%x\n",i,(unsigned long)outBuffer.bits);
surface->unlockAndPost();
}
IPCThreadState::self()->joinThreadPool();
return 0;
}
主要流程如下:
int main()
// create a client to surfaceflinger
/*获得SurfaceComposerClient服务*/
sp client = new SurfaceComposerClient();
sp surface = surfaceControl->getSurface();ANativeWindow_Buffer outBuffer;
/*获得这个surface的一个buf*/
surface->lock(&outBuffer, NULL);
ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format);
/*填充buf*/
android_memset16((uint16_t*)outBuffer.bits, 0xF800, bpr*outBuffer.height);
/*提交buf给surfaceflinger,让其显示*/
surface->unlockAndPost();
下面是一个框图: 中间是SurfaceFlinger,左边是应用程序APP,每一个应用程序都对应SurfaceFlinger中的一个client。每个APP中存在一个一个或者多个Surface,每个Surface对应一个Layer,之前提到过,Layer中存在生产者和消费者,他们中都存在mcore,mcore中存在mslots[],包含了一个或者多个buffer。
应用程序通过:
/*获得这个surface的一个buf*/
surface->lock(&outBuffer, NULL);
获得buffer,通过:
/*提交buf给surfaceflinger,让其显示*/
surface->unlockAndPost();
提交buffer,最终都会调用到Layer中的生产者。如果是获得buffer调用生产者中的dequeueBuffer,获得buffer调用生产者的queueBuffer。APP的应用程序提交了buffer之后,需要将他们buffer合并起来,然后通过DisplyDevice显示器上显示出来。
在应用程序中,使用surface管理buffer,对于DisplyDevice其也存在多个buffer,如之前我们提到为了提性能创建有3个buffer。那么问你题来呢: 在哪个buffer上合并数据 这些buffer如何获得 答案很简单,DisplyDevice也是通过Surface管理这些buffer。其中也存在dequeueBuffer(向SurfaceFlinger申请FrameBuffer-显存),queueBuffer(提交FrameBuffer给SF进行显示)。
该小节我们讲解DisplyDevice的创建过程,同时也讲解其是如何管理这些buffer的,下面是创建DisplyDevice的流程图: 打开SurfaceFlinger.cpp中的init()方法,其是在main_SurfaceFlinger.cpp中被调用(flinger->init()):
void SurfaceFlinger::init() {
在DisplayDevice端的Surface也存在生产者和消费者,他们之中同样存在mcore成员,mcore成员成员存在mslot。进入DisplayDevice.cpp,找到其构造函数:
DisplayDevice::DisplayDevice(
mNativeWindow = surface = new Surface(producer, false);
其中Surface:
class Surface
: public ANativeObjectBase
并且ANativeObjectBase
class ANativeObjectBase : public NATIVE_TYPE, public REF
所以Surface可以转换为一个ANativeWindow指针
DisplayDevice::DisplayDevice(
mNativeWindow = surface = new Surface(producer, false);
ANativeWindow* const window = mNativeWindow.get();
他们的继承关系如下: 在Surface的最前面就是一个ANativeWindow对象。后面才是Surface成员。继续往下查看:
DisplayDevice::DisplayDevice(
mNativeWindow = surface = new Surface(producer, false);
ANativeWindow* const window = mNativeWindow.get();
eglSurface = eglCreateWindowSurface(display, config, window, NULL);
进入eglCreateWindowSurface:
EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
NativeWindowType window,
const EGLint *attrib_list)
{
return createWindowSurface(dpy, config, window, attrib_list);
}
我们再进入createWindowSurface:
static EGLSurface createWindowSurface(EGLDisplay dpy, EGLConfig config,NativeWindowType window, const EGLint* /*attrib_list*/)
surface = new egl_window_surface_v2_t(dpy, config, depthFormat,static_cast(window));
通过上面的分析我们可以知道: DisplayDevice中存在结构体.mSurface指向
surface = new egl_window_surface_v2_t(dpy, config, depthFormat,static_cast(window));
其上的window指向一个Surface对象,其egl_window_surface_v2_t函数如下:
egl_window_surface_v2_t::egl_window_surface_v2_t(EGLDisplay dpy,
EGLConfig config,
int32_t depthFormat,
ANativeWindow* window)
: egl_surface_t(dpy, config, depthFormat),
nativeWindow(window), buffer(0), previousBuffer(0), bits(NULL)
{
可以知道egl_window_surface_v2_t中存在一个成员为nativeWindow,他指向surface。我们在surface.cpp找到surface的构造函数:
Surface::Surface(
const sp& bufferProducer,
bool controlledByApp)
: mGraphicBufferProducer(bufferProducer),
mCrop(Rect::EMPTY_RECT),
mGenerationNumber(0),
mSharedBufferMode(false),
mAutoRefresh(false),
mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
mSharedBufferHasBeenQueued(false),
mNextFrameNumber(1)
{
// Initialize the ANativeWindow function pointers.
ANativeWindow::setSwapInterval = hook_setSwapInterval;
ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
ANativeWindow::cancelBuffer = hook_cancelBuffer;
ANativeWindow::queueBuffer = hook_queueBuffer;
ANativeWindow::query = hook_query;
ANativeWindow::perform = hook_perform;
ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
这个结果是非常的重要的。