Skip to content

Commit

Permalink
MacOS: simpler implementation of extra code necessary at first displa…
Browse files Browse the repository at this point in the history
…y of layer-backed GL windows

git-svn-id: file:///fltk/svn/fltk/branches/branch-1.4@13123 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
  • Loading branch information
Manolo Gouy authored and Manolo Gouy committed Nov 16, 2018
1 parent d312284 commit 015d460
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 31 deletions.
9 changes: 9 additions & 0 deletions src/Fl_Gl_Window.cxx
Expand Up @@ -524,6 +524,15 @@ void Fl_Cocoa_Gl_Window_Driver::after_show(int need_redraw) {
if(need_redraw) pWindow->redraw();//necessary only after creation of a top-level GL window
}

int Fl_Cocoa_Gl_Window_Driver::flush_begin(char& valid_f_) {
Fl_Cocoa_Window_Driver *d = Fl_Cocoa_Window_Driver::driver(pWindow);
if (d->wait_for_expose_value) { // 1st drawing of layer-backed GL window
Fl_Cocoa_Window_Driver::GLcontext_update(pWindow->context()); // layer-backed GL windows may be empty without this
d->wait_for_expose_value = 0;
}
return 0;
}

float Fl_Cocoa_Gl_Window_Driver::pixels_per_unit()
{
int retina = (fl_mac_os_version >= 100700 && Fl::use_high_res_GL() && Fl_X::i(pWindow) &&
Expand Down
1 change: 1 addition & 0 deletions src/Fl_Gl_Window_Driver.H
Expand Up @@ -109,6 +109,7 @@ class Fl_Cocoa_Gl_Window_Driver : public Fl_Gl_Window_Driver {
virtual void after_show(int need_redraw);
virtual int mode_(int m, const int *a);
virtual void make_current_before();
virtual int flush_begin(char& valid_f);
virtual void swap_buffers();
virtual void resize(int is_a_resize, int w, int h);
virtual char swap_type();
Expand Down
44 changes: 13 additions & 31 deletions src/Fl_cocoa.mm
Expand Up @@ -529,12 +529,6 @@ - (NSPoint)convertBaseToScreen:(NSPoint)aPoint;
#endif
@end

#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
typedef union {
CGContextRef gc;
NSOpenGLContext *context;
} bitmap_or_gl_context;
#endif

@interface FLView : NSView <NSTextInput
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
Expand All @@ -553,7 +547,7 @@ @interface FLView : NSView <NSTextInput
NSRange selectedRange;
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
@public
bitmap_or_gl_context layer_data;
CGContextRef layer_data;
#endif
}
+ (void)prepareEtext:(NSString*)aString;
Expand Down Expand Up @@ -2169,16 +2163,16 @@ -(BOOL)handleEvent:(NSEvent*)theEvent {
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
CGContextRef layer_data.gc, a bitmap context the size of the view (double on Retina).
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.gc.
FLView receives the viewFrameDidChange message which deletes the bitmap and zeros layer_data.
This ensures the bitmap is recreated after the window was resized.

Each layer-backed OpenGL window has an associated FLViewGL object, derived from FLView.
FLViewGL objects are drawn by the displayLayer: method which calls drawRect:
which draws the GL scene. Member variable layer_data.context contains the OpenGL context.
which draws the GL scene.
*/

#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
Expand All @@ -2194,7 +2188,6 @@ @interface FLViewGL : FLView // only for layered GL windows
- (void)displayLayer:(CALayer *)layer;
- (void)drawRect:(NSRect)rect;
- (void)viewFrameDidChange;
- (void)dealloc;
@end

@implementation FLViewGL
Expand All @@ -2209,8 +2202,6 @@ - (void)drawRect:(NSRect)rect {
if (d->wait_for_expose_value) {
// 1st drawing of GL window
[self did_view_resolution_change];
[layer_data.context update]; // GL window may be empty without this
d->wait_for_expose_value = 0;
}
through_drawRect = YES;
window->clear_damage(FL_DAMAGE_ALL);
Expand All @@ -2223,10 +2214,6 @@ - (void)drawRect:(NSRect)rect {
fl_unlock_function();
}
-(void)viewFrameDidChange { ; }
-(void)dealloc {
layer_data.context = NULL;
[super dealloc];
}
@end
#endif //>= MAC_OS_X_VERSION_10_8

Expand All @@ -2248,15 +2235,15 @@ - (void)displayLayer:(CALayer *)layer {
rect.size.width/scale, rect.size.height/scale);
[self viewFrameDidChange];
}
if (!layer_data.gc) { // runs when window is created, resized, changed screen resolution
if (!layer_data) { // runs when window is created, resized, changed screen resolution
layer.bounds = NSRectToCGRect(rect);
d->wait_for_expose_value = 0;
[self did_view_resolution_change];
if (d->mapped_to_retina()) {
rect.size.width *= 2; rect.size.height *= 2;
layer.contentsScale = 2.;
} else layer.contentsScale = 1.;
layer_data.gc = prepare_bitmap_for_layer(rect.size.width, rect.size.height);
layer_data = prepare_bitmap_for_layer(rect.size.width, rect.size.height);
Fl_X *i = Fl_X::i(window);
if ( i->region ) {
Fl_Graphics_Driver::default_driver().XDestroyRegion(i->region);
Expand All @@ -2270,8 +2257,8 @@ - (void)displayLayer:(CALayer *)layer {
Fl_Cocoa_Window_Driver::q_release_context();
through_drawRect = NO;
window->clear_damage();
if (layer_data.gc) {
CGImageRef cgimg = CGBitmapContextCreateImage(layer_data.gc); // requires 10.4
if (layer_data) {
CGImageRef cgimg = CGBitmapContextCreateImage(layer_data); // requires 10.4
layer.contents = (id)cgimg;
CGImageRelease(cgimg);
}
Expand All @@ -2280,11 +2267,11 @@ - (void)displayLayer:(CALayer *)layer {

-(void)viewFrameDidChange
{
CGContextRelease(layer_data.gc);
layer_data.gc = NULL;
CGContextRelease(layer_data);
layer_data = NULL;
}
-(void)dealloc {
CGContextRelease(layer_data.gc);
CGContextRelease(layer_data);
[super dealloc];
}
#endif //>= MAC_OS_X_VERSION_10_8
Expand Down Expand Up @@ -2913,11 +2900,6 @@ - (NSDragOperation)draggingSession:(NSDraggingSession *)session sourceOperationM
addr(view, @selector(setWantsBestResolutionOpenGLSurface:), YES);
}
[context setView:view];
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
if (views_use_CA
&& !view->layer_data.gc // avoid use of gl_start()/gl_finish()
) view->layer_data.context = context;
#endif
}
return context;
}
Expand Down Expand Up @@ -3420,7 +3402,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;
gc = ((FLView*)[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 @@ -4262,7 +4244,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.gc;
CGContextRef gc = ((FLView*)[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

0 comments on commit 015d460

Please sign in to comment.