blob: c7e3778e22f2828b0dd090b4a24be057059b4985 [file] [log] [blame]
Pierre Ossman403ac272017-01-02 17:00:41 +01001/* Copyright 2016 Pierre Ossman for Cendio AB
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19#include <assert.h>
20
21#include <FL/Fl_RGB_Image.H>
22#include <FL/x.H>
23
24#include <rdr/Exception.h>
25
26#include "Surface.h"
27
28void Surface::clear(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
29{
30 XRenderColor color;
31
32 color.red = (unsigned)r * 65535 / 255 * a / 255;
33 color.green = (unsigned)g * 65535 / 255 * a / 255;
34 color.blue = (unsigned)b * 65535 / 255 * a / 255;
35 color.alpha = (unsigned)a * 65535 / 255;
36
37 XRenderFillRectangle(fl_display, PictOpSrc, picture, &color,
38 0, 0, width(), height());
39}
40
41void Surface::draw(int src_x, int src_y, int x, int y, int w, int h)
42{
43 Picture winPict;
44
45 winPict = XRenderCreatePicture(fl_display, fl_window, visFormat, 0, NULL);
46 XRenderComposite(fl_display, PictOpSrc, picture, None, winPict,
47 src_x, src_y, 0, 0, x, y, w, h);
48 XRenderFreePicture(fl_display, winPict);
49}
50
51void Surface::alloc()
52{
53 XRenderPictFormat* format;
54
55 // Might not be open at this point
56 fl_open_display();
57
58 pixmap = XCreatePixmap(fl_display, XDefaultRootWindow(fl_display),
59 width(), height(), 32);
60
61 format = XRenderFindStandardFormat(fl_display, PictStandardARGB32);
62 picture = XRenderCreatePicture(fl_display, pixmap, format, 0, NULL);
63
64 visFormat = XRenderFindVisualFormat(fl_display, fl_visual->visual);
65}
66
67void Surface::dealloc()
68{
69 XRenderFreePicture(fl_display, picture);
70 XFreePixmap(fl_display, pixmap);
71}
72
73void Surface::update(const Fl_RGB_Image* image)
74{
75 XImage* img;
76 GC gc;
77
78 int x, y;
79 const unsigned char* in;
80 unsigned char* out;
81
82 assert(image->w() == width());
83 assert(image->h() == height());
84
85 img = XCreateImage(fl_display, CopyFromParent, 32,
86 ZPixmap, 0, NULL, width(), height(),
87 32, 0);
88 if (!img)
89 throw rdr::Exception("XCreateImage");
90
91 img->data = (char*)malloc(img->bytes_per_line * img->height);
92 if (!img->data)
93 throw rdr::Exception("malloc");
94
95 // Convert data and pre-multiply alpha
96 in = (const unsigned char*)image->data()[0];
97 out = (unsigned char*)img->data;
98 for (y = 0;y < img->height;y++) {
99 for (x = 0;x < img->width;x++) {
100 switch (image->d()) {
101 case 1:
102 *out++ = in[0];
103 *out++ = in[0];
104 *out++ = in[0];
105 *out++ = 0xff;
106 break;
107 case 2:
108 *out++ = (unsigned)in[0] * in[1] / 255;
109 *out++ = (unsigned)in[0] * in[1] / 255;
110 *out++ = (unsigned)in[0] * in[1] / 255;
111 *out++ = in[1];
112 break;
113 case 3:
114 *out++ = in[2];
115 *out++ = in[1];
116 *out++ = in[0];
117 *out++ = 0xff;
118 break;
119 case 4:
120 *out++ = (unsigned)in[2] * in[3] / 255;
121 *out++ = (unsigned)in[1] * in[3] / 255;
122 *out++ = (unsigned)in[0] * in[3] / 255;
123 *out++ = in[3];
124 break;
125 }
126 in += image->d();
127 }
128 if (image->ld() != 0)
129 in += image->ld() - image->w() * image->d();
130 }
131
132 gc = XCreateGC(fl_display, pixmap, 0, NULL);
133 XPutImage(fl_display, pixmap, gc, img,
134 0, 0, 0, 0, img->width, img->height);
135 XFreeGC(fl_display, gc);
136
137 XDestroyImage(img);
138}
139