dc_hw.c 10.1 KB
Newer Older
B
bernard.xiong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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>
15
#include <rtgui/dc_hw.h>
B
bernard.xiong 已提交
16
#include <rtgui/driver.h>
B
bernard.xiong 已提交
17
#include <rtgui/rtgui_system.h>
18
#include <rtgui/rtgui_app.h>
19 20 21
#include <rtgui/rtgui_server.h>

#include <rtgui/widgets/container.h>
B
bernard.xiong 已提交
22 23 24
#include <rtgui/widgets/window.h>
#include <rtgui/widgets/title.h>

25 26 27 28 29 30 31 32 33 34 35 36 37 38
#define _int_swap(x, y)         do {x ^= y; y ^= x; x ^= y;} while (0)

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);
B
bernard.xiong 已提交
39

40
const struct rtgui_dc_engine dc_hw_engine =
B
bernard.xiong 已提交
41
{
42 43 44 45 46 47 48
    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,
B
bernard.xiong 已提交
49

50 51
    rtgui_dc_hw_set_gc,
    rtgui_dc_hw_get_gc,
B
bernard.xiong 已提交
52

53 54
    rtgui_dc_hw_get_visible,
    rtgui_dc_hw_get_rect,
B
bernard.xiong 已提交
55

56
    rtgui_dc_hw_fini,
57
};
B
bernard.xiong 已提交
58 59 60 61

extern struct rt_mutex cursor_mutex;
extern void rtgui_mouse_show_cursor(void);
extern void rtgui_mouse_hide_cursor(void);
62
struct rtgui_dc *rtgui_dc_hw_create(rtgui_widget_t *owner)
B
bernard.xiong 已提交
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
    struct rtgui_dc_hw *dc;
    rtgui_widget_t *widget;

    /* adjudge owner */
    if (owner == RT_NULL || owner->toplevel == RT_NULL) return RT_NULL;
    if (!RTGUI_IS_WIN(owner->toplevel)) 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);
            return RT_NULL;
        }

        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));
    dc->parent.type = RTGUI_DC_HW;
    dc->parent.engine = &dc_hw_engine;
    dc->owner = owner;
    dc->hw_driver = rtgui_graphic_driver_get_default();

    if (RTGUI_IS_WINTITLE(owner->toplevel))
    {
        struct rtgui_win *top = RTGUI_WIN(owner->toplevel);
        top->drawing ++;

        if (top->drawing == 1)
        {
B
bernard.xiong 已提交
103
#ifdef RTGUI_USING_MOUSE_CURSOR
104 105 106 107
#ifdef _WIN32_NATIVE
            rt_mutex_take(&cursor_mutex, RT_WAITING_FOREVER);
            rt_kprintf("hide cursor\n");
            rtgui_mouse_hide_cursor();
B
bernard.xiong 已提交
108
#else
109 110
            /* hide cursor */
            rtgui_mouse_hide_cursor();
B
bernard.xiong 已提交
111 112
#endif
#endif
113 114 115 116 117 118 119 120 121 122 123
        }
    }
    else if (RTGUI_IS_APP(owner->toplevel) ||
             RTGUI_IS_WIN(owner->toplevel))
    {
        struct rtgui_win *top = RTGUI_WIN(owner->toplevel);
        top->drawing ++;

        if (top->drawing == 1)
        {
#ifdef _WIN32_NATIVE
B
bernard.xiong 已提交
124
#ifdef RTGUI_USING_MOUSE_CURSOR
125 126 127
            rt_mutex_take(&cursor_mutex, RT_WAITING_FOREVER);
            rt_kprintf("hide cursor\n");
            rtgui_mouse_hide_cursor();
B
bernard.xiong 已提交
128 129
#endif
#else
130 131 132 133
            /* send draw begin to server */
            struct rtgui_event_update_begin eupdate;
            RTGUI_EVENT_UPDATE_BEGIN_INIT(&(eupdate));
            eupdate.rect = RTGUI_WIDGET(top)->extent;
B
bernard.xiong 已提交
134

135
            rtgui_server_post_event((struct rtgui_event *)&eupdate, sizeof(eupdate));
B
bernard.xiong 已提交
136
#endif
137 138
        }
    }
B
bernard.xiong 已提交
139

140
    return &(dc->parent);
B
bernard.xiong 已提交
141 142
}

143
static rt_bool_t rtgui_dc_hw_fini(struct rtgui_dc *dc)
B
bernard.xiong 已提交
144
{
145 146
    rtgui_widget_t *owner;
    struct rtgui_dc_hw *self;
B
bernard.xiong@gmail.com 已提交
147

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

150 151 152
    self = (struct rtgui_dc_hw *)dc;
    /* get owner */
    owner = self->owner;
B
bernard.xiong 已提交
153

154 155 156 157
    if (RTGUI_IS_WINTITLE(owner->toplevel))
    {
        /* update title extent */
        struct rtgui_win *top = RTGUI_WIN(owner->toplevel);
B
bernard.xiong 已提交
158

159 160 161 162
        top->drawing --;
        if ((top->drawing == 0) && RTGUI_WIDGET_IS_DC_VISIBLE(owner))
        {
#ifdef _WIN32_NATIVE
B
bernard.xiong 已提交
163
#ifdef RTGUI_USING_MOUSE_CURSOR
164 165 166 167
            rt_mutex_release(&cursor_mutex);
            /* show cursor */
            rtgui_mouse_show_cursor();
            rt_kprintf("show cursor\n");
B
bernard.xiong 已提交
168
#endif
169 170
            /* update screen */
            rtgui_graphic_driver_screen_update(self->hw_driver, &(owner->extent));
B
bernard.xiong 已提交
171 172
#else
#ifdef RTGUI_USING_MOUSE_CURSOR
173 174
            /* show cursor */
            rtgui_mouse_show_cursor();
B
bernard.xiong 已提交
175 176
#endif

177 178
            /* update screen */
            rtgui_graphic_driver_screen_update(self->hw_driver, &(owner->extent));
B
bernard.xiong 已提交
179
#endif
180 181 182 183 184 185 186 187 188 189 190
        }
    }
    else if (RTGUI_IS_APP(owner->toplevel) ||
             RTGUI_IS_WIN(owner->toplevel))
    {
        struct rtgui_win *top = RTGUI_WIN(owner->toplevel);
        top->drawing --;

        if ((top->drawing == 0) && RTGUI_WIDGET_IS_DC_VISIBLE(owner))
        {
#ifdef _WIN32_NATIVE
B
bernard.xiong 已提交
191
#ifdef RTGUI_USING_MOUSE_CURSOR
192 193 194 195
            rt_mutex_release(&cursor_mutex);
            /* show cursor */
            rtgui_mouse_show_cursor();
            rt_kprintf("show cursor\n");
B
bernard.xiong 已提交
196
#endif
197 198
            /* update screen */
            rtgui_graphic_driver_screen_update(self->hw_driver, &(owner->extent));
B
bernard.xiong 已提交
199
#else
200 201 202 203
            /* send to server to end drawing */
            struct rtgui_event_update_end eupdate;
            RTGUI_EVENT_UPDATE_END_INIT(&(eupdate));
            eupdate.rect = owner->extent;
B
bernard.xiong 已提交
204

205
            rtgui_server_post_event((struct rtgui_event *)&eupdate, sizeof(eupdate));
B
bernard.xiong 已提交
206
#endif
207 208
        }
    }
B
bernard.xiong 已提交
209

210 211
    /* release hardware dc */
    rtgui_free(self);
B
bernard.xiong@gmail.com 已提交
212

213
    return RT_TRUE;
B
bernard.xiong 已提交
214 215 216 217 218
}

/*
 * draw a logic point on device
 */
219
static void rtgui_dc_hw_draw_point(struct rtgui_dc *self, int x, int y)
B
bernard.xiong 已提交
220
{
221
    struct rtgui_dc_hw *dc;
B
bernard.xiong 已提交
222

223 224
    RT_ASSERT(self != RT_NULL);
    dc = (struct rtgui_dc_hw *) self;
B
bernard.xiong 已提交
225

226 227 228 229 230
    x = x + dc->owner->extent.x1;
    y = y + dc->owner->extent.y1;

    /* draw this point */
    dc->hw_driver->ops->set_pixel(&(dc->owner->gc.foreground), x, y);
B
bernard.xiong 已提交
231 232
}

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

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

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

243 244
    /* draw this point */
    dc->hw_driver->ops->set_pixel(&color, x, y);
245 246
}

B
bernard.xiong 已提交
247 248 249
/*
 * draw a logic vertical line on device
 */
250
static void rtgui_dc_hw_draw_vline(struct rtgui_dc *self, int x, int y1, int y2)
B
bernard.xiong 已提交
251
{
252
    struct rtgui_dc_hw *dc;
B
bernard.xiong 已提交
253

254 255
    RT_ASSERT(self != RT_NULL);
    dc = (struct rtgui_dc_hw *) self;
B
bernard.xiong 已提交
256

257 258 259 260
    x = x + dc->owner->extent.x1;
    y1 = y1 + dc->owner->extent.y1;
    y2 = y2 + dc->owner->extent.y1;
    if (y1 > y2) _int_swap(y1, y2);
B
bernard.xiong 已提交
261

262 263
    /* draw vline */
    dc->hw_driver->ops->draw_vline(&(dc->owner->gc.foreground), x, y1, y2);
B
bernard.xiong 已提交
264 265 266 267 268
}

/*
 * draw a logic horizontal line on device
 */
269
static void rtgui_dc_hw_draw_hline(struct rtgui_dc *self, int x1, int x2, int y)
B
bernard.xiong 已提交
270
{
271
    struct rtgui_dc_hw *dc;
B
bernard.xiong 已提交
272

273 274
    RT_ASSERT(self != RT_NULL);
    dc = (struct rtgui_dc_hw *) self;
B
bernard.xiong 已提交
275

276 277 278 279 280
    /* convert logic to device */
    x1 = x1 + dc->owner->extent.x1;
    x2 = x2 + dc->owner->extent.x1;
    if (x1 > x2) _int_swap(x1, x2);
    y  = y + dc->owner->extent.y1;
B
bernard.xiong 已提交
281

282 283
    /* draw hline */
    dc->hw_driver->ops->draw_hline(&(dc->owner->gc.foreground), x1, x2, y);
B
bernard.xiong 已提交
284 285
}

286
static void rtgui_dc_hw_fill_rect(struct rtgui_dc *self, struct rtgui_rect *rect)
B
bernard.xiong 已提交
287
{
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
    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->ops->draw_hline(&color, x1, x2, index);
    }
B
bernard.xiong 已提交
306 307
}

308
static void rtgui_dc_hw_blit_line(struct rtgui_dc *self, int x1, int x2, int y, rt_uint8_t *line_data)
B
bernard.xiong 已提交
309
{
310
    struct rtgui_dc_hw *dc;
B
bernard.xiong 已提交
311

312 313
    RT_ASSERT(self != RT_NULL);
    dc = (struct rtgui_dc_hw *) self;
B
bernard.xiong 已提交
314

315 316 317 318 319
    /* convert logic to device */
    x1 = x1 + dc->owner->extent.x1;
    x2 = x2 + dc->owner->extent.x1;
    if (x1 > x2) _int_swap(x1, x2);
    y  = y + dc->owner->extent.y1;
B
bernard.xiong 已提交
320

321
    dc->hw_driver->ops->draw_raw_hline(line_data, x1, x2, y);
B
bernard.xiong 已提交
322 323
}

324
static void rtgui_dc_hw_blit(struct rtgui_dc *dc, struct rtgui_point *dc_point, struct rtgui_dc *dest, rtgui_rect_t *rect)
B
bernard.xiong 已提交
325
{
326 327
    /* not blit in hardware dc */
    return ;
B
bernard.xiong 已提交
328 329
}

330
static void rtgui_dc_hw_set_gc(struct rtgui_dc *self, rtgui_gc_t *gc)
B
bernard.xiong 已提交
331
{
332
    struct rtgui_dc_hw *dc;
B
bernard.xiong 已提交
333

334 335
    RT_ASSERT(self != RT_NULL);
    dc = (struct rtgui_dc_hw *) self;
B
bernard.xiong@gmail.com 已提交
336

337 338
    /* set gc */
    dc->owner->gc = *gc;
B
bernard.xiong 已提交
339 340
}

341
static rtgui_gc_t *rtgui_dc_hw_get_gc(struct rtgui_dc *self)
B
bernard.xiong 已提交
342
{
343
    struct rtgui_dc_hw *dc;
B
bernard.xiong 已提交
344

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

348
    return &(dc->owner->gc);
B
bernard.xiong 已提交
349 350
}

351
static rt_bool_t rtgui_dc_hw_get_visible(struct rtgui_dc *self)
B
bernard.xiong 已提交
352
{
353
    struct rtgui_dc_hw *dc;
354

355 356
    RT_ASSERT(self != RT_NULL);
    dc = (struct rtgui_dc_hw *) self;
B
bernard.xiong@gmail.com 已提交
357

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

360
    return RT_TRUE;
B
bernard.xiong 已提交
361 362
}

363
static void rtgui_dc_hw_get_rect(struct rtgui_dc *self, rtgui_rect_t *rect)
B
bernard.xiong 已提交
364
{
365
    struct rtgui_dc_hw *dc;
B
bernard.xiong@gmail.com 已提交
366

367 368
    RT_ASSERT(self != RT_NULL);
    dc = (struct rtgui_dc_hw *) self;
B
bernard.xiong 已提交
369

370 371
    /* get owner */
    rtgui_widget_get_rect(dc->owner, rect);
B
bernard.xiong 已提交
372
}