dc_hw.c 6.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
/*
 * File      : dc_hw.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/dc.h>
#include <rtgui/dc_hw.h>
#include <rtgui/driver.h>
#include <rtgui/rtgui_system.h>
#include <rtgui/widgets/view.h>
#include <rtgui/widgets/window.h>

static void rtgui_dc_hw_draw_point(struct rtgui_dc* dc, int x, int y);
static void rtgui_dc_hw_draw_color_point(struct rtgui_dc* dc, int x, int y, rtgui_color_t color);
static void rtgui_dc_hw_draw_hline(struct rtgui_dc* dc, int x1, int x2, int y);
static void rtgui_dc_hw_draw_vline(struct rtgui_dc* dc, int x, int y1, int y2);
static void rtgui_dc_hw_fill_rect (struct rtgui_dc* dc, rtgui_rect_t* rect);
static void rtgui_dc_hw_blit_line (struct rtgui_dc* self, int x1, int x2, int y, rt_uint8_t* line_data);
static void rtgui_dc_hw_blit	  (struct rtgui_dc* dc, struct rtgui_point* dc_point, struct rtgui_dc* dest, rtgui_rect_t* rect);
static void rtgui_dc_hw_set_gc (struct rtgui_dc* dc, rtgui_gc_t *gc);
static rtgui_gc_t *rtgui_dc_hw_get_gc (struct rtgui_dc* dc);
static rt_bool_t rtgui_dc_hw_fini(struct rtgui_dc* dc);
static rt_bool_t rtgui_dc_hw_get_visible(struct rtgui_dc* dc);
static void rtgui_dc_hw_get_rect(struct rtgui_dc* dc, rtgui_rect_t* rect);

struct rtgui_dc_hw
{
	struct rtgui_dc parent;
	rtgui_widget_t *owner;
	const struct rtgui_graphic_driver* hw_driver;
};

const struct rtgui_dc_engine dc_hw_engine = 
{
	rtgui_dc_hw_draw_point,
	rtgui_dc_hw_draw_color_point,
	rtgui_dc_hw_draw_vline,
	rtgui_dc_hw_draw_hline,
	rtgui_dc_hw_fill_rect,
	rtgui_dc_hw_blit_line,
	rtgui_dc_hw_blit,

	rtgui_dc_hw_set_gc,
	rtgui_dc_hw_get_gc,

	rtgui_dc_hw_get_visible,
	rtgui_dc_hw_get_rect,

	rtgui_dc_hw_fini,
};

extern struct rt_mutex cursor_mutex;
extern void rtgui_mouse_show_cursor(void);
extern void rtgui_mouse_hide_cursor(void);
struct rtgui_dc* rtgui_dc_hw_create(rtgui_widget_t* owner)
{
	struct rtgui_dc_hw* dc;
	rtgui_widget_t* widget;

	/* adjudge owner */
	if (owner == RT_NULL) return RT_NULL;

	/* set init visible as true */
	RTGUI_WIDGET_DC_SET_VISIBLE(owner);
	/* check widget visible */
	widget = owner;
	while (widget != RT_NULL)
	{
		if (RTGUI_WIDGET_IS_HIDE(widget))
		{
			RTGUI_WIDGET_DC_SET_UNVISIBLE(owner);
			break;
		}

		widget = widget->parent;
	}

	if (!RTGUI_WIDGET_IS_DC_VISIBLE(owner)) return RT_NULL;

	/* create DC */
	dc = (struct rtgui_dc_hw*) rtgui_malloc(sizeof(struct rtgui_dc_hw));
	if(dc == RT_NULL)return RT_NULL;

	dc->parent.type = RTGUI_DC_HW;
	dc->parent.engine = &dc_hw_engine;
	dc->owner = owner;
	dc->hw_driver = rtgui_graphic_driver_get_default();

	return &(dc->parent);
}

static rt_bool_t rtgui_dc_hw_fini(struct rtgui_dc* dc)
{
	struct rtgui_dc_hw* self;

	if (dc == RT_NULL || dc->type != RTGUI_DC_HW) return RT_FALSE;

	self = (struct rtgui_dc_hw*)dc;

	/* release hardware dc */
	rtgui_free(self);

	return RT_TRUE;
}

/*
 * draw a logic point on device
 */
static void rtgui_dc_hw_draw_point(struct rtgui_dc* self, int x, int y)
{
	struct rtgui_dc_hw* dc;

	RT_ASSERT(self != RT_NULL);
	dc = (struct rtgui_dc_hw*) self;

	x = x + dc->owner->extent.x1;
	y = y + dc->owner->extent.y1;

	/* draw this point */
	dc->hw_driver->set_pixel(&(dc->owner->gc.foreground), x, y);
}

static void rtgui_dc_hw_draw_color_point(struct rtgui_dc* self, int x, int y, rtgui_color_t color)
{
	struct rtgui_dc_hw* dc;

	RT_ASSERT(self != RT_NULL);
	dc = (struct rtgui_dc_hw*) self;

	x = x + dc->owner->extent.x1;
	y = y + dc->owner->extent.y1;

	/* draw this point */
	dc->hw_driver->set_pixel(&color, x, y);
}

/*
 * draw a logic vertical line on device
 */
static void rtgui_dc_hw_draw_vline(struct rtgui_dc* self, int x, int y1, int y2)
{
	struct rtgui_dc_hw* dc;

	RT_ASSERT(self != RT_NULL);
	dc = (struct rtgui_dc_hw*) self;

	x = x + dc->owner->extent.x1;
	y1 = y1 + dc->owner->extent.y1;
	y2 = y2 + dc->owner->extent.y1;

	/* draw vline */
	dc->hw_driver->draw_vline(&(dc->owner->gc.foreground), x, y1, y2);
}

/*
 * draw a logic horizontal line on device
 */
static void rtgui_dc_hw_draw_hline(struct rtgui_dc* self, int x1, int x2, int y)
{
	struct rtgui_dc_hw* dc;

	RT_ASSERT(self != RT_NULL);
	dc = (struct rtgui_dc_hw*) self;

	/* convert logic to device */
	x1 = x1 + dc->owner->extent.x1;
	x2 = x2 + dc->owner->extent.x1;
	y  = y + dc->owner->extent.y1;

	/* draw hline */
	dc->hw_driver->draw_hline(&(dc->owner->gc.foreground), x1, x2, y);
}

static void rtgui_dc_hw_fill_rect (struct rtgui_dc* self, struct rtgui_rect* rect)
{
	rtgui_color_t color;
	register rt_base_t index, x1, x2;
	struct rtgui_dc_hw* dc;

	RT_ASSERT(self != RT_NULL);
	dc = (struct rtgui_dc_hw*) self;

	/* get background color */
	color = dc->owner->gc.background;
	/* convert logic to device */
	x1 = rect->x1 + dc->owner->extent.x1;
	x2 = rect->x2 + dc->owner->extent.x1;

	/* fill rect */
	for (index = dc->owner->extent.y1 + rect->y1; index < dc->owner->extent.y1 + rect->y2; index ++)
	{
		dc->hw_driver->draw_hline(&color, x1, x2, index);
	}
}

static void rtgui_dc_hw_blit_line (struct rtgui_dc* self, int x1, int x2, int y, rt_uint8_t* line_data)
{
	struct rtgui_dc_hw* dc;

	RT_ASSERT(self != RT_NULL);
	dc = (struct rtgui_dc_hw*) self;

	/* convert logic to device */
	x1 = x1 + dc->owner->extent.x1;
	x2 = x2 + dc->owner->extent.x1;
	y  = y + dc->owner->extent.y1;

	dc->hw_driver->draw_raw_hline(line_data, x1, x2, y);
}

static void rtgui_dc_hw_blit(struct rtgui_dc* dc, struct rtgui_point* dc_point, struct rtgui_dc* dest, rtgui_rect_t* rect)
{
	/* not blit in hardware dc */
	return ;
}

static void rtgui_dc_hw_set_gc(struct rtgui_dc* self, rtgui_gc_t *gc)
{
	struct rtgui_dc_hw* dc;

	RT_ASSERT(self != RT_NULL);
	dc = (struct rtgui_dc_hw*) self;

	/* set gc */
	dc->owner->gc = *gc;
}

static rtgui_gc_t* rtgui_dc_hw_get_gc(struct rtgui_dc* self)
{
	struct rtgui_dc_hw* dc;

	RT_ASSERT(self != RT_NULL);
	dc = (struct rtgui_dc_hw*) self;

	return &(dc->owner->gc);
}

static rt_bool_t rtgui_dc_hw_get_visible(struct rtgui_dc* self)
{
	struct rtgui_dc_hw* dc;

	RT_ASSERT(self != RT_NULL);
	dc = (struct rtgui_dc_hw*) self;

	if (!RTGUI_WIDGET_IS_DC_VISIBLE(dc->owner)) return RT_FALSE;

	return RT_TRUE;
}

static void rtgui_dc_hw_get_rect(struct rtgui_dc* self, rtgui_rect_t* rect)
{
	struct rtgui_dc_hw* dc;

	RT_ASSERT(self != RT_NULL);
	dc = (struct rtgui_dc_hw*) self;

	/* get owner */
	rtgui_widget_get_rect(dc->owner, rect);
}