source: svn/trunk/newcon3bcm2_21bu/dta/src/settop_api/bsettop_graphics.c

Last change on this file was 2, checked in by phkim, 11 years ago

1.phkim

  1. revision copy newcon3sk r27
  • Property svn:executable set to *
File size: 34.5 KB
Line 
1/***************************************************************************
2 *     Copyright (c) 2003-2006, Broadcom Corporation
3 *     All Rights Reserved
4 *     Confidential Property of Broadcom Corporation
5 *
6 *  THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE
7 *  AGREEMENT  BETWEEN THE USER AND BROADCOM.  YOU HAVE NO RIGHT TO USE OR
8 *  EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT.
9 *
10 * $brcm_Workfile:  $
11 * $brcm_Revision:  $
12 * $brcm_Date: $
13 *
14 * Module Description:
15 *
16 * Revision History:
17 *
18 * $brcm_Log:  $
19 *
20 ***************************************************************************/
21
22#include "bsettop_graphics.h"
23#include "bsettop_display.h"
24#include "bsettop_display_priv.h"
25#include "bstd.h"
26#include "bos.h"
27#include "cache_util.h"
28#include "gist.h"
29
30#ifdef CONFIG_GFX_ARGB32
31#include "bgrc.h"
32#endif
33
34BDBG_MODULE(bsettop_graphics);
35
36typedef enum bgraphics_state_t
37{
38        eGS_UNINITIALIZED,
39        eGS_OPENED
40}bgraphics_state_t;
41
42struct bgraphics
43{
44        bgraphics_state_t       state;
45        bdisplay_t display;
46        BFMT_VideoFmt   eSurfaceFmt;
47        BFMT_VideoFmt   eOSDFmt;
48#ifdef CONFIG_GFX_ARGB32
49        BGRC_Handle      grc;               /* grc handle */
50        int32_t          coeffs_ycbcr[20];  /* coefficient table from RGB to YCbCr */
51        int32_t          coeffs_rgb[20];
52        BKNI_EventHandle event;             /* event for waiting GRC is complete */
53        b_mutex_t        mutex;
54#endif
55};
56
57static struct bgraphics s_graphics = { eGS_UNINITIALIZED, NULL, BFMT_VideoFmt_eNTSC, BFMT_VideoFmt_eNTSC };
58
59#ifdef CONFIG_GFX_ARGB32
60typedef struct bsurface_create_settings
61{
62        uint32_t        width;
63        uint32_t        height;
64        BPXL_Format     pxl_format;
65} bsurface_create_settings;
66
67static BERR_Code bgraphics_p_surface_create(bgraphics_t graphics, const bsurface_create_settings *psettings, bsettop_surf_t *surface);
68static void bgraphics_p_surface_destroy(bgraphics_t graphics, bsettop_surf_t surface);
69static BERR_Code bgraphics_p_flip_osd(bgraphics_t graphics, bgraphics_id id, bool overlay);
70
71static BERR_Code bgraphics_p_set_event(BGRC_Handle hGrc, void *pData);
72static void b_graphics_calculate_coeffs_table(bool src_rgb, bool dest_rgb, int32_t *pcoeffs);
73#endif
74static bgraphics_settings s_settings = {bgraphics_pixel_format_a8_r8_g8_b8};
75
76/**
77Summary:
78    Release graphics surface resources.
79**/
80static BERR_Code bgraphics_surface_destroy(bgraphics_t graphics)
81{
82        BERR_Code rc;
83        bdisplay_t display;
84
85        BDBG_ASSERT(graphics);
86        BDBG_ASSERT(graphics->display);
87
88        display = graphics->display;
89
90#if HAS_HDMI
91        if (display->disp[eBDISPLAY_HDMI].hGfxSource)
92        {
93                rc = BVDC_Source_Destroy(display->disp[eBDISPLAY_HDMI].hGfxSource);
94                if (rc != BERR_SUCCESS)
95                {
96                        BDBG_ERR(("BVDC_Source_Destroy failed %d\n", rc));
97                }
98                display->disp[eBDISPLAY_HDMI].hGfxSource = NULL;
99        }
100#endif
101        if (display->disp[eBDISPLAY_COMPOSITE].hGfxSource)
102        {
103                rc = BVDC_Source_Destroy(display->disp[eBDISPLAY_COMPOSITE].hGfxSource);
104                if (rc != BERR_SUCCESS)
105                {
106                        BDBG_ERR(("BVDC_Source_Destroy failed %d\n", rc));
107                }
108                display->disp[eBDISPLAY_COMPOSITE].hGfxSource = NULL;
109        }
110
111#ifdef CONFIG_GFX_ARGB32
112        bgraphics_p_surface_destroy(graphics, display->offscreen);
113        bgraphics_p_surface_destroy(graphics, display->osd);
114        bgraphics_p_surface_destroy(graphics, display->overlay);
115#else
116        for (display->surface_idx = eBSURFACE_0; display->surface_idx < eBSURFACE_MAX; display->surface_idx++)
117        {
118                if (display->osd[display->surface_idx].hSurface)
119                {
120                        rc = BSUR_Surface_Destroy(display->osd[display->surface_idx].hSurface);
121                        if (rc != BERR_SUCCESS)
122                        {
123                                BDBG_ERR(("BSUR_Surface_Destroy failed %d\n", rc));
124                        }
125                        display->osd[display->surface_idx].hSurface = NULL;
126                }
127                if (display->osd[display->surface_idx].hPalette)
128                {
129                        rc = BSUR_Palette_Destroy(display->osd[display->surface_idx].hPalette);
130                        if (rc != BERR_SUCCESS)
131                        {
132                                BDBG_ERR(("BSUR_Surface_Create failed %d\n", rc));
133                        }
134                        display->osd[display->surface_idx].hPalette = NULL;
135                }
136        }
137#endif
138
139        rc = BVDC_ApplyChanges(display->hVdc);
140        if (rc != BERR_SUCCESS)
141        {
142                BDBG_ERR(("BVDC_ApplyChanges failed %d\n", rc));
143        }
144        return BERR_SUCCESS;
145}
146
147/**
148Summary:
149    Allocate and reserve resources.   Expects allocated graphics
150    structure.
151**/
152static BERR_Code bgraphics_surface_create(bgraphics_t graphics)
153{
154        BERR_Code rc;
155        bdisplay_t display;
156        BFMT_VideoInfo formatInfo;
157        BFMT_VideoInfo osdFormatInfo;
158        BAVC_Gfx_Picture gfxPicture;
159
160#ifdef CONFIG_GFX_ARGB32
161        bsurface_create_settings settings;
162#endif
163        BDBG_ASSERT(graphics);
164        BDBG_ASSERT(graphics->display);
165
166        display = graphics->display;
167
168        rc = BFMT_GetVideoFormatInfo(graphics->eSurfaceFmt,&formatInfo);
169        if (rc != BERR_SUCCESS)
170        {
171                BDBG_ERR(("BFMT_GetVideoFormatInfo failed %d\n", rc));
172                goto error;
173        }
174
175        rc = BFMT_GetVideoFormatInfo(graphics->eOSDFmt,&osdFormatInfo);
176        if (rc != BERR_SUCCESS)
177        {
178                BDBG_ERR(("BFMT_GetVideoFormatInfo failed %d\n", rc));
179                goto error;
180        }
181
182#ifdef CONFIG_GFX_ARGB32
183        settings.width = osdFormatInfo.ulDigitalWidth;
184        settings.height = osdFormatInfo.ulDigitalHeight;
185        settings.pxl_format = BPXL_eA8_R8_G8_B8;
186
187        /* create offscreen RGB888 surface */
188        bgraphics_p_surface_create(graphics, &settings, &display->offscreen);
189        /* create overlay RGB888 surface */
190        bgraphics_p_surface_create(graphics, &settings, &display->overlay);
191        /* create frame buffer for both GFD0/GFD1 */
192        bgraphics_p_surface_create(graphics, &settings, &display->osd);
193#else   
194        /* Create surfaces so graphics sources can set the sureface */
195        for (display->surface_idx = eBSURFACE_0; display->surface_idx < eBSURFACE_MAX; display->surface_idx++)
196        {
197                rc = BSUR_Palette_Create(GetHEAP(),BPXL_NUM_PALETTE_ENTRIES(display->pxl_format),
198                        NULL,BPXL_eA8_Y8_Cb8_Cr8, BSUR_CONSTRAINT_ADDRESS_PIXEL_ALIGNED, &display->osd[display->surface_idx].hPalette);
199                if (rc != BERR_SUCCESS)
200                {
201                        BDBG_ERR(("BSUR_Palette_Create failed %d\n", rc));
202                        goto error;
203                }
204
205                rc = BSUR_Palette_GetAddress(display->osd[display->surface_idx].hPalette,
206                                                                         (void**)&(display->osd[display->surface_idx].p_palette));
207                if (rc != BERR_SUCCESS)
208                {
209                        BDBG_ERR(("BSUR_Palette_GetAddress failed %d\n", rc));
210                        goto error;
211                }
212
213                rc = BSUR_Surface_Create(GetHEAP(),osdFormatInfo.ulDigitalWidth,osdFormatInfo.ulDigitalHeight,
214                                                                 0,NULL,display->pxl_format, display->osd[display->surface_idx].hPalette,
215                                                                 BSUR_CONSTRAINT_ADDRESS_PIXEL_ALIGNED,NULL,&(display->osd[display->surface_idx].hSurface));
216                if (rc != BERR_SUCCESS)
217                {
218                        BDBG_ERR(("BSUR_Surface_Create failed %d\n", rc));
219                        goto error;
220                }
221
222                rc = BSUR_Surface_GetAddress(display->osd[display->surface_idx].hSurface,
223                                                                (void**)&(display->osd[display->surface_idx].p_pixels),
224                                                                &(display->osd[display->surface_idx].pitch));
225                if (rc != BERR_SUCCESS)
226                {
227                        BDBG_ERR(("BSUR_Surface_GetAddress failed %d\n", rc));
228                        goto error;
229                }
230        }
231        display->surface_idx = eBSURFACE_0;
232#endif
233#if HAS_HDMI
234        rc = BVDC_Source_Create(display->hVdc,&(display->disp[eBDISPLAY_HDMI].hGfxSource),BAVC_SourceId_eGfx0 + eBDISPLAY_HDMI,NULL);
235        if (rc != BERR_SUCCESS)
236        {
237                BDBG_ERR(("BFMT_GetVideoFormatInfo failed %d\n", rc));
238                goto error;
239        }
240       
241        BVDC_Source_DisableColorKey(display->disp[eBDISPLAY_HDMI].hGfxSource);
242        BVDC_Source_SetHorizontalScaleCoeffs(display->disp[eBDISPLAY_HDMI].hGfxSource,BVDC_FilterCoeffs_eAnisotropic);
243
244#ifdef CONFIG_GFX_ARGB32
245        gfxPicture.hSurface = display->osd->hSurface;
246#else
247        gfxPicture.hSurface = display->osd[display->surface_idx]->hSurface;
248#endif
249        /* same for R surface */
250        gfxPicture.hRSurface = NULL;
251        gfxPicture.hAlphaSurface = NULL;
252        gfxPicture.hAlphaRSurface = NULL;
253        gfxPicture.ucW0Alpha = 0;
254        gfxPicture.ucW1Alpha = 0;
255        gfxPicture.eInOrientation = BFMT_Orientation_e2D;
256
257        rc = BVDC_Source_SetSurface( display->disp[eBDISPLAY_HDMI].hGfxSource,
258                                                                 &gfxPicture );
259        if (rc != BERR_SUCCESS)
260        {
261                BDBG_ERR(("BVDC_Source_SetSurface failed %d\n", rc));
262                goto error;
263        }
264
265        rc = BVDC_ApplyChanges(display->hVdc);
266        if (rc != BERR_SUCCESS)
267        {
268                BDBG_ERR(("BVDC_ApplyChanges failed %d\n", rc));
269        }
270#endif
271
272#ifndef CONFIG_NO_SD_OUTPUT
273
274        rc = BVDC_Source_Create(display->hVdc,&(display->disp[eBDISPLAY_COMPOSITE].hGfxSource),BAVC_SourceId_eGfx0 + eBDISPLAY_COMPOSITE,NULL);
275        if (rc != BERR_SUCCESS)
276        {
277                BDBG_ERR(("BFMT_GetVideoFormatInfo failed %d\n", rc));
278                goto error;
279        }
280
281        BVDC_Source_DisableColorKey(display->disp[eBDISPLAY_COMPOSITE].hGfxSource);
282        BVDC_Source_SetHorizontalScaleCoeffs(display->disp[eBDISPLAY_COMPOSITE].hGfxSource,BVDC_FilterCoeffs_eAnisotropic);
283
284#ifdef CONFIG_GFX_ARGB32
285        gfxPicture.hSurface = display->osd->hSurface;
286#else
287        gfxPicture.hSurface = display->osd[display->surface_idx]->hSurface;
288#endif
289        /* same for R surface */
290        gfxPicture.hRSurface = NULL;
291        gfxPicture.hAlphaSurface = NULL;
292        gfxPicture.hAlphaRSurface = NULL;
293        gfxPicture.ucW0Alpha = 0;
294        gfxPicture.ucW1Alpha = 0;
295        gfxPicture.eInOrientation = BFMT_Orientation_e2D;
296        rc = BVDC_Source_SetSurface( display->disp[eBDISPLAY_COMPOSITE].hGfxSource,
297                                                                 &gfxPicture );
298        if (rc != BERR_SUCCESS)
299        {
300                BDBG_ERR(("BVDC_Source_SetSurface failed %d\n", rc));
301                goto error;
302        }
303
304        rc = BVDC_ApplyChanges(display->hVdc);
305        if (rc != BERR_SUCCESS)
306        {
307                BDBG_ERR(("BVDC_ApplyChanges failed %d\n", rc));
308        }
309#endif
310        display->surface_idx = eBSURFACE_1;
311
312        return BERR_SUCCESS;
313error:
314        bgraphics_surface_destroy(graphics);
315        return rc;
316}
317
318/**
319Summary:
320        Open and enable a graphics engine.
321Description:
322        Each display can have only one graphics engine. A graphics
323        engine can only go to one display.
324
325        Graphics can also appear on a second
326        display when it is driven by a cloned window (see bdecode_window_close),
327        however those graphics are identical to the other display.
328
329        When the display format for the linked display changes, the graphics engine
330        will be automatically disabled. See bdisplay_set for details.
331        bgraphics_t bgraphics_open(
332                bobject_t id,
333                bdisplay_t display      display on which the graphics are displayed
334                );
335**/
336
337bgraphics_t bgraphics_open( bobject_t id, bdisplay_t display)
338{
339        if (s_graphics.state > eGS_UNINITIALIZED)
340                return (bgraphics_t)0;
341
342        s_graphics.display = display;
343
344        if (s_settings.format == bgraphics_pixel_format_palette4)
345                display->pxl_format = BPXL_eP4;
346        else if  (s_settings.format == bgraphics_pixel_format_palette8)
347                display->pxl_format = BPXL_eP8;
348        else if (s_settings.format == bgraphics_pixel_format_r5_g6_b5)
349                display->pxl_format = BPXL_eR5_G6_B5;
350
351#ifdef CONFIG_GFX_ARGB32
352        if (BGRC_Open(&s_graphics.grc, GetCHP(), GetREG(), GetHEAP(), GetINT(), NULL) != BERR_SUCCESS) 
353        {
354                BDBG_WRN(("BGRC_Open failed"));
355                return (bgraphics_t)0;
356        }
357        if (BGRC_Source_ToggleFilter(s_graphics.grc, false, false) != BERR_SUCCESS) 
358        {
359                BDBG_WRN(("BGRC_Source_ToggleFilter failed"));
360        }
361        if (BGRC_Source_ToggleColorKey(s_graphics.grc, false) != BERR_SUCCESS)
362        {
363                BDBG_WRN(("BGRC_Source_ToggleColorKey failed"));
364        }
365        if (BGRC_Output_SetColorKeySelection(s_graphics.grc, 
366                        BGRC_Output_ColorKeySelection_eTakeBlend,
367                        BGRC_Output_ColorKeySelection_eTakeSource,
368                        BGRC_Output_ColorKeySelection_eTakeDestination,
369                        BGRC_Output_ColorKeySelection_eTakeDestination) != BERR_SUCCESS)
370        {
371                BDBG_WRN(("BGRC_Output_SetColorKeySelection failed"));
372        }
373
374        /* calculate coefficient table for offscreen->SD frame buffer */
375        b_graphics_calculate_coeffs_table(true, false, s_graphics.coeffs_ycbcr);
376        b_graphics_calculate_coeffs_table(false, true, s_graphics.coeffs_rgb);
377        BKNI_CreateEvent(&s_graphics.event);
378        bos_create_mutex(&s_graphics.mutex);
379#endif
380        b_lock_vdc();
381        if (bgraphics_surface_create(&s_graphics) != BERR_SUCCESS)
382        {
383                goto error;
384        }
385#if HAS_HDMI
386        if (bdisplay_graphics_window_open(display,eBDISPLAY_HDMI) != BERR_SUCCESS)
387        {
388                bgraphics_surface_destroy(&s_graphics);
389                goto error;
390        }
391#endif
392#ifndef CONFIG_NO_SD_OUTPUT
393
394        if (bdisplay_graphics_window_open(display,eBDISPLAY_COMPOSITE) != BERR_SUCCESS)
395        {
396#if HAS_HDMI
397                bdisplay_graphics_window_close(display,eBDISPLAY_HDMI);
398#endif
399                bgraphics_surface_destroy(&s_graphics);
400                goto error;
401        }
402#endif
403        s_graphics.state = eGS_OPENED;
404        b_unlock_vdc();
405        return (bgraphics_t)&s_graphics;
406error:
407        b_unlock_vdc();
408        return (bgraphics_t)0;
409}
410
411
412/*
413Summary:
414        Close a graphics engine.
415Description:
416        You should close all surfaces before closing a graphics engine.
417        Also, you must close graphics before closing the display to which it is linked.
418        Generally, close things in the opposite order in which you brought them up.
419        void bgraphics_close(
420                bgraphics_t graphics    handle returned by bgraphics_open
421                );
422*/
423
424void bgraphics_close( bgraphics_t graphics )
425{
426        if (graphics->state == eGS_OPENED)
427        {
428                b_lock_vdc();
429                bgraphics_surface_destroy(graphics);
430#if HAS_HDMI
431                bdisplay_graphics_window_close(graphics->display,eBDISPLAY_HDMI);
432#endif
433                bdisplay_graphics_window_close(graphics->display,eBDISPLAY_COMPOSITE);
434#ifdef CONFIG_GFX_ARGB32
435                BGRC_Close(graphics->grc);
436                BKNI_DestroyEvent(graphics->event);
437                bos_delete_mutex(&graphics->mutex);
438#endif
439                graphics->state = eGS_UNINITIALIZED;
440                b_unlock_vdc();
441        }
442}
443
444/*
445Summary:
446Get the graphics framebuffer parameters
447        bresult bgraphics_get_framebuffer(
448                bgraphics_t graphics,    handle returned by bgraphics_open
449                void **buffer,                  [out] address of framebuffer memory
450                unsinged int **palette, [out] address of palette
451                int *width,                             [out] width of the OSD surface
452                int *height,                    [out] height of the OSD surface
453                int *pitch                              [out] pitch of the OSD surface
454                );
455*/
456
457bresult bgraphics_get_framebuffer( bgraphics_t graphics, void **buffer, 
458                unsigned int **palette, int *width, int *height, int *pitch)
459{
460        BFMT_VideoInfo osdFormatInfo;
461        bdisplay_t display;
462        BERR_Code rc;
463
464        BDBG_ASSERT(graphics);
465        BDBG_ASSERT(graphics->display);
466
467        display = graphics->display;
468
469        rc = BFMT_GetVideoFormatInfo(graphics->eOSDFmt,&osdFormatInfo);
470        if (rc != BERR_SUCCESS)
471        {
472                BDBG_ERR(("BFMT_GetVideoFormatInfo failed %d\n", rc));
473                return rc;
474        }
475#ifdef CONFIG_GFX_ARGB32
476        /* return offscreen frame buffer */
477        *buffer = display->offscreen->p_pixels;
478        *palette = (unsigned int *)display->offscreen->p_palette;
479        *pitch = display->offscreen->pitch;
480        *width = display->offscreen->width;
481        *height = display->offscreen->height;
482#else
483        *buffer = display->osd[display->surface_idx]->p_pixels;
484        *palette = (unsigned int*)display->osd[display->surface_idx]->p_palette;
485        *width = osdFormatInfo.ulDigitalWidth;
486        *height = osdFormatInfo.ulDigitalHeight;
487        *pitch = display->osd[display->surface_idx]->pitch;
488#endif
489        return b_ok;
490}
491
492#ifdef CONFIG_GFX_ARGB32
493bresult bgraphics_get_osd_params(bgraphics_t graphics, 
494                                                                 bsettop_surf_t *p_osd_surf,
495                                                                 void **osd_buffer,
496                                                                 bsettop_surf_t *p_overlay_surf,
497                                                                 void **overlay_mem)
498{
499        bdisplay_t display;
500
501        BDBG_ASSERT(graphics);
502        BDBG_ASSERT(graphics->display);
503
504        display = graphics->display;
505        if (p_osd_surf && osd_buffer)
506        {
507                *p_osd_surf = display->offscreen;
508                *osd_buffer = display->offscreen->p_pixels;
509        }
510        if (overlay_mem && p_overlay_surf)
511        {
512                *overlay_mem = display->overlay->p_pixels;
513                *p_overlay_surf = display->overlay;
514        }
515        return b_ok;
516}
517#endif
518
519/**
520Summary:
521        Wait for vsync.
522        id : bgraphics_id to be updated
523**/
524bresult bgraphics_osd_flush(bgraphics_t graphics, bgraphics_id id, bool overlay)
525{
526        BERR_Code rc;
527        bdisplay_t display;
528#ifndef CONFIG_GFX_ARGB32
529        BAVC_Gfx_Picture gfxPicture;
530#endif
531        BDBG_ASSERT(graphics);
532        BDBG_ASSERT(graphics->display);
533       
534        display = graphics->display;
535
536#ifdef CONFIG_GFX_ARGB32
537        rc = bgraphics_p_flip_osd(graphics, id, overlay);
538#else   
539        b_lock_vdc();
540        gfxPicture.hSurface = display->osd[display->surface_idx].hSurface;
541        /* same for R surface */
542        gfxPicture.hRSurface = NULL;
543        gfxPicture.hAlphaSurface = NULL;
544        gfxPicture.hAlphaRSurface = NULL;
545        gfxPicture.ucW0Alpha = 0;
546        gfxPicture.ucW1Alpha = 0;
547        gfxPicture.eInOrientation = BFMT_Orientation_e2D;
548
549#if HAS_HDMI
550        rc = BVDC_Source_SetSurface( display->disp[eBDISPLAY_HDMI].hGfxSource, 
551                                                         &gfxPicture );
552        if (rc != BERR_SUCCESS)
553        {
554                BDBG_ERR(("BVDC_Source_SetSurface failed %d\n", rc));
555                b_unlock_vdc();
556                return rc;
557        }
558#endif
559
560#ifndef CONFIG_NO_SD_OUTPUT
561        gfxPicture.hSurface = display->osd[display->surface_idx].hSurface;
562        /* same for R surface */
563        gfxPicture.hRSurface = NULL;
564        gfxPicture.hAlphaSurface = NULL;
565        gfxPicture.hAlphaRSurface = NULL;
566        gfxPicture.ucW0Alpha = 0;
567        gfxPicture.ucW1Alpha = 0;
568        gfxPicture.eInOrientation = BFMT_Orientation_e2D;
569       
570        rc = BVDC_Source_SetSurface( display->disp[eBDISPLAY_COMPOSITE].hGfxSource, 
571                                                         &gfxPicture );
572        if (rc != BERR_SUCCESS)
573        {
574                BDBG_ERR(("BVDC_Source_SetSurface failed %d\n", rc));
575                b_unlock_vdc();
576                return rc;
577        }
578#endif 
579        rc = BVDC_ApplyChanges(display->hVdc);
580        if (rc != BERR_SUCCESS)
581        {
582                BDBG_ERR(("BVDC_ApplyChanges failed %d\n", rc));
583        }
584
585        display->surface_idx++;
586        if (display->surface_idx >= eBSURFACE_MAX)
587        {
588                display->surface_idx = eBSURFACE_0;
589        }
590        rc = b_ok;
591        b_unlock_vdc();
592#endif
593        return rc;
594}
595
596/**
597Summary:
598Block until vsync.
599        bresult bgraphics_sync(
600                bgraphics_t g   handle returned by bgraphics_open
601                );
602**/
603
604bresult bgraphics_sync( bgraphics_t g, bool overlay)
605{
606        return bgraphics_osd_flush(g, eGRAPHICS_BOTH, overlay);
607}
608
609/**
610 * Summary:
611 * update HD frame buffer only
612 * id: graphics_id to be updated
613 */
614bresult bgraphics_sync_partial(bgraphics_t g, bgraphics_id id, bool overlay)
615{
616        if (!(id&eGRAPHICS_BOTH))
617                id = eGRAPHICS_BOTH;
618
619        return bgraphics_osd_flush(g, id, overlay);
620}
621/*
622Summary:
623Reload the graphics palette
624        bresult bgraphics_load_palette(
625                bgraphics_t g    handle returned by bgraphics_open
626                );
627*/
628
629
630bresult bgraphics_load_palette( bgraphics_t graphics )
631{
632#ifndef CONFIG_GFX_ARGB32
633        bsettop_surf_idx_t              osd_surface_idx;
634        bdisplay_t display;
635
636        BDBG_ASSERT(graphics);
637        BDBG_ASSERT(graphics->display);
638
639        display = graphics->display;
640
641        for (osd_surface_idx=0; osd_surface_idx<eBSURFACE_MAX; osd_surface_idx++) {
642                if (osd_surface_idx != display->surface_idx) {
643                        BKNI_Memcpy(display->osd[osd_surface_idx].p_palette, 
644                                display->osd[display->surface_idx].p_palette, BPXL_NUM_PALETTE_ENTRIES(display->pxl_format)* sizeof(unsigned int));
645                }
646        }
647        return bgraphics_osd_flush(graphics, eGRAPHICS_BOTH, false);
648#endif
649        return b_ok;
650}
651
652/**
653Summary:
654        Set graphics settings.
655**/
656bresult bgraphics_set(
657        const bgraphics_settings *settings
658        )
659{
660        if ((settings->format != bgraphics_pixel_format_palette4) &&
661                (settings->format != bgraphics_pixel_format_palette8) &&
662                (settings->format != bgraphics_pixel_format_r5_g6_b5))
663        {
664                return berr_invalid_parameter;
665        }
666
667
668        s_settings.format = settings->format;
669
670        return b_ok;
671}
672
673/**
674Summary:
675        Get current graphics settings.
676**/
677void bgraphics_get(
678        bgraphics_settings *settings /* [out] */
679        )
680{
681        *settings = s_settings;
682        return;
683}
684
685#ifdef CONFIG_GFX_ARGB32
686static BERR_Code bgraphics_p_set_event(BGRC_Handle hGrc, void *pData)
687{
688        BKNI_SetEvent((BKNI_EventHandle)pData);
689        return 0;
690}
691
692/*
693 * Description:
694 *   RGB to YCbCr color matrix table.
695 * 
696 * Y  = R *  0.257 + G *  0.504 + B *  0.098 + A * 0 + 16
697 * Cb = R * -0.148 + G * -0.291 + B *  0.439 + A * 0 + 128
698 * Cr = R *  0.439 + G * -0.368 + B * -0.071 + A * 0 + 128
699 * A = R *  0 + G * 0 + B * 0 + A * 1 + 0
700 */
701static const float s_Matrix_RGBtoYCbCr[25] = {
7020.257, 0.504, 0.098, 0, 16,
703-0.148, -0.291, 0.439, 0, 128,
7040.439, -0.368, -0.071, 0, 128,
7050, 0, 0, 1, 0,
7060, 0, 0, 0, 1
707};
708
709/***************************************************************************
710 * Description:
711 *         YCbCr to RGB color matrix table.
712 *****************************************************************************/
713/* R = Y * 1.164 + Cb * 0 + Cr * 1.596 + A * 0 - 223 */
714/* G = Y * 1.164 + Cb * -0.391 + Cr * -0.813 + A * 0 + 136 */
715/* B = Y * 1.164 + Cb * 2.018 + Cr * 0 + A * 0 - 277 */
716/* A = Y * 0 + Cb * 0 + Cr * 0 + A * 1 - 0 */
717
718static const float s_Matrix_YCbCrtoRGB[25] = {
7191.164, 0, 1.596, 0, -223,
7201.164, -0.391, -0.813, 0, 136,
7211.164, 2.018, 0, 0, -277,
7220, 0, 0, 1, 0,
7230, 0, 0, 0, 1
724};
725
726
727static void bgraphics_cacheflush(bsettop_surf_t surf);
728
729/* Description:
730 * Coefficient table for RGB (offscreen) to YCbCr(GFD1 for composite)
731 */
732static void b_graphics_calculate_coeffs_table(bool src_rgb, bool dest_rgb, int32_t *pcoeffs)
733{
734        float a[25], b[25];
735        int i,j,k;
736
737        BKNI_Memset(a, 0, sizeof(a));
738        BKNI_Memset(b, 0, sizeof(b));
739
740        for (i=0; i<=24; i+=6)
741                a[i] = 1.0;
742
743        if (src_rgb) {
744                for (i=0; i<=4; i++)
745                        for (j=0; j<=4; j++)
746                                for (k=0; k<=4; k++)
747                                        b[i*5+j] += a[i*5+k]*s_Matrix_RGBtoYCbCr[k*5+j];
748
749                for (i=0; i<=24; i++)
750                        a[i] = b[i];
751
752                BKNI_Memset(b, 0, sizeof(b));
753        }
754
755        if (dest_rgb) {
756                for (i=0; i<=4; i++)
757                        for (j=0; j<=4; j++)
758                                for (k=0; k<=4; k++) 
759                                        b[i * 5 + j] += s_Matrix_YCbCrtoRGB[i * 5 + k] * a[k * 5 + j];
760                for (i=0; i<=24; i++)
761                        a[i] = b[i];
762        }
763
764        for (i=0; i<=19; i++)
765                pcoeffs[i] = (int32_t)(a[i]*(1<<10));
766
767        return; 
768}
769
770/*
771 * Description
772 * Blit source surface + destination to output surface
773 */
774bresult bsurface_blit_surface(bsettop_surf_t src_surf, bsettop_rect *src_r, 
775                                                         bsettop_surf_t dst_surf, bsettop_rect *dst_r,
776                                                         bsettop_surf_t out_surf, bsettop_rect *out_r)
777{
778        BGRC_Handle grc;
779        BERR_Code rc;
780        bgraphics_t graphics = &s_graphics;     
781        int32_t *coeffs = NULL;
782
783        BDBG_ASSERT(graphics->grc);
784
785        grc = s_graphics.grc;
786
787        if (bos_acquire_mutex(&s_graphics.mutex, 500) != b_ok) {
788                BDBG_WRN(("fail to get graphics mutex"));
789                return BERR_SUCCESS;
790        }
791
792        /* clear the GRC state */
793        BGRC_ResetState(grc);
794
795        /* set source surface and rectangle */
796        rc = BGRC_Source_SetSurface(grc, src_surf->hSurface);
797        if (rc != BERR_SUCCESS) {
798                BDBG_ERR(("BGRC_Source_SetSurface failed: %d", rc));
799                goto blit_err;
800        }
801
802        rc = BGRC_Source_SetRectangle(grc, src_r->x, src_r->y, src_r->width, src_r->height);
803        if (rc != BERR_SUCCESS) {
804                BDBG_ERR(("BGRC_Source_SetRectangle failed: %d", rc));
805                goto blit_err;
806        }
807       
808        /* set scale coefficients */
809        if ((src_r->width != out_r->width) || (src_r->height != out_r->height)) 
810        {
811                rc = BGRC_Source_ToggleFilter(grc, true, true);
812                if (rc != BERR_SUCCESS) {
813                        BDBG_ERR(("BGRC_Source_ToggleFilter failed: %d"));
814                        goto blit_err;
815                }
816                rc = BGRC_Source_SetFilterCoeffs(grc, BGRC_FilterCoeffs_eAnisotropic, BGRC_FilterCoeffs_eAnisotropic);
817                if (rc != BERR_SUCCESS) {
818                        BDBG_ERR(("BGRC_Source_SetFilterCoeffs failed: %d", rc));
819                        goto blit_err;
820                }
821        }
822        else
823        { /* no scaling */
824                rc = BGRC_Source_ToggleFilter(grc, false, false);       
825                if (rc != BERR_SUCCESS) {
826                        BDBG_ERR(("BGRC_Source_ToggleFilter failed: %d", rc));
827                        goto blit_err;
828                }
829        }
830
831
832        /* disable color keying */
833        rc = BGRC_Source_ToggleColorKey(grc, false);
834        if (rc != BERR_SUCCESS) {
835                BDBG_ERR(("BGRC_Source_ToggleColorKey failed: %d", rc));
836                goto blit_err;
837        }
838
839        if (src_surf->pixel_format != out_surf->pixel_format) {
840                /* color space conversion required */
841                rc = BGRC_Source_ToggleColorMatrix(grc, true);
842                if (rc != BERR_SUCCESS) {
843                        BDBG_ERR(("BGRC_Source_ToggleColorMatrix failed: %d", rc));
844                        goto blit_err;
845                }
846                if (src_surf->pixel_format == BPXL_eA8_R8_G8_B8) {
847                        if (out_surf->pixel_format == BPXL_eA8_Y8_Cb8_Cr8) {
848                                coeffs = graphics->coeffs_ycbcr;
849                        }
850                }
851                else if (src_surf->pixel_format == BPXL_eA8_Y8_Cb8_Cr8) {
852                        if (out_surf->pixel_format == BPXL_eA8_R8_G8_B8) {
853                                coeffs = graphics->coeffs_rgb;
854                        }
855                }
856                if (coeffs == NULL) {
857                        BDBG_WRN(("Unsupported CSC : %d -> %d", src_surf->pixel_format, out_surf->pixel_format));
858                        /* disable CSC */
859                        BGRC_Source_ToggleColorMatrix(grc, false);
860                }
861                else
862                {
863                        rc = BGRC_Source_SetColorMatrix5x4(grc, coeffs, 10);
864                        if (rc != BERR_SUCCESS) {
865                                BDBG_ERR(("BGRC_Source_SetColorMatrix5x4 failed: %d", rc));
866                                goto blit_err;
867                        }
868                }
869        }
870        else {
871                rc = BGRC_Source_ToggleColorMatrix(grc, false);         
872                if (rc != BERR_SUCCESS) {
873                        BDBG_ERR(("BGRC_Source_ToggleColorMatrx failed:%d", rc));
874                }
875        }
876       
877        /* set destination surface if exist */
878        if (dst_surf) {
879                rc = BGRC_Destination_SetSurface(grc, dst_surf->hSurface);
880                if (rc != BERR_SUCCESS) {
881                        BDBG_ERR(("BGRC_Destination_SetSurface failed: %d",rc));
882                        goto blit_err;
883                }
884
885                rc = BGRC_Destination_SetRectangle(grc, dst_r->x, dst_r->y, dst_r->width, dst_r->height);
886                if (rc != BERR_SUCCESS) {
887                        BDBG_ERR(("BGRC_Destination_SetRectangle failed: %d", rc));
888                        goto blit_err;
889                }
890        }
891
892        /* set output surface and rectangle */
893        rc = BGRC_Output_SetSurface(grc, out_surf->hSurface);
894        if (rc != BERR_SUCCESS) {
895                BDBG_ERR(("BGRC_Output_SetSurface failed: %d", rc));
896                goto blit_err;
897        }
898
899        rc = BGRC_Output_SetRectangle(grc, out_r->x, out_r->y, out_r->width, out_r->height); 
900        if (rc != BERR_SUCCESS) {
901                BDBG_ERR(("BGRC_Output_SetRectangle faield: %d", rc));
902                goto blit_err;
903        }
904
905        /* setup blend setting */
906        if (dst_surf) {
907                rc = BGRC_Output_SetColorKeySelection(grc,
908                        BGRC_Output_ColorKeySelection_eTakeBlend, /* default is eTakeSource */
909                        BGRC_Output_ColorKeySelection_eTakeSource,
910                        BGRC_Output_ColorKeySelection_eTakeDestination,
911                        BGRC_Output_ColorKeySelection_eTakeDestination);
912                if (rc != BERR_SUCCESS) {
913                        BDBG_ERR(("BGRC_Output_SetColorKeySelection failed: %d", rc));
914                        goto blit_err;
915                }
916                rc = BGRC_Blend_SetColorBlend(grc,
917                        BGRC_Blend_Source_eSourceColor,BGRC_Blend_Source_eSourceAlpha,
918                        false,
919                        BGRC_Blend_Source_eDestinationColor,BGRC_Blend_Source_eInverseSourceAlpha,
920                        false,
921                        BGRC_Blend_Source_eZero);
922                if (rc != BERR_SUCCESS) {
923                        BDBG_ERR(("BGRC_Blend_SetColorBlend failed: %d", rc));
924                        goto blit_err;
925                }
926                rc = BGRC_Blend_SetAlphaBlend(grc,
927                        BGRC_Blend_Source_eSourceAlpha,BGRC_Blend_Source_eOne,
928                        false, 
929                        BGRC_Blend_Source_eDestinationAlpha, BGRC_Blend_Source_eInverseSourceAlpha,
930                        false, 
931                        BGRC_Blend_Source_eZero);       
932                if (rc != BERR_SUCCESS) {
933                        BDBG_ERR(("BGRC_Blend_SetAlphaBlend failed: %d", rc));
934                        goto blit_err;
935                }
936        }
937        else {
938                rc = BGRC_Blend_SetAlphaBlend(grc,
939                        BGRC_Blend_Source_eSourceAlpha,BGRC_Blend_Source_eOne,
940                        false, 
941                        BGRC_Blend_Source_eZero, BGRC_Blend_Source_eZero,
942                        false, 
943                        BGRC_Blend_Source_eZero);       
944        }       
945
946        bgraphics_cacheflush(src_surf);
947        if (dst_surf != out_surf) {
948                bgraphics_cacheflush(out_surf);
949        }
950        if (dst_surf) {
951                bgraphics_cacheflush(dst_surf);
952        }
953       
954        rc = BGRC_IssueState(grc, bgraphics_p_set_event, graphics->event);
955        if (rc != BERR_SUCCESS) {
956                BDBG_ERR(("BGRC_IssueState failed: %d", rc));
957                goto blit_err;
958        }
959       
960        bos_release_mutex(&s_graphics.mutex);
961        rc = BKNI_WaitForEvent(graphics->event, 500000);
962        if (rc == BERR_TIMEOUT) 
963                rc = berr_timeout;
964        else if (rc != BERR_SUCCESS)
965                BDBG_ERR(("BKNI_WaitForEvent failed  %d (%d)", rc, __LINE__));
966        return rc;
967
968blit_err:
969        bos_release_mutex(&s_graphics.mutex);
970        return (rc == BERR_SUCCESS) ? b_ok : berr_invalid_parameter;
971}
972
973/* Description:
974 * Blit offscreen to OSD frame buffer
975 */
976static BERR_Code bgraphics_p_flip_osd(bgraphics_t graphics, bgraphics_id id, bool overlay)
977{
978        BERR_Code rc;
979        bsettop_surf_t out_surf,dst_surf,src_surf;     
980        bsettop_rect out_r,dst_r,src_r;
981
982        BDBG_ASSERT(graphics);
983        rc = BERR_UNKNOWN;
984        BKNI_Memset(&src_r, 0, sizeof(src_r));
985        BKNI_Memset(&dst_r, 0, sizeof(dst_r));
986        BKNI_Memset(&out_r, 0, sizeof(out_r));
987
988        out_surf = graphics->display->osd;
989        out_r.width = out_surf->width;
990        out_r.height = out_surf->height;
991
992        if (!overlay) {
993                src_surf = graphics->display->offscreen;
994                dst_surf = NULL; /* blit offscreen into framebuffer */
995
996                src_r.width = src_surf->width;
997                src_r.height = src_surf->height;
998                rc = bsurface_blit_surface(src_surf,&src_r, dst_surf,&dst_r, out_surf, &out_r ); 
999        }
1000        else {
1001                /* alpha blending : overlay+offscreen -> HD frame buffer */
1002                src_surf = graphics->display->overlay;
1003                dst_surf = graphics->display->offscreen;
1004
1005                src_r.width = src_surf->width;
1006                src_r.height = src_surf->height;
1007                dst_r.width = dst_surf->width;
1008                dst_r.height = dst_surf->height;
1009                rc = bsurface_blit_surface(src_surf,&src_r, dst_surf,&dst_r, out_surf, &out_r );
1010        }
1011
1012        return rc;
1013}
1014
1015/* Description:
1016 * Fill surface
1017 */
1018bresult bsurface_fill(bsettop_surf_t surface, bsettop_rect *rect, bgraphics_pixel pixel)
1019{
1020        BGRC_Handle grc;
1021        BERR_Code rc;
1022        unsigned int aycbcr_pixel;
1023
1024        grc = s_graphics.grc;
1025        BDBG_ASSERT(grc);
1026        BDBG_ASSERT(surface);
1027
1028        if (!rect->width || !rect->height) 
1029                return b_ok;
1030
1031        if (bos_acquire_mutex(&s_graphics.mutex, 500) != b_ok) {
1032                BDBG_WRN(("fail to get graphics mutex"));
1033                return b_ok;
1034        }
1035
1036        BGRC_ResetState(grc);
1037        rc = BGRC_Destination_SetSurface(grc, NULL);
1038        if (rc != BERR_SUCCESS) {
1039                BDBG_ERR(("BGRC_Destination_SetSurface failed: %d", rc));
1040                goto fill_err;
1041        }
1042
1043        switch (surface->pixel_format) {
1044                case bgraphics_pixel_format_palette2:
1045                        pixel = (pixel & 0x3) << 6;
1046                        break;
1047                case bgraphics_pixel_format_palette4:
1048                        pixel = (pixel & 0xF) << 4;
1049                        break;
1050                case bgraphics_pixel_format_a8_palette8:
1051                        pixel = ((pixel&0xFF00)<<16) | (pixel&0xFF);
1052                        break;
1053                case bgraphics_pixel_format_a8:
1054                        pixel = (pixel & 0xFF)<<24;
1055                        break;
1056                case bgraphics_pixel_format_y08_cb8_y18_cr8:
1057                        BPXL_ConvertPixel_RGBtoYCbCr(BPXL_eA8_Y8_Cb8_Cr8, BPXL_eA8_R8_G8_B8, pixel, &aycbcr_pixel);
1058                        pixel = aycbcr_pixel;
1059                        break;
1060                default:
1061                        break;
1062        }
1063
1064        /* turn off features possibly turned on by copy */
1065        rc = BGRC_Source_ToggleColorKey(grc, false);
1066        if (rc != BERR_SUCCESS) {
1067                BDBG_ERR(("BGRC_Source_ToggleColorKey failed: %d", rc));
1068                goto fill_err;
1069        }
1070
1071        rc = BGRC_Source_ToggleFilter(grc, false, false);
1072        if (rc != BERR_SUCCESS) {
1073                BDBG_ERR(("BGRC_Source_ToggleFilter failed: %d", rc));
1074                goto fill_err;
1075        }
1076
1077        rc = BGRC_Source_ToggleColorMatrix(grc, false);
1078        if (rc != BERR_SUCCESS) {
1079                BDBG_ERR(("BGRC_Source_ToggleColorMatrix failed: %d", rc));
1080                goto fill_err;
1081        }
1082
1083        /* set the output */
1084        rc = BGRC_Output_SetSurface(grc, surface->hSurface);
1085        if (rc != BERR_SUCCESS) {
1086                BDBG_ERR(("BGRC_Output_SetSurface failed: %d", rc));
1087                goto fill_err;
1088        }
1089
1090        if ((rect->x + rect->width) > surface->width) 
1091                rect->width = surface->width-rect->x;
1092        if ((rect->y + rect->height) > surface->height) 
1093                rect->height = surface->height-rect->y;
1094
1095        rc = BGRC_Output_SetRectangle(grc, rect->x, rect->y, rect->width, rect->height);
1096        if (rc != BERR_SUCCESS) {
1097                BDBG_ERR(("BGRC_Output_SetRectangle failed: %d (%d %d %d %d)", rc, rect->x, rect->y, rect->width, rect->height));
1098                goto fill_err;
1099        }
1100
1101        rc = BGRC_Source_SetSurface(grc, NULL);
1102        if (rc != BERR_SUCCESS) {
1103                BDBG_ERR(("BGRC_Source_SetSurface failed: %d", rc));
1104                goto fill_err;
1105        }
1106        rc = BGRC_Source_SetColor(grc, pixel);
1107        if (rc != BERR_SUCCESS) {
1108                BDBG_ERR(("BGRC_Source_SetColor failed: %d", rc));
1109                goto fill_err;
1110        }
1111#if 0
1112        rc = BGRC_Output_SetColorKeySelection(grc,
1113                        BGRC_Output_ColorKeySelection_eTakeSource,
1114                        BGRC_Output_ColorKeySelection_eTakeSource,
1115                        BGRC_Output_ColorKeySelection_eTakeDestination,
1116                        BGRC_Output_ColorKeySelection_eTakeDestination);
1117
1118        if (rc != BERR_SUCCESS) {
1119                BDBG_ERR(("BGRC_Output_SetColorKeySelection failed: %d", rc));
1120                return rc;
1121        }
1122#endif
1123        bgraphics_cacheflush(surface);
1124        rc = BGRC_IssueState(grc, bgraphics_p_set_event, s_graphics.event);
1125        bos_release_mutex(&s_graphics.mutex);
1126        if (rc == BERR_TIMEOUT) { 
1127                rc = berr_timeout; 
1128        }
1129        else if (rc != BERR_SUCCESS)
1130                BDBG_ERR(("BKNI_WaitForEvent failed  %d (%d)", rc, __LINE__));
1131        BKNI_WaitForEvent(s_graphics.event, 500000);
1132        return rc;
1133
1134fill_err:
1135        bos_release_mutex(&s_graphics.mutex);
1136        return (rc == BERR_SUCCESS) ? b_ok : berr_invalid_parameter;
1137}
1138
1139/* Description:
1140 * destroy surface
1141 */
1142static void bgraphics_p_surface_destroy(bgraphics_t graphics, bsettop_surf_t surface)
1143{
1144        BERR_Code rc;
1145
1146        if (surface->hSurface) {
1147                rc = BSUR_Surface_Destroy(surface->hSurface);
1148                if (rc != BERR_SUCCESS) {
1149                        BDBG_ERR(("BSUR_Surface_Destroy failed %d", rc));
1150                }
1151        }
1152
1153        surface->hSurface = NULL;
1154        if (surface->hPalette) {
1155                rc = BSUR_Palette_Destroy(surface->hPalette);
1156                if (rc != BERR_SUCCESS) {
1157                        BDBG_ERR(("BSUR_Palette_Destroy failed %d", rc));
1158                }
1159                surface->hPalette = NULL;
1160        }
1161        BKNI_Free(surface);
1162}
1163
1164/* Description:
1165 * create surface
1166 */
1167static BERR_Code bgraphics_p_surface_create(bgraphics_t graphics, const bsurface_create_settings *psettings, bsettop_surf_t *surface)
1168{
1169        BERR_Code rc;
1170        bdisplay_t display;
1171        void *uncached;
1172
1173        BDBG_ASSERT(graphics);
1174        BDBG_ASSERT(graphics->display);
1175
1176        display = graphics->display;
1177
1178        *surface = BKNI_Malloc(sizeof(struct bsettop_surf));
1179        if (*surface == NULL) {
1180                BDBG_ERR(("bgraphics_p_surface_create failed allocating surface structure"));
1181                goto error;
1182        }
1183
1184        /* TODO: check pxl_format or palette_format */
1185        if (BPXL_IS_PALETTE_FORMAT(psettings->pxl_format))
1186        {
1187                rc = BSUR_Palette_Create(GetHEAP(), BPXL_NUM_PALETTE_ENTRIES(psettings->pxl_format),
1188                                NULL, psettings->pxl_format, BSUR_CONSTRAINT_ADDRESS_PIXEL_ALIGNED, &(*surface)->hPalette);
1189                if (rc != BERR_SUCCESS) {
1190                        BDBG_ERR(("BSUR_Palette_Create failed %d", rc));
1191                        goto error;
1192                }
1193                rc = BSUR_Palette_GetAddress((*surface)->hPalette, (void **)&uncached);
1194                if (rc != BERR_SUCCESS) {
1195                        BDBG_ERR(("BSUR_Palette_GetAddress failed %d", rc));
1196                        goto error;
1197                }
1198                rc = BMEM_Heap_ConvertAddressToCached(GetHEAP(), uncached, (void **)&((*surface)->p_palette));
1199                if (rc != BERR_SUCCESS) {
1200                        BDBG_ERR(("BMEM_Heap_ConvertAddressToCached failed %d", rc));
1201                        goto error;
1202                }
1203
1204        } else {
1205                (*surface)->hPalette = NULL;
1206        }
1207
1208        rc = BSUR_Surface_Create(GetHEAP(), psettings->width, psettings->height, 0, NULL, psettings->pxl_format, (*surface)->hPalette,
1209                        BSUR_CONSTRAINT_ADDRESS_PIXEL_ALIGNED, NULL, &(*surface)->hSurface);
1210        if (rc != BERR_SUCCESS) {
1211                BDBG_ERR(("BSUR_Surface_Create failed %d", rc));
1212                goto error;
1213        }
1214
1215        (*surface)->pixel_format = psettings->pxl_format;
1216        (*surface)->width = psettings->width;
1217        (*surface)->height = psettings->height;
1218
1219        rc = BSUR_Surface_GetAddress((*surface)->hSurface, (void **)&uncached, &(*surface)->pitch);
1220        if (rc != BERR_SUCCESS) {
1221                BDBG_ERR(("BSUR_Surface_GetAddress failed %d", rc));
1222                goto error;
1223        }
1224        rc = BMEM_Heap_ConvertAddressToCached(GetHEAP(), uncached, (void **)&((*surface)->p_pixels));
1225        if (rc != BERR_SUCCESS) {
1226                BDBG_ERR(("BMEM_Heap_ConvertAddressToCached failed %d", rc));
1227                goto error;
1228        }
1229        /* clear the buffer */
1230        BKNI_Memset((*surface)->p_pixels, 0, (*surface)->pitch * psettings->height);
1231error:
1232        return rc;
1233}
1234
1235/*
1236 * Description:
1237 * create BSUR_Surface. RGB888 or YCbCr888 only
1238 */
1239bresult bsurface_create_surface(uint32_t width, uint32_t height, 
1240                bgraphics_pixel_format format, 
1241                uint8_t **mem, uint32_t *pitch,
1242                bsettop_surf_t *p_settop_surf)
1243{
1244        bsurface_create_settings settings;
1245        BERR_Code rc = BERR_SUCCESS;
1246
1247        BDBG_ASSERT(p_settop_surf);
1248        switch (format) {
1249                case bgraphics_pixel_format_a8_r8_g8_b8:
1250                        settings.pxl_format = BPXL_eA8_R8_G8_B8;
1251                        break;
1252                case bgraphics_pixel_format_y08_cb8_y18_cr8:
1253                        settings.pxl_format = BPXL_eA8_Y8_Cb8_Cr8;
1254                        break;
1255                default:
1256                        BDBG_WRN(("unsupported.."));
1257                        settings.pxl_format = BPXL_eA8_R8_G8_B8;
1258                        break;
1259        }
1260
1261
1262        settings.width = width;
1263        settings.height = height;
1264
1265        rc = bgraphics_p_surface_create(&s_graphics, &settings, p_settop_surf);
1266
1267        if (rc == BERR_SUCCESS) {
1268                *mem = (*p_settop_surf)->p_pixels;
1269                *pitch = (*p_settop_surf)->pitch;
1270                BDBG_MSG(("%s: (%dx%d) 0x%x", __FUNCTION__, width, height, (*p_settop_surf)->p_pixels));
1271        }
1272        return (rc == BERR_SUCCESS) ? b_ok : berr_invalid_parameter;   
1273}
1274
1275/*
1276 * Description:
1277 * destroy BSUR_Surface.
1278 */
1279void bsurface_destroy_surface(bsettop_surf_t surf)
1280{
1281        BDBG_MSG(("%s: 0x%x", surf->p_pixels));
1282        bgraphics_p_surface_destroy(&s_graphics, surf);
1283}
1284
1285/*
1286 * Description:
1287 * flush cache before HW uses cached address.
1288 * Heap's callback function is flush_dcache
1289 */
1290static void bgraphics_cacheflush(bsettop_surf_t surf)
1291{
1292        BMEM_Heap_FlushCache(GetHEAP(), surf->p_pixels, surf->pitch*surf->height);
1293}
1294#endif
Note: See TracBrowser for help on using the repository browser.