2009-10-16 08:03:50 +08:00
|
|
|
/*
|
|
|
|
* File : dc_buffer.c
|
|
|
|
* This file is part of RT-Thread RTOS
|
|
|
|
* COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* The license and distribution terms for this file may be
|
|
|
|
* found in the file LICENSE in this distribution or at
|
|
|
|
* http://www.rt-thread.org/license/LICENSE
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2009-10-16 Bernard first version
|
|
|
|
*/
|
|
|
|
#include <rtgui/rtgui.h>
|
|
|
|
#include <rtgui/dc.h>
|
2010-04-10 18:06:35 +08:00
|
|
|
#include <rtgui/dc_hw.h>
|
2009-10-16 08:03:50 +08:00
|
|
|
#include <rtgui/color.h>
|
2009-11-24 07:34:58 +08:00
|
|
|
#include <rtgui/rtgui_system.h>
|
2009-10-16 08:03:50 +08:00
|
|
|
|
|
|
|
struct rtgui_dc_buffer
|
|
|
|
{
|
|
|
|
struct rtgui_dc parent;
|
|
|
|
|
2010-04-12 07:57:21 +08:00
|
|
|
/* graphic context */
|
|
|
|
rtgui_gc_t gc;
|
|
|
|
|
2009-10-16 08:03:50 +08:00
|
|
|
/* width and height */
|
|
|
|
rt_uint16_t width, height;
|
|
|
|
rt_uint16_t pitch;
|
|
|
|
|
|
|
|
/* blit info */
|
|
|
|
rtgui_region_t clip;
|
|
|
|
|
|
|
|
/* pixel data */
|
|
|
|
rt_uint8_t* pixel;
|
|
|
|
};
|
|
|
|
|
|
|
|
static rt_bool_t rtgui_dc_buffer_fini(struct rtgui_dc* dc);
|
|
|
|
static void rtgui_dc_buffer_draw_point(struct rtgui_dc* dc, int x, int y);
|
2010-04-12 07:57:21 +08:00
|
|
|
static void rtgui_dc_buffer_draw_color_point(struct rtgui_dc* dc, int x, int y, rtgui_color_t color);
|
2009-10-16 08:03:50 +08:00
|
|
|
static void rtgui_dc_buffer_draw_vline(struct rtgui_dc* dc, int x, int y1, int y2);
|
|
|
|
static void rtgui_dc_buffer_draw_hline(struct rtgui_dc* dc, int x1, int x2, int y);
|
|
|
|
static void rtgui_dc_buffer_fill_rect (struct rtgui_dc* dc, struct rtgui_rect* rect);
|
|
|
|
static void rtgui_dc_buffer_blit(struct rtgui_dc* self, struct rtgui_point* dc_point,
|
|
|
|
struct rtgui_dc* dest, rtgui_rect_t* rect);
|
2010-04-12 07:57:21 +08:00
|
|
|
|
|
|
|
static void rtgui_dc_buffer_set_gc (struct rtgui_dc* dc, rtgui_gc_t *gc);
|
|
|
|
static rtgui_gc_t* rtgui_dc_buffer_get_gc(struct rtgui_dc* dc);
|
|
|
|
|
2009-10-16 08:03:50 +08:00
|
|
|
static rt_bool_t rtgui_dc_buffer_get_visible(struct rtgui_dc* dc);
|
|
|
|
static void rtgui_dc_buffer_get_rect(struct rtgui_dc* dc, rtgui_rect_t* rect);
|
|
|
|
|
2010-06-01 08:07:59 +08:00
|
|
|
const static struct rtgui_dc_engine dc_buffer_engine =
|
2009-10-16 08:03:50 +08:00
|
|
|
{
|
2010-06-01 08:07:59 +08:00
|
|
|
rtgui_dc_buffer_draw_point,
|
|
|
|
rtgui_dc_buffer_draw_color_point,
|
|
|
|
rtgui_dc_buffer_draw_vline,
|
|
|
|
rtgui_dc_buffer_draw_hline,
|
|
|
|
rtgui_dc_buffer_fill_rect,
|
|
|
|
rtgui_dc_buffer_blit,
|
2009-10-16 08:03:50 +08:00
|
|
|
|
2010-06-01 08:07:59 +08:00
|
|
|
rtgui_dc_buffer_set_gc,
|
|
|
|
rtgui_dc_buffer_get_gc,
|
2009-10-16 08:03:50 +08:00
|
|
|
|
2010-06-01 08:07:59 +08:00
|
|
|
rtgui_dc_buffer_get_visible,
|
|
|
|
rtgui_dc_buffer_get_rect,
|
2009-10-16 08:03:50 +08:00
|
|
|
|
2010-06-01 08:07:59 +08:00
|
|
|
rtgui_dc_buffer_fini,
|
|
|
|
};
|
2009-10-16 08:03:50 +08:00
|
|
|
|
|
|
|
struct rtgui_dc* rtgui_dc_buffer_create(int w, int h)
|
|
|
|
{
|
|
|
|
struct rtgui_dc_buffer* dc;
|
|
|
|
|
|
|
|
dc = (struct rtgui_dc_buffer*)rtgui_malloc(sizeof(struct rtgui_dc_buffer));
|
2010-06-01 08:07:59 +08:00
|
|
|
dc->parent.type = RTGUI_DC_BUFFER;
|
|
|
|
dc->parent.engine = &dc_buffer_engine;
|
2010-04-28 18:08:14 +08:00
|
|
|
dc->gc.foreground = default_foreground;
|
|
|
|
dc->gc.background = default_background;
|
|
|
|
dc->gc.font = rtgui_font_default();
|
|
|
|
dc->gc.textalign = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_TOP;
|
2009-10-16 08:03:50 +08:00
|
|
|
|
|
|
|
dc->width = w;
|
|
|
|
dc->height = h;
|
|
|
|
dc->pitch = w * sizeof(rtgui_color_t);
|
|
|
|
|
|
|
|
rtgui_region_init(&(dc->clip));
|
|
|
|
|
|
|
|
dc->pixel = rtgui_malloc(h * dc->pitch);
|
|
|
|
rt_memset(dc->pixel, 0, h * dc->pitch);
|
|
|
|
|
|
|
|
return &(dc->parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_uint8_t* rtgui_dc_buffer_get_pixel(struct rtgui_dc* dc)
|
|
|
|
{
|
|
|
|
struct rtgui_dc_buffer* dc_buffer;
|
|
|
|
|
|
|
|
dc_buffer = (struct rtgui_dc_buffer*)dc;
|
|
|
|
|
|
|
|
return dc_buffer->pixel;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_bool_t rtgui_dc_buffer_fini(struct rtgui_dc* dc)
|
|
|
|
{
|
|
|
|
struct rtgui_dc_buffer* buffer = (struct rtgui_dc_buffer*)dc;
|
|
|
|
|
|
|
|
if (dc->type != RTGUI_DC_BUFFER) return RT_FALSE;
|
|
|
|
|
|
|
|
rtgui_free(buffer->pixel);
|
|
|
|
buffer->pixel = RT_NULL;
|
|
|
|
|
|
|
|
return RT_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rtgui_dc_buffer_draw_point(struct rtgui_dc* self, int x, int y)
|
|
|
|
{
|
|
|
|
rtgui_color_t* ptr;
|
|
|
|
struct rtgui_dc_buffer* dc;
|
|
|
|
|
|
|
|
dc = (struct rtgui_dc_buffer*)self;
|
|
|
|
|
|
|
|
/* note: there is no parameter check in this function */
|
|
|
|
ptr = (rtgui_color_t*)(dc->pixel + y * dc->pitch + x * sizeof(rtgui_color_t));
|
|
|
|
|
2010-04-28 18:08:14 +08:00
|
|
|
*ptr = dc->gc.foreground;
|
2009-10-16 08:03:50 +08:00
|
|
|
}
|
|
|
|
|
2010-04-12 07:57:21 +08:00
|
|
|
static void rtgui_dc_buffer_draw_color_point(struct rtgui_dc* self, int x, int y, rtgui_color_t color)
|
|
|
|
{
|
|
|
|
rtgui_color_t* ptr;
|
|
|
|
struct rtgui_dc_buffer* dc;
|
|
|
|
|
|
|
|
dc = (struct rtgui_dc_buffer*)self;
|
|
|
|
|
|
|
|
/* note: there is no parameter check in this function */
|
|
|
|
ptr = (rtgui_color_t*)(dc->pixel + y * dc->pitch + x * sizeof(rtgui_color_t));
|
|
|
|
|
|
|
|
*ptr = color;
|
|
|
|
}
|
|
|
|
|
2009-10-16 08:03:50 +08:00
|
|
|
static void rtgui_dc_buffer_draw_vline(struct rtgui_dc* self, int x, int y1, int y2)
|
|
|
|
{
|
|
|
|
rtgui_color_t* ptr;
|
|
|
|
register rt_base_t index;
|
|
|
|
struct rtgui_dc_buffer* dc;
|
|
|
|
|
|
|
|
dc = (struct rtgui_dc_buffer*)self;
|
|
|
|
|
|
|
|
if (x >= dc->width) return;
|
|
|
|
if (y1 > dc->height) y1 = dc->height;
|
|
|
|
if (y2 > dc->height) y2 = dc->height;
|
|
|
|
|
|
|
|
ptr = (rtgui_color_t*)(dc->pixel + y1 * dc->pitch + x * sizeof(rtgui_color_t));
|
|
|
|
for (index = y1; index < y2; index ++)
|
|
|
|
{
|
|
|
|
/* draw this point */
|
2010-04-28 18:08:14 +08:00
|
|
|
*ptr = dc->gc.foreground;
|
2009-10-16 08:03:50 +08:00
|
|
|
ptr += dc->width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rtgui_dc_buffer_draw_hline(struct rtgui_dc* self, int x1, int x2, int y)
|
|
|
|
{
|
|
|
|
rtgui_color_t* ptr;
|
|
|
|
register rt_base_t index;
|
|
|
|
struct rtgui_dc_buffer* dc;
|
|
|
|
|
|
|
|
dc = (struct rtgui_dc_buffer*)self;
|
|
|
|
if (y >= dc->height) return;
|
|
|
|
if (x1 > dc->width) x1 = dc->width;
|
|
|
|
if (x2 > dc->width) x2 = dc->width;
|
|
|
|
|
|
|
|
ptr = (rtgui_color_t*)(dc->pixel + y * dc->pitch + x1 * sizeof(rtgui_color_t));
|
|
|
|
for (index = x1; index < x2; index ++)
|
|
|
|
{
|
|
|
|
/* draw this point */
|
2010-04-28 18:08:14 +08:00
|
|
|
*ptr++ = dc->gc.foreground;
|
2009-10-16 08:03:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rtgui_dc_buffer_fill_rect (struct rtgui_dc* self, struct rtgui_rect* rect)
|
|
|
|
{
|
|
|
|
rtgui_rect_t r;
|
|
|
|
struct rtgui_dc_buffer* dc;
|
|
|
|
|
|
|
|
r = *rect;
|
|
|
|
dc = (struct rtgui_dc_buffer*)self;
|
|
|
|
if (r.x1 > dc->width) r.x1 = dc->width;
|
|
|
|
if (r.x2 > dc->width) r.x2 = dc->width;
|
|
|
|
if (r.y1 > dc->height) r.y1 = dc->height;
|
|
|
|
if (r.y2 > dc->height) r.y2 = dc->height;
|
|
|
|
|
|
|
|
/* fill first line */
|
|
|
|
rtgui_dc_buffer_draw_hline(&(dc->parent), r.x1, r.x2, r.y1);
|
|
|
|
|
|
|
|
/* memory copy other lines */
|
|
|
|
if (r.y2 > r.y1)
|
|
|
|
{
|
|
|
|
register rt_base_t index;
|
|
|
|
for (index = r.y1 + 1; index < r.y2; index ++)
|
|
|
|
{
|
|
|
|
rt_memcpy(dc->pixel + index * dc->pitch,
|
|
|
|
dc->pixel + r.y1 * dc->pitch,
|
|
|
|
(r.x2 - r.x1) * sizeof(rtgui_color_t));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rtgui_color to RGB323 */
|
|
|
|
rt_inline void rtgui_blit_line_1(rtgui_color_t* color, rt_uint8_t* dest, int line)
|
|
|
|
{
|
|
|
|
struct _color {rt_uint8_t r, g, b, a;} *c;
|
|
|
|
|
|
|
|
c = (struct _color*)color;
|
|
|
|
while (line-- > 0)
|
|
|
|
{
|
|
|
|
*dest = (c->r & 0xe0) | (c->g & 0xc0) >> 3 | (c->b & 0xe0) >> 5 ;
|
|
|
|
|
|
|
|
c ++;
|
|
|
|
dest ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rtgui_color to RGB565 */
|
|
|
|
rt_inline void rtgui_blit_line_2(rtgui_color_t* color, rt_uint8_t* dest, int line)
|
|
|
|
{
|
|
|
|
struct _color {rt_uint8_t r, g, b, a;} *c;
|
|
|
|
rt_uint16_t* ptr;
|
|
|
|
|
|
|
|
c = (struct _color*)color;
|
|
|
|
ptr = (rt_uint16_t*)dest;
|
|
|
|
|
|
|
|
while (line-- > 0)
|
|
|
|
{
|
|
|
|
*ptr = ((c->r & 0xf8) << 8) | ((c->g & 0xfc) << 3) | (c->b >> 3);
|
|
|
|
|
|
|
|
c ++;
|
|
|
|
ptr ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rtgui_color to RGB888 */
|
|
|
|
rt_inline void rtgui_blit_line_4(rtgui_color_t* color, rt_uint8_t* dest, int line)
|
|
|
|
{
|
|
|
|
rt_memcpy(dest, color, line * 4);
|
|
|
|
}
|
|
|
|
|
2010-04-06 07:05:13 +08:00
|
|
|
/* blit a dc to a hardware dc */
|
2009-10-16 08:03:50 +08:00
|
|
|
static void rtgui_dc_buffer_blit(struct rtgui_dc* self, struct rtgui_point* dc_point, struct rtgui_dc* dest, rtgui_rect_t* rect)
|
|
|
|
{
|
|
|
|
struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
|
2010-07-05 07:58:35 +08:00
|
|
|
struct rtgui_dc* hw = dest;
|
2009-10-16 08:03:50 +08:00
|
|
|
|
2010-04-06 07:05:13 +08:00
|
|
|
if (dc_point == RT_NULL) dc_point = &rtgui_empty_point;
|
|
|
|
|
2010-08-09 18:11:45 +08:00
|
|
|
if ((dest->type == RTGUI_DC_HW) || (dest->type == RTGUI_DC_CLIENT))
|
2009-10-16 08:03:50 +08:00
|
|
|
{
|
2010-05-08 23:50:45 +08:00
|
|
|
rtgui_color_t* pixel;
|
2010-04-10 18:06:35 +08:00
|
|
|
rt_uint8_t *line_ptr;
|
|
|
|
rt_uint16_t rect_width, rect_height, index;
|
2009-10-16 08:03:50 +08:00
|
|
|
void (*blit_line)(rtgui_color_t* color, rt_uint8_t* dest, int line);
|
|
|
|
|
2010-04-10 18:06:35 +08:00
|
|
|
/* calculate correct width and height */
|
|
|
|
if (rtgui_rect_width(*rect) > (dc->width - dc_point->x))
|
|
|
|
rect_width = dc->width - dc_point->x;
|
|
|
|
else
|
|
|
|
rect_width = rtgui_rect_width(*rect);
|
2009-10-16 08:03:50 +08:00
|
|
|
|
2010-04-10 18:06:35 +08:00
|
|
|
if (rtgui_rect_height(*rect) > (dc->height - dc_point->y))
|
|
|
|
rect_height = dc->height - dc_point->y;
|
|
|
|
else
|
|
|
|
rect_height = rtgui_rect_height(*rect);
|
2009-10-16 08:03:50 +08:00
|
|
|
|
2010-04-10 18:06:35 +08:00
|
|
|
/* get blit line function */
|
2010-07-05 07:58:35 +08:00
|
|
|
switch (rtgui_graphic_driver_get_default()->byte_per_pixel)
|
2009-10-16 08:03:50 +08:00
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
blit_line = rtgui_blit_line_1;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
blit_line = rtgui_blit_line_2;
|
|
|
|
break;
|
2010-04-10 18:06:35 +08:00
|
|
|
|
|
|
|
case 4:
|
2009-10-16 08:03:50 +08:00
|
|
|
blit_line = rtgui_blit_line_4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2010-04-10 18:06:35 +08:00
|
|
|
/* not support byte per pixel */
|
2009-10-16 08:03:50 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-10 18:06:35 +08:00
|
|
|
/* create line buffer */
|
2010-07-05 07:58:35 +08:00
|
|
|
line_ptr = (rt_uint8_t*) rtgui_malloc(rect_width * rtgui_graphic_driver_get_default()->byte_per_pixel);
|
2009-10-16 08:03:50 +08:00
|
|
|
|
2010-04-10 18:06:35 +08:00
|
|
|
/* prepare pixel line */
|
2010-05-08 23:50:45 +08:00
|
|
|
pixel = (rtgui_color_t*)(dc->pixel + dc_point->y * dc->pitch + dc_point->x * sizeof(rtgui_color_t));
|
2009-10-16 08:03:50 +08:00
|
|
|
|
2010-04-10 18:06:35 +08:00
|
|
|
/* draw each line */
|
|
|
|
for (index = rect->y1; index < rect->y1 + rect_height; index ++)
|
|
|
|
{
|
|
|
|
/* blit on line buffer */
|
2010-05-08 23:50:45 +08:00
|
|
|
blit_line(pixel, line_ptr, rect_width);
|
|
|
|
pixel += dc->width;
|
|
|
|
|
|
|
|
/* draw on hardware dc */
|
2010-08-09 18:11:45 +08:00
|
|
|
rtgui_dc_client_draw_raw_hline(hw, line_ptr, rect->x1, rect->x1 + rect_width, index);
|
2009-10-16 08:03:50 +08:00
|
|
|
}
|
2010-04-10 18:06:35 +08:00
|
|
|
|
|
|
|
/* release line buffer */
|
|
|
|
rtgui_free(line_ptr);
|
2009-10-16 08:03:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-12 07:57:21 +08:00
|
|
|
static void rtgui_dc_buffer_set_gc(struct rtgui_dc* self, rtgui_gc_t *gc)
|
2009-10-16 08:03:50 +08:00
|
|
|
{
|
|
|
|
struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
|
|
|
|
|
2010-04-12 07:57:21 +08:00
|
|
|
dc->gc = *gc;
|
2009-10-16 08:03:50 +08:00
|
|
|
}
|
|
|
|
|
2010-04-12 07:57:21 +08:00
|
|
|
static rtgui_gc_t *rtgui_dc_buffer_get_gc(struct rtgui_dc* self)
|
2009-10-16 08:03:50 +08:00
|
|
|
{
|
|
|
|
struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
|
|
|
|
|
2010-04-12 07:57:21 +08:00
|
|
|
return &dc->gc;
|
2009-10-16 08:03:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static rt_bool_t rtgui_dc_buffer_get_visible(struct rtgui_dc* dc)
|
|
|
|
{
|
|
|
|
return RT_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rtgui_dc_buffer_get_rect(struct rtgui_dc* self, rtgui_rect_t* rect)
|
|
|
|
{
|
|
|
|
struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
|
|
|
|
|
|
|
|
rect->x1 = rect->y1 = 0;
|
|
|
|
|
|
|
|
rect->x2 = dc->width;
|
|
|
|
rect->y2 = dc->height;
|
|
|
|
}
|
2010-01-04 08:05:01 +08:00
|
|
|
|