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

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