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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
|
/*
Copyright (C) 2009 Red Hat, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define GL_GLEXT_PROTOTYPES
#include "common.h"
#include "red_drawable.h"
#include "pixels_source_p.h"
#include "debug.h"
#include "x_platform.h"
#include "utils.h"
#include "gl_utils.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>
static inline void copy_to_gldrawable_from_gltexture(const RedDrawable_p* dest,
const SpiceRect& area,
const SpicePoint& offset,
const PixelsSource_p* source,
int src_x, int src_y)
{
float text_x1, text_x2;
float text_y1, text_y2;
int vertex_x1, vertex_x2;
int vertex_y1, vertex_y2;
GLXPbuffer pbuffer;
GLXContext context;
RenderType rendertype;
Window win;
text_x1 = (float)src_x / source->gl.width_powed;
text_x2 = text_x1 + (float)(area.right - area.left) / source->gl.width_powed;
text_y1 = ((float)source->gl.height - (area.bottom - area.top) - src_y) /
source->gl.height_powed;
text_y2 = text_y1 + (float)(area.bottom - area.top) / source->gl.height_powed;
vertex_x1 = area.left + offset.x;
vertex_y1 = dest->source.x_drawable.height - (area.top + offset.y) - (area.bottom - area.top);
vertex_x2 = vertex_x1 + (area.right - area.left);
vertex_y2 = vertex_y1 + (area.bottom - area.top);
glEnable(GL_TEXTURE_2D);
rendertype = source->gl.rendertype;
if (rendertype == RENDER_TYPE_FBO) {
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
} else {
win = source->gl.win;
context = source->gl.context;
glXMakeCurrent(XPlatform::get_display(), win, context);
}
glBindTexture(GL_TEXTURE_2D, source->gl.tex);
glBegin(GL_QUADS);
glTexCoord2f(text_x1, text_y1);
glVertex2i(vertex_x1, vertex_y1);
glTexCoord2f(text_x1, text_y2);
glVertex2i(vertex_x1, vertex_y2);
glTexCoord2f(text_x2, text_y2);
glVertex2i(vertex_x2, vertex_y2);
glTexCoord2f(text_x2, text_y1);
glVertex2i(vertex_x2, vertex_y1);
glEnd();
if (rendertype == RENDER_TYPE_FBO) {
GLuint fbo;
fbo = source->gl.fbo;
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
} else {
pbuffer = source->gl.pbuff;
glXMakeCurrent(XPlatform::get_display(), pbuffer, context);
}
}
static inline void copy_to_gldrawable_from_pixmap(const RedDrawable_p* dest,
const SpiceRect& area,
const SpicePoint& offset,
const PixelsSource_p* source,
int src_x, int src_y)
{
uint8_t *addr;
GLXContext context = NULL;
GLXPbuffer pbuffer;
RenderType rendertype;
Window win;
rendertype = dest->source.x_drawable.rendertype;
if (rendertype == RENDER_TYPE_FBO) {
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glBindTexture(GL_TEXTURE_2D, 0);
} else {
context = dest->source.x_drawable.context;
win = dest->source.x_drawable.drawable;
glXMakeCurrent(XPlatform::get_display(), win, context);
glDisable(GL_TEXTURE_2D);
}
glPixelStorei(GL_UNPACK_ROW_LENGTH, source->pixmap.x_image->bytes_per_line / 4);
glPixelZoom(1, -1);
addr = (uint8_t *)source->pixmap.x_image->data;
addr += (src_x * 4 + src_y * source->pixmap.x_image->bytes_per_line);
glWindowPos2i(area.left + offset.x, dest->source.x_drawable.height -
(area.top + offset.y)); //+ (area.bottom - area.top)));
glDrawPixels(area.right - area.left, area.bottom - area.top,
GL_BGRA, GL_UNSIGNED_BYTE, addr);
if (rendertype == RENDER_TYPE_FBO) {
GLuint fbo;
fbo = dest->source.x_drawable.fbo;
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
} else {
pbuffer = dest->source.x_drawable.pbuff;
glXMakeCurrent(XPlatform::get_display(), pbuffer, context);
}
}
static inline void copy_to_drawable_from_drawable(const RedDrawable_p* dest,
const SpiceRect& area,
const SpicePoint& offset,
const PixelsSource_p* source,
int src_x, int src_y)
{
XGCValues gc_vals;
gc_vals.function = GXcopy;
XChangeGC(XPlatform::get_display(), dest->source.x_drawable.gc, GCFunction, &gc_vals);
XCopyArea(XPlatform::get_display(), source->x_drawable.drawable,
dest->source.x_drawable.drawable, dest->source.x_drawable.gc,
src_x, src_y,
area.right - area.left, area.bottom - area.top,
area.left + offset.x, area.top + offset.y);
}
static inline void copy_to_drawable_from_pixmap(const RedDrawable_p* dest,
const SpiceRect& area,
const SpicePoint& offset,
const PixelsSource_p* source,
int src_x, int src_y)
{
XGCValues gc_vals;
gc_vals.function = GXcopy;
XChangeGC(XPlatform::get_display(), dest->source.x_drawable.gc, GCFunction, &gc_vals);
XPutImage(XPlatform::get_display(), dest->source.x_drawable.drawable,
dest->source.x_drawable.gc, source->pixmap.x_image, src_x,
src_y, area.left + offset.x, area.top + offset.y,
area.right - area.left, area.bottom - area.top);
}
static inline void copy_to_drawable_from_shmdrawable(const RedDrawable_p* dest,
const SpiceRect& area,
const SpicePoint& offset,
const PixelsSource_p* source,
int src_x, int src_y)
{
XGCValues gc_vals;
gc_vals.function = GXcopy;
XChangeGC(XPlatform::get_display(), dest->source.x_drawable.gc, GCFunction, &gc_vals);
XShmPutImage(XPlatform::get_display(), dest->source.x_drawable.drawable,
dest->source.x_drawable.gc, source->x_shm_drawable.x_image,
src_x, src_y, area.left + offset.x, area.top + offset.y,
area.right - area.left, area.bottom - area.top, false);
XSync(XPlatform::get_display(), 0);
}
static inline void copy_to_x_drawable(const RedDrawable_p* dest,
const SpiceRect& area,
const SpicePoint& offset,
const PixelsSource_p* source,
int src_x, int src_y)
{
switch (source->type) {
case PIXELS_SOURCE_TYPE_X_DRAWABLE:
copy_to_drawable_from_drawable(dest, area, offset, source, src_x, src_y);
break;
case PIXELS_SOURCE_TYPE_PIXMAP:
copy_to_drawable_from_pixmap(dest, area, offset, source, src_x, src_y);
break;
case PIXELS_SOURCE_TYPE_XSHM_DRAWABLE:
copy_to_drawable_from_shmdrawable(dest, area, offset, source, src_x, src_y);
break;
default:
THROW("invalid source type %d", source->type);
}
}
static inline void copy_to_gl_drawable(const RedDrawable_p* dest,
const SpiceRect& area,
const SpicePoint& offset,
const PixelsSource_p* source,
int src_x, int src_y)
{
switch (source->type) {
case PIXELS_SOURCE_TYPE_GL_TEXTURE:
copy_to_gldrawable_from_gltexture(dest, area, offset, source, src_x, src_y);
break;
case PIXELS_SOURCE_TYPE_PIXMAP:
copy_to_gldrawable_from_pixmap(dest, area, offset, source, src_x, src_y);
break;
default:
THROW("invalid source type %d", source->type);
}
}
static inline void copy_to_pixmap_from_drawable(const RedDrawable_p* dest,
const SpiceRect& area,
const SpicePoint& offset,
const PixelsSource_p* source,
int src_x, int src_y)
{
LOG_WARN("not implemented");
}
static inline void copy_to_pixmap_from_shmdrawable(const RedDrawable_p* dest,
const SpiceRect& area,
const SpicePoint& offset,
const PixelsSource_p* source,
int src_x, int src_y)
{
cairo_t* cairo = dest->cairo;
cairo_surface_t* surf = source->x_shm_drawable.cairo_surf;
ASSERT(cairo);
ASSERT(surf);
cairo_set_source_surface(cairo, surf, area.left + offset.x - src_x,
area.top + offset.y - src_y);
cairo_set_operator(cairo, CAIRO_OPERATOR_RASTER_COPY);
cairo_rectangle(cairo, area.left + offset.x, area.top + offset.y, area.right - area.left,
area.bottom - area.top);
cairo_fill(cairo);
}
static inline void copy_to_pixmap_from_pixmap(const RedDrawable_p* dest,
const SpiceRect& area,
const SpicePoint& offset,
const PixelsSource_p* source,
int src_x, int src_y)
{
cairo_t* cairo = dest->cairo;
cairo_surface_t* surf = source->pixmap.cairo_surf;
ASSERT(cairo);
ASSERT(surf);
cairo_set_source_surface(cairo, surf, area.left + offset.x - src_x,
area.top + offset.y - src_y);
cairo_set_operator(cairo, CAIRO_OPERATOR_RASTER_COPY);
cairo_rectangle(cairo, area.left + offset.x, area.top + offset.y, area.right - area.left,
area.bottom - area.top);
cairo_fill(cairo);
}
static inline void copy_to_pixmap_from_gltexture(const RedDrawable_p* dest,
const SpiceRect& area,
const SpicePoint& offset,
const PixelsSource_p* source,
int src_x, int src_y)
{
int y, height;
GLXContext context = NULL;
GLXPbuffer pbuffer;
Window win;
RenderType rendertype;
y = source->gl.height - src_y;
height = area.bottom - area.top;
rendertype = source->gl.rendertype;
if (rendertype == RENDER_TYPE_FBO) {
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, source->gl.fbo);
glBindTexture(GL_TEXTURE_2D, 0);
} else {
context = source->gl.context;
pbuffer = source->gl.pbuff;
glXMakeCurrent(XPlatform::get_display(), pbuffer, context);
glDisable(GL_TEXTURE_2D);
}
glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
glPixelStorei(GL_PACK_ROW_LENGTH,
dest->source.pixmap.x_image->bytes_per_line / 4);
while (height > 0) {
glReadPixels(src_x, y - height, area.right - area.left, 1,
GL_BGRA, GL_UNSIGNED_BYTE,
dest->source.pixmap.x_image->data +
(area.left + offset.x) * 4 +
(area.top + offset.y + height - 1) *
dest->source.pixmap.x_image->bytes_per_line);
height--;
}
if (rendertype != RENDER_TYPE_FBO) {
win = source->gl.win;
glXMakeCurrent(XPlatform::get_display(), win, context);
}
}
static inline void copy_to_pixmap(const RedDrawable_p* dest,
const SpiceRect& area,
const SpicePoint& offset,
const PixelsSource_p* source,
int src_x, int src_y)
{
switch (source->type) {
case PIXELS_SOURCE_TYPE_GL_TEXTURE:
copy_to_pixmap_from_gltexture(dest, area, offset, source, src_x, src_y);
break;
case PIXELS_SOURCE_TYPE_X_DRAWABLE:
copy_to_pixmap_from_drawable(dest, area, offset, source, src_x, src_y);
break;
case PIXELS_SOURCE_TYPE_PIXMAP:
copy_to_pixmap_from_pixmap(dest, area, offset, source, src_x, src_y);
break;
case PIXELS_SOURCE_TYPE_XSHM_DRAWABLE:
copy_to_pixmap_from_shmdrawable(dest, area, offset, source, src_x, src_y);
break;
default:
THROW("invalid source type %d", source->type);
}
}
void RedDrawable::copy_pixels(const PixelsSource& src, int src_x, int src_y, const SpiceRect& area)
{
PixelsSource_p* source = (PixelsSource_p*)src.get_opaque();
RedDrawable_p* dest = (RedDrawable_p*)get_opaque();
switch (dest->source.type) {
case PIXELS_SOURCE_TYPE_GL_DRAWABLE:
copy_to_gl_drawable(dest, area, _origin, source, src_x + src._origin.x,
src_y + src._origin.y);
break;
case PIXELS_SOURCE_TYPE_X_DRAWABLE:
copy_to_x_drawable(dest, area, _origin, source, src_x + src._origin.x,
src_y + src._origin.y);
break;
case PIXELS_SOURCE_TYPE_PIXMAP:
copy_to_pixmap(dest, area, _origin, source, src_x + src._origin.x, src_y + src._origin.y);
break;
default:
THROW("invalid dest type %d", dest->source.type);
}
}
static inline void blend_to_drawable(const RedDrawable_p* dest,
const SpiceRect& area,
const SpicePoint& offset,
const PixelsSource_p* source,
int src_x, int src_y)
{
LOG_WARN("not implemented");
}
static inline void blend_to_pixmap_from_drawable(const RedDrawable_p* dest,
const SpiceRect& area,
const SpicePoint& offset,
const PixelsSource_p* source,
int src_x, int src_y)
{
LOG_WARN("not implemented");
}
static inline void blend_to_pixmap_from_pixmap(const RedDrawable_p* dest,
const SpiceRect& area,
const SpicePoint& offset,
const PixelsSource_p* source,
int src_x, int src_y)
{
cairo_t* cairo = dest->cairo;
cairo_surface_t* surf = source->pixmap.cairo_surf;
ASSERT(cairo);
ASSERT(surf);
cairo_set_source_surface(cairo, surf, (area.left + offset.x - src_x),
(area.top + offset.y - src_y));
cairo_set_operator(cairo, CAIRO_OPERATOR_ATOP);
cairo_rectangle(cairo, area.left + offset.x, area.top + offset.y, area.right - area.left,
area.bottom - area.top);
cairo_fill(cairo);
}
static inline void blend_to_pixmap(const RedDrawable_p* dest,
const SpiceRect& area,
const SpicePoint& offset,
const PixelsSource_p* source,
int src_x, int src_y)
{
switch (source->type) {
case PIXELS_SOURCE_TYPE_X_DRAWABLE:
blend_to_pixmap_from_drawable(dest, area, offset, source, src_x, src_y);
break;
case PIXELS_SOURCE_TYPE_PIXMAP:
blend_to_pixmap_from_pixmap(dest, area, offset, source, src_x, src_y);
break;
default:
THROW("invalid source type %d", source->type);
}
}
void RedDrawable::blend_pixels(const PixelsSource& src, int src_x, int src_y, const SpiceRect& area)
{
PixelsSource_p* source = (PixelsSource_p*)src.get_opaque();
RedDrawable_p* dest = (RedDrawable_p*)get_opaque();
switch (dest->source.type) {
case PIXELS_SOURCE_TYPE_X_DRAWABLE:
blend_to_drawable(dest, area, _origin, source, src_x + src._origin.x,
src_y + src._origin.y);
break;
case PIXELS_SOURCE_TYPE_PIXMAP:
blend_to_pixmap(dest, area, _origin, source, src_x + src._origin.x, src_y + src._origin.y);
break;
default:
THROW("invalid dest type %d", dest->source.type);
}
}
static inline void combine_to_drawable(const RedDrawable_p* dest,
const SpiceRect& area,
const SpicePoint& offset,
const PixelsSource_p* source,
int src_x, int src_y,
RedDrawable::CombineOP op)
{
LOG_WARN("not implemented");
}
static inline void combine_to_pixmap_from_drawable(const RedDrawable_p* dest,
const SpiceRect& area,
const SpicePoint& offset,
const PixelsSource_p* source,
int src_x, int src_y,
RedDrawable::CombineOP op)
{
LOG_WARN("not implemented");
}
static inline void combine_to_pixmap_from_pixmap(const RedDrawable_p* dest,
const SpiceRect& area,
const SpicePoint& offset,
const PixelsSource_p* source,
int src_x, int src_y,
RedDrawable::CombineOP op)
{
cairo_t* cairo = dest->cairo;
cairo_surface_t* surf = source->pixmap.cairo_surf;
ASSERT(cairo);
ASSERT(surf);
cairo_operator_t cairo_op;
switch (op) {
case RedDrawable::OP_COPY:
cairo_op = CAIRO_OPERATOR_RASTER_COPY;
break;
case RedDrawable::OP_AND:
cairo_op = CAIRO_OPERATOR_RASTER_AND;
break;
case RedDrawable::OP_XOR:
cairo_op = CAIRO_OPERATOR_RASTER_XOR;
break;
default:
THROW("invalid op %d", op);
}
cairo_set_operator(cairo, cairo_op);
if (cairo_image_surface_get_format(surf) == CAIRO_FORMAT_A1) {
cairo_rectangle(cairo, area.left + offset.x, area.top + offset.y, area.right - area.left,
area.bottom - area.top);
cairo_clip(cairo);
cairo_set_source_rgb(cairo, 1, 1, 1);
cairo_mask_surface(cairo, surf, area.left + offset.x - src_x, area.top + offset.y - src_y);
cairo_reset_clip(cairo);
} else {
cairo_set_source_surface(cairo, surf, area.left + offset.x - src_x,
area.top + offset.y - src_y);
cairo_rectangle(cairo, area.left + offset.x, area.top + offset.y, area.right - area.left,
area.bottom - area.top);
cairo_fill(cairo);
}
}
static inline void combine_to_pixmap(const RedDrawable_p* dest,
const SpiceRect& area,
const SpicePoint& offset,
const PixelsSource_p* source,
int src_x, int src_y,
RedDrawable::CombineOP op)
{
switch (source->type) {
case PIXELS_SOURCE_TYPE_X_DRAWABLE:
combine_to_pixmap_from_drawable(dest, area, offset, source, src_x, src_y, op);
break;
case PIXELS_SOURCE_TYPE_PIXMAP:
combine_to_pixmap_from_pixmap(dest, area, offset, source, src_x, src_y, op);
break;
default:
THROW("invalid source type %d", source->type);
}
}
void RedDrawable::combine_pixels(const PixelsSource& src, int src_x, int src_y, const SpiceRect& area,
CombineOP op)
{
PixelsSource_p* source = (PixelsSource_p*)src.get_opaque();
RedDrawable_p* dest = (RedDrawable_p*)get_opaque();
switch (dest->source.type) {
case PIXELS_SOURCE_TYPE_X_DRAWABLE:
combine_to_drawable(dest, area, _origin, source, src_x + src._origin.x,
src_y + src._origin.y, op);
break;
case PIXELS_SOURCE_TYPE_PIXMAP:
combine_to_pixmap(dest, area, _origin, source, src_x + src._origin.x,
src_y + src._origin.y, op);
break;
default:
THROW("invalid dest type %d", dest->source.type);
}
}
void RedDrawable::erase_rect(const SpiceRect& area, rgb32_t color)
{
LOG_WARN("not implemented");
}
static inline void fill_drawable(RedDrawable_p* dest, const SpiceRect& area, rgb32_t color,
const SpicePoint& offset)
{
Drawable drawable = dest->source.x_drawable.drawable;
GC gc = dest->source.x_drawable.gc;
Colormap color_map = DefaultColormap(XPlatform::get_display(),
DefaultScreen(XPlatform::get_display()));
XColor x_color;
x_color.red = (uint16_t)rgb32_get_red(color) << 8;
x_color.green = (uint16_t)rgb32_get_green(color) << 8;
x_color.blue = (uint16_t)rgb32_get_blue(color) << 8;
x_color.flags = DoRed | DoGreen | DoBlue;
//todo: optimize color map
if (!XAllocColor(XPlatform::get_display(), color_map, &x_color)) {
LOG_WARN("color map failed");
}
XGCValues gc_vals;
gc_vals.foreground = x_color.pixel;
gc_vals.function = GXcopy;
gc_vals.fill_style = FillSolid;
XChangeGC(XPlatform::get_display(), gc, GCFunction | GCForeground | GCFillStyle, &gc_vals);
XFillRectangle(XPlatform::get_display(), drawable,
gc, area.left + offset.x, area.top + offset.y,
area.right - area.left, area.bottom - area.top);
}
static inline void fill_gl_drawable(RedDrawable_p* dest, const SpiceRect& area, rgb32_t color,
const SpicePoint& offset)
{
int vertex_x1, vertex_x2;
int vertex_y1, vertex_y2;
GLXContext context;
context = glXGetCurrentContext();
if (!context) {
return;
}
vertex_x1 = area.left + offset.x;
vertex_y1 = dest->source.x_drawable.height - (area.top + offset.y) - (area.bottom - area.top);
vertex_x2 = vertex_x1 + (area.right - area.left);
vertex_y2 = vertex_y1 + (area.bottom - area.top);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glColor3f(rgb32_get_red(color), rgb32_get_green(color),
rgb32_get_blue(color));
glBegin(GL_QUADS);
glVertex2i(vertex_x1, vertex_y1);
glVertex2i(vertex_x1, vertex_y2);
glVertex2i(vertex_x2, vertex_y2);
glVertex2i(vertex_x2, vertex_y1);
glEnd();
glFlush();
glColor3f(1, 1, 1);
}
static inline void fill_pixmap(RedDrawable_p* dest, const SpiceRect& area, rgb32_t color,
const SpicePoint& offset)
{
cairo_t* cairo = dest->cairo;
ASSERT(cairo);
cairo_set_source_rgb(cairo,
(double)rgb32_get_red(color) / 0xff,
(double)rgb32_get_green(color) / 0xff,
(double)rgb32_get_blue(color) / 0xff);
cairo_rectangle(cairo, area.left + offset.x, area.top + offset.y, area.right - area.left,
area.bottom - area.top);
cairo_set_operator(cairo, CAIRO_OPERATOR_RASTER_COPY);
cairo_fill(cairo);
}
void RedDrawable::fill_rect(const SpiceRect& area, rgb32_t color)
{
RedDrawable_p* dest = (RedDrawable_p*)get_opaque();
switch (dest->source.type) {
case PIXELS_SOURCE_TYPE_GL_DRAWABLE:
fill_gl_drawable(dest, area, color, _origin);
break;
case PIXELS_SOURCE_TYPE_X_DRAWABLE:
fill_drawable(dest, area, color, _origin);
break;
case PIXELS_SOURCE_TYPE_PIXMAP:
fill_pixmap(dest, area, color, _origin);
break;
default:
THROW("invalid dest type %d", dest->source.type);
}
}
static inline void frame_drawable(RedDrawable_p* dest, const SpiceRect& area, rgb32_t color,
const SpicePoint& offset)
{
Drawable drawable = dest->source.x_drawable.drawable;
GC gc = dest->source.x_drawable.gc;
Colormap color_map = DefaultColormap(XPlatform::get_display(),
DefaultScreen(XPlatform::get_display()));
XColor x_color;
x_color.red = (uint16_t)rgb32_get_red(color) << 8;
x_color.green = (uint16_t)rgb32_get_green(color) << 8;
x_color.blue = (uint16_t)rgb32_get_blue(color) << 8;
x_color.flags = DoRed | DoGreen | DoBlue;
//todo: optimize color map
if (!XAllocColor(XPlatform::get_display(), color_map, &x_color)) {
LOG_WARN("color map failed");
}
XGCValues gc_vals;
gc_vals.foreground = x_color.pixel;
gc_vals.function = GXcopy;
gc_vals.fill_style = FillSolid;
XChangeGC(XPlatform::get_display(), gc, GCFunction | GCForeground | GCFillStyle, &gc_vals);
XFillRectangle(XPlatform::get_display(), drawable,
gc, area.left + offset.x, area.top + offset.y,
area.right - area.left, area.bottom - area.top);
}
static inline void frame_pixmap(RedDrawable_p* dest, const SpiceRect& area, rgb32_t color,
const SpicePoint& offset)
{
cairo_t* cairo = dest->cairo;
ASSERT(cairo);
cairo_set_source_rgb(cairo,
(double)rgb32_get_red(color) / 0xff,
(double)rgb32_get_green(color) / 0xff,
(double)rgb32_get_blue(color) / 0xff);
cairo_rectangle(cairo, area.left + offset.x, area.top + offset.y, area.right - area.left,
area.bottom - area.top);
cairo_set_line_width(cairo, 1);
cairo_set_operator(cairo, CAIRO_OPERATOR_RASTER_COPY);
cairo_stroke(cairo);
}
void RedDrawable::frame_rect(const SpiceRect& area, rgb32_t color)
{
RedDrawable_p* dest = (RedDrawable_p*)get_opaque();
switch (dest->source.type) {
case PIXELS_SOURCE_TYPE_X_DRAWABLE:
frame_drawable(dest, area, color, _origin);
break;
case PIXELS_SOURCE_TYPE_PIXMAP:
frame_pixmap(dest, area, color, _origin);
break;
default:
THROW("invalid dest type %d", dest->source.type);
}
}
|