现在的位置: 首页 > 综合 > 正文

ADROID 2.1 架构解析 12 图形系统 1 Pixelflinger

2012年04月15日 ⁄ 综合 ⁄ 共 13520字 ⁄ 字号 评论关闭

1 Pixelflinger

1.1 Pixelflinger 简介

PixelflingerAndroid系统中为OpenGLES引擎提供的一套软件渲染器(renderer)。OpenGLES引擎提供了一系列基础绘图功能。这些功能包括定义各种颜色格式像素位置、画点画线、绘制矩形及三角形、填充纹理等等。由于OpenGLES相当于一个状态机,配置OpenGLEs状态的函数也均由Pixelflinger提供。

Pixelflinger涉及到的代码路径:

system/core/libpixelflinger

system/core/include/libpixelflinger

system/core/include/private/pixelflinger

1.2 Pixelflinger生成的库

libpixelflinger_armv6.a

libpixelflinger_static.a

libpixelflinger.so

1.3 Pixelflinger两大数据结构

1.3.1 context_t 结构

该结构是Pixelflinger的主结构,里面含有GGLContext接口结构和stateformat等属性。

system/core/include/private/pixelflinger/ggl_context.h

struct context_t {

GGLContext procs;

state_t state;

shade_t shade;

iterators_t iterators;

generated_vars_t generated_vars __attribute__((aligned(32)));

uint8_t ditherMatrix[GGL_DITHER_SIZE] __attribute__((aligned(32)));

uint32_t packed;

uint32_t packed8888;

const GGLFormat* formats;

uint32_t dirty;

texture_t* activeTMU;

uint32_t activeTMUIndex;

void (*init_y)(context_t* c, int32_t y);

void (*step_y)(context_t* c);

void (*scanline)(context_t* c);

void (*span)(context_t* c);

void (*rect)(context_t* c, size_t yc);

void* base;

Assembly* scanline_as;

GGLenum error;

};

1.3.2 GGLContext 结构

Pixelflinger对外是用GGLContext结构提供接口引用的,如下是GGLContext结构形式。

system/core/include/pixelflinger/pixelflinger.h

typedef struct {

// immediate rendering

void (*pointx)(void *con,const GGLcoord* v, GGLcoord r);

void (*linex)(void *con,

const GGLcoord*v0, const GGLcoord* v1, GGLcoord width);

void (*recti)(void* c,GGLint l, GGLint t, GGLint r, GGLint b);

void (*trianglex)(void* c,

GGLcoord const*v0, GGLcoord const* v1, GGLcoord const* v2);

// scissor

void (*scissor)(void* c,GGLint x, GGLint y, GGLsizei width, GGLsizei height);

// Set the textures andcolor buffers

void(*activeTexture)(void* c, GGLuint tmu);

void (*bindTexture)(void*c, const GGLSurface* surface);

void (*colorBuffer)(void*c, const GGLSurface* surface);

void (*readBuffer)(void*c, const GGLSurface* surface);

void (*depthBuffer)(void*c, const GGLSurface* surface);

void(*bindTextureLod)(void* c, GGLuint tmu, const GGLSurface* surface);

// enable/disable features

void (*enable)(void* c,GGLenum name);

void (*disable)(void* c,GGLenum name);

void(*enableDisable)(void* c, GGLenum name, GGLboolean en);

// specify the fragment'scolor

void (*shadeModel)(void*c, GGLenum mode);

void (*color4xv)(void* c,const GGLclampx* color);

// specify color iterators(16.16)

void(*colorGrad12xv)(void* c, const GGLcolor* grad);

// specify Z coordinateiterators (0.32)

void (*zGrad3xv)(void* c,const GGLfixed32* grad);

// specify W coordinateiterators (16.16)

void (*wGrad3xv)(void* c,const GGLfixed* grad);

// specify fog iterator &color (16.16)

void (*fogGrad3xv)(void*c, const GGLfixed* grad);

void (*fogColor3xv)(void*c, const GGLclampx* color);

// specify blendingparameters

void (*blendFunc)(void* c,GGLenum src, GGLenum dst);

void(*blendFuncSeparate)(void* c, GGLenum src, GGLenum dst,

GGLenumsrcAlpha, GGLenum dstAplha);

// texture environnement(REPLACE / MODULATE / DECAL / BLEND)

void (*texEnvi)(void* c, GGLenum target,

GGLenumpname,

GGLintparam);

void (*texEnvxv)(void* c,GGLenum target,

GGLenum pname,const GGLfixed* params);

// texture parameters(Wrapping, filter)

void(*texParameteri)(void* c, GGLenum target,

GGLenumpname,

GGLintparam);

// texture iterators(16.16)

void (*texCoord2i)(void*c, GGLint s, GGLint t);

void (*texCoord2x)(void*c, GGLfixed s, GGLfixed t);

// s, dsdx, dsdy, scale,t, dtdx, dtdy, tscale

// This api uses blockfloating-point for S and T texture coordinates.

// All values are given in16.16, scaled by 'scale'. In other words,

// set scale to 0, for16.16 values.

void(*texCoordGradScale8xv)(void* c, GGLint tmu, const int32_t* grad8);

void (*texGeni)(void* c,GGLenum coord, GGLenum pname, GGLint param);

// masking

void (*colorMask)(void* c, GGLboolean red,

GGLbooleangreen,

GGLbooleanblue,

GGLbooleanalpha);

void (*depthMask)(void* c,GGLboolean flag);

void (*stencilMask)(void*c, GGLuint mask);

// alpha func

void (*alphaFuncx)(void*c, GGLenum func, GGLclampx ref);

// depth func

void (*depthFunc)(void* c,GGLenum func);

// logic op

void (*logicOp)(void* c,GGLenum opcode);

// clear

void (*clear)(void* c,GGLbitfield mask);

void (*clearColorx)(void*c,

GGLclampx r,GGLclampx g, GGLclampx b, GGLclampx a);

void (*clearDepthx)(void*c, GGLclampx depth);

void (*clearStencil)(void*c, GGLint s);

// framebuffer operations

void (*copyPixels)(void*c, GGLint x, GGLint y,

GGLsizei width,GGLsizei height, GGLenum type);

void (*rasterPos2x)(void*c, GGLfixed x, GGLfixed y);

void (*rasterPos2i)(void*c, GGLint x, GGLint y);

} GGLContext;

1.4 Pixelflinger的引用和释放

下面是pixelflinger的初始化函数和释放函数

system/core/libpixelflinger/pixelflinger.cpp

ssize_t gglInit(GGLContext**context)

{

void* const base =malloc(sizeof(context_t) + 32);

if (base) {

// always align thecontext on cache lines

context_t *c =(context_t *)((ptrdiff_t(base)+31) & ~0x1FL);

ggl_init_context(c);

c->base = base;

*context = (GGLContext*)c;

} else {

return -1;

}

return 0;

}

ssize_t gglUninit(GGLContext*con)

{

GGL_CONTEXT(c,(void*)con);

ggl_uninit_context(c);

free(c->base);

return 0;

}

gglInit gglUninit是初opengl引用的,它们分别被引用的位置如下:

frameworks/base/opengl/libagl/texture.cpp

GGLContext*getRasterizer(ogles_context_t* c)

{

GGLContext* ggl =c->textures.ggl;

..

gglInit(&ggl);

}

voidogles_uninit_texture(ogles_context_t* c)

{

if (c->textures.ggl)

gglUninit(c->textures.ggl);

}

1.5 Pixelflinger支持的像素格式

voidggl_init_context(context_t* c)

{

...

c->formats =gglGetPixelFormatTable();

}

formats 指向pixelformat支持的像素格式表,该表在下面文件里:

system/core/libpixelflinger/format.cpp

const GGLFormat*gglGetPixelFormatTable(size_t* numEntries)

{

if (numEntries) {

*numEntries =sizeof(android::gPixelFormatInfos)/sizeof(GGLFormat);

}

returnandroid::gPixelFormatInfos;

}

static GGLFormat constgPixelFormatInfos[] =

{ // Alpha Red Green Blue

{ 0, 0, {{ 0, 0, 0, 0, 0, 0, 0, 0 }}, 0 }, // PIXEL_FORMAT_NONE

{ 4, 32, {{32,24, 8, 0, 16, 8, 24,16 }}, GGL_RGBA }, // PIXEL_FORMAT_RGBA_8888

{ 4, 24, {{ 0, 0, 8, 0, 16, 8, 24,16 }}, GGL_RGB }, // PIXEL_FORMAT_RGBX_8888

{ 3, 24, {{ 0, 0, 8, 0, 16, 8, 24,16 }}, GGL_RGB }, // PIXEL_FORMAT_RGB_888

{ 2, 16, {{ 0, 0, 16,11, 11, 5, 5, 0 }}, GGL_RGB }, // PIXEL_FORMAT_RGB_565

{ 4, 32, {{32,24, 24,16, 16, 8, 8, 0 }}, GGL_RGBA }, // PIXEL_FORMAT_BGRA_8888

{ 2, 16, {{ 1, 0, 16,11, 11, 6, 6, 1 }}, GGL_RGBA }, // PIXEL_FORMAT_RGBA_5551

{ 2, 16, {{ 4, 0, 16,12, 12, 8, 8, 4 }}, GGL_RGBA }, // PIXEL_FORMAT_RGBA_4444

{ 1, 8, {{ 8, 0, 0, 0, 0, 0, 0, 0 }}, GGL_ALPHA}, // PIXEL_FORMAT_A8

{ 1, 8, {{ 0, 0, 8, 0, 8, 0, 8, 0 }}, GGL_LUMINANCE},//PIXEL_FORMAT_L8

{ 2, 16, {{16, 8, 8, 0, 8, 0, 8, 0 }}, GGL_LUMINANCE_ALPHA},// PIXEL_FORMAT_LA_88

{ 1, 8, {{ 0, 0, 8, 5, 5, 2, 2, 0 }}, GGL_RGB }, // PIXEL_FORMAT_RGB_332

{ 0, 0, {{ 0, 0, 0, 0, 0, 0, 0, 0 }}, 0 }, // PIXEL_FORMAT_NONE

{ 0, 0, {{ 0, 0, 0, 0, 0, 0, 0, 0 }}, 0 }, // PIXEL_FORMAT_NONE

{ 0, 0, {{ 0, 0, 0, 0, 0, 0, 0, 0 }}, 0 }, // PIXEL_FORMAT_NONE

{ 0, 0, {{ 0, 0, 0, 0, 0, 0, 0, 0 }}, 0 }, // PIXEL_FORMAT_NONE

{ 1, 16, {{ 0, 8, 0, 8, 0, 8, 0, 0 }}, GGL_Y_CB_CR_SP },// PIXEL_FORMAT_YCbCr_422_SP

{ 1, 12, {{ 0, 8, 0, 8, 0, 8, 0, 0 }}, GGL_Y_CB_CR_SP },// PIXEL_FORMAT_YCbCr_420_SP

{ 1, 16, {{ 0, 8, 0, 8, 0, 8, 0, 0 }}, GGL_Y_CB_CR_P }, // PIXEL_FORMAT_YCbCr_422_P

{ 1, 12, {{ 0, 8, 0, 8, 0, 8, 0, 0 }}, GGL_Y_CB_CR_P }, // PIXEL_FORMAT_YCbCr_420_P

{ 1, 16, {{ 0, 8, 0, 8, 0, 8, 0, 0 }}, GGL_Y_CB_CR_I }, // PIXEL_FORMAT_YCbCr_422_I

{ 1, 12, {{ 0, 8, 0, 8, 0, 8, 0, 0 }}, GGL_Y_CB_CR_I }, // PIXEL_FORMAT_YCbCr_420_I

{ 0, 0, {{ 0, 0, 0, 0, 0, 0, 0, 0 }}, 0 }, // PIXEL_FORMAT_NONE

{ 0, 0, {{ 0, 0, 0, 0, 0, 0, 0, 0 }}, 0 }, // PIXEL_FORMAT_NONE

{ 2, 16, {{ 0, 0, 16, 0, 0, 0, 0, 0 }}, GGL_DEPTH_COMPONENT},

{ 1, 8, {{ 8, 0, 0, 0, 0, 0, 0, 0 }}, GGL_STENCIL_INDEX },

{ 4, 24, {{ 0, 0, 24, 0, 0, 0, 0, 0 }}, GGL_DEPTH_COMPONENT},

{ 4, 8, {{ 32,24, 0, 0, 0, 0, 0, 0 }}, GGL_STENCIL_INDEX },

};

1.6 Pixelflinger 接口

1.6.1 裁剪操作函数

void(*scissor)(void* c, GGLint x, GGLint y, GGLsizei width, GGLsizeiheight);

该函数的功能是设置裁剪范围:

c->state.scissor.left = x;

c->state.scissor.top = y;

c->state.scissor.right = width;

c->state.scissor.bottom= height;

1.6.2 纹理和色彩缓冲设置函数

void(*activeTexture)(void* c, GGLuint tmu);

该函数的功能是设置activeTexture索引及值:

c->activeTMUIndex= tmu;

c->activeTMU= &(c->state.texture[tmu]);

void(*bindTexture)(void* c, const GGLSurface* surface);

该函数的功能是将activeTMUsurface绑定到源surface

ggl_set_surface(c,&(c->activeTMU->surface), surface);

void(*colorBuffer)(void* c, const GGLSurface* surface);

该函数的功能是将state.buffers.color 绑定到源surface

ggl_set_surface(c,&(c->state.buffers.color), surface);

void(*readBuffer)(void* c, const GGLSurface* surface);

该函数的功能是将state.buffers.read 绑定到源surface

ggl_set_surface(c,&(c->state.buffers.read), surface);

void(*depthBuffer)(void* c, const GGLSurface* surface);

该函数的功能是将state.buffers.depth.format初始化为GGL_PIXEL_FORMAT_NONE

c->state.buffers.depth.format= GGL_PIXEL_FORMAT_NONE;

1.6.3 使能或禁止某种属性

Pixelflinger开放如下属性可供使能或禁止:

GGL_BLEND

混合使能:

if (enable) c->state.enables|= GGL_ENABLE_BLENDING;

else c->state.enables &= ~GGL_ENABLE_BLENDING;

GGL_SCISSOR_TEST

裁剪测试使能:

if (enable) c->state.enables|= GGL_ENABLE_SCISSOR_TEST;

else c->state.enables &= ~GGL_ENABLE_SCISSOR_TEST;

GGL_ALPHA_TEST

ALPHA测试使能:

if (enable) c->state.enables|= GGL_ENABLE_ALPHA_TEST;

else c->state.enables &= ~GGL_ENABLE_ALPHA_TEST;

GGL_COLOR_LOGIC_OP

顔色逻辑操作使能:

if (enable) c->state.enables|= GGL_ENABLE_LOGIC_OP;

else c->state.enables &= ~GGL_ENABLE_LOGIC_OP;

GGL_DITHER

抖动使能:

if (enable) c->state.enables|= GGL_ENABLE_DITHER;

else c->state.enables &= ~GGL_ENABLE_DITHER;

static voidenable_disable(context_t* c, GGLenum name, int en)

{

switch (name) {

case GGL_BLEND: ggl_enable_blending(c, en); break;

case GGL_SCISSOR_TEST: ggl_enable_scissor_test(c, en); break;

case GGL_ALPHA_TEST: ggl_enable_alpha_test(c, en); break;

case GGL_COLOR_LOGIC_OP: ggl_enable_logic_op(c, en); break;

case GGL_DITHER: ggl_enable_dither(c, en); break;

case GGL_STENCIL_TEST: ggl_enable_stencil_test(c, en); break;

case GGL_DEPTH_TEST: ggl_enable_depth_test(c, en); break;

case GGL_AA: ggl_enable_aa(c, en); break;

case GGL_TEXTURE_2D: ggl_enable_texture2d(c, en); break;

case GGL_W_LERP: ggl_enable_w_lerp(c, en); break;

case GGL_FOG: ggl_enable_fog(c, en); break;

caseGGL_POINT_SMOOTH_NICE: ggl_enable_point_aa_nice(c, en); break;

}

}

void (*enable)(void* c,GGLenum name);

该函数的功能是使用某种属性:

enable_disable(c, name, 1);

void(*disable)(void* c, GGLenum name);

该函数的功能是禁止某种属性:

enable_disable(c, name, 0);

void(*enableDisable)(void* c, GGLenum name, GGLboolean en);

该函数的功能是使用或禁止某种属性:

enable_disable(c, name, en ?1 : 0);

1.6.4 指定片段顔色

void(*shadeModel)(void* c, GGLenum mode);

该函数的功能是指定阴影模式,是FLAT还是SMOOTH

caseGGL_FLAT:

if(c->state.enables & GGL_ENABLE_SMOOTH) {

c->state.enables&= ~GGL_ENABLE_SMOOTH;

ggl_state_changed(c,GGL_PIXEL_PIPELINE_STATE);

}

break;

caseGGL_SMOOTH:

if(!(c->state.enables & GGL_ENABLE_SMOOTH)) {

c->state.enables|= GGL_ENABLE_SMOOTH;

ggl_state_changed(c,GGL_PIXEL_PIPELINE_STATE);

}

void(*color4xv)(void* c, const GGLclampx* color);

该函数的功能是指定渐变的顔色:

c->shade.r0 =gglFixedToIteratedColor(color[0]);

c->shade.g0 =gglFixedToIteratedColor(color[1]);

c->shade.b0 =gglFixedToIteratedColor(color[2]);

c->shade.a0 =gglFixedToIteratedColor(color[3]);

1.6.5 迭代程序

void(*colorGrad12xv)(void* c, const GGLcolor* grad);

该函数的功能是指定渐变的顔色及顔色位置:

constint32_t round = 0x8000;

c->shade.r0 = grad[ 0] + round;

c->shade.drdx= grad[ 1];

c->shade.drdy= grad[ 2];

c->shade.g0 = grad[ 3] + round;

c->shade.dgdx= grad[ 4];

c->shade.dgdy= grad[ 5];

c->shade.b0 = grad[ 6] + round;

c->shade.dbdx= grad[ 7];

c->shade.dbdy= grad[ 8];

c->shade.a0 = grad[ 9] + round;

c->shade.dadx= grad[10];

c->shade.dady= grad[11];

void (*zGrad3xv)(void*c, const GGLfixed32* grad);

该函数的功能是指定渐变的Z轴值及Z值位置:

const uint32_t round =0x8000;

c->shade.z0 = grad[0] +round;

c->shade.dzdx = grad[1];

c->shade.dzdy = grad[2];

void (*wGrad3xv)(void*c, const GGLfixed* grad);

该函数的功能是指定渐变的W轴值及W值位置:

const uint32_t round =0x8000;

c->shade.z0 = grad[0] +round;

c->shade.dzdx = grad[1];

c->shade.dzdy = grad[2];

void(*fogGrad3xv)(void* c, const GGLfixed* grad);

该函数的功能是指定渐变的雾化值:

c->shade.f0 = grad[0];

c->shade.dfdx = grad[1];

c->shade.dfdy = grad[2];

void(*fogColor3xv)(void* c, const GGLclampx* color);

该函数的功能是指定雾化顔色:

c->state.fog.color[GGLFormat::RED] = (r - (r>>8))>>8;

c->state.fog.color[GGLFormat::GREEN]=(g - (g>>8))>>8;

c->state.fog.color[GGLFormat::BLUE]= (b - (b>>8))>>8;

1.6.6 指定混合参数

void(*blendFunc)(void* c, GGLenum src, GGLenum dst);

该函数的功能是指定混合源和混合目的地,同srcsrc_alpha一致,dstdst_alpha一致:

c->state.blend.src = src;

c->state.blend.src_alpha= src;

c->state.blend.dst =dst;

c->state.blend.dst_alpha= dst;

c->state.blend.alpha_separate= 0;

void(*blendFuncSeparate)(void* c, GGLenum src, GGLenum dst,

GGLenumsrcAlpha, GGLenum dstAplha);

该函数的功能是指定混合源和混合目的地,同srcsrc_alpha分开,dstdst_alpha分开:

c->state.blend.src = src;

c->state.blend.src_alpha= srcAlpha;

c->state.blend.dst =dst;

c->state.blend.dst_alpha= dstAplha;

c->state.blend.alpha_separate= 1;

1.6.7 指定纹理参数

void (*texEnvi)(void*c, GGLenum target,

GGLenumpname,

GGLintparam);

该函数的功能是指定纹理环境参数:

case GGL_REPLACE:

case GGL_MODULATE:

case GGL_DECAL:

case GGL_BLEND:

case GGL_ADD:

if (c->activeTMU->env!= param) {

c->activeTMU->env= param;

void(*texEnvxv)(void* c, GGLenum target,

GGLenumpname, const GGLfixed* params);

该函数的功能是指定纹理环境模式:

switch (pname) {

case GGL_TEXTURE_ENV_MODE:

ggl_texEnvi(con,target, pname, params[0]);

break;

caseGGL_TEXTURE_ENV_COLOR: {

uint8_t* const color =c->activeTMU->env_color;

const GGLclampx r =gglClampx(params[0]);

const GGLclampx g =gglClampx(params[1]);

const GGLclampx b =gglClampx(params[2]);

const GGLclampx a =gglClampx(params[3]);

color[0] =(a-(a>>8))>>8;

color[1] =(r-(r>>8))>>8;

color[2] =(g-(g>>8))>>8;

color[3] =(b-(b>>8))>>8;

break;

}

void(*texParameteri)(void* c, GGLenum target,

GGLenumpname,

GGLintparam);

该函数的功能是指定纹理参数:

switch (pname) {

case GGL_TEXTURE_WRAP_S:

what =&c->activeTMU->s_wrap;

case GGL_TEXTURE_WRAP_T:

caseGGL_TEXTURE_MIN_FILTER:

caseGGL_TEXTURE_MAG_FILTER:

*what = param;

void(*texCoord2x)(void* c, GGLfixed s, GGLfixed t);

该函数的功能是设置纹理渐变的st

c->activeTMU->shade.is0= s;

c->activeTMU->shade.it0= t;

c->activeTMU->shade.sscale=0;

c->activeTMU->shade.tscale=0;

void(*texCoordGradScale8xv)(void* c, GGLint tmu, const int32_t* grad8);

该函数的功能是设置渐变的ST以及SCALE

texture_t& u =c->state.texture[tmu];

u.shade.is0 = grad[0];

u.shade.idsdx = grad[1];

u.shade.idsdy = grad[2];

u.shade.it0 = grad[3];

u.shade.idtdx = grad[4];

u.shade.idtdy = grad[5];

u.shade.sscale= grad[6];

u.shade.tscale= grad[7];

void(*texGeni)(void* c, GGLenum coord, GGLenum pname, GGLint param);

该函数的功能是设置 ST 的参数:

if (coord == GGL_S) coord_ptr = &(c->activeTMU->s_coord);

else if (coord == GGL_T) coord_ptr = &(c->activeTMU->t_coord);

*coord_ptr = uint32_t(param);

1.6.8 屏蔽函数

void (*colorMask)(void*c, GGLboolean red,

GGLbooleangreen,

GGLbooleanblue,

GGLbooleanalpha);

该函数的功能是设置顔色掩码:

if (a) mask |= 1 <<GGLFormat::ALPHA;

if (r) mask |= 1 <<GGLFormat::RED;

if (g) mask |= 1 <<GGLFormat::GREEN;

if (b) mask |= 1 <<GGLFormat::BLUE;

c->state.mask.color= mask;

void(*depthMask)(void* c, GGLboolean flag);

该函数的功能是设置深度掩码:

c->state.mask.depth =flag?1:0;

void(*stencilMask)(void* c, GGLuint mask);

该函数的功能是设置蜡版掩码:

c->state.mask.stencil =mask;

抱歉!评论已关闭.