Chinaunix首页 | 论坛 | 博客
  • 博客访问: 275705
  • 博文数量: 95
  • 博客积分: 2047
  • 博客等级: 大尉
  • 技术积分: 1022
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-14 16:18
文章分类

全部博文(95)

文章存档

2013年(1)

2011年(94)

我的朋友

分类: 嵌入式

2011-08-24 22:49:46

//这是CUPCAKE中ui/EGLDisplaySurface raw代码


/*
 **
 ** Copyright 2007 The Android Open Source Project
 **
 ** Licensed under the Apache License Version 2.0(the "License");
 ** you may not use this file except in compliance with the License.
 ** You may obtain a copy of the License at
 **
 **    
 **
 ** Unless required by applicable law or agreed to in writing software
 ** distributed under the License is distributed on an "AS IS" BASIS
 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND either express or implied.
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 */

#define LOG_TAG "EGLDisplaySurface"

#include
#include
#include
#include
#include
#include
#include
#include

#include
#include
#include

#include

#include
#include
#include
#include
#include

#if HAVE_ANDROID_OS
#include
#endif

#include

#include


// ----------------------------------------------------------------------------

egl_native_window_t* android_createDisplaySurface()
{
    egl_native_window_t* s = new android::EGLDisplaySurface();
    s->memory_type = NATIVE_MEMORY_TYPE_GPU;
    return s;
}

#define LIKELY( exp )       (__builtin_expect( (exp) != 0, true  ))
#define UNLIKELY( exp )     (__builtin_expect( (exp) != 0, false ))

// ----------------------------------------------------------------------------
namespace android {
// ----------------------------------------------------------------------------

EGLDisplaySurface::EGLDisplaySurface() //构造函数
    : EGLNativeSurface()
{
//填充基类成员
    egl_native_window_t::version = sizeof(egl_native_window_t); //must be sizeof(egl_native_window_t)
    egl_native_window_t::ident = 0; //ident is reserved for the Android platform
    egl_native_window_t::incRef = &EGLDisplaySurface::hook_incRef;//Hook called by EGL to hold a reference on this structure
    egl_native_window_t::decRef = &EGLDisplaySurface::hook_decRef;//Hook called by EGL to release a reference on this structure
        /*
     * Hook called by EGL to perform a page flip. This function
     * may update the size attributes above, in which case it returns
     * the EGL_NATIVES_FLAG_SIZE_CHANGED bit set.
     */
    egl_native_window_t::swapBuffers = &EGLDisplaySurface::hook_swapBuffers;
        /*
     * Hook called by EGL when the native surface is associated to EGL
     * (eglCreateWindowSurface). Can be NULL.
     */
    egl_native_window_t::connect = 0;
        /*
     * Hook called by EGL when eglDestroySurface is called.  Can be NULL.
     */
    egl_native_window_t::disconnect = 0;

    mFb[0].data = 0;//mFb是GGLSurface struct的对象
    mFb[1].data = 0;
    mBlitEngine = 0;//默认不使用硬件引擎
    
        /*
     *  Heap the offset above is based from
     */
    egl_native_window_t::fd = mapFrameBuffer();//fd是int型,应该是Framebuffer的句柄
    if (egl_native_window_t::fd >= 0) {
        
        hw_module_t const* module;  //HAL 接口指针
        //#define COPYBIT_HARDWARE_MODULE_ID "copybit"
        //获取copybit的HAL stub
        //libhardhare/hardware.c
        if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &module) == 0) {
            copybit_open(module, &mBlitEngine);//copy_bit提供的API,mBlitEngine为copybit_device_t对象
        }
        
        const float in2mm = 25.4f;//英尺和毫米换算
        float refreshRate = 1000000000000000LLU / (
                float( mInfo.upper_margin + mInfo.lower_margin + mInfo.yres )
                * ( mInfo.left_margin  + mInfo.right_margin + mInfo.xres )
                * mInfo.pixclock);

        const GGLSurface& buffer = mFb[1 - mIndex];
            /*
     * width, height and stride of the window in pixels
     * Any of these value can be nul in which case GL commands are
     * accepted and processed as usual, but not rendering occurs.
     */
        egl_native_window_t::width  = buffer.width;
        egl_native_window_t::height = buffer.height;
        egl_native_window_t::stride = buffer.stride;
        
        egl_native_window_t::format = buffer.format;//* format of the native window (see ui/PixelFormat.h)
        egl_native_window_t::base   = intptr_t(mFb[0].data);//Base memory virtual address of the surface in the CPU side
        egl_native_window_t::offset =
            intptr_t(buffer.data) - egl_native_window_t::base;//Offset of the bits in the VRAM
            /*
     * flags describing some attributes of this surface
     * EGL_NATIVES_FLAG_DESTROY_BACKBUFFER: backbuffer not preserved after 
     * eglSwapBuffers 这个标志位比较重要,关系到是否打开page_flip功能
     */
        egl_native_window_t::flags  = 0;
            /*
     * horizontal and vertical resolution in DPI
     */
        egl_native_window_t::xdpi = (mInfo.xres * in2mm) / mInfo.width;
        egl_native_window_t::ydpi = (mInfo.yres * in2mm) / mInfo.height;
        
        egl_native_window_t::fps  = refreshRate;//refresh rate in frames per second (Hz)
        egl_native_window_t::memory_type = NATIVE_MEMORY_TYPE_FB;//Memory type the surface resides into
        // no error, set the magic word
        egl_native_window_t::magic = 0x600913;
    }
    mSwapCount = -1;
    mPageFlipCount = 0;
}

//析构函数
EGLDisplaySurface::~EGLDisplaySurface()
{
    magic = 0;
    copybit_close(mBlitEngine);//关闭copybit
    mBlitEngine = 0;
    close(egl_native_window_t::fd);//关闭句柄
    munmap(mFb[0].data, mSize);//unmap空间
    if (!(mFlags & PAGE_FLIP))
        free((void*)mFb[1].data);
}

//three hook
void EGLDisplaySurface::hook_incRef(NativeWindowType window) {
    EGLDisplaySurface* that = static_cast(window);
    that->incStrong(that);
}
void EGLDisplaySurface::hook_decRef(NativeWindowType window) {
    EGLDisplaySurface* that = static_cast(window);
    that->decStrong(that);
}
uint32_t EGLDisplaySurface::hook_swapBuffers(NativeWindowType window) {
    EGLDisplaySurface* that = static_cast(window);
    return that->swapBuffers();
}

void EGLDisplaySurface::setSwapRectangle(int l, int t, int w, int h)
{
    mInfo.reserved[0] = 0x54445055; // "UPDT";
    mInfo.reserved[1] = (uint16_t)l | ((uint32_t)t << 16);
    mInfo.reserved[2] = (uint16_t)(l+w) | ((uint32_t)(t+h) << 16);
}

//重要函数swapBuffers()
uint32_t EGLDisplaySurface::swapBuffers()
{
#define SHOW_FPS 0
#if SHOW_FPS //计算FPS部分代码
    nsecs_t now = systemTime();
    if (mSwapCount == -1) {
        mTime = now;
        mSwapCount = 0;
        mSleep = 0;
    } else {
        nsecs_t d = now-mTime;
        if (d >= seconds(1)) {
            double fps = (mSwapCount * double(seconds(1))) / double(d);
            LOGD("%f fps, sleep=%d / frame",
                    fps, (int)ns2us(mSleep / mSwapCount));
            mSwapCount = 0;
            mTime = now;
            mSleep = 0;
        } else {
            mSwapCount++;
        }
    }
#endif
    /* If we can't do the page_flip, just copy the back buffer to the front */
    //如果无法利用page_flip功能,显示时仅仅是从back向front拷贝
    if (!(mFlags & PAGE_FLIP)) {
        memcpy(mFb[0].data, mFb[1].data, mInfo.xres*mInfo.yres*2);
        return 0;
    }

    // do the actual flip 硬件PAGE_FLIP
    mIndex = 1 - mIndex;//reflesh mindex number
    mInfo.activate = FB_ACTIVATE_VBL;
    mInfo.yoffset = mIndex ? mInfo.yres : 0;//根据mindex判断y轴的offset
    //通过FBIOPUT_VSCREENINFO获取fb参数
    if (ioctl(egl_native_window_t::fd, FBIOPUT_VSCREENINFO, &mInfo) == -1) {
        LOGE("FBIOPUT_VSCREENINFO failed");
        return 0;
    }

    /*
     * this is a monstrous hack: Because the h/w accelerator is not able
     * to render directly into the framebuffer, we need to copy its
     * internal framebuffer out to the fb.
     这是一个古怪的hack,由于硬件加速单元无法直接作用在framebuffer上,因此需要拷贝到fb上。
     * oem[0] is used to access the fd of internal fb.//oem用于访问internal fb
     * All this is needed only in standalone mode, in SurfaceFlinger mode
     * we control where the GPU renders.
     * We do this only if we have copybit, since this hack is needed only
     * with msm7k.
     有copybit时才这样做,只有msm7k才需要这个hack。
     */
    if (egl_native_window_t::memory_type == NATIVE_MEMORY_TYPE_GPU && oem[0] && mBlitEngine) {
        copybit_device_t *copybit = mBlitEngine;
        /*fill Rectangle */
        copybit_rect_t sdrect = { 0, 0,
                egl_native_window_t::width, egl_native_window_t::height };
        /*fill dst Image structure */
        copybit_image_t dst = {
                egl_native_window_t::width,
                egl_native_window_t::height,
                egl_native_window_t::format,
                egl_native_window_t::offset,
                (void*)egl_native_window_t::base,
                egl_native_window_t::fd
        };
        /*fill src Image structure */
        copybit_image_t src = {
                egl_native_window_t::width,
                egl_native_window_t::height,
                egl_native_window_t::format, // XXX: use proper format
                egl_native_window_t::offset,
                (void*)egl_native_window_t::base,  // XXX: use proper base
                egl_native_window_t::oem[0]//内部fb句柄
        };
        //实例一个region_iterator 的it对象
        region_iterator it(Region(Rect( egl_native_window_t::width, egl_native_window_t::height)));
            /**
     * Set a copybit parameter.
     *
     * @param dev from open
     * @param name one for the COPYBIT_NAME_xxx
     * @param value one of the COPYBIT_VALUE_xxx
     *
     * @return 0 if successful
     */
        copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
        copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 0xFF);
        copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_DISABLE);
            /**
     * Execute the stretch bit blit copy operation 通过这个函数真正拷贝
     *
     * @param dev from open
     * @param dst is the destination image
     * @param src is the source image
     * @param dst_rect is the destination rectangle
     * @param src_rect is the source rectangle
     * @param region the clip region
     *
     * @return 0 if successful
     */
             copybit->stretch(copybit, &dst, &src, &sdrect, &sdrect, &it);
    }

    // update the address of the buffer to draw to next
    const GGLSurface& buffer = mFb[1 - mIndex];//更新next to draw address
    egl_native_window_t::offset =
        intptr_t(buffer.data) - egl_native_window_t::base;

#if SHOW_FPS
    mSleep += systemTime()-now;
#endif

    mPageFlipCount++;//增加计数器

    // We don't support screen-size changes for now 目前不支持屏幕改变屏幕尺寸
    return 0;
}

int32_t EGLDisplaySurface::getPageFlipCount() const
{
    return mPageFlipCount;
}

void EGLDisplaySurface::copyFrontToBack(const Region& copyback) //从front 拷贝到 back
{
#if HAVE_ANDROID_OS
    if (mBlitEngine) {
        copybit_image_t dst = {
                w:      egl_native_window_t::stride,
                h:      egl_native_window_t::height,
                format: egl_native_window_t::format,
                offset: mFb[1-mIndex].data - mFb[0].data,
                base:   (void*)egl_native_window_t::base,
                fd:     egl_native_window_t::fd
        };
        copybit_image_t src = {
                w:      egl_native_window_t::stride,
                h:      egl_native_window_t::height,
                format: egl_native_window_t::format,
                offset: mFb[mIndex].data - mFb[0].data,
                base:   (void*)egl_native_window_t::base,
                fd:     egl_native_window_t::fd
        };
        region_iterator it(copyback);
            /**调用blit函数
     * Execute the bit blit copy operation
     *
     * @param dev from open
     * @param dst is the destination image
     * @param src is the source image
     * @param region the clip region
     *
     * @return 0 if successful
     */
        mBlitEngine->blit(mBlitEngine, &dst, &src, &it);
    } else
#endif
    {
        /* no extra copy needed since we copied back to front instead of
         * flipping 如果使用copy代替flip的方法,则不需要额外拷贝*/
        if (!(mFlags & PAGE_FLIP)) {
            return;
        }
//软件方法copy
        Region::iterator iterator(copyback);//Region的子类iterator 对象
        if (iterator) {
            Rect r;
            uint8_t* const screen_src = mFb[  mIndex].data;
            uint8_t* const screen_dst = mFb[1-mIndex].data;
            const size_t bpp = bytesPerPixel(egl_native_window_t::format);
            const size_t bpr = egl_native_window_t::stride * bpp;
            while (iterator.iterate(&r)) {
                ssize_t h = r.bottom - r.top;
                if (h) {
                    size_t size = (r.right - r.left) * bpp;
                    size_t o = (r.left + egl_native_window_t::stride * r.top) * bpp;
                    uint8_t* s = screen_src + o;
                    uint8_t* d = screen_dst + o;
                    if (size == bpr) {
                        size *= h;
                        h = 1;
                    }
                    do {
                        memcpy(d, s, size);
                        d += bpr;
                        s += bpr;
                    } while (--h > 0);
                }
            }
        }
    }
}

void EGLDisplaySurface::copyFrontToImage(const copybit_image_t& dst)//copy front to image  image是什么?
{
#if HAVE_ANDROID_OS
    if (mBlitEngine) {//copybit方法
        copybit_image_t src = {
                w:      egl_native_window_t::stride,
                h:      egl_native_window_t::height,
                format: egl_native_window_t::format,
                offset: mFb[mIndex].data - mFb[0].data,
                base:   (void*)egl_native_window_t::base,
                fd:     egl_native_window_t::fd
        };
        region_iterator it(Region(Rect(
                egl_native_window_t::width, egl_native_window_t::height)));
        //copybit->blit 操作
        mBlitEngine->blit(mBlitEngine, &dst, &src, &it);
    } else
#endif
    { //软件方法src to des
        uint8_t* const screen_src = mFb[  mIndex].data;
        const size_t bpp = bytesPerPixel(egl_native_window_t::format);
        const size_t bpr = egl_native_window_t::stride * bpp;
        memcpy((char*)dst.base + dst.offset, screen_src, bpr*egl_native_window_t::height);
    }
}

void EGLDisplaySurface::copyBackToImage(const copybit_image_t& dst) //back to image,和上个函数的区别在于mFb不同
{
#if HAVE_ANDROID_OS
    if (mBlitEngine) {//硬件方法
        copybit_image_t src = {
                w:      egl_native_window_t::stride,
                h:      egl_native_window_t::height,
                format: egl_native_window_t::format,
                offset: mFb[1-mIndex].data - mFb[0].data,
                base:   (void*)egl_native_window_t::base,
                fd:     egl_native_window_t::fd
        };
        region_iterator it(Region(Rect(
                egl_native_window_t::width, egl_native_window_t::height)));
        //copybit->blit 操作
        mBlitEngine->blit(mBlitEngine, &dst, &src, &it);
    } else
#endif
    { //软件方法src to des
        uint8_t* const screen_src = mFb[1-mIndex].data;
        const size_t bpp = bytesPerPixel(egl_native_window_t::format);
        const size_t bpr = egl_native_window_t::stride * bpp;
        memcpy((char*)dst.base + dst.offset, screen_src, bpr*egl_native_window_t::height);
    }
}

//初始化调用的map函数
status_t EGLDisplaySurface::mapFrameBuffer()
{
    char const * const device_template[] = {
            "/dev/graphics/fb%u",
            "/dev/fb%u",
            0 };
    int fd = -1;
    int i=0;
    char name[64];
    while ((fd==-1) && device_template[i]) {
        snprintf(name, 64, device_template[i], 0);
        fd = open(name, O_RDWR, 0);//打开fb
        i++;
    }
    if (fd < 0)
        return -errno;

    struct fb_fix_screeninfo finfo;//获取fb_fix_screeninfo信息
    if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
        return -errno;

    struct fb_var_screeninfo info;//获取fb_var_screeninfo信息
    if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
        return -errno;

//填充默认数据
    info.reserved[0] = 0;
    info.reserved[1] = 0;
    info.reserved[2] = 0;
    info.xoffset = 0;
    info.yoffset = 0;
    info.yres_virtual = info.yres * 2;//双buffer是实际值的2倍
    info.bits_per_pixel = 16;
    /* Explicitly request 5/6/5 */
    info.red.offset = 11;
    info.red.length = 5;
    info.green.offset = 5;
    info.green.length = 6;
    info.blue.offset = 0;
    info.blue.length = 5;
    info.transp.offset = 0;
    info.transp.length = 0;
    info.activate = FB_ACTIVATE_NOW;

    uint32_t flags = PAGE_FLIP;
    //默认支持PAGE_FLIP,然后判断是否支持双buffer
    if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
        info.yres_virtual = info.yres;
        flags &= ~PAGE_FLIP;
        LOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
    }
//如果info.yres_virtual < info.yres * 2,同上
    if (info.yres_virtual < info.yres * 2) {
        info.yres_virtual = info.yres;
        flags &= ~PAGE_FLIP;
        LOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
                info.yres_virtual, info.yres*2);
    }

    if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
        return -errno;
//计算刷新率,貌似三星的总是算不对
    int refreshRate = 1000000000000000LLU /
    (
            uint64_t( info.upper_margin + info.lower_margin + info.yres )
            * ( info.left_margin  + info.right_margin + info.xres )
            * info.pixclock
    );

    if (refreshRate == 0) {
        // bleagh, bad info from the driver
        refreshRate = 60*1000;  // 60 Hz
    }
    
    //如果驱动没有返回,使用默认值
    if (int(info.width) <= 0 || int(info.height) <= 0) {
        // the driver doesn't return that information
        // default to 160 dpi
        info.width  = ((info.xres * 25.4f)/160.0f + 0.5f);
        info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
    }

    float xdpi = (info.xres * 25.4f) / info.width;
    float ydpi = (info.yres * 25.4f) / info.height;
    float fps  = refreshRate / 1000.0f;
//启动时的输出
    LOGI(   "using (fd=%d)\n"
            "id           = %s\n"
            "xres         = %d px\n"
            "yres         = %d px\n"
            "xres_virtual = %d px\n"
            "yres_virtual = %d px\n"
            "bpp          = %d\n"
            "r            = %2u:%u\n"
            "g            = %2u:%u\n"
            "b            = %2u:%u\n",
            fd,
            finfo.id,
            info.xres,
            info.yres,
            info.xres_virtual,
            info.yres_virtual,
            info.bits_per_pixel,
            info.red.offset, info.red.length,
            info.green.offset, info.green.length,
            info.blue.offset, info.blue.length
    );

    LOGI(   "width        = %d mm (%f dpi)\n"
            "height       = %d mm (%f dpi)\n"
            "refresh rate = %.2f Hz\n",
            info.width,  xdpi,
            info.height, ydpi,
            fps
    );

//检查参数
    if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
        return -errno;

    if (finfo.smem_len <= 0)
        return -errno;

    /*
     * Open and map the display.
     */
//通过mmap将fd映射到用户空间
    void* buffer  = (uint16_t*) mmap(
            0, finfo.smem_len,
            PROT_READ | PROT_WRITE,
            MAP_SHARED,
            fd, 0);

    if (buffer == MAP_FAILED)
        return -errno;
//清空buffer
    // at least for now, always clear the fb
    memset(buffer, 0, finfo.smem_len);

    uint8_t* offscreen[2];
    offscreen[0] = (uint8_t*)buffer;
    if (flags & PAGE_FLIP) {
        offscreen[1] = (uint8_t*)buffer + finfo.line_length*info.yres;
    } else {
        offscreen[1] = (uint8_t*)malloc(finfo.smem_len);
        if (offscreen[1] == 0) {
            munmap(buffer, finfo.smem_len);
            return NO_MEMORY;
        }
    }

//填充class EGLDisplaySurface成员,private成员可随意使用
    mFlags = flags;
    mInfo = info;
    mFinfo = finfo;
    mSize = finfo.smem_len;
    mIndex = 0;
    //填充mFb[0]和mFb[1]
    for (int i=0 ; i<2 ; i++) {
        mFb[i].version = sizeof(GGLSurface);
        mFb[i].width   = info.xres;
        mFb[i].height  = info.yres;
        mFb[i].stride  = finfo.line_length / (info.bits_per_pixel >> 3);
        mFb[i].data    = (GGLubyte*)(offscreen[i]);
        mFb[i].format  = GGL_PIXEL_FORMAT_RGB_565;
    }
    return fd;
}

// ----------------------------------------------------------------------------
}; // namespace android
// ----------------------------------------------------------------------------

阅读(1597) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~