kb_machblue_core_surface.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:13k
- //*****************************************************************************
- //File Name: kb_machblue_core_surface.c
- //
- //Description: surface function
- //
- // used by Machblue to access the platform's surface api
- //
- //Author: steven
- //
- //Date: 2006.12.29
- //
- //Version: v1.0
- //*****************************************************************************
- #include "osd.h"
- #include "stddefs.h"
- #include "stlayer.h"
- #include "layer.h"
- #include "stblit.h"
- #include "avmem.h"
- #include "evt.h"
- #include "machblue_defines.h"
- #include "machblue_porting_core.h"
- #include "kb_machblue_client.h"
- #include "kb_machblue_client_define.h"
- #define PIXEL_FORMAT_CLUT8 0 // Palettized Clut8 rendering (ex: as seen on OpenTV/DCT)
- #define PIXEL_FORMAT_RGB565 1 // RGB565 rendering (ex: as seen on PTV)
- #define PIXEL_FORMAT_RGBA4444 2 // RGBA4444 renderring (ex: as seen on ST7100 for NDS)
- #define PIXEL_FORMAT_RGBA8888 3 // RGBA4444 renderring (ex: as seen on ST7100 for OpenTV)
- #define PIXEL_FORMAT PIXEL_FORMAT_RGBA4444
- #define AVMEM1_SIZE 720*576*2
- #define MEM_PARTITION_HANDLE_COUNT 10
- /**
- * Creates a new surface.
- * surface < pointer to surface to create >
- * width < width of the surface to create >
- * height < height of the surface to create >
- * format < pixel format of the surface to create >
-
- * @return MB_SUCCESS and updates "surface" on success, MB_FAILURE otherwise.
- */
- mb_error_t mb_surface_create(mb_surface_t *surface,int width,int height,mb_pixel_format_t format)
- {
- ST_ErrorCode_t ErrCode=ST_NO_ERROR;
- STGXOBJ_BitmapAllocParams_t BitmapParams1,BitmapParams2;
- STAVMEM_MemoryRange_t RangeArea[2];
- U8 NbForbiddenRange;
- STAVMEM_AllocBlockParams_t AllocParams;
- kb_movie_surface_t *p_surface;
- *surface=NULL;
- p_surface=(kb_movie_surface_t*)mb_malloc(sizeof(kb_movie_surface_t));
- if(p_surface==NULL)
- {
- mb_printf("n[Machblue]:Surface create malloc error.");
- return MB_FAILURE ;
- }
- p_surface->m_width=width;
- p_surface->m_height=height;
- p_surface->m_format=format;
- #if (PIXEL_FORMAT==PIXEL_FORMAT_RGB565)
- p_surface->m_bitmap.ColorType=STGXOBJ_COLOR_TYPE_RGB565;
- p_surface->m_bitmap.PreMultipliedColor=FALSE;
- #elif (PIXEL_FORMAT==PIXEL_FORMAT_RGBA8888)
- p_surface->m_bitmap.ColorType=STGXOBJ_COLOR_TYPE_ARGB8888;
- p_surface->m_bitmap.PreMultipliedColor=FALSE;
- #elif (PIXEL_FORMAT==PIXEL_FORMAT_RGBA4444)
- p_surface->m_bitmap.ColorType=STGXOBJ_COLOR_TYPE_ARGB4444;
- p_surface->m_bitmap.PreMultipliedColor=FALSE;
- #else
- #error "PIXEL_FORMAT not supported"
- #endif
-
- p_surface->m_bitmap.BitmapType=STGXOBJ_BITMAP_TYPE_RASTER_PROGRESSIVE;
- p_surface->m_bitmap.ColorSpaceConversion=STGXOBJ_ITU_R_BT601;
- p_surface->m_bitmap.AspectRatio=STGXOBJ_ASPECT_RATIO_4TO3;
- p_surface->m_bitmap.Width=width;
- p_surface->m_bitmap.Height=height;
-
- #if (PIXEL_FORMAT==PIXEL_FORMAT_RGB565)
- p_surface->m_bitmap.SubByteFormat=STGXOBJ_SUBBYTE_FORMAT_RPIX_LSB;
- #elif (PIXEL_FORMAT==PIXEL_FORMAT_RGBA8888)
- p_surface->m_bitmap.SubByteFormat=STGXOBJ_SUBBYTE_FORMAT_RPIX_MSB;
- #elif (PIXEL_FORMAT==PIXEL_FORMAT_RGBA4444)
- p_surface->m_bitmap.SubByteFormat=STGXOBJ_SUBBYTE_FORMAT_RPIX_MSB;
- #endif
-
- p_surface->m_bitmap.BigNotLittle=0;
- #define VIRTUAL_BASE_ADDRESS 0xA0000000
- #define VIRTUAL_SIZE AVMEM1_SIZE
- #define VIRTUAL_WINDOW_SIZE AVMEM1_SIZE
- #define LAYER_OSD 1
- ErrCode=STLAYER_GetBitmapAllocParams(g_KB_LayerHandle[LAYER_OSD],&p_surface->m_bitmap,&BitmapParams1,&BitmapParams2);
- if (ErrCode!=ST_NO_ERROR)
- {
- mb_printf("n[Machblue]:Surface create get bmp alloc params error.");
- return MB_FAILURE;
- }
- /* Memory allocation for bitmap */
- NbForbiddenRange=1;
- RangeArea[0].StartAddr_p=(void *)VIRTUAL_BASE_ADDRESS;
- RangeArea[0].StopAddr_p=(void *)RangeArea[0].StartAddr_p;
- if((VIRTUAL_BASE_ADDRESS+VIRTUAL_WINDOW_SIZE)!=VIRTUAL_SIZE)
- {
- RangeArea[1].StartAddr_p=(void *)((U32)(RangeArea[0].StartAddr_p)+(U32)(0)+(U32)(VIRTUAL_WINDOW_SIZE));
- RangeArea[1].StopAddr_p=(void *)((U32)(VIRTUAL_BASE_ADDRESS)+(U32)(VIRTUAL_SIZE)-1);
- NbForbiddenRange=2;
- }
-
- AllocParams.PartitionHandle=g_KBAvmemPartitionHandle[0];
- AllocParams.ForbiddenBorderArray_p=(void **)NULL;
- AllocParams.AllocMode=STAVMEM_ALLOC_MODE_BOTTOM_TOP;
- AllocParams.NumberOfForbiddenRanges=NbForbiddenRange;
- AllocParams.ForbiddenRangeArray_p=&RangeArea[0];
- AllocParams.NumberOfForbiddenBorders=0;
- AllocParams.Size=BitmapParams1.AllocBlockParams.Size;
- AllocParams.Alignment=16;
-
- p_surface->m_bitmap.Pitch=BitmapParams1.Pitch;
- p_surface->m_bitmap.Offset=BitmapParams1.Offset;
- p_surface->m_bitmap.Data1_p=NULL;
- p_surface->m_bitmap.Size1=AllocParams.Size;
- p_surface->m_bitmap.Data2_p=NULL;
- p_surface->m_bitmap.Size2=0;
- p_surface->m_bitmap.Data1_p=mb_malloc(AllocParams.Size);
-
- *surface=(mb_surface_t)p_surface;
- return MB_SUCCESS ;
- }
- /**
- * Deletes a surface.
- * surface < surface to delete >
-
- * @return MB_SUCCESS on success and MB_FAILURE on failure.
- */
- mb_error_t mb_surface_delete(mb_surface_t surface)
- {
- kb_movie_surface_t *pSurface=(kb_movie_surface_t *)surface;
- if(pSurface==NULL)
- {
- mb_printf("nmb_surface_delete surface NULL.");
- return MB_FAILURE;
- }
-
- mb_free((void*)pSurface->m_bitmap.Data1_p);
- mb_free((void*)pSurface);
-
- return MB_SUCCESS;
- }
- /**
- * Get the pixel size in bytes.
- * format < pixel format >
-
- * @return the pixel bytes success, -1 on failure.
- */
- int mb_surface_pixel_bytes(mb_pixel_format_t format)
- {
- int bytes=-1;
-
- switch(format)
- {
- case MB_CLUT8_FORMAT:
- bytes=1;
- break;
- case MB_ARGB4444_FORMAT:
- case MB_ARGB1555_FORMAT:
- case MB_RGB565_FORMAT:
- bytes=2;
- break;
- case MB_ARGB8888_FORMAT:
- bytes=4;
- break;
- }
- return bytes;
- }
- /**
- * Computes the amount of memory that would be required to create a surface
- * with the provided attributes. size_req is updated with the amount of memory
- * required in case of success.
- * width < width of the surface to estimate >
- * height < height of the surface to estimate >
- * format < pixel format of the surface to estimate >
- * size_req < pointer to location to store size requirements >
-
- * @return MB_SUCCESS and MB_FAILURE on failure.
- */
- mb_error_t mb_surface_requirement_get(int width,int height,mb_pixel_format_t format,mb_size_t *size_req)
- {
- int pixSize;
- if(size_req==NULL)
- {
- mb_printf("nmb_surface_requirement_get size NULL.");
- return MB_FAILURE;
- }
- pixSize=mb_surface_pixel_bytes(format);
- if(pixSize<0)
- {
- mb_printf("n[Machblue]:Surface requirement get error.");
- return MB_FAILURE;
- }
-
- *size_req=width*height*pixSize;
-
- return MB_SUCCESS ;
- }
- /**
- * Queries the attributes of a surface.
- * surface < surface to query >
- * width < pointer to location to store the width of the surface >
- * height < pointer to location to store the height of the surface >
- * format < pointer to location to store the pixel format of the surface >
-
- * @return MB_SUCCESS and MB_FAILURE on failure.
- */
- mb_error_t mb_surface_attributes_get(mb_surface_t surface,int *width,int *height,mb_pixel_format_t *format)
- {
- kb_movie_surface_t *pSurface=(kb_movie_surface_t *)surface;
-
- if((pSurface==NULL)||(width==NULL)||(height==NULL)||(format==(mb_pixel_format_t*)NULL))
- {
- mb_printf("n[Machblue]:Surface attributes get error.");
- return MB_FAILURE;
- }
- *width=pSurface->m_width;
- *height=pSurface->m_height;
- *format=pSurface->m_format;
- return MB_SUCCESS;
- }
- /**
- * Clears a surface.
- * surface < surface to clear >
- * rect < rectangle area to clear >
- * color < color in surface color format >
-
- * @return MB_SUCCESS on success and MB_FAILURE on failure.
- */
- mb_error_t mb_surface_clear(mb_surface_t surface,mb_rect_t *rect,unsigned int color)
- {
- kb_movie_surface_t *pSurface=(kb_movie_surface_t *)surface;
- unsigned char *pDest=pSurface->m_bitmap.Data1_p;
- unsigned long index,desPitch;
-
- for (index=0;index<rect->height;index++)
- {
- desPitch=((rect->y+index)*pSurface->m_width+rect->x)*KB_OSD_BYTES;
- mb_memset(pDest+desPitch,0x00,rect->width*KB_OSD_BYTES);
- }
-
- return MB_SUCCESS;
- }
- /**
- * Locks a surface.
- * surface < surface to lock >
- * pitch < pointer to int to store surface pitch >
- * locked_ptr < pointer to the location to store pointer to the locked surface buffer >
-
- * @return MB_SUCCESS and updates locked_ptr on success and MB_FAILURE on failure.
- * The pitch value is updated in case of success.
- */
- mb_error_t mb_surface_lock(mb_surface_t surface,int *pitch,void **locked_ptr)
- {
- kb_movie_surface_t *pSurface=(kb_movie_surface_t *)surface;
- if(pSurface==NULL)
- {
- mb_printf("n[Machblue]:Surface lock error.");
- return MB_FAILURE;
- }
- *pitch=pSurface->m_bitmap.Pitch;
- *locked_ptr=pSurface->m_bitmap.Data1_p;
- return MB_SUCCESS;
- }
- /**
- * Unlocks a surface.
- * surface < surface to unlock >
-
- * @return MB_SUCCESS on success, MB_FAILURE on failure.
- */
- mb_error_t mb_surface_unlock(mb_surface_t surface)
- {
- return MB_SUCCESS;
- }
- /**
- * Blits a surface into another surface.
- * dest_surface < destination surface >
- * dest_rect < destination rectangle area to blit to >
- * src_surface < source surface >
- * src_rect < source rectangle area to blit from >
- * options < blit options >
-
- * @return MB_SUCCESS on success, MB_FAILURE on failure.
- */
- mb_error_t mb_surface_blit(mb_surface_t dest_surface,mb_rect_t *dest_rect,mb_surface_t src_surface,mb_rect_t *src_rect,mb_blit_option_t options)
- {
- kb_movie_surface_t *pSrcSurface=(kb_movie_surface_t *)src_surface;
- kb_movie_surface_t *pDestSurface=(kb_movie_surface_t *)dest_surface;
- unsigned char *pSrc=pSrcSurface->m_bitmap.Data1_p;
- unsigned char *pDest=pDestSurface->m_bitmap.Data1_p;
- unsigned long index,srcPitch,desPitch;
- for (index=0;index<src_rect->height;index++)
- {
- srcPitch=((src_rect->y+index)*pSrcSurface->m_width+src_rect->x)*KB_OSD_BYTES;
- desPitch=((dest_rect->y+index)*pDestSurface->m_width+dest_rect->x)*KB_OSD_BYTES;
- mb_memcpy(pDest+desPitch,pSrc+srcPitch,src_rect->width*KB_OSD_BYTES);
- }
-
- return MB_SUCCESS;
- }
- /**
- * Copy data to display buf.
- * pRect < rectangle parameter >
- * pMemory < pointer to source data >
-
- * @return None.
- */
- void mb_surface_osd_copy(mb_rect_t *pRect,void *pMemory)
- {
- unsigned char *pSrc,*pDest;
- unsigned long index,srcPitch,desPitch;
-
- pSrc=(unsigned char*)pMemory;
- pDest=(unsigned char*)Bitmap[VPORT_OSD].Data1_p;
-
- for (index=0;index<pRect->height;index++)
- {
- srcPitch=index*KB_OSD_WIDTH*KB_OSD_BYTES;
- desPitch=((pRect->y+index)*KB_OSD_WIDTH+pRect->x)*KB_OSD_BYTES;
- mb_memcpy(pDest+desPitch,pSrc+srcPitch,pRect->width*KB_OSD_BYTES);
- }
- }
- /**
- * Clear display buf.
- * @return None.
- */
- void mb_surface_osd_clear(void)
- {
- unsigned char *pDest;
-
- pDest=(unsigned char*)Bitmap[VPORT_OSD].Data1_p;
- mb_memset(pDest,0x00,Bitmap[VPORT_OSD].Size1*KB_OSD_BYTES);
- }
- /**
- * Callback of surface.
- * @return MB_SUCCESS on success, MB_FAILURE on failure.
- */
- mb_error_t mb_surface_notify( mb_gfx_cb_reason_t reason, mb_args_t *args, void *client_data )
- {
- mb_rect_t *dirtyRect;
- mb_surface_t src_surface;
- unsigned char *bmpData;
-
- switch(reason)
- {
- case MB_GFX_REDRAW_CB:
- dirtyRect=(mb_rect_t *)MB_GFX_RECT_ARG(args);
- src_surface=(mb_surface_t)MB_GFX_SURFACE_ARG(args);
-
- bmpData=((kb_movie_surface_t*)src_surface)->m_bitmap.Data1_p;
- bmpData+=((dirtyRect->y*KB_OSD_WIDTH+dirtyRect->x)*2);
-
- mb_surface_osd_copy(dirtyRect,bmpData);
- break;
-
- case MB_GFX_SET_PALETTE_CB:
- mb_printf("n[Machblue]:Surface graphics context set pallette notification.");
- break;
- }
-
- return MB_SUCCESS;
- }
- /**
- * Init graphics context which include the surface.
- * format < pixel format >
-
- * @return MB_SUCCESS on success, MB_FAILURE on failure.
- */
- mb_error_t mb_surface_graphics_init(mb_gfx_ctx_t *p_gfx_ctx, void *client_data)
- {
- mb_pixel_format_t format;
-
switch(PIXEL_FORMAT)
- {
- case PIXEL_FORMAT_CLUT8:
- format=MB_CLUT8_FORMAT;
- break;
- case PIXEL_FORMAT_RGB565:
- format=MB_RGB565_FORMAT;
- break;
- case PIXEL_FORMAT_RGBA4444:
- format=MB_ARGB4444_FORMAT;
- break;
- case PIXEL_FORMAT_RGBA8888:
- format=MB_ARGB8888_FORMAT;
- break;
- default:
- return MB_FAILURE;
- }
-
//* Creates a new surface and return the surface pointer to be initialized.
- if(mb_surface_create(&(p_gfx_ctx->surface),720,576,format)!=MB_SUCCESS)
- {
- mb_printf("n[Machblue]:Surface graphics init error.");
- return MB_FAILURE;
- }
- p_gfx_ctx->bounds.x=0;
- p_gfx_ctx->bounds.y=0;
- p_gfx_ctx->bounds.width=720;
- p_gfx_ctx->bounds.height=576;
- p_gfx_ctx->callback_f=mb_surface_notify;
- p_gfx_ctx->cb_client_data=client_data;
-
return MB_SUCCESS;
- }