Surface的三种使用方式

Surface的三种使用方式

Native层的Surface工作方式有:仅在Proxy端、Proxy和Service端协同、仅在Service端工作三种方式(第三种方式有待确认)。前两种方式在Proxy端都有共同的预处理工作要做,如下:
Proxy端
1、创建SurfaceFlinger的本地代理
sp<SurfaceComposerClient> client = new SurfaceComposerClient();

2、创建SurfaceControl的本地代理
sp<SurfaceControl> surfaceControl = client->createSurface(……);

3、获取Surface本地代理
sp<Surface> surface = surfaceControl->getSurface();

拿到Surface本地代理后,就可以利用它进行后续相关操作了

方式一:参照resize.cpp的实现
Proxy端
1、获取Surface的缓冲区
ANativeWindow_Buffer outBuffer;
surface->lock(&outBuffer, NULL);

2、直接操作outBuffer.bits成员
android_memset16((uint16_t*)outBuffer.bits, 0xF800, bpr*outBuffer.height);

3、投送给SurfaceFlinger显示
surface->unlockAndPost();

方式二:参照Camera的实现
Proxy端
1、获取IGraphicBufferProducer本地代理
sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();

2、传递给Service端
camera-> setPreviewTarget(gbp);

Service端
3、根据gbp创建新的Surface
sp<ANativeWindow> window = new Surface(gpb, /*controlledByApp*/ true);

4、连接window到对应的api集合(用途???)
result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);

5、使用window的相关接口,如设置队列缓冲区数量为3
window->perform(window, NATIVE_WINDOW_SET_BUFFER_COUNT, 3);

方式三:在Surface的构造函数中有一个参数controlledByApp表示是否由Proxy端使用,如果仅在Service端使用可按默认值创建Surface。在SurfaceMediaSource_test有相关代码:
1、创建BufferQueue
sp<BufferQueuem> BufferQueue = new BufferQueue();

2、创建消费者监听器
wp<ConsumerListener> listener = static_cast<ConsumerListener*>(this);

3、创建消费者监听器代理
sp<BufferQueue::ProxyConsumerListener> proxy = new BufferQueue::ProxyConsumerListener(listener);

4、连接消费者到BufferQueue
mBufferQueue->consumerConnect(proxy, false);

5、根据BufferQueue创建新的Surface
sp<IGraphicBufferProducer> sms = mBufferQueue;
sp<Surface> stc = new Surface(sms);

6、使用window的相关接口,如perform()
sp<ANativeWindow> window = stc;
window->perform(window, NATIVE_WINDOW_SET_BUFFER_COUNT, 3);

注:
1、 方式三有待确认
2、 对于ANativeWindow的操作,官方提供了一个window.h辅助工具。

From:http://blog.csdn.net/alien75/article/details/41078963

猜你喜欢

转载自sunj.iteye.com/blog/2373124