Android Graphics - Synthesis and Display - SurfaceTestDemo

Table of contents

introduction:

Main program code:

The result is presented:

summary:


introduction:

Visualize the rendering and display process of the native layer Surface of the Android system through the simplest test program.

Main program code:

#include <cutils/memory.h>
#include <utils/Log.h>
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <gui/Surface.h>
#include <gui/SurfaceComposerClient.h>
#include <android/native_window.h>

using namespace android;

//int main(int argc, char** argv)
int main()
{
    // 设置线程池
    sp<ProcessState> proc(ProcessState::self());
    ProcessState::self()->startThreadPool();

    // 创建与Surfaceflinger通信的客户端
    sp<SurfaceComposerClient> client = new SurfaceComposerClient();
    // 创建SurfaceControl并设置名称“resize”、宽高、像素格式,红色分量使用 5 位,绿色分量使用 6 位,蓝色分量使用 5 位。
    sp<SurfaceControl> surfaceControl = client->createSurface(String8("resize"),
            400, 600, PIXEL_FORMAT_RGB_565, 0);
    //通过SurfaceControl获取一个Surface
    sp<Surface> surface = surfaceControl->getSurface();

    SurfaceComposerClient::Transaction t;
    //设置Layer层级,数值越大层级越高
    t.setLayer(surfaceControl, 100000)
        .apply();

    ANativeWindow_Buffer outBuffer;
    //从BufferQueue中获取获取一个Buffer
    surface->lock(&outBuffer, NULL);
    //计算每个像素点的字节大小
    ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format);
    //使用memset16对buffer赋值(颜色值)
    android_memset16((uint16_t*)outBuffer.bits, 0xF800, bpr*outBuffer.height);
    //提交填充后的buffer
    surface->unlockAndPost();
    sleep(1);

    surface->lock(&outBuffer, NULL);
    android_memset16((uint16_t*)outBuffer.bits, 0x07E0, bpr*outBuffer.height);
    surface->unlockAndPost();
    sleep(1);

    surface->lock(&outBuffer, NULL);
    android_memset16((uint16_t*)outBuffer.bits, 0x001F, bpr*outBuffer.height);
    surface->unlockAndPost();
    sleep(1);

    //对outbuffer进行100次连续的获取与释放
    for (int i = 0; i < 100; i++) {
        surface->lock(&outBuffer, NULL);
        printf("%03d buff addr = 0x%x\n", i, (unsigned int)(uintptr_t)outBuffer.bits);
        surface->unlockAndPost();
    }
    
    IPCThreadState::self()->joinThreadPool();

    return 0;
}

The result is presented:

summary:

1. Based on the thread pool, create a client and connect with the server SurfaceFlinger, which has a basic channel for communication and interaction

2. Create a Surface and directly manage the Surface through SurfaceControl (the demo only shows the lock and unlockAndPost of the Surface to obtain the buffer)

3. The bottom layer implements the pipeline mode of the producer and consumer models through BufferQueue, reads and writes the graphics buffer, and performs drawing and rendering.

Guess you like

Origin blog.csdn.net/haigand/article/details/132228531