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.5KB

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