• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Android > android graphic(15)—fence

android graphic(15)—fence

作者:网友 字体:[增加 减小] 来源:互联网 时间:2017-05-26

网友通过本文主要向大家介绍了android graphic,e graphic15 eq,android fence,android fence机制,graphic等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

android graphic(15)—fence


首先,fence的产生和GPU有很大的关系,下面是wiki上GPU的介绍。

A graphics processing unit (GPU), also occasionally called visual processing unit (VPU), is a specialized electronic circuit designed to rapidly manipulate and alter memory to accelerate the creation of images in a frame buffer intended for output to a display. GPUs are used in embedded systems, mobile phones, personal computers, workstations, and game consoles. Modern GPUs are very efficient at manipulating computer graphics and image processing, and their highly parallel structure makes them more effective than general-purpose CPUs for algorithms where the processing of large blocks of visual data is done in parallel.

GPU的产生就是为了加速图形显示到display的过程。现在广泛使用在嵌入式设备,手机,pc,服务器,游戏机等上。GPU在图形处理上非常高效,此外它的并行架构使得在处理大规模的并行数据上性能远超CPU,以前上学的时候做过CUDA相关的东西,印象很深,对于并行数据处理能力提升了起码10倍。
而CPU和GPU两个硬件是异步的,当使用opengl时,首先在CPU上调用一系列gl命令,然后这些命令去GPU执行真正的绘图过程,绘图何时结束,CPU根本不知道,当然可以让CPU阻塞等待GPU绘图完成,然后再去处理后续工作,但是这样效率就太低了。

下面的例子非常形象,说明了fence在GPU和CPU之间协调工作,fence让GPU和CPU并行运行,提高了图像显示的速度。

For example, an application may queue up work to be carried out in the GPU. The GPU then starts drawing that image. Although the image hasn’t been drawn into memory yet, the buffer pointer can still be passed to the window compositor along with a fence that indicates when the GPU work will be finished. The window compositor may then start processing ahead of time and hand off the work to the display controller. In this manner, the CPU work can be done ahead of time. Once the GPU finishes, the display controller can immediately display the image.

fence如何使用

一般fence的使用方法如下,

            //首先创建一个EGLSyncKHR 同步对象
            EGLSyncKHR sync = eglCreateSyncKHR(dpy,
                    EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
                         if (sync == EGL_NO_SYNC_KHR) {
                ST_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
                        eglGetError());
                return UNKNOWN_ERROR;
            }
            //将opengl cmd 缓冲队列中的cmd全部flush去执行,而不用去等cmd缓冲区满了再执行
            glFlush();
            //将同步对象sync转换为fencefd
            int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
            eglDestroySyncKHR(dpy, sync);
            if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
                ST_LOGE("syncForReleaseLocked: error dup'ing native fence "
                        "fd: %#x", eglGetError());
                return UNKNOWN_ERROR;
            }
            //利用fencefd,新建一个fence对象
            sp fence(new Fence(fenceFd));
            //将新创建的fence和老的fence merge
            status_t err = addReleaseFenceLocked(mCurrentTexture,
                    mCurrentTextureBuf, fence);

其中,addReleaseFenceLocked为

status_t ConsumerBase::addReleaseFenceLocked(int slot,
        const sp graphicBuffer, const sp& fence) {
    CB_LOGV("addReleaseFenceLocked: slot=%d", slot);

    // If consumer no longer tracks this graphicBuffer, we can safely
    // drop this fence, as it will never be received by the producer.
    if (!stillTracking(slot, graphicBuffer)) {
        return OK;
    }

    //老的fence为null,直接赋值
    if (!mSlots[slot].mFence.get()) {
        mSlots[slot].mFence = fence;
    } else {
       //否则执行merge
        sp mergedFence = Fence::merge(
                String8::format("%.28s:%d", mName.string(), slot),
                mSlots[slot].mFence, fence);
        if (!mergedFence.get()) {
            CB_LOGE("failed to merge release fences");
            // synchronization is broken, the best we can do is hope fences
            // signal in order so the new fence will act like a union
            mSlots[slot].mFence = fence;
            return BAD_VALUE;
        }
        mSlots[slot].mFence = mergedFence;
    }

    return OK;
}

关于Fence对象,只有当mFenceFd不等于-1的时候才是有效的fence,即可以起到“拦截”作用,让CPU和GPU进行同步。

//NO_FENCE对应的mFenceFd为-1
const sp Fence::NO_FENCE = sp(new Fence);

Fence::Fence() :
    mFenceFd(-1) {
}

Fence::Fence(int fenceFd) :
    mFenceFd(fenceFd) {
}

而Fence这个类,由于实现了Flattenable协议,所以可以利用binder传递。

Most recent Android devices support the “sync framework”. This allows the system to do some nifty thing when combined with hardware components that can manipulate graphics data asynchronously. For example, a producer can submit a series of OpenGL ES drawing commands and then enqueue the output buffer before rendering completes. The buffer is accompanied by a fence that signals when the contents are ready. A second fence accompanies the buffer when it is returned to the free list, so that the consumer can release the buffer while the contents are still in use. This approach improves latency and throughput as the buffers move through the system.

上面这段话结合BufferQueue的生产者和消费者模式更容易理解,描述了fence如何提升graphic的显示性能。生产者利用opengl绘图,不用等绘图完成,直接queue buffer,在queue buffer的同时,需要传递给BufferQueue一个fence,而消费者acquire这个buffer后同时也会获取到这个fence,这个fence在GPU绘图完成后signal。这就是所谓的“acquireFence”,用于生产者通知消费者生产已完成。
当消费者对acquire到的buffer做完自己要做的事情后(例如把buffer交给surfaceflinger去合成),就要把buffer release到BufferQueue的free list,由于该buffer的内容可能正在被surfaceflinger使用,所以release时也需要传递一个fence,用来指示该buffer的内容是否依然在被使用,接下来生产者在继续dequeue buffer时,如果dequeue到了这个buffer,在使用前先要等待该fence signal。这就是所谓的“releaseFence”,后者用于消费者通知生产者消费已完成。

一般来说,fence对象(new Fence)在一个BufferQueue对应的生产者和消费者之间通过binder传递,不会在不同的BufferQueue中传递(但是对利用overlay合成的layer,其所对应的acquire fence,会被传递到HWComposer中,因为overlay直接会由hal层的hwcomposer去合成,其使用的graphic buffer是上层surface中render的buffer,如果上层surface使用opengl合成,那么在hwcomposer对overlay合成前先要保证render完成(画图完成),即在hwcomposer中等待这个fence触发,所以fence需要首先被传递到hal层,但是这个fence的传递不是通过BufferQueue的binder传递,而是利用具体函数去实现,后续有分析)。

由于opengl的实现分为软件和硬件,所以下面结合代码分别分析。

软件实现的opengl

opengl的软件实现,也就是agl,虽然4.4上已经舍弃了,但是在一个项目中由于没有GPU,overlay,所以只能使用agl去进行layer的合成。agl的eglCreateSyncKHR函数如下,其中的注释写的很清晰,agl是同步的,因为不牵扯GPU。所以通过agl创建的fence的mFenceFd都是-1。

EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type,
        const EGLint *attrib_list)
{
    if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
        return setError(EGL_BAD_DISPLAY, EGL_NO_SYNC_KHR);
    }

    if (type != EGL_SYNC_FENCE_KHR ||
            (attrib_list != NULL && attrib_list[0] != EGL_NONE)) {
        return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SYNC_KHR);
    }

    if (eglGetCurrentContext() == EGL_NO_CONTEXT) {
        return setError(EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
    }

    // AGL is synchronous; nothing to do h



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • android graphic(15)—fence

相关文章

  • 2017-05-26安卓003快速入门
  • 2017-05-26安卓第十六天笔记-音频与视频播放,安卓第视频播放
  • 2017-05-26解析View的getDrawingCache方法,getdrawingcache
  • 2017-05-26Android版本和API Level对应关系,androidlevel
  • 2017-05-26安卓开源项目周报0411,安卓开源项目0411
  • 2017-05-26在 Android 上使用 RxNetty
  • 2017-05-26Android之使用Bundle进行IPC,androidbundleipc
  • 2017-05-26android StringBuffer 和StringBuilder,androidstringbuffer
  • 2017-08-0258同城Android端HTTPS实践之旅
  • 2017-05-26360多渠道打包,360打包

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 安卓界面高级组件------拖动条和评星条,安卓------拖动
    • HandlerThread,handlerthread用法
    • android开发时间和日期的代码实现工具类(一),android工具类
    • 2.4.5 ListView简单实用
    • [android] 手机卫士接收短信指令执行相应操作,android接收短信
    • linux2.4.18----25.文件系统的构建
    • AsyncTask基础(笔记)
    • Afinal,afinal框架
    • Android中Activity的四大启动模式实验简述,androidactivity
    • Android学习笔记-TextView(文本框)(一),android-textview

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有