blob: d04d9b303e6bdada1acc3ff88891395132fdb0bc [file] [log] [blame]
Pierre Ossmanac13abe2014-02-07 14:46:26 +01001/* Copyright 2011-2014 Pierre Ossman for Cendio AB
Pierre Ossmanc18753c2011-06-17 07:35:56 +00002 *
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 Åstrandc359f362011-08-23 12:04:46 +000019#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22
Pierre Ossmanc18753c2011-06-17 07:35:56 +000023#include <ApplicationServices/ApplicationServices.h>
24
25#include <FL/Fl_Window.H>
26#include <FL/x.H>
27
28#include <rfb/LogWriter.h>
29#include <rfb/Exception.h>
30
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +020031#include "i18n.h"
Pierre Ossmanc18753c2011-06-17 07:35:56 +000032#include "OSXPixelBuffer.h"
33
34using namespace rfb;
35
Pierre Ossmanac13abe2014-02-07 14:46:26 +010036static rfb::LogWriter vlog("OSXPixelBuffer");
Pierre Ossmanc18753c2011-06-17 07:35:56 +000037
Pierre Ossmanac13abe2014-02-07 14:46:26 +010038OSXPixelBuffer::OSXPixelBuffer(int width, int height) :
39 PlatformPixelBuffer(rfb::PixelFormat(32, 24, false, true,
40 255, 255, 255, 16, 8, 0),
Pierre Ossman2e5a1062014-01-30 17:57:27 +010041 width, height, NULL, width),
Pierre Ossmancb9eefa2012-08-17 13:37:42 +000042 bitmap(NULL)
Pierre Ossmanc18753c2011-06-17 07:35:56 +000043{
44 CGColorSpaceRef lut;
Pierre Ossmanc18753c2011-06-17 07:35:56 +000045
Pierre Ossmanac13abe2014-02-07 14:46:26 +010046 data = new rdr::U8[width * height * format.bpp/8];
47 if (data == NULL)
Pierre Ossman86750632014-10-10 13:33:19 +020048 throw rfb::Exception(_("Not enough memory for framebuffer"));
Pierre Ossmanac13abe2014-02-07 14:46:26 +010049
Pierre Ossmanc18753c2011-06-17 07:35:56 +000050 lut = CGColorSpaceCreateDeviceRGB();
Pierre Ossmand1a853b2014-10-10 13:37:35 +020051 if (!lut)
Pierre Ossmanfefd9002016-12-17 14:04:39 +010052 throw rfb::Exception("CGColorSpaceCreateDeviceRGB");
Pierre Ossmanc18753c2011-06-17 07:35:56 +000053
Pierre Ossmancb9eefa2012-08-17 13:37:42 +000054 bitmap = CGBitmapContextCreate(data, width, height, 8, width*4, lut,
55 kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little);
Pierre Ossmanc18753c2011-06-17 07:35:56 +000056 CGColorSpaceRelease(lut);
Pierre Ossmand1a853b2014-10-10 13:37:35 +020057 if (!bitmap)
Pierre Ossmanfefd9002016-12-17 14:04:39 +010058 throw rfb::Exception("CGBitmapContextCreate");
Pierre Ossmanc18753c2011-06-17 07:35:56 +000059}
60
61
Pierre Ossmanac13abe2014-02-07 14:46:26 +010062OSXPixelBuffer::~OSXPixelBuffer()
Pierre Ossmanc18753c2011-06-17 07:35:56 +000063{
Pierre Ossmancb9eefa2012-08-17 13:37:42 +000064 CFRelease((CGContextRef)bitmap);
Pierre Ossmanac13abe2014-02-07 14:46:26 +010065 delete [] data;
Pierre Ossmanc18753c2011-06-17 07:35:56 +000066}
67
68
Pierre Ossmanac13abe2014-02-07 14:46:26 +010069void OSXPixelBuffer::draw(int src_x, int src_y, int x, int y, int w, int h)
Pierre Ossmanc18753c2011-06-17 07:35:56 +000070{
71 CGRect rect;
72 CGContextRef gc;
73 CGAffineTransform at;
Pierre Ossmancb9eefa2012-08-17 13:37:42 +000074 CGImageRef image;
Pierre Ossmanc18753c2011-06-17 07:35:56 +000075
76 gc = (CGContextRef)fl_gc;
77
78 CGContextSaveGState(gc);
79
80 // We have to use clipping to partially display an image
81
82 rect.origin.x = x - 0.5;
83 rect.origin.y = y - 0.5;
84 rect.size.width = w;
85 rect.size.height = h;
86
87 CGContextClipToRect(gc, rect);
88
89 // Oh the hackiness that is OS X image handling...
90 // The CGContextDrawImage() routine magically flips the images and offsets
91 // them by 0.5,0.5 in order to compensate for OS X unfamiliar coordinate
92 // system. But this breaks horribly when you set up the CTM to get the
93 // more familiar top-down system (which FLTK does), meaning we have to
94 // reset the CTM back to the identity matrix and calculate new origin
95 // coordinates.
96
97 at = CGContextGetCTM(gc);
98 CGContextScaleCTM(gc, 1, -1);
99 CGContextTranslateCTM(gc, -at.tx, -at.ty);
100
101 rect.origin.x = x - src_x;
102 rect.origin.y = Fl_Window::current()->h() - (y - src_y);
103 rect.size.width = width();
104 rect.size.height = -height(); // Negative height does _not_ flip the image
105
Pierre Ossmancb9eefa2012-08-17 13:37:42 +0000106 image = CGBitmapContextCreateImage((CGContextRef)bitmap);
107 CGContextDrawImage(gc, rect, image);
108 CGImageRelease(image);
Pierre Ossmanc18753c2011-06-17 07:35:56 +0000109
110 CGContextRestoreGState(gc);
111}