changeset 4695:c6eecbe2396f calayer

Some cleanup in CAOpenGLLayer prototype (including extracting layer code into CGLLayer.h|m and removing stale code)
author dcherepanov
date Thu, 01 Sep 2011 13:08:27 +0400
parents 27061fc5ad63
children 5fc2e84e985a
files make/sun/lwawt/FILES_c_macosx.gmk make/sun/lwawt/FILES_c_macosx.gmk.all src/macosx/classes/sun/java2d/opengl/CGLGraphicsConfig.java src/macosx/classes/sun/java2d/opengl/CGLRenderQueue.java src/macosx/classes/sun/java2d/opengl/CGLSurfaceData.java src/macosx/classes/sun/lwawt/macosx/CPlatformView.java src/macosx/native/sun/awt/AWTView.h src/macosx/native/sun/awt/AWTView.m src/macosx/native/sun/awt/ThreadUtilities.m src/macosx/native/sun/java2d/opengl/CGLGraphicsConfig.m src/macosx/native/sun/java2d/opengl/CGLLayer.h src/macosx/native/sun/java2d/opengl/CGLLayer.m src/macosx/native/sun/java2d/opengl/CGLSurfaceData.m src/share/classes/sun/java2d/opengl/OGLRenderQueue.java src/share/classes/sun/java2d/pipe/BufferedContext.java src/share/native/sun/java2d/opengl/OGLSurfaceData.h test/experimental/layers/ButtonTest.java
diffstat 17 files changed, 191 insertions(+), 270 deletions(-) [+]
line wrap: on
line diff
--- a/make/sun/lwawt/FILES_c_macosx.gmk	Tue Aug 30 13:10:26 2011 +0400
+++ b/make/sun/lwawt/FILES_c_macosx.gmk	Thu Sep 01 13:08:27 2011 +0400
@@ -27,6 +27,7 @@
 	CFRetainedResource.m \
 	CGLGraphicsConfig.m \
 	CGLSurfaceData.m \
+        CGLLayer.m \
 	CGraphicsConfig.m \
 	CGraphicsDevice.m \
 	CGraphicsEnv.m \
--- a/make/sun/lwawt/FILES_c_macosx.gmk.all	Tue Aug 30 13:10:26 2011 +0400
+++ b/make/sun/lwawt/FILES_c_macosx.gmk.all	Thu Sep 01 13:08:27 2011 +0400
@@ -26,6 +26,7 @@
 FILES_objc = \
 	CGLGraphicsConfig.m \
 	CGLSurfaceData.m \
+        CGLLayer.m \
 	CGraphicsConfig.m \
 	CGraphicsDevice.m \
 	CGraphicsEnv.m \
--- a/src/macosx/classes/sun/java2d/opengl/CGLGraphicsConfig.java	Tue Aug 30 13:10:26 2011 +0400
+++ b/src/macosx/classes/sun/java2d/opengl/CGLGraphicsConfig.java	Thu Sep 01 13:08:27 2011 +0400
@@ -73,7 +73,6 @@
     private static ImageCapabilities imageCaps = new CGLImageCaps();
 
     private int pixfmt;
-    private long nsContextPtr = 0;
     private BufferCapabilities bufferCaps;
     private long pConfigInfo;
     private ContextCapabilities oglCaps;
@@ -85,7 +84,6 @@
     private static native long getCGLConfigInfo(int screennum, int visualnum,
                                                 int swapInterval);
     private static native int getOGLCapabilities(long configInfo);
-    private static native long getNSContextPtr(long configInfo);
         
     static {
         cglAvailable = initCGL();
@@ -100,7 +98,6 @@
         this.pConfigInfo = configInfo;
         this.oglCaps = oglCaps;
         context = new OGLContext(OGLRenderQueue.getInstance(), this);
-        this.nsContextPtr = getNSContextPtr(configInfo);
         
         // add a record to the Disposer so that we destroy the native
         // CGLGraphicsConfigInfo data when this object goes away
@@ -196,10 +193,6 @@
         return pConfigInfo;
     }
 
-    public long getNSContextPtr() {
-        return nsContextPtr;
-    }    
-    
     /**
      * {@inheritDoc}
      *
--- a/src/macosx/classes/sun/java2d/opengl/CGLRenderQueue.java	Tue Aug 30 13:10:26 2011 +0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,37 +0,0 @@
-package sun.java2d.opengl;
-
-import sun.java2d.SurfaceData;
-import sun.java2d.opengl.CGLSurfaceData.CGLWindowSurfaceData;
-import sun.java2d.pipe.RenderBuffer;
-
-import java.util.List;
-import java.util.ArrayList;
-
-import sun.misc.Unsafe;
-
-public class CGLRenderQueue extends OGLRenderQueue
-{    
-    private static CGLSurfaceData currentSurfaceData = null;
-    
-    public static void setCurrentSurfaceData(SurfaceData surfaceData) {
-        currentSurfaceData = (CGLSurfaceData)surfaceData;
-    }
-
-    @Override
-    protected void flushBuffer(long buf, int limit) {
-        super.flushBuffer(buf, limit);                
-
-        if (currentSurfaceData != null) {
-            currentSurfaceData.setNeedsDisplay();
-        }
-    }
-
-    @Override
-    protected void invokeTask(Runnable task) {        
-        super.invokeTask(task);
-        
-        if (currentSurfaceData != null) {
-            currentSurfaceData.setNeedsDisplay();
-        }
-    }
-}
--- a/src/macosx/classes/sun/java2d/opengl/CGLSurfaceData.java	Tue Aug 30 13:10:26 2011 +0400
+++ b/src/macosx/classes/sun/java2d/opengl/CGLSurfaceData.java	Thu Sep 01 13:08:27 2011 +0400
@@ -37,13 +37,8 @@
 import sun.java2d.SunGraphics2D;
 import sun.java2d.SurfaceData;
 
-import sun.java2d.pipe.RenderBuffer;
-
 import sun.lwawt.macosx.CPlatformView;
 
-import java.util.List;
-import java.util.ArrayList;
-
 public abstract class CGLSurfaceData extends OGLSurfaceData {
 
     protected CPlatformView pView;
@@ -72,14 +67,6 @@
         initOps(pConfigInfo, pPeerData, 0, 0);
     }
     
-    // Asks layer to re-cache its content
-    // called on the renderer thread
-    void setNeedsDisplay() {
-        if (pView != null) {
-            pView.setNeedsDisplay(true);
-        }
-    }
-
     @Override //SurfaceData
     public GraphicsConfiguration getDeviceConfiguration() {
         return graphicsConfig;
--- a/src/macosx/classes/sun/lwawt/macosx/CPlatformView.java	Tue Aug 30 13:10:26 2011 +0400
+++ b/src/macosx/classes/sun/lwawt/macosx/CPlatformView.java	Thu Sep 01 13:08:27 2011 +0400
@@ -34,11 +34,8 @@
 import sun.lwawt.LWWindowPeer;
 import sun.lwawt.macosx.event.NSEvent;
 
-import sun.java2d.opengl.CGLGraphicsConfig;
-import sun.java2d.opengl.CGLSurfaceData;
-
 public class CPlatformView extends CFRetainedResource {
-    private native long nativeCreateView(int x, int y, int width, int height, long nsContextPtr);
+    private native long nativeCreateView(int x, int y, int width, int height);
     
     private LWWindowPeer peer;
     private SurfaceData surfaceData;
@@ -47,27 +44,15 @@
         super(0, true);
     }
     
-    private long getNSContextPtr() {
-        return ((CGLGraphicsConfig)peer.getGraphicsConfiguration()).getNSContextPtr();        
-    }
-    
     public void initialize(LWWindowPeer peer) {
         this.peer = peer;
-        setPtr(nativeCreateView(0, 0, 0, 0, getNSContextPtr()));
+        setPtr(nativeCreateView(0, 0, 0, 0));
     }
     
     public long getAWTView() {
         return ptr;
     }
 
-    // TODO: consider using CWrapper
-    private native void setNeedsDisplay(long nsViewPtr, boolean flag);
-    
-    public void setNeedsDisplay(boolean flag) {
-        setNeedsDisplay(ptr, flag);
-        // CWrapper.NSView.setNeedsDisplay(ptr, flag);
-    }
-	
     /*
      * All coordinates passed to the method should be based on the origin being in the bottom-left corner (standard
      * Cocoa coordinates).
--- a/src/macosx/native/sun/awt/AWTView.h	Tue Aug 30 13:10:26 2011 +0400
+++ b/src/macosx/native/sun/awt/AWTView.h	Thu Sep 01 13:08:27 2011 +0400
@@ -45,14 +45,13 @@
 
     // mask used to set the correct modifiers for java mouseEntered/mouseExited
     NSInteger mouseDownButtonMask;
-
-    // intermediate buffer
-    GLuint textureID;
+    
+    id cglLayer; // is a sublayer of view.layer
 }
 
-@property (readwrite, assign) GLuint textureID;
+@property (nonatomic, retain) id cglLayer;
 
-- (id) initWithRect:(NSRect) rect platformView:(jobject)cPlatformView context:(NSOpenGLContext*)nsContext;
+- (id) initWithRect:(NSRect) rect platformView:(jobject)cPlatformView;
 - (void) deliverJavaMouseEvent: (NSEvent *) event;
 - (void) resetTrackingRect;
 - (void) deliverJavaKeyEventHelper: (NSEvent *) event;
--- a/src/macosx/native/sun/awt/AWTView.m	Tue Aug 30 13:10:26 2011 +0400
+++ b/src/macosx/native/sun/awt/AWTView.m	Thu Sep 01 13:08:27 2011 +0400
@@ -32,97 +32,8 @@
 #import "LWCToolkit.h"
 #import "JavaComponentAccessibility.h"
 #import "JavaTextAccessibility.h"
-#import <OpenGL/OpenGL.h>
-#import <Cocoa/Cocoa.h>
-#import <QuartzCore/QuartzCore.h>
+#import "CGLLayer.h"
 
-extern NSOpenGLPixelFormat *sharedPixelFormat;
-extern NSOpenGLContext *sharedContext;
-
-// TODO: extract the layers code into separate file
-//       to macosx/native/sun/java2d/opengl ?
-
-@interface AWTCAOpenGLLayer : CAOpenGLLayer
-{
-    jobject m_cPlatformView;
-    NSOpenGLContext *m_nsContext;
-	AWTView *view;
-}
-- (void) setPlatformView:(jobject)cPlatformView;
-- (void) setNSContext: (NSOpenGLContext *)nsContext;
-- (void) setAWTView: (AWTView *)aView;
-- (void) _blitTexture;
-@end
-
-@implementation AWTCAOpenGLLayer
-
-- (void) setPlatformView: (jobject) cPlatformView
-{
-    m_cPlatformView = cPlatformView;
-}
-
-- (void) setNSContext: (NSOpenGLContext *)nsContext
-{
-    m_nsContext = nsContext;
-}
-
-- (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask {
-    return sharedPixelFormat.CGLPixelFormatObj;    
-}
- 
-- (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat {
-    CGLContextObj contextObj = NULL;
-    CGLCreateContext(pixelFormat, sharedContext.CGLContextObj, &contextObj);
-    return contextObj;
-}
-
-- (void) setAWTView: (AWTView *) aView
-{
-    view = aView;
-}
-
-// use texture as src and blit it to the layer
-- (void) _blitTexture
-{
-    if (view.textureID == 0)
-        return;
-    
-    glEnable(GL_TEXTURE_2D);    
-    glBindTexture(GL_TEXTURE_2D, view.textureID);
-
-    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); // srccopy
-
-    glBegin(GL_QUADS);
-    glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
-    glTexCoord2f(1.0f, 0.0f); glVertex2f( 1.0f, -1.0f);
-    glTexCoord2f(1.0f, 1.0f); glVertex2f( 1.0f,  1.0f);
-    glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f,  1.0f);
-    glEnd();
-    
-    glBindTexture(GL_TEXTURE_2D, 0);
-    glDisable(GL_TEXTURE_2D);
-}
-
--(void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
-{
-    AWT_ASSERT_APPKIT_THREAD;
-       
-    // Set the current context to the one given to us.
-    CGLSetCurrentContext(glContext);
-
-    // Updates viewport to window size.
-    NSRect bounds = [[view window] frame];
-    glViewport(0, 0, bounds.size.width, bounds.size.height);
-
-    [self _blitTexture];
-
-	// Call super to finalize the drawing. By default all it does is call glFlush().
-    [super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:timeInterval displayTime:timeStamp];
-
-    CGLSetCurrentContext(NULL);
-}
-
-@end
 
 @interface AWTView()
 @property (retain) CDropTarget *_dropTarget;
@@ -134,44 +45,27 @@
 
 @synthesize _dropTarget;
 @synthesize _dragSource;
-@synthesize textureID;
+@synthesize cglLayer;
 
 // Note: Must be called on main (AppKit) thread only
 - (id) initWithRect: (NSRect) rect
        platformView: (jobject) cPlatformView
-       context: (NSOpenGLContext*) nsContext
 {
 AWT_ASSERT_APPKIT_THREAD;
     // Initialize ourselves
     self = [super initWithFrame: rect];
     if (self == nil) return self;
 
-    textureID = 0; // texture will be created by rendering pipe
     m_cPlatformView = cPlatformView;
-    
-    AWTCAOpenGLLayer *layer = [AWTCAOpenGLLayer layer];
-    [layer setPlatformView: cPlatformView];
-    [layer setNSContext: nsContext];
-    [layer setAWTView: self];
 
-    // NOTE: async=YES means that the layer is re-cached periodically
-    layer.asynchronous = FALSE;    
-    layer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
+    cglLayer = [CGLLayer layer];
     
     [self setWantsLayer: YES];
-    // TODO: do we really want to add our layer as a sublayer?
-    [self.layer addSublayer:layer];
+    [self.layer addSublayer: (CALayer *)cglLayer];
 
     return self;
 }
 
-//	Override setFrameSize
-//- (void)setFrameSize:(NSSize)newSize {
-//	NSLog(@"size : %@", NSStringFromSize(newSize));
-//	
-//	[super setFrameSize:newSize];
-//}
-
 - (void) dealloc {
 AWT_ASSERT_APPKIT_THREAD;
     
@@ -793,7 +687,7 @@
  */
 JNIEXPORT jlong JNICALL
 Java_sun_lwawt_macosx_CPlatformView_nativeCreateView
-(JNIEnv *env, jobject obj, jint originX, jint originY, jint width, jint height, jlong nsContextPtr)
+(JNIEnv *env, jobject obj, jint originX, jint originY, jint width, jint height)
 {
     __block AWTView *newView = nil;
     
@@ -807,8 +701,7 @@
         AWT_ASSERT_APPKIT_THREAD;
         
         AWTView *view = [[AWTView alloc] initWithRect:rect
-                                         platformView:cPlatformView
-                                         context:jlong_to_ptr(nsContextPtr)];
+                                         platformView:cPlatformView];
         CFRetain(view);
         [view release]; // GC
         
@@ -819,30 +712,3 @@
     
     return ptr_to_jlong(newView);
 }
-
-/*
- * Class:     sun_lwawt_macosx_CPlatformWindow
- * Method:    setNeedsDisplay
- * Signature: (JZ)V
- */
-JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformView_setNeedsDisplay
-(JNIEnv *env, jclass clazz, jlong viewPtr, jboolean flag)
-{
-    JNF_COCOA_ENTER(env);
-    AWT_ASSERT_NOT_APPKIT_THREAD;
-
-    AWTView *view = OBJC(viewPtr);
-	
-    [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
-        AWT_ASSERT_APPKIT_THREAD;
-
-        // NOTE: looks like calling view doesn't re-cache sublayers
-        // [view setNeedsDisplay: YES];
-
-        NSArray *layers = view.layer.sublayers;
-        CALayer *layer = (CALayer *)[layers objectAtIndex:0];
-        [layer setNeedsDisplay];
-    }];
-
-    JNF_COCOA_EXIT(env);
-}
--- a/src/macosx/native/sun/awt/ThreadUtilities.m	Tue Aug 30 13:10:26 2011 +0400
+++ b/src/macosx/native/sun/awt/ThreadUtilities.m	Thu Sep 01 13:08:27 2011 +0400
@@ -215,10 +215,7 @@
 }
 
 + (JNIEnv*)getAppKitJNIEnv {
-    AWT_ASSERT_APPKIT_THREAD;
-	
-    //NSLog(@"%@", [NSThread callStackSymbols]);
-	
+    AWT_ASSERT_APPKIT_THREAD;	
     if (appKitEnv == NULL) {
         (*jvm)->AttachCurrentThreadAsDaemon(jvm, (void **)&appKitEnv, NULL);
     }
--- a/src/macosx/native/sun/java2d/opengl/CGLGraphicsConfig.m	Tue Aug 30 13:10:26 2011 +0400
+++ b/src/macosx/native/sun/java2d/opengl/CGLGraphicsConfig.m	Thu Sep 01 13:08:27 2011 +0400
@@ -377,16 +377,3 @@
         return cglinfo->context->caps;
     }
 }
-
-JNIEXPORT jlong JNICALL
-Java_sun_java2d_opengl_CGLGraphicsConfig_getNSContextPtr
-    (JNIEnv *env, jclass cglgc, jlong configInfo)
-{
-    J2dTraceLn(J2D_TRACE_INFO, "CGLGraphicsConfig_getNSContextPtr");
-    CGLGraphicsConfigInfo *cglinfo =
-        (CGLGraphicsConfigInfo *)jlong_to_ptr(configInfo);
-
-    OGLContext *oglc = (OGLContext*)cglinfo->context;
-    CGLCtxInfo *ctxinfo = (CGLCtxInfo *)oglc->ctxInfo;
-    return ctxinfo->context;
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/macosx/native/sun/java2d/opengl/CGLLayer.h	Thu Sep 01 13:08:27 2011 +0400
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+#ifndef CGLLayer_h_Included
+#define CGLLayer_h_Included
+
+#import "AWTView.h"
+
+@interface CGLLayer : CAOpenGLLayer
+{    
+    // intermediate buffer
+    GLuint textureID;    
+    float textureWidth;
+    float textureHeight;
+}
+
+@property (readwrite, assign) GLuint textureID;
+@property (readwrite, assign) float textureWidth;
+@property (readwrite, assign) float textureHeight;
+
+- (void) _blitTexture;
+@end
+
+#endif /* CGLLayer_h_Included */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/macosx/native/sun/java2d/opengl/CGLLayer.m	Thu Sep 01 13:08:27 2011 +0400
@@ -0,0 +1,103 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+#import "CGLLayer.h"
+#import "ThreadUtilities.h"
+
+extern NSOpenGLPixelFormat *sharedPixelFormat;
+extern NSOpenGLContext *sharedContext;
+
+@implementation CGLLayer
+
+@synthesize textureID;
+@synthesize textureWidth;
+@synthesize textureHeight;
+
+- (id)init
+{
+AWT_ASSERT_APPKIT_THREAD;
+    // Initialize ourselves
+    self = [super init];
+    if (self == nil) return self;
+
+    // NOTE: async=YES means that the layer is re-cached periodically
+    self.asynchronous = FALSE;
+    self.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;        
+    
+    textureID = 0; // texture will be created by rendering pipe
+
+    return self;
+}
+
+- (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask {
+    return sharedPixelFormat.CGLPixelFormatObj;    
+}
+
+- (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat {
+    CGLContextObj contextObj = NULL;
+    CGLCreateContext(pixelFormat, sharedContext.CGLContextObj, &contextObj);
+    return contextObj;
+}
+
+// use texture (intermediate buffer) as src and blit it to the layer
+- (void) _blitTexture
+{    
+    if (textureID == 0)
+        return;
+    
+    glEnable(GL_TEXTURE_2D);    
+    glBindTexture(GL_TEXTURE_2D, textureID);
+    
+    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); // srccopy
+    
+    glBegin(GL_QUADS);
+    glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
+    glTexCoord2f(1.0f, 0.0f); glVertex2f( 1.0f, -1.0f);
+    glTexCoord2f(1.0f, 1.0f); glVertex2f( 1.0f,  1.0f);
+    glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f,  1.0f);
+    glEnd();
+    
+    glBindTexture(GL_TEXTURE_2D, 0);
+    glDisable(GL_TEXTURE_2D);
+}
+
+-(void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
+{
+    AWT_ASSERT_APPKIT_THREAD;
+    
+    // Set the current context to the one given to us.
+    CGLSetCurrentContext(glContext);
+     
+    glViewport(0, 0, textureWidth, textureHeight);
+    
+    [self _blitTexture];
+    
+    // Call super to finalize the drawing. By default all it does is call glFlush().
+    [super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:timeInterval displayTime:timeStamp];
+    
+    CGLSetCurrentContext(NULL);
+}
+
+@end
--- a/src/macosx/native/sun/java2d/opengl/CGLSurfaceData.m	Tue Aug 30 13:10:26 2011 +0400
+++ b/src/macosx/native/sun/java2d/opengl/CGLSurfaceData.m	Thu Sep 01 13:08:27 2011 +0400
@@ -33,7 +33,9 @@
 #import "OGLRenderQueue.h"
 #import "CGLGraphicsConfig.h"
 #import "CGLSurfaceData.h"
-#import "AWTView.h"
+#import "CGLLayer.h"
+#import "ThreadUtilities.h"
+
 
 /**
  * The methods in this file implement the native windowing system specific
@@ -187,7 +189,7 @@
     CGLCtxInfo *ctxinfo = (CGLCtxInfo *)oglc->ctxInfo;
 
 JNF_COCOA_ENTER(env);
-    
+
     // avoid changing the context's target view whenever possible, since
     // calling setView causes flickering; as long as our context is current
     // to some view, it's not necessary to switch to the scratch surface
@@ -246,6 +248,17 @@
         j2d_glFlush();
     }
 
+#if USE_INTERMEDIATE_BUFFER
+    // looks like we should also explicitly re-cache layers between context changes
+    [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
+        AWT_ASSERT_APPKIT_THREAD;
+        
+        AWTView *view = dstCGLOps->peerData;
+        [view.cglLayer setNeedsDisplay];
+    }];
+
+#endif
+    
     if (dstOps->drawableType == OGLSD_FBOBJECT) {
         // first make sure we have a current context (if the context isn't
         // already current to some drawable, we will make it current to
@@ -362,15 +375,18 @@
         OGLSurfaceData_initFBObject(env, NULL, ptr_to_jlong(oglsdo), oglsdo->isOpaque,
                                     isTexNonPow2Available(cglsdo->configInfo),
                                     isTexRectAvailable(cglsdo->configInfo),
-                                    oglsdo->width, oglsdo->height);    
+                                    oglsdo->width, oglsdo->height);
     
-    // NOTE: WINDOW type is reused for offscreen rendering
+    // NOTE: OGLSD_WINDOW type is reused for offscreen rendering
     //       when intermediate buffer is enabled
     oglsdo->drawableType = OGLSD_WINDOW;
 
     AWTView *view = cglsdo->peerData;
-    [view setTextureID: (GLuint)oglsdo->textureID];
-    
+    CGLLayer *layer = (CGLLayer *)view.cglLayer;
+    layer.textureID = oglsdo->textureID;
+    layer.textureWidth = oglsdo->width;
+    layer.textureHeight = oglsdo->height;
+
     return result;
 }
 
--- a/src/share/classes/sun/java2d/opengl/OGLRenderQueue.java	Tue Aug 30 13:10:26 2011 +0400
+++ b/src/share/classes/sun/java2d/opengl/OGLRenderQueue.java	Thu Sep 01 13:08:27 2011 +0400
@@ -40,7 +40,7 @@
     private static OGLRenderQueue theInstance;
     private final QueueFlusher flusher;
 
-    protected OGLRenderQueue() {
+    private OGLRenderQueue() {
         flusher = new QueueFlusher();
     }
 	
@@ -51,9 +51,7 @@
      */
     public static synchronized OGLRenderQueue getInstance() {
         if (theInstance == null) {
-            // TODO: factory?
-            theInstance = new CGLRenderQueue();
-//            theInstance = new OGLRenderQueue();
+            theInstance = new OGLRenderQueue();
         }
         return theInstance;
     }
@@ -131,7 +129,7 @@
         }
     }
 
-    protected native void flushBuffer(long buf, int limit);
+    private native void flushBuffer(long buf, int limit);
 
     private void flushBuffer() {
         // assert lock.isHeldByCurrentThread();
@@ -146,10 +144,6 @@
         refSet.clear();
     }
 
-    protected void invokeTask(Runnable task) {
-        task.run();
-    }
-    
 	private class QueueFlusher extends Thread {
         private boolean needsFlush;
         private Runnable task;
@@ -223,8 +217,7 @@
                     flushBuffer();
                     // if there's a task, invoke that now as well
                     if (task != null) {
-                        // task.run();
-                        invokeTask(task);
+                        task.run();
                     }
                 } catch (Error e) {
                     error = e;
--- a/src/share/classes/sun/java2d/pipe/BufferedContext.java	Tue Aug 30 13:10:26 2011 +0400
+++ b/src/share/classes/sun/java2d/pipe/BufferedContext.java	Thu Sep 01 13:08:27 2011 +0400
@@ -36,9 +36,6 @@
 import sun.java2d.loops.XORComposite;
 import static sun.java2d.pipe.BufferedOpCodes.*;
 import static sun.java2d.pipe.BufferedRenderPipe.BYTES_PER_SPAN;
-import sun.java2d.opengl.CGLRenderQueue;
-import sun.java2d.SurfaceData;
-
 
 /**
  * Base context class for managing state in a single-threaded rendering
@@ -327,9 +324,6 @@
         buf.putInt(SET_SURFACES);
         buf.putLong(srcData.getNativeOps());
         buf.putLong(dstData.getNativeOps());
-        
-        // TODO: any better way to split "global" render buffer
-        CGLRenderQueue.setCurrentSurfaceData((SurfaceData)srcData);
     }
 
     private void resetClip() {
--- a/src/share/native/sun/java2d/opengl/OGLSurfaceData.h	Tue Aug 30 13:10:26 2011 +0400
+++ b/src/share/native/sun/java2d/opengl/OGLSurfaceData.h	Thu Sep 01 13:08:27 2011 +0400
@@ -291,6 +291,7 @@
 jboolean OGLSD_InitFBObject(GLuint *fbobjectID, GLuint *depthID,
                             GLuint textureID, GLenum textureTarget,
                             jint textureWidth, jint textureHeight);
+// needed by Mac OS X port
 jboolean OGLSurfaceData_initFBObject(JNIEnv *env, jobject oglsd,
                                      jlong pData, jboolean isOpaque,
                                      jboolean texNonPow2, jboolean texRect,
--- a/test/experimental/layers/ButtonTest.java	Tue Aug 30 13:10:26 2011 +0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,11 +0,0 @@
-import java.awt.*;
-
-public class ButtonTest{
-    public static final void main(String args[]){
-	Frame f = new Frame();
-	f.setBounds(100,100,400,400);
-	f.setLayout(new BorderLayout());
-	f.add(new Button("I'm AWT button. Don't resize me!"));
-	f.setVisible(true);
-    }
-}