Pierre Ossman | ac13abe | 2014-02-07 14:46:26 +0100 | [diff] [blame] | 1 | /* Copyright 2011-2014 Pierre Ossman for Cendio AB |
Pierre Ossman | c18753c | 2011-06-17 07:35:56 +0000 | [diff] [blame] | 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 | |
Peter Åstrand | c359f36 | 2011-08-23 12:04:46 +0000 | [diff] [blame] | 19 | #ifdef HAVE_CONFIG_H |
| 20 | #include <config.h> |
| 21 | #endif |
| 22 | |
Pierre Ossman | c18753c | 2011-06-17 07:35:56 +0000 | [diff] [blame] | 23 | #include <assert.h> |
| 24 | |
| 25 | #include <ApplicationServices/ApplicationServices.h> |
| 26 | |
| 27 | #include <FL/Fl_Window.H> |
| 28 | #include <FL/x.H> |
| 29 | |
| 30 | #include <rfb/LogWriter.h> |
| 31 | #include <rfb/Exception.h> |
| 32 | |
Pierre Ossman | 8ca4c1d | 2014-09-22 12:54:26 +0200 | [diff] [blame] | 33 | #include "i18n.h" |
Pierre Ossman | c18753c | 2011-06-17 07:35:56 +0000 | [diff] [blame] | 34 | #include "OSXPixelBuffer.h" |
| 35 | |
| 36 | using namespace rfb; |
| 37 | |
Pierre Ossman | ac13abe | 2014-02-07 14:46:26 +0100 | [diff] [blame] | 38 | static rfb::LogWriter vlog("OSXPixelBuffer"); |
Pierre Ossman | c18753c | 2011-06-17 07:35:56 +0000 | [diff] [blame] | 39 | |
Pierre Ossman | ac13abe | 2014-02-07 14:46:26 +0100 | [diff] [blame] | 40 | OSXPixelBuffer::OSXPixelBuffer(int width, int height) : |
| 41 | PlatformPixelBuffer(rfb::PixelFormat(32, 24, false, true, |
| 42 | 255, 255, 255, 16, 8, 0), |
Pierre Ossman | 2e5a106 | 2014-01-30 17:57:27 +0100 | [diff] [blame] | 43 | width, height, NULL, width), |
Pierre Ossman | cb9eefa | 2012-08-17 13:37:42 +0000 | [diff] [blame] | 44 | bitmap(NULL) |
Pierre Ossman | c18753c | 2011-06-17 07:35:56 +0000 | [diff] [blame] | 45 | { |
| 46 | CGColorSpaceRef lut; |
Pierre Ossman | c18753c | 2011-06-17 07:35:56 +0000 | [diff] [blame] | 47 | |
Pierre Ossman | ac13abe | 2014-02-07 14:46:26 +0100 | [diff] [blame] | 48 | data = new rdr::U8[width * height * format.bpp/8]; |
| 49 | if (data == NULL) |
Pierre Ossman | 8675063 | 2014-10-10 13:33:19 +0200 | [diff] [blame^] | 50 | throw rfb::Exception(_("Not enough memory for framebuffer")); |
Pierre Ossman | ac13abe | 2014-02-07 14:46:26 +0100 | [diff] [blame] | 51 | |
Pierre Ossman | c18753c | 2011-06-17 07:35:56 +0000 | [diff] [blame] | 52 | lut = CGColorSpaceCreateDeviceRGB(); |
| 53 | assert(lut); |
Pierre Ossman | c18753c | 2011-06-17 07:35:56 +0000 | [diff] [blame] | 54 | |
Pierre Ossman | cb9eefa | 2012-08-17 13:37:42 +0000 | [diff] [blame] | 55 | bitmap = CGBitmapContextCreate(data, width, height, 8, width*4, lut, |
| 56 | kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little); |
| 57 | assert(bitmap); |
Pierre Ossman | c18753c | 2011-06-17 07:35:56 +0000 | [diff] [blame] | 58 | |
Pierre Ossman | c18753c | 2011-06-17 07:35:56 +0000 | [diff] [blame] | 59 | CGColorSpaceRelease(lut); |
| 60 | } |
| 61 | |
| 62 | |
Pierre Ossman | ac13abe | 2014-02-07 14:46:26 +0100 | [diff] [blame] | 63 | OSXPixelBuffer::~OSXPixelBuffer() |
Pierre Ossman | c18753c | 2011-06-17 07:35:56 +0000 | [diff] [blame] | 64 | { |
Pierre Ossman | cb9eefa | 2012-08-17 13:37:42 +0000 | [diff] [blame] | 65 | CFRelease((CGContextRef)bitmap); |
Pierre Ossman | ac13abe | 2014-02-07 14:46:26 +0100 | [diff] [blame] | 66 | delete [] data; |
Pierre Ossman | c18753c | 2011-06-17 07:35:56 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | |
Pierre Ossman | ac13abe | 2014-02-07 14:46:26 +0100 | [diff] [blame] | 70 | void OSXPixelBuffer::draw(int src_x, int src_y, int x, int y, int w, int h) |
Pierre Ossman | c18753c | 2011-06-17 07:35:56 +0000 | [diff] [blame] | 71 | { |
| 72 | CGRect rect; |
| 73 | CGContextRef gc; |
| 74 | CGAffineTransform at; |
Pierre Ossman | cb9eefa | 2012-08-17 13:37:42 +0000 | [diff] [blame] | 75 | CGImageRef image; |
Pierre Ossman | c18753c | 2011-06-17 07:35:56 +0000 | [diff] [blame] | 76 | |
| 77 | gc = (CGContextRef)fl_gc; |
| 78 | |
| 79 | CGContextSaveGState(gc); |
| 80 | |
| 81 | // We have to use clipping to partially display an image |
| 82 | |
| 83 | rect.origin.x = x - 0.5; |
| 84 | rect.origin.y = y - 0.5; |
| 85 | rect.size.width = w; |
| 86 | rect.size.height = h; |
| 87 | |
| 88 | CGContextClipToRect(gc, rect); |
| 89 | |
| 90 | // Oh the hackiness that is OS X image handling... |
| 91 | // The CGContextDrawImage() routine magically flips the images and offsets |
| 92 | // them by 0.5,0.5 in order to compensate for OS X unfamiliar coordinate |
| 93 | // system. But this breaks horribly when you set up the CTM to get the |
| 94 | // more familiar top-down system (which FLTK does), meaning we have to |
| 95 | // reset the CTM back to the identity matrix and calculate new origin |
| 96 | // coordinates. |
| 97 | |
| 98 | at = CGContextGetCTM(gc); |
| 99 | CGContextScaleCTM(gc, 1, -1); |
| 100 | CGContextTranslateCTM(gc, -at.tx, -at.ty); |
| 101 | |
| 102 | rect.origin.x = x - src_x; |
| 103 | rect.origin.y = Fl_Window::current()->h() - (y - src_y); |
| 104 | rect.size.width = width(); |
| 105 | rect.size.height = -height(); // Negative height does _not_ flip the image |
| 106 | |
Pierre Ossman | cb9eefa | 2012-08-17 13:37:42 +0000 | [diff] [blame] | 107 | image = CGBitmapContextCreateImage((CGContextRef)bitmap); |
| 108 | CGContextDrawImage(gc, rect, image); |
| 109 | CGImageRelease(image); |
Pierre Ossman | c18753c | 2011-06-17 07:35:56 +0000 | [diff] [blame] | 110 | |
| 111 | CGContextRestoreGState(gc); |
| 112 | } |