From 7758d472c2356f6908bce9fec6aa3e6d7e6a63e1 Mon Sep 17 00:00:00 2001 From: Manolo Gouy Date: Tue, 26 Dec 2017 16:07:51 +0000 Subject: Continue support for GUI rescaling under MacOS: fix fl_XXX_offscreen() functions and Fl_Image_Surface class git-svn-id: file:///fltk/svn/fltk/branches/branch-1.4@12605 ea41ed52-d2ee-0310-a9c1-e6b18d33e121 --- src/Fl_Image_Surface.cxx | 10 +++------ src/drivers/Cocoa/Fl_Cocoa_Window_Driver.cxx | 25 ++++++---------------- src/drivers/Quartz/Fl_Quartz_Graphics_Driver.cxx | 25 ++++++++++++++++------ .../Quartz/Fl_Quartz_Image_Surface_Driver.cxx | 13 ++++++++--- 4 files changed, 38 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/Fl_Image_Surface.cxx b/src/Fl_Image_Surface.cxx index bdd47fcf3..96358deaf 100644 --- a/src/Fl_Image_Surface.cxx +++ b/src/Fl_Image_Surface.cxx @@ -102,7 +102,7 @@ Fl_RGB_Image *Fl_Image_Surface::image() { Fl_Shared_Image* Fl_Image_Surface::highres_image() { if (!platform_surface) return NULL; - Fl_Shared_Image *s_img = Fl_Shared_Image::get(platform_surface->image()); + Fl_Shared_Image *s_img = Fl_Shared_Image::get(image()); int width, height; platform_surface->printable_rect(&width, &height); s_img->scale(width, height, 1, 1); @@ -146,9 +146,7 @@ static int find_slot(void) { // return an available slot to memorize an Fl_Image */ Fl_Offscreen fl_create_offscreen(int w, int h) { int rank = find_slot(); - float d = Fl_Graphics_Driver::default_driver().scale(); - int high_res = d != 1; - offscreen_api_surface[rank] = new Fl_Image_Surface(w, h, high_res); + offscreen_api_surface[rank] = new Fl_Image_Surface(w, h, 1/*high_res*/); return offscreen_api_surface[rank]->offscreen(); } @@ -197,11 +195,9 @@ void fl_scale_offscreen(Fl_Offscreen &ctx) { } } if (i >= count_offscreens) return; - Fl_RGB_Image *rgb = offscreen_api_surface[i]->image(); + Fl_Shared_Image *shared = offscreen_api_surface[i]->highres_image(); offscreen_api_surface[i]->printable_rect(&w, &h); fl_delete_offscreen(ctx); - Fl_Shared_Image *shared = Fl_Shared_Image::get(rgb); - shared->scale(w, h, 0, 1); ctx = fl_create_offscreen(w, h); fl_begin_offscreen(ctx); shared->draw(0, 0); diff --git a/src/drivers/Cocoa/Fl_Cocoa_Window_Driver.cxx b/src/drivers/Cocoa/Fl_Cocoa_Window_Driver.cxx index 59a4c538c..d46134c87 100644 --- a/src/drivers/Cocoa/Fl_Cocoa_Window_Driver.cxx +++ b/src/drivers/Cocoa/Fl_Cocoa_Window_Driver.cxx @@ -26,7 +26,6 @@ #include #include #include -#include Fl_Window_Driver *Fl_Window_Driver::newWindowDriver(Fl_Window *w) { @@ -66,32 +65,20 @@ void Fl_Cocoa_Window_Driver::flush_overlay() if (!oWindow->shown()) return; pWindow->make_current(); // make sure fl_gc is non-zero - Fl_Quartz_Graphics_Driver *g_driver = (Fl_Quartz_Graphics_Driver*)&Fl_Graphics_Driver::default_driver(); - float s = g_driver->scale() * (mapped_to_retina() ? 2 : 1); - if (!other_xid) { // create offscreen accounting for GUI scaling and retina - other_xid = (Fl_Offscreen)new Fl_Image_Surface(s * oWindow->w(), s * oWindow->h()); + if (!other_xid) { + other_xid = fl_create_offscreen(oWindow->w(), oWindow->h()); oWindow->clear_damage(FL_DAMAGE_ALL); - // scale the offscreen buffer's graphics context - Fl_Surface_Device::push_current((Fl_Image_Surface*)other_xid); - CGContextRestoreGState(fl_gc); - CGContextScaleCTM(fl_gc, s, s); - CGContextSaveGState(fl_gc); - Fl_Surface_Device::pop_current(); } if (oWindow->damage() & ~FL_DAMAGE_EXPOSE) { Fl_X *myi = Fl_X::i(pWindow); fl_clip_region(myi->region); myi->region = 0; - Fl_Surface_Device::push_current((Fl_Image_Surface*)other_xid); + fl_begin_offscreen(other_xid); draw(); - Fl_Surface_Device::pop_current(); + fl_end_offscreen(); } if (erase_overlay) fl_clip_region(0); if (other_xid) { - CGContextSaveGState(fl_gc); - CGContextScaleCTM(fl_gc, 1/s, 1/s); - // copy offscreen to window using adequate scaling - fl_copy_offscreen(0, 0, s * oWindow->w(),s * oWindow->h(), ((Fl_Image_Surface*)other_xid)->offscreen(), 0, 0); - CGContextRestoreGState(fl_gc); + fl_copy_offscreen(0, 0, oWindow->w(), oWindow->h(), other_xid, 0, 0); } if (overlay() == oWindow) oWindow->draw_overlay(); } @@ -99,7 +86,7 @@ void Fl_Cocoa_Window_Driver::flush_overlay() void Fl_Cocoa_Window_Driver::destroy_double_buffer() { - if (pWindow->as_overlay_window()) delete (Fl_Image_Surface*)other_xid; + if (pWindow->as_overlay_window()) fl_delete_offscreen(other_xid); other_xid = 0; } diff --git a/src/drivers/Quartz/Fl_Quartz_Graphics_Driver.cxx b/src/drivers/Quartz/Fl_Quartz_Graphics_Driver.cxx index 26f12d6e5..8e9ae0906 100644 --- a/src/drivers/Quartz/Fl_Quartz_Graphics_Driver.cxx +++ b/src/drivers/Quartz/Fl_Quartz_Graphics_Driver.cxx @@ -20,6 +20,8 @@ #include "Fl_Quartz_Graphics_Driver.H" #include "../Darwin/Fl_Darwin_System_Driver.H" #include +#include +#include #if HAS_ATSU && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 Fl_Quartz_Graphics_Driver::pter_to_draw_member Fl_Quartz_Graphics_Driver::CoreText_or_ATSU_draw; @@ -84,7 +86,8 @@ void Fl_Quartz_Graphics_Driver::global_gc() fl_gc = (CGContextRef)gc(); } -void Fl_Quartz_Graphics_Driver::copy_offscreen(int x,int y,int w,int h,Fl_Offscreen osrc,int srcx,int srcy) { +void Fl_Quartz_Graphics_Driver::copy_offscreen(int x, int y, int w, int h, Fl_Offscreen osrc, int srcx, int srcy) { + // draw portion srcx,srcy,w,h of osrc to position x,y (top-left) of the graphics driver's surface CGContextRef src = (CGContextRef)osrc; void *data = CGBitmapContextGetData(src); int sw = CGBitmapContextGetWidth(src); @@ -92,16 +95,26 @@ void Fl_Quartz_Graphics_Driver::copy_offscreen(int x,int y,int w,int h,Fl_Offscr CGImageAlphaInfo alpha = CGBitmapContextGetAlphaInfo(src); CGColorSpaceRef lut = CGColorSpaceCreateDeviceRGB(); // when output goes to a Quartz printercontext, release of the bitmap must be - // delayed after the end of the print page + // delayed after the end of the printed page CFRetain(src); CGDataProviderRef src_bytes = CGDataProviderCreateWithData( src, data, sw*sh*4, bmProviderRelease); CGImageRef img = CGImageCreate( sw, sh, 8, 4*8, 4*sw, lut, alpha, src_bytes, 0L, false, kCGRenderingIntentDefault); - draw_CGImage(img, x, y, w, h, srcx, srcy, sw, sh); - - CGImageRelease(img); - CGColorSpaceRelease(lut); CGDataProviderRelease(src_bytes); + CGColorSpaceRelease(lut); + float s = scale_; + Fl_Surface_Device *current = Fl_Surface_Device::surface(); + // test whether osrc was created by fl_create_offscreen() + fl_begin_offscreen(osrc); // does nothing if osrc was not created by fl_create_offscreen() + if (current != Fl_Surface_Device::surface()) { // osrc was created by fl_create_offscreen() + Fl_Image_Surface *imgs = (Fl_Image_Surface*)Fl_Surface_Device::surface(); + int pw, ph; + imgs->printable_rect(&pw, &ph); + s = sw / float(pw); + fl_end_offscreen(); + } + draw_CGImage(img, x, y, w, h, srcx, srcy, sw/s, sh/s); + CGImageRelease(img); } // so a CGRect matches exactly what is denoted x,y,w,h for clipping purposes diff --git a/src/drivers/Quartz/Fl_Quartz_Image_Surface_Driver.cxx b/src/drivers/Quartz/Fl_Quartz_Image_Surface_Driver.cxx index c8d751aa1..573a5df8e 100644 --- a/src/drivers/Quartz/Fl_Quartz_Image_Surface_Driver.cxx +++ b/src/drivers/Quartz/Fl_Quartz_Image_Surface_Driver.cxx @@ -23,6 +23,7 @@ #include #include #include "Fl_Quartz_Graphics_Driver.H" +#include "../Cocoa/Fl_Cocoa_Window_Driver.H" #include class Fl_Quartz_Image_Surface_Driver : public Fl_Image_Surface_Driver { @@ -46,15 +47,21 @@ Fl_Image_Surface_Driver *Fl_Image_Surface_Driver::newImageSurfaceDriver(int w, i Fl_Quartz_Image_Surface_Driver::Fl_Quartz_Image_Surface_Driver(int w, int h, int high_res, Fl_Offscreen off) : Fl_Image_Surface_Driver(w, h, high_res, 0) { - int W = high_res ? 2*w : w; - int H = high_res ? 2*h : h; + int W = w, H = h; + float s = 1; + if (high_res) { + s = Fl_Graphics_Driver::default_driver().scale(); + Fl_Window *cw = Fl_Window::current(); + if (cw && ((Fl_Cocoa_Window_Driver*)cw->driver())->mapped_to_retina()) s *= 2; + W *= s; H *= s; + } CGColorSpaceRef lut = CGColorSpaceCreateDeviceRGB(); offscreen = off ? off : CGBitmapContextCreate(calloc(W*H,4), W, H, 8, W*4, lut, kCGImageAlphaPremultipliedLast); CGColorSpaceRelease(lut); driver(new Fl_Quartz_Graphics_Driver); CGContextTranslateCTM(offscreen, 0.5, -0.5); // as when drawing to a window if (high_res) { - CGContextScaleCTM(offscreen, 2, 2); + CGContextScaleCTM(offscreen, s, s); } CGContextSetShouldAntialias(offscreen, false); CGContextTranslateCTM(offscreen, 0, height); -- cgit v1.2.3