blob: 3523da3d7a542304d1b1bde7e6659a29379ad9a9 [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
Pierre Ossman3d74d882017-01-02 19:49:52 +010051void Surface::draw(Surface* dst, int src_x, int src_y, int x, int y, int w, int h)
52{
53 XRenderComposite(fl_display, PictOpSrc, picture, None, dst->picture,
54 src_x, src_y, 0, 0, x, y, w, h);
55}
56
Pierre Ossman455566e2017-02-10 16:37:52 +010057static Picture alpha_mask(int a)
Pierre Ossmande6a5802017-01-02 20:07:10 +010058{
Pierre Ossman455566e2017-02-10 16:37:52 +010059 Pixmap pixmap;
60 XRenderPictFormat* format;
61 XRenderPictureAttributes rep;
62 Picture pict;
63 XRenderColor color;
Pierre Ossmande6a5802017-01-02 20:07:10 +010064
Pierre Ossman455566e2017-02-10 16:37:52 +010065 if (a == 255)
66 return None;
67
68 pixmap = XCreatePixmap(fl_display, XDefaultRootWindow(fl_display),
69 1, 1, 8);
70
71 format = XRenderFindStandardFormat(fl_display, PictStandardA8);
72 rep.repeat = RepeatNormal;
73 pict = XRenderCreatePicture(fl_display, pixmap, format, CPRepeat, &rep);
74 XFreePixmap(fl_display, pixmap);
75
76 color.alpha = (unsigned)a * 65535 / 255;
77
78 XRenderFillRectangle(fl_display, PictOpSrc, pict, &color,
79 0, 0, 1, 1);
80
81 return pict;
Pierre Ossmande6a5802017-01-02 20:07:10 +010082}
83
Pierre Ossman455566e2017-02-10 16:37:52 +010084void Surface::blend(int src_x, int src_y, int x, int y, int w, int h, int a)
Pierre Ossmande6a5802017-01-02 20:07:10 +010085{
Pierre Ossman455566e2017-02-10 16:37:52 +010086 Picture winPict, alpha;
87
88 winPict = XRenderCreatePicture(fl_display, fl_window, visFormat, 0, NULL);
89 alpha = alpha_mask(a);
90 XRenderComposite(fl_display, PictOpOver, picture, alpha, winPict,
Pierre Ossmande6a5802017-01-02 20:07:10 +010091 src_x, src_y, 0, 0, x, y, w, h);
Pierre Ossman455566e2017-02-10 16:37:52 +010092 XRenderFreePicture(fl_display, winPict);
93
94 if (alpha != None)
95 XRenderFreePicture(fl_display, alpha);
96}
97
98void Surface::blend(Surface* dst, int src_x, int src_y, int x, int y, int w, int h, int a)
99{
100 Picture alpha;
101
102 alpha = alpha_mask(a);
103 XRenderComposite(fl_display, PictOpOver, picture, alpha, dst->picture,
104 src_x, src_y, 0, 0, x, y, w, h);
105 if (alpha != None)
106 XRenderFreePicture(fl_display, alpha);
Pierre Ossmande6a5802017-01-02 20:07:10 +0100107}
108
109
Pierre Ossman403ac272017-01-02 17:00:41 +0100110void Surface::alloc()
111{
112 XRenderPictFormat* format;
113
114 // Might not be open at this point
115 fl_open_display();
116
117 pixmap = XCreatePixmap(fl_display, XDefaultRootWindow(fl_display),
118 width(), height(), 32);
119
120 format = XRenderFindStandardFormat(fl_display, PictStandardARGB32);
121 picture = XRenderCreatePicture(fl_display, pixmap, format, 0, NULL);
122
123 visFormat = XRenderFindVisualFormat(fl_display, fl_visual->visual);
124}
125
126void Surface::dealloc()
127{
128 XRenderFreePicture(fl_display, picture);
129 XFreePixmap(fl_display, pixmap);
130}
131
132void Surface::update(const Fl_RGB_Image* image)
133{
134 XImage* img;
135 GC gc;
136
137 int x, y;
138 const unsigned char* in;
139 unsigned char* out;
140
141 assert(image->w() == width());
142 assert(image->h() == height());
143
144 img = XCreateImage(fl_display, CopyFromParent, 32,
145 ZPixmap, 0, NULL, width(), height(),
146 32, 0);
147 if (!img)
148 throw rdr::Exception("XCreateImage");
149
150 img->data = (char*)malloc(img->bytes_per_line * img->height);
151 if (!img->data)
152 throw rdr::Exception("malloc");
153
154 // Convert data and pre-multiply alpha
155 in = (const unsigned char*)image->data()[0];
156 out = (unsigned char*)img->data;
157 for (y = 0;y < img->height;y++) {
158 for (x = 0;x < img->width;x++) {
159 switch (image->d()) {
160 case 1:
161 *out++ = in[0];
162 *out++ = in[0];
163 *out++ = in[0];
164 *out++ = 0xff;
165 break;
166 case 2:
167 *out++ = (unsigned)in[0] * in[1] / 255;
168 *out++ = (unsigned)in[0] * in[1] / 255;
169 *out++ = (unsigned)in[0] * in[1] / 255;
170 *out++ = in[1];
171 break;
172 case 3:
173 *out++ = in[2];
174 *out++ = in[1];
175 *out++ = in[0];
176 *out++ = 0xff;
177 break;
178 case 4:
179 *out++ = (unsigned)in[2] * in[3] / 255;
180 *out++ = (unsigned)in[1] * in[3] / 255;
181 *out++ = (unsigned)in[0] * in[3] / 255;
182 *out++ = in[3];
183 break;
184 }
185 in += image->d();
186 }
187 if (image->ld() != 0)
188 in += image->ld() - image->w() * image->d();
189 }
190
191 gc = XCreateGC(fl_display, pixmap, 0, NULL);
192 XPutImage(fl_display, pixmap, gc, img,
193 0, 0, 0, 0, img->width, img->height);
194 XFreeGC(fl_display, gc);
195
196 XDestroyImage(img);
197}
198