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 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 "Surface.h"
  25. static CGImageRef create_image(const unsigned char* data,
  26. int w, int h)
  27. {
  28. CGColorSpaceRef lut;
  29. CGDataProviderRef provider;
  30. CGImageRef image;
  31. lut = CGDisplayCopyColorSpace(kCGDirectMainDisplay);
  32. if (!lut) {
  33. lut = CGColorSpaceCreateDeviceRGB();
  34. if (!lut)
  35. throw rdr::Exception("CGColorSpaceCreateDeviceRGB");
  36. }
  37. provider = CGDataProviderCreateWithData(NULL, data,
  38. w * h * 4, NULL);
  39. if (!provider)
  40. throw rdr::Exception("CGDataProviderCreateWithData");
  41. image = CGImageCreate(w, h, 8, 32, w * 4, lut,
  42. kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little,
  43. provider, NULL, false, kCGRenderingIntentDefault);
  44. CGColorSpaceRelease(lut);
  45. CGDataProviderRelease(provider);
  46. if (!image)
  47. throw rdr::Exception("CGImageCreate");
  48. return image;
  49. }
  50. static void render(CGContextRef gc,
  51. const unsigned char* data,
  52. CGBlendMode mode, CGFloat alpha,
  53. int src_x, int src_y, int src_w, int src_h,
  54. int x, int y, int w, int h)
  55. {
  56. CGRect rect;
  57. CGImageRef image, subimage;
  58. image = create_image(data, src_w, src_h);
  59. rect.origin.x = src_x;
  60. rect.origin.y = src_y;
  61. rect.size.width = w;
  62. rect.size.height = h;
  63. subimage = CGImageCreateWithImageInRect(image, rect);
  64. if (!subimage)
  65. throw rdr::Exception("CGImageCreateImageWithImageInRect");
  66. CGContextSaveGState(gc);
  67. CGContextSetBlendMode(gc, mode);
  68. CGContextSetAlpha(gc, alpha);
  69. rect.origin.x = x;
  70. rect.origin.y = y;
  71. rect.size.width = w;
  72. rect.size.height = h;
  73. CGContextDrawImage(gc, rect, subimage);
  74. CGContextRestoreGState(gc);
  75. CGImageRelease(subimage);
  76. CGImageRelease(image);
  77. }
  78. static CGContextRef make_bitmap(int width, int height, unsigned char* data)
  79. {
  80. CGColorSpaceRef lut;
  81. CGContextRef bitmap;
  82. lut = CGDisplayCopyColorSpace(kCGDirectMainDisplay);
  83. if (!lut) {
  84. lut = CGColorSpaceCreateDeviceRGB();
  85. if (!lut)
  86. throw rdr::Exception("CGColorSpaceCreateDeviceRGB");
  87. }
  88. bitmap = CGBitmapContextCreate(data, width, height, 8, width*4, lut,
  89. kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
  90. CGColorSpaceRelease(lut);
  91. if (!bitmap)
  92. throw rdr::Exception("CGBitmapContextCreate");
  93. return bitmap;
  94. }
  95. void Surface::clear(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
  96. {
  97. unsigned char* out;
  98. int x, y;
  99. r = (unsigned)r * a / 255;
  100. g = (unsigned)g * a / 255;
  101. b = (unsigned)b * a / 255;
  102. out = data;
  103. for (y = 0;y < width();y++) {
  104. for (x = 0;x < height();x++) {
  105. *out++ = b;
  106. *out++ = g;
  107. *out++ = r;
  108. *out++ = a;
  109. }
  110. }
  111. }
  112. void Surface::draw(int src_x, int src_y, int x, int y, int w, int h)
  113. {
  114. CGContextSaveGState(fl_gc);
  115. // Reset the transformation matrix back to the default identity
  116. // matrix as otherwise we get a massive performance hit
  117. CGContextConcatCTM(fl_gc, CGAffineTransformInvert(CGContextGetCTM(fl_gc)));
  118. // macOS Coordinates are from bottom left, not top left
  119. y = Fl_Window::current()->h() - (y + h);
  120. render(fl_gc, data, kCGBlendModeCopy, 1.0,
  121. src_x, src_y, width(), height(), x, y, w, h);
  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, 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. CGContextSaveGState(fl_gc);
  137. // Reset the transformation matrix back to the default identity
  138. // matrix as otherwise we get a massive performance hit
  139. CGContextConcatCTM(fl_gc, CGAffineTransformInvert(CGContextGetCTM(fl_gc)));
  140. // macOS Coordinates are from bottom left, not top left
  141. y = Fl_Window::current()->h() - (y + h);
  142. render(fl_gc, data, kCGBlendModeNormal, (CGFloat)a/255.0,
  143. src_x, src_y, width(), height(), x, y, w, h);
  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, 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. }