Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 1 | /* 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 | |
| 28 | void 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 | |
| 41 | void 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 Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame^] | 51 | void 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 Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 57 | void Surface::alloc() |
| 58 | { |
| 59 | XRenderPictFormat* format; |
| 60 | |
| 61 | // Might not be open at this point |
| 62 | fl_open_display(); |
| 63 | |
| 64 | pixmap = XCreatePixmap(fl_display, XDefaultRootWindow(fl_display), |
| 65 | width(), height(), 32); |
| 66 | |
| 67 | format = XRenderFindStandardFormat(fl_display, PictStandardARGB32); |
| 68 | picture = XRenderCreatePicture(fl_display, pixmap, format, 0, NULL); |
| 69 | |
| 70 | visFormat = XRenderFindVisualFormat(fl_display, fl_visual->visual); |
| 71 | } |
| 72 | |
| 73 | void Surface::dealloc() |
| 74 | { |
| 75 | XRenderFreePicture(fl_display, picture); |
| 76 | XFreePixmap(fl_display, pixmap); |
| 77 | } |
| 78 | |
| 79 | void Surface::update(const Fl_RGB_Image* image) |
| 80 | { |
| 81 | XImage* img; |
| 82 | GC gc; |
| 83 | |
| 84 | int x, y; |
| 85 | const unsigned char* in; |
| 86 | unsigned char* out; |
| 87 | |
| 88 | assert(image->w() == width()); |
| 89 | assert(image->h() == height()); |
| 90 | |
| 91 | img = XCreateImage(fl_display, CopyFromParent, 32, |
| 92 | ZPixmap, 0, NULL, width(), height(), |
| 93 | 32, 0); |
| 94 | if (!img) |
| 95 | throw rdr::Exception("XCreateImage"); |
| 96 | |
| 97 | img->data = (char*)malloc(img->bytes_per_line * img->height); |
| 98 | if (!img->data) |
| 99 | throw rdr::Exception("malloc"); |
| 100 | |
| 101 | // Convert data and pre-multiply alpha |
| 102 | in = (const unsigned char*)image->data()[0]; |
| 103 | out = (unsigned char*)img->data; |
| 104 | for (y = 0;y < img->height;y++) { |
| 105 | for (x = 0;x < img->width;x++) { |
| 106 | switch (image->d()) { |
| 107 | case 1: |
| 108 | *out++ = in[0]; |
| 109 | *out++ = in[0]; |
| 110 | *out++ = in[0]; |
| 111 | *out++ = 0xff; |
| 112 | break; |
| 113 | case 2: |
| 114 | *out++ = (unsigned)in[0] * in[1] / 255; |
| 115 | *out++ = (unsigned)in[0] * in[1] / 255; |
| 116 | *out++ = (unsigned)in[0] * in[1] / 255; |
| 117 | *out++ = in[1]; |
| 118 | break; |
| 119 | case 3: |
| 120 | *out++ = in[2]; |
| 121 | *out++ = in[1]; |
| 122 | *out++ = in[0]; |
| 123 | *out++ = 0xff; |
| 124 | break; |
| 125 | case 4: |
| 126 | *out++ = (unsigned)in[2] * in[3] / 255; |
| 127 | *out++ = (unsigned)in[1] * in[3] / 255; |
| 128 | *out++ = (unsigned)in[0] * in[3] / 255; |
| 129 | *out++ = in[3]; |
| 130 | break; |
| 131 | } |
| 132 | in += image->d(); |
| 133 | } |
| 134 | if (image->ld() != 0) |
| 135 | in += image->ld() - image->w() * image->d(); |
| 136 | } |
| 137 | |
| 138 | gc = XCreateGC(fl_display, pixmap, 0, NULL); |
| 139 | XPutImage(fl_display, pixmap, gc, img, |
| 140 | 0, 0, 0, 0, img->width, img->height); |
| 141 | XFreeGC(fl_display, gc); |
| 142 | |
| 143 | XDestroyImage(img); |
| 144 | } |
| 145 | |