From ba8abd6cad39035ebff3f708af07823b54b7cb30 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Fri, 1 May 2009 09:09:38 -0500 Subject: [PATCH] cocoa backend support for ui.pixel-formats; world-pixel-format-attributes generic --- basis/cocoa/views/views.factor | 20 ++---- basis/ui/backend/cocoa/cocoa.factor | 73 ++++++++++++++++++--- basis/ui/backend/cocoa/views/views.factor | 4 +- basis/ui/gadgets/worlds/worlds.factor | 11 +++- basis/ui/pixel-formats/pixel-formats.factor | 2 +- 5 files changed, 80 insertions(+), 30 deletions(-) diff --git a/basis/cocoa/views/views.factor b/basis/cocoa/views/views.factor index 69d0f98618..f65fddac58 100644 --- a/basis/cocoa/views/views.factor +++ b/basis/cocoa/views/views.factor @@ -42,22 +42,10 @@ CONSTANT: NSOpenGLPFAAllowOfflineRenderers 96 CONSTANT: NSOpenGLPFAVirtualScreenCount 128 CONSTANT: NSOpenGLCPSwapInterval 222 -: ( attributes -- pixelfmt ) - NSOpenGLPixelFormat -> alloc swap [ - % - NSOpenGLPFADepthSize , 16 , - ! NSOpenGLPFARendererID , kCGLRendererGenericFloatID , - ! NSOpenGLPFASupersample , - ! NSOpenGLPFASampleBuffers , 1 , - ! NSOpenGLPFASamples , 8 , - 0 , - ] int-array{ } make - -> initWithAttributes: - -> autorelease ; - -: ( class dim -- view ) - [ -> alloc 0 0 ] dip first2 - NSOpenGLPFAWindow NSOpenGLPFADoubleBuffer 2array +: ( class dim pixel-format -- view ) + [ -> alloc ] + [ [ 0 0 ] dip first2 ] + [ handle>> ] tri* -> initWithFrame:pixelFormat: dup 1 -> setPostsBoundsChangedNotifications: dup 1 -> setPostsFrameChangedNotifications: ; diff --git a/basis/ui/backend/cocoa/cocoa.factor b/basis/ui/backend/cocoa/cocoa.factor index 362305c8f7..fbc713ac9a 100755 --- a/basis/ui/backend/cocoa/cocoa.factor +++ b/basis/ui/backend/cocoa/cocoa.factor @@ -1,14 +1,15 @@ ! Copyright (C) 2006, 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: accessors math arrays assocs cocoa cocoa.application -command-line kernel memory namespaces cocoa.messages +command-line kernel memory namespaces cocoa.messages classes cocoa.runtime cocoa.subclassing cocoa.pasteboard cocoa.types cocoa.windows cocoa.classes cocoa.nibs sequences ui ui.private ui.backend ui.clipboards ui.gadgets ui.gadgets.worlds ui.backend.cocoa.views core-foundation core-foundation.run-loop core-graphics.types threads math.rectangles fry libc generalizations alien.c-types cocoa.views -combinators io.thread locals ; +combinators io.thread locals ui.pixel-formats +specialized-arrays.int literals core-graphics ; IN: ui.backend.cocoa TUPLE: handle ; @@ -20,6 +21,57 @@ C: offscreen-handle SINGLETON: cocoa-ui-backend +NSOpenGLPFA ( attribute -- NSOpenGLPFAs ) + +CONSTANT: attribute>NSOpenGLPFA-map H{ + { double-buffered { $ NSOpenGLPFADoubleBuffer } } + { stereo { $ NSOpenGLPFAStereo } } + { offscreen { $ NSOpenGLPFAOffScreen } } + { fullscreen { $ NSOpenGLPFAFullScreen } } + { windowed { $ NSOpenGLPFAWindow } } + { accelerated { $ NSOpenGLPFAAccelerated } } + { software-rendered { $ NSOpenGLPFASingleRenderer $ kCGLRendererGenericFloatID } } + { robust { $ NSOpenGLPFARobust } } + { backing-store { $ NSOpenGLPFABackingStore } } + { multisampled { $ NSOpenGLPFAMultisample } } + { supersampled { $ NSOpenGLPFASupersample } } + { sample-alpha { $ NSOpenGLPFASampleAlpha } } + { color-float { $ NSOpenGLPFAColorFloat } } + { color-bits { $ NSOpenGLPFAColorSize } } + { alpha-bits { $ NSOpenGLPFAAlphaSize } } + { accum-bits { $ NSOpenGLPFAAccumSize } } + { depth-bits { $ NSOpenGLPFADepthSize } } + { stencil-bits { $ NSOpenGLPFAStencilSize } } + { aux-buffers { $ NSOpenGLPFAAuxBuffers } } + { sample-buffers { $ NSOpenGLPFASampleBuffers } } + { samples { $ NSOpenGLPFASamples } } +} + +M: object >NSOpenGLPFA + drop { } ; +M: symbol >NSOpenGLPFA + attribute>NSOpenGLPFA-map at [ { } ] unless* ; +M: pixel-format-attribute >NSOpenGLPFA + dup class attribute>NSOpenGLPFA-map at + [ swap value>> suffix ] + [ drop { } ] if ; + +PRIVATE> + +M: cocoa-ui-backend (make-pixel-format) + [ >NSOpenGLPFA ] map concat >int-array + NSOpenGLPixelFormat -> alloc swap -> initWithAttributes: ; + +M: cocoa-ui-backend (free-pixel-format) + -> release ; + +M: cocoa-ui-backend (pixel-format-attribute) + attribute>NSOpenGLPFA-map at + [ first 0 [ swap 0 -> getValues:forAttribute:forVirtualScreen: ] keep *int ] + [ f ] if* ; + TUPLE: pasteboard handle ; C: pasteboard @@ -70,7 +122,7 @@ M: cocoa-ui-backend fullscreen* ( world -- ? ) handle>> view>> -> isInFullScreenMode zero? not ; M:: cocoa-ui-backend (open-window) ( world -- ) - world dim>> :> view + [ [ dim>> ] dip ] with-world-pixel-format :> view view world world>NSRect :> window view -> release world view register-window @@ -97,18 +149,19 @@ M: cocoa-ui-backend raise-window* ( world -- ) ] when* ; : pixel-size ( pixel-format -- size ) - 0 [ NSOpenGLPFAColorSize 0 -> getValues:forAttribute:forVirtualScreen: ] - keep *int -3 shift ; + color-bits pixel-format-attribute -3 shift ; : offscreen-buffer ( world pixel-format -- alien w h pitch ) [ dim>> first2 ] [ pixel-size ] bi* { [ * * malloc ] [ 2drop ] [ drop nip ] [ nip * ] } 3cleave ; -: gadget-offscreen-context ( world -- context buffer ) - NSOpenGLPFAOffScreen 1array - [ nip NSOpenGLContext -> alloc swap f -> initWithFormat:shareContext: dup ] - [ offscreen-buffer ] 2bi - 4 npick [ -> setOffScreen:width:height:rowbytes: ] dip ; +:: gadget-offscreen-context ( world -- context buffer ) + world world-pixel-format-attributes offscreen suffix + [ + :> pf + NSOpenGLContext -> alloc pf handle>> f -> initWithFormat:shareContext: + dup world pf offscreen-buffer -> setOffScreen:width:height:rowbytes: + ] with-disposal ; M: cocoa-ui-backend (open-offscreen-buffer) ( world -- ) dup gadget-offscreen-context >>handle drop ; diff --git a/basis/ui/backend/cocoa/views/views.factor b/basis/ui/backend/cocoa/views/views.factor index 602c9bec73..4a16e3bd37 100644 --- a/basis/ui/backend/cocoa/views/views.factor +++ b/basis/ui/backend/cocoa/views/views.factor @@ -365,8 +365,8 @@ CLASS: { -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 CGLSetParameter drop ; -: ( dim -- view ) - FactorView swap [ sync-refresh-to-screen ] keep ; +: ( dim pixel-format -- view ) + [ FactorView ] 2dip [ sync-refresh-to-screen ] keep ; : save-position ( world window -- ) -> frame CGRect-top-left 2array >>window-loc drop ; diff --git a/basis/ui/gadgets/worlds/worlds.factor b/basis/ui/gadgets/worlds/worlds.factor index a186de7670..d4162fa630 100644 --- a/basis/ui/gadgets/worlds/worlds.factor +++ b/basis/ui/gadgets/worlds/worlds.factor @@ -4,7 +4,7 @@ USING: accessors arrays assocs continuations kernel math models namespaces opengl opengl.textures sequences io combinators combinators.short-circuit fry math.vectors math.rectangles cache ui.gadgets ui.gestures ui.render ui.backend ui.gadgets.tracks -ui.commands ; +ui.commands ui.pixel-formats destructors ; IN: ui.gadgets.worlds TUPLE: world < track @@ -149,3 +149,12 @@ M: world handle-gesture ( gesture gadget -- ? ) : close-global ( world global -- ) [ get-global find-world eq? ] keep '[ f _ set-global ] when ; + +GENERIC: world-pixel-format-attributes ( world -- attributes ) + +M: world world-pixel-format-attributes + { windowed double-buffered T{ depth-bits { value 16 } } } ; + +: with-world-pixel-format ( world quot -- ) + [ dup world-pixel-format-attributes ] + dip with-disposal ; inline diff --git a/basis/ui/pixel-formats/pixel-formats.factor b/basis/ui/pixel-formats/pixel-formats.factor index 09450f2c72..66de98ea1c 100644 --- a/basis/ui/pixel-formats/pixel-formats.factor +++ b/basis/ui/pixel-formats/pixel-formats.factor @@ -1,7 +1,7 @@ USING: destructors math ui.backend ; IN: ui.pixel-formats -SINGLETONS: +SYMBOLS: double-buffered stereo offscreen