分享

android 的canvas渲染

 传真阅览室 2014-01-17
#define LOG_TAG "VideoCanvas"
#include <android/api-level.h>

#define LOG(...) ((void)0)
#include "utils/log.h"

#include <core/skCanvas.h>
#include <core/SkPaint.h>
#include <core/SkColor.h>
#include <core/SkBitmap.h>

#if __ANDROID_API__ >= 9
#include <surfaceflinger/Surface.h>
#else
#include <ui/Surface.h>
#endif

#include "media_VideoCanvas.h"

using namespace android;

static void* VideoLockCanvas(Surface* native_surface, int *width, int *height, int *stride)
{
    Surface::SurfaceInfo info;
    void *canvasptr = NULL;

    status_t ret = 0;

    ret = native_surface->lock(&info);
    if (ret == OK) {
        canvasptr = info.bits;
        *width = info.w;
        *height = info.h;
        *stride = info.s;
//      LOGI("surface format %d", info.format);
    }
    return canvasptr;
}

static void VideoUnlockCanvas(Surface* native_surface)
{
    native_surface->unlockAndPost();
}

//sp<Surface> surfaceInstance;

static Surface* previous_surface = NULL;

void VideoCanvasSet(void *videocanvas){
    Surface* native_surface = (Surface*)videocanvas;
}

void VideoCanvasDisplay(void *videocanvas, yuv2rgb_cb cb, void* cb_data){
    Surface* native_surface = (Surface*)videocanvas;
    if(native_surface == NULL) {
        return;
    }
    LOGI("enter %p previous %p", native_surface, previous_surface);
    if(native_surface != previous_surface) {
        if(previous_surface != NULL) {
            previous_surface->decStrong((void*)VideoCanvasDisplay); //function name only as id
        }
        native_surface->incStrong((void*)VideoCanvasDisplay);
        previous_surface = native_surface;
    }

    int width, height, stride;
    void* bits = VideoLockCanvas(native_surface, &width, &height, &stride);
    if(bits == NULL) {
        return;
    }
    LOGI("lock %p width %d heigth %d stride %d", bits, width, height, stride);

    cb(cb_data, bits, width, height, stride);
    VideoUnlockCanvas(native_surface);
    LOGI("unlock");
}

/*
 * The only purpose of class "MediaPlayer" is to call Surface::getISurface()
 * in frameworks/base/include/ui/Surface.h, which is private function and accessed
 * by friend class MediaPlayer.
 *
 * We define a fake one to cheat compiler
 */
/*
namespace android {
  class MediaPlayer {
  public:
    static sp < ISurface > getSurface (const Surface * surface){
        if(surface != NULL) {
            return surface->getISurface ();
        }
        return sp < ISurface >;
    };
  };
};
*/
void VideoCanvasClear(void *videocanvas){
    Surface* native_surface = (Surface*)videocanvas;
    if(native_surface == NULL) {
        return;
    }
    LOGI("clear %p previous %p", native_surface, previous_surface);
    if(native_surface != previous_surface) {
        return;
    }

    int width, height, stride;
    void* bits = VideoLockCanvas(native_surface, &width, &height, &stride);
    if(bits == NULL) {
        return;
    }

    LOGI("clear lock %p width %d heigth %d stride %d", bits, width, height, stride);

    memset(bits, 0, stride*height*2);
    VideoUnlockCanvas(native_surface);
    LOGI("clear unlock");
}

void VideoCanvasDestroy(void *videocanvas){
    Surface* native_surface = (Surface*)videocanvas;
    //native_surface->clear();
}
仅供参考!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多