Skip to content

Commit

Permalink
MacOS: introduce classes FLViewLayer and FLGLViewLayer to handle laye…
Browse files Browse the repository at this point in the history
…r-backed views.

Both new classes are derived from FLView.

git-svn-id: file:///fltk/svn/fltk/branches/branch-1.4@13124 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
  • Loading branch information
Manolo Gouy authored and Manolo Gouy committed Nov 16, 2018
1 parent 015d460 commit de797c7
Showing 1 changed file with 51 additions and 38 deletions.
89 changes: 51 additions & 38 deletions src/Fl_cocoa.mm
Expand Up @@ -537,18 +537,11 @@ @interface FLView : NSView <NSTextInput
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
,NSDraggingSource
#endif
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12
, CALayerDelegate
#endif
> {
BOOL in_key_event; // YES means keypress is being processed by handleEvent
BOOL need_handle; // YES means Fl::handle(FL_KEYBOARD,) is needed after handleEvent processing
NSInteger identifier;
NSRange selectedRange;
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
@public
CGContextRef layer_data;
#endif
}
+ (void)prepareEtext:(NSString*)aString;
+ (void)concatEtext:(NSString*)aString;
Expand Down Expand Up @@ -590,13 +583,23 @@ - (NSInteger)windowLevel;
- (NSDragOperation)draggingSession:(NSDraggingSession *)session sourceOperationMaskForDraggingContext:(NSDraggingContext)context;
#endif
- (BOOL)did_view_resolution_change;
@end

#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
- (BOOL)wantsLayer;
@interface FLViewLayer : FLView // for layer-backed non-GL windows
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12
< CALayerDelegate >
#endif
{
@public
CGContextRef layer_data;
}
- (void)displayLayer:(CALayer *)layer;
- (void)viewFrameDidChange;
#endif
- (BOOL)wantsLayer;
- (void)dealloc;
@end

#endif //10_8

@implementation FLWindow
- (void)close
Expand Down Expand Up @@ -1254,8 +1257,8 @@ - (void)windowDidMove:(NSNotification *)notif
FLView *view = (FLView*)[nsw contentView];
if (views_use_CA && [view did_view_resolution_change]) {
if (!window->as_gl_window()) { // move layered non-GL window to different resolution
[view viewFrameDidChange];
[view displayLayer:[view layer]]; // useful for Mandelbrot to recreate the layer's bitmap
[(FLViewLayer*)view viewFrameDidChange];
[(FLViewLayer*)view displayLayer:[view layer]]; // useful for Mandelbrot to recreate the layer's bitmap
}
if (fl_mac_os_version < 101401 && window->parent() && window->as_gl_window() && Fl::use_high_res_GL()) {
Fl_Cocoa_Window_Driver *d = Fl_Cocoa_Window_Driver::driver(window);
Expand Down Expand Up @@ -1292,7 +1295,7 @@ - (void)windowDidResize:(NSNotification *)notif
[nsw recursivelySendToSubwindows:@selector(checkSubwindowFrame)];
if (window->as_gl_window() && Fl_X::i(window)) d->in_windowDidResize(false);
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
if (views_use_CA && [[nsw contentView] layer]) [(FLView*)[nsw contentView] viewFrameDidChange];
if (views_use_CA && !window->as_gl_window()) [(FLViewLayer*)[nsw contentView] viewFrameDidChange];
#endif
fl_unlock_function();
}
Expand Down Expand Up @@ -2160,18 +2163,19 @@ -(BOOL)handleEvent:(NSEvent*)theEvent {
that is, for apps running under 10.14 and linked to SDK 10.14.
When views_use_CA is NO, views are not supposed to be layer-backed.
Each layer-backed non-OpenGL window has a single FLView object which itself has an associated CALayer.
FLView implements displayLayer:. Consequently, FLView objects are drawn
by the displayLayer: method. An FLView manages also a member variable
Each layer-backed non-OpenGL window has a single FLViewLayer object, derived
from FLView, which itself has an associated CALayer.
FLViewLayer implements displayLayer:. Consequently, FLViewLayer objects are drawn
by the displayLayer: method. An FLViewLayer contains also a member variable
CGContextRef layer_data, a bitmap context the size of the view (double on Retina).
All Quartz drawings go to this bitmap. displayLayer: finishes by using an image copy
of the bitmap as the layer's contents. That step fills the window.
When resized or when the window flips between low/high resolution displays,
FLView receives the viewFrameDidChange message which deletes the bitmap and zeros layer_data.
This ensures the bitmap is recreated after the window was resized.
FLViewLayer receives the viewFrameDidChange message which deletes the bitmap and zeros layer_data.
This ensures the bitmap is recreated after the window was resized or changed resolution.
Each layer-backed OpenGL window has an associated FLViewGL object, derived from FLView.
FLViewGL objects are drawn by the displayLayer: method which calls drawRect:
Each layer-backed OpenGL window has an associated FLGLViewLayer object, derived from FLView.
FLGLViewLayer objects are drawn by the displayLayer: method which calls drawRect:
which draws the GL scene.
*/

Expand All @@ -2184,13 +2188,16 @@ static CGContextRef prepare_bitmap_for_layer(int w, int h ) {
return gc;
}

@interface FLViewGL : FLView // only for layered GL windows
@interface FLGLViewLayer : FLView // for layer-backed GL windows
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12
< CALayerDelegate >
#endif
- (void)displayLayer:(CALayer *)layer;
- (void)drawRect:(NSRect)rect;
- (void)viewFrameDidChange;
- (BOOL)wantsLayer;
@end

@implementation FLViewGL
@implementation FLGLViewLayer
- (void)displayLayer:(CALayer *)layer {
[self drawRect:[self frame]];
}
Expand All @@ -2213,15 +2220,17 @@ - (void)drawRect:(NSRect)rect {
}
fl_unlock_function();
}
-(void)viewFrameDidChange { ; }
-(BOOL)wantsLayer {
return YES;
}
@end
#endif //>= MAC_OS_X_VERSION_10_8

@implementation FLView
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8

@implementation FLViewLayer
- (BOOL)wantsLayer {
return views_use_CA;
return YES;
}

- (void)displayLayer:(CALayer *)layer {
// called if views are layered (but not for GL) : all drawing to window goes through this
Fl_Window *window = [(FLWindow*)[self window] getFl_Window];
Expand Down Expand Up @@ -2267,15 +2276,17 @@ - (void)displayLayer:(CALayer *)layer {

-(void)viewFrameDidChange
{
CGContextRelease(layer_data);
layer_data = NULL;
CGContextRelease(layer_data);
layer_data = NULL;
}
-(void)dealloc {
CGContextRelease(layer_data);
[super dealloc];
}
@end
#endif //>= MAC_OS_X_VERSION_10_8

@implementation FLView
- (BOOL)did_view_resolution_change {
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
if (fl_mac_os_version >= 100700) { // determine whether window is mapped to a retina display
Expand Down Expand Up @@ -2938,7 +2949,7 @@ - (NSDragOperation)draggingSession:(NSDraggingSession *)session sourceOperationM
Fl_Window_Driver::flush();
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
} else if (views_use_CA) {
FLView *view = (FLView*)[fl_xid(pWindow) contentView];
FLViewLayer *view = (FLViewLayer*)[fl_xid(pWindow) contentView];
[view displayLayer:[view layer]];
#endif
} else {
Expand Down Expand Up @@ -3110,11 +3121,13 @@ - (NSDragOperation)draggingSession:(NSDraggingSession *)session sourceOperationM
x->next = NULL;
Fl_X::first = x;
}
FLView *myview =
FLView *myview;
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
views_use_CA && w->as_gl_window() ? [FLViewGL alloc] :
if (views_use_CA) {
myview = w->as_gl_window() ? [FLGLViewLayer alloc] : [FLViewLayer alloc];
} else
#endif
[FLView alloc];
myview = [FLView alloc];
myview = [myview initWithFrame:crect];
[cw setContentView:myview];
[myview release];
Expand Down Expand Up @@ -3402,7 +3415,7 @@ - (NSDragOperation)draggingSession:(NSDraggingSession *)session sourceOperationM
}
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
if (views_use_CA) {
gc = ((FLView*)[fl_window contentView])->layer_data;
gc = ((FLViewLayer*)[fl_window contentView])->layer_data;
# ifdef FLTK_HAVE_CAIRO
// make sure the GC starts with an identity transformation matrix as do native Cocoa GC's
// because cairo may have changed it
Expand Down Expand Up @@ -4244,7 +4257,7 @@ static void write_bitmap_inside(NSBitmapImageRep *to, int to_width, NSBitmapImag
{ // capture window data for layer-based views because initWithFocusedViewRect: does not work for them
NSBitmapImageRep *bitmap = nil;
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
CGContextRef gc = ((FLView*)[fl_xid(win) contentView])->layer_data;
CGContextRef gc = ((FLViewLayer*)[fl_xid(win) contentView])->layer_data;
CGImageRef cgimg = CGBitmapContextCreateImage(gc); // requires 10.4
float s = Fl::screen_driver()->scale(0);
int resolution = Fl_Cocoa_Window_Driver::driver(win->top_window())->mapped_to_retina() ? 2 : 1;
Expand Down Expand Up @@ -4449,8 +4462,8 @@ static void nsbitmapProviderReleaseData (void *info, const void *data, size_t si
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
if (views_use_CA) {
Fl_Cocoa_Window_Driver::q_release_context();
[(FLView*)[fl_window contentView] viewFrameDidChange];
[(FLView*)[fl_window contentView] layer].contentsScale = 1.;
[(FLViewLayer*)[fl_window contentView] viewFrameDidChange];
[[fl_window contentView] layer].contentsScale = 1.;
}
#endif
[ctxt update]; // supports window resizing
Expand Down

0 comments on commit de797c7

Please sign in to comment.