You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Surface_OSX.cxx 7.0KB

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