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 <ApplicationServices/ApplicationServices.h> |
| 22 | |
| 23 | #include <FL/Fl_RGB_Image.H> |
| 24 | #include <FL/Fl_Window.H> |
| 25 | #include <FL/x.H> |
| 26 | |
| 27 | #include <rdr/Exception.h> |
| 28 | |
Pierre Ossman | 1669a2d | 2017-04-28 11:37:12 +0200 | [diff] [blame] | 29 | #include "cocoa.h" |
Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 30 | #include "Surface.h" |
| 31 | |
Pierre Ossman | 1669a2d | 2017-04-28 11:37:12 +0200 | [diff] [blame] | 32 | static CGColorSpaceRef srgb = CGColorSpaceCreateWithName(kCGColorSpaceSRGB); |
| 33 | |
| 34 | static CGImageRef create_image(CGColorSpaceRef lut, |
| 35 | const unsigned char* data, |
Pierre Ossman | a88991b | 2017-04-28 11:40:02 +0200 | [diff] [blame^] | 36 | int w, int h, bool skip_alpha) |
Pierre Ossman | 3b34731 | 2017-04-28 11:33:28 +0200 | [diff] [blame] | 37 | { |
Pierre Ossman | 3b34731 | 2017-04-28 11:33:28 +0200 | [diff] [blame] | 38 | CGDataProviderRef provider; |
Pierre Ossman | a88991b | 2017-04-28 11:40:02 +0200 | [diff] [blame^] | 39 | CGImageAlphaInfo alpha; |
Pierre Ossman | 3b34731 | 2017-04-28 11:33:28 +0200 | [diff] [blame] | 40 | |
| 41 | CGImageRef image; |
| 42 | |
Pierre Ossman | 3b34731 | 2017-04-28 11:33:28 +0200 | [diff] [blame] | 43 | provider = CGDataProviderCreateWithData(NULL, data, |
| 44 | w * h * 4, NULL); |
| 45 | if (!provider) |
| 46 | throw rdr::Exception("CGDataProviderCreateWithData"); |
| 47 | |
Pierre Ossman | a88991b | 2017-04-28 11:40:02 +0200 | [diff] [blame^] | 48 | // FIXME: This causes a performance hit, but is necessary to avoid |
| 49 | // artifacts in the edges of the window |
| 50 | if (skip_alpha) |
| 51 | alpha = kCGImageAlphaNoneSkipFirst; |
| 52 | else |
| 53 | alpha = kCGImageAlphaPremultipliedFirst; |
| 54 | |
Pierre Ossman | 3b34731 | 2017-04-28 11:33:28 +0200 | [diff] [blame] | 55 | image = CGImageCreate(w, h, 8, 32, w * 4, lut, |
Pierre Ossman | a88991b | 2017-04-28 11:40:02 +0200 | [diff] [blame^] | 56 | alpha | kCGBitmapByteOrder32Little, |
Pierre Ossman | 3b34731 | 2017-04-28 11:33:28 +0200 | [diff] [blame] | 57 | provider, NULL, false, kCGRenderingIntentDefault); |
Pierre Ossman | 3b34731 | 2017-04-28 11:33:28 +0200 | [diff] [blame] | 58 | CGDataProviderRelease(provider); |
| 59 | if (!image) |
| 60 | throw rdr::Exception("CGImageCreate"); |
| 61 | |
| 62 | return image; |
| 63 | } |
| 64 | |
Pierre Ossman | 1669a2d | 2017-04-28 11:37:12 +0200 | [diff] [blame] | 65 | static void render(CGContextRef gc, CGColorSpaceRef lut, |
Pierre Ossman | 3b34731 | 2017-04-28 11:33:28 +0200 | [diff] [blame] | 66 | const unsigned char* data, |
Pierre Ossman | 455566e | 2017-02-10 16:37:52 +0100 | [diff] [blame] | 67 | CGBlendMode mode, CGFloat alpha, |
Pierre Ossman | 3b34731 | 2017-04-28 11:33:28 +0200 | [diff] [blame] | 68 | int src_x, int src_y, int src_w, int src_h, |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 69 | int x, int y, int w, int h) |
| 70 | { |
| 71 | CGRect rect; |
Pierre Ossman | 3b34731 | 2017-04-28 11:33:28 +0200 | [diff] [blame] | 72 | CGImageRef image, subimage; |
| 73 | |
Pierre Ossman | a88991b | 2017-04-28 11:40:02 +0200 | [diff] [blame^] | 74 | image = create_image(lut, data, src_w, src_h, mode == kCGBlendModeCopy); |
Pierre Ossman | 2d0dc3a | 2017-04-27 13:28:49 +0200 | [diff] [blame] | 75 | |
| 76 | rect.origin.x = src_x; |
| 77 | rect.origin.y = src_y; |
| 78 | rect.size.width = w; |
| 79 | rect.size.height = h; |
| 80 | |
| 81 | subimage = CGImageCreateWithImageInRect(image, rect); |
| 82 | if (!subimage) |
| 83 | throw rdr::Exception("CGImageCreateImageWithImageInRect"); |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 84 | |
| 85 | CGContextSaveGState(gc); |
| 86 | |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 87 | CGContextSetBlendMode(gc, mode); |
Pierre Ossman | 455566e | 2017-02-10 16:37:52 +0100 | [diff] [blame] | 88 | CGContextSetAlpha(gc, alpha); |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 89 | |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 90 | rect.origin.x = x; |
| 91 | rect.origin.y = y; |
| 92 | rect.size.width = w; |
| 93 | rect.size.height = h; |
| 94 | |
Pierre Ossman | 2d0dc3a | 2017-04-27 13:28:49 +0200 | [diff] [blame] | 95 | CGContextDrawImage(gc, rect, subimage); |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 96 | |
| 97 | CGContextRestoreGState(gc); |
Pierre Ossman | 2d0dc3a | 2017-04-27 13:28:49 +0200 | [diff] [blame] | 98 | |
| 99 | CGImageRelease(subimage); |
Pierre Ossman | 3b34731 | 2017-04-28 11:33:28 +0200 | [diff] [blame] | 100 | CGImageRelease(image); |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 103 | static CGContextRef make_bitmap(int width, int height, unsigned char* data) |
| 104 | { |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 105 | CGContextRef bitmap; |
| 106 | |
Pierre Ossman | 1669a2d | 2017-04-28 11:37:12 +0200 | [diff] [blame] | 107 | bitmap = CGBitmapContextCreate(data, width, height, 8, width*4, srgb, |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 108 | kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little); |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 109 | if (!bitmap) |
| 110 | throw rdr::Exception("CGBitmapContextCreate"); |
| 111 | |
| 112 | return bitmap; |
| 113 | } |
| 114 | |
Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 115 | void Surface::clear(unsigned char r, unsigned char g, unsigned char b, unsigned char a) |
| 116 | { |
| 117 | unsigned char* out; |
| 118 | int x, y; |
| 119 | |
| 120 | r = (unsigned)r * a / 255; |
| 121 | g = (unsigned)g * a / 255; |
| 122 | b = (unsigned)b * a / 255; |
| 123 | |
| 124 | out = data; |
| 125 | for (y = 0;y < width();y++) { |
| 126 | for (x = 0;x < height();x++) { |
| 127 | *out++ = b; |
| 128 | *out++ = g; |
| 129 | *out++ = r; |
| 130 | *out++ = a; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void Surface::draw(int src_x, int src_y, int x, int y, int w, int h) |
| 136 | { |
Pierre Ossman | 1669a2d | 2017-04-28 11:37:12 +0200 | [diff] [blame] | 137 | CGColorSpaceRef lut; |
| 138 | |
Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 139 | CGContextSaveGState(fl_gc); |
| 140 | |
| 141 | // Reset the transformation matrix back to the default identity |
| 142 | // matrix as otherwise we get a massive performance hit |
| 143 | CGContextConcatCTM(fl_gc, CGAffineTransformInvert(CGContextGetCTM(fl_gc))); |
| 144 | |
| 145 | // macOS Coordinates are from bottom left, not top left |
Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 146 | y = Fl_Window::current()->h() - (y + h); |
| 147 | |
Pierre Ossman | 1669a2d | 2017-04-28 11:37:12 +0200 | [diff] [blame] | 148 | lut = cocoa_win_color_space(Fl_Window::current()); |
| 149 | render(fl_gc, lut, data, kCGBlendModeCopy, 1.0, |
Pierre Ossman | 3b34731 | 2017-04-28 11:33:28 +0200 | [diff] [blame] | 150 | src_x, src_y, width(), height(), x, y, w, h); |
Pierre Ossman | 1669a2d | 2017-04-28 11:37:12 +0200 | [diff] [blame] | 151 | CGColorSpaceRelease(lut); |
Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 152 | |
| 153 | CGContextRestoreGState(fl_gc); |
| 154 | } |
| 155 | |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 156 | void Surface::draw(Surface* dst, int src_x, int src_y, int x, int y, int w, int h) |
| 157 | { |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 158 | CGContextRef bitmap; |
| 159 | |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 160 | bitmap = make_bitmap(dst->width(), dst->height(), dst->data); |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 161 | |
| 162 | // macOS Coordinates are from bottom left, not top left |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 163 | y = dst->height() - (y + h); |
| 164 | |
Pierre Ossman | 1669a2d | 2017-04-28 11:37:12 +0200 | [diff] [blame] | 165 | render(bitmap, srgb, data, kCGBlendModeCopy, 1.0, |
Pierre Ossman | 3b34731 | 2017-04-28 11:33:28 +0200 | [diff] [blame] | 166 | src_x, src_y, width(), height(), x, y, w, h); |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 167 | |
| 168 | CGContextRelease(bitmap); |
| 169 | } |
| 170 | |
Pierre Ossman | 455566e | 2017-02-10 16:37:52 +0100 | [diff] [blame] | 171 | void Surface::blend(int src_x, int src_y, int x, int y, int w, int h, int a) |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 172 | { |
Pierre Ossman | 1669a2d | 2017-04-28 11:37:12 +0200 | [diff] [blame] | 173 | CGColorSpaceRef lut; |
| 174 | |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 175 | CGContextSaveGState(fl_gc); |
| 176 | |
| 177 | // Reset the transformation matrix back to the default identity |
| 178 | // matrix as otherwise we get a massive performance hit |
| 179 | CGContextConcatCTM(fl_gc, CGAffineTransformInvert(CGContextGetCTM(fl_gc))); |
| 180 | |
| 181 | // macOS Coordinates are from bottom left, not top left |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 182 | y = Fl_Window::current()->h() - (y + h); |
| 183 | |
Pierre Ossman | 1669a2d | 2017-04-28 11:37:12 +0200 | [diff] [blame] | 184 | lut = cocoa_win_color_space(Fl_Window::current()); |
| 185 | render(fl_gc, lut, data, kCGBlendModeNormal, (CGFloat)a/255.0, |
Pierre Ossman | 3b34731 | 2017-04-28 11:33:28 +0200 | [diff] [blame] | 186 | src_x, src_y, width(), height(), x, y, w, h); |
Pierre Ossman | 1669a2d | 2017-04-28 11:37:12 +0200 | [diff] [blame] | 187 | CGColorSpaceRelease(lut); |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 188 | |
| 189 | CGContextRestoreGState(fl_gc); |
| 190 | } |
| 191 | |
Pierre Ossman | 455566e | 2017-02-10 16:37:52 +0100 | [diff] [blame] | 192 | void Surface::blend(Surface* dst, int src_x, int src_y, int x, int y, int w, int h, int a) |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 193 | { |
| 194 | CGContextRef bitmap; |
| 195 | |
| 196 | bitmap = make_bitmap(dst->width(), dst->height(), dst->data); |
| 197 | |
| 198 | // macOS Coordinates are from bottom left, not top left |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 199 | y = dst->height() - (y + h); |
| 200 | |
Pierre Ossman | 1669a2d | 2017-04-28 11:37:12 +0200 | [diff] [blame] | 201 | render(bitmap, srgb, data, kCGBlendModeNormal, (CGFloat)a/255.0, |
Pierre Ossman | 3b34731 | 2017-04-28 11:33:28 +0200 | [diff] [blame] | 202 | src_x, src_y, width(), height(), x, y, w, h); |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 203 | |
| 204 | CGContextRelease(bitmap); |
| 205 | } |
| 206 | |
Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 207 | void Surface::alloc() |
| 208 | { |
Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 209 | data = new unsigned char[width() * height() * 4]; |
Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void Surface::dealloc() |
| 213 | { |
Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 214 | delete [] data; |
| 215 | } |
| 216 | |
| 217 | void Surface::update(const Fl_RGB_Image* image) |
| 218 | { |
| 219 | int x, y; |
| 220 | const unsigned char* in; |
| 221 | unsigned char* out; |
| 222 | |
| 223 | assert(image->w() == width()); |
| 224 | assert(image->h() == height()); |
| 225 | |
| 226 | // Convert data and pre-multiply alpha |
| 227 | in = (const unsigned char*)image->data()[0]; |
| 228 | out = data; |
| 229 | for (y = 0;y < image->h();y++) { |
| 230 | for (x = 0;x < image->w();x++) { |
| 231 | switch (image->d()) { |
| 232 | case 1: |
| 233 | *out++ = in[0]; |
| 234 | *out++ = in[0]; |
| 235 | *out++ = in[0]; |
| 236 | *out++ = 0xff; |
| 237 | break; |
| 238 | case 2: |
| 239 | *out++ = (unsigned)in[0] * in[1] / 255; |
| 240 | *out++ = (unsigned)in[0] * in[1] / 255; |
| 241 | *out++ = (unsigned)in[0] * in[1] / 255; |
| 242 | *out++ = in[1]; |
| 243 | break; |
| 244 | case 3: |
| 245 | *out++ = in[2]; |
| 246 | *out++ = in[1]; |
| 247 | *out++ = in[0]; |
| 248 | *out++ = 0xff; |
| 249 | break; |
| 250 | case 4: |
| 251 | *out++ = (unsigned)in[2] * in[3] / 255; |
| 252 | *out++ = (unsigned)in[1] * in[3] / 255; |
| 253 | *out++ = (unsigned)in[0] * in[3] / 255; |
| 254 | *out++ = in[3]; |
| 255 | break; |
| 256 | } |
| 257 | in += image->d(); |
| 258 | } |
| 259 | if (image->ld() != 0) |
| 260 | in += image->ld() - image->w() * image->d(); |
| 261 | } |
| 262 | } |