Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/classes/sun/java2d/opengl/CGLSurfaceData.java
38918 views
/*1* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.opengl;2627import java.awt.Graphics;28import java.awt.GraphicsConfiguration;29import java.awt.GraphicsDevice;30import java.awt.GraphicsEnvironment;31import java.awt.Image;32import java.awt.Rectangle;33import java.awt.image.ColorModel;3435import sun.java2d.SunGraphics2D;36import sun.java2d.SurfaceData;3738import sun.lwawt.macosx.CPlatformView;3940public abstract class CGLSurfaceData extends OGLSurfaceData {4142protected final int scale;43protected final int width;44protected final int height;45protected CPlatformView pView;46private CGLGraphicsConfig graphicsConfig;4748native void validate(int xoff, int yoff, int width, int height, boolean isOpaque);4950private native void initOps(OGLGraphicsConfig gc, long pConfigInfo,51long pPeerData, long layerPtr, int xoff,52int yoff, boolean isOpaque);5354protected native boolean initPbuffer(long pData, long pConfigInfo,55boolean isOpaque, int width, int height);5657protected CGLSurfaceData(CGLGraphicsConfig gc, ColorModel cm, int type,58int width, int height) {59super(gc, cm, type);60// TEXTURE shouldn't be scaled, it is used for managed BufferedImages.61scale = type == TEXTURE ? 1 : gc.getDevice().getScaleFactor();62this.width = width * scale;63this.height = height * scale;64}6566protected CGLSurfaceData(CPlatformView pView, CGLGraphicsConfig gc,67ColorModel cm, int type,int width, int height)68{69this(gc, cm, type, width, height);70this.pView = pView;71this.graphicsConfig = gc;7273long pConfigInfo = gc.getNativeConfigInfo();74long pPeerData = 0L;75boolean isOpaque = true;76if (pView != null) {77pPeerData = pView.getAWTView();78isOpaque = pView.isOpaque();79}80initOps(gc, pConfigInfo, pPeerData, 0, 0, 0, isOpaque);81}8283protected CGLSurfaceData(CGLLayer layer, CGLGraphicsConfig gc,84ColorModel cm, int type,int width, int height)85{86this(gc, cm, type, width, height);87this.graphicsConfig = gc;8889long pConfigInfo = gc.getNativeConfigInfo();90long layerPtr = 0L;91boolean isOpaque = true;92if (layer != null) {93layerPtr = layer.getPointer();94isOpaque = layer.isOpaque();95}96initOps(gc, pConfigInfo, 0, layerPtr, 0, 0, isOpaque);97}9899@Override //SurfaceData100public GraphicsConfiguration getDeviceConfiguration() {101return graphicsConfig;102}103104/**105* Creates a SurfaceData object representing the primary (front) buffer of106* an on-screen Window.107*/108public static CGLWindowSurfaceData createData(CPlatformView pView) {109CGLGraphicsConfig gc = getGC(pView);110return new CGLWindowSurfaceData(pView, gc);111}112113/**114* Creates a SurfaceData object representing the intermediate buffer115* between the Java2D flusher thread and the AppKit thread.116*/117public static CGLLayerSurfaceData createData(CGLLayer layer) {118CGLGraphicsConfig gc = getGC(layer);119Rectangle r = layer.getBounds();120return new CGLLayerSurfaceData(layer, gc, r.width, r.height);121}122123/**124* Creates a SurfaceData object representing the back buffer of a125* double-buffered on-screen Window.126*/127public static CGLOffScreenSurfaceData createData(CPlatformView pView,128Image image, int type) {129CGLGraphicsConfig gc = getGC(pView);130Rectangle r = pView.getBounds();131if (type == FLIP_BACKBUFFER) {132return new CGLOffScreenSurfaceData(pView, gc, r.width, r.height,133image, gc.getColorModel(), FLIP_BACKBUFFER);134} else {135return new CGLVSyncOffScreenSurfaceData(pView, gc, r.width,136r.height, image, gc.getColorModel(), type);137}138}139140/**141* Creates a SurfaceData object representing an off-screen buffer (either a142* Pbuffer or Texture).143*/144public static CGLOffScreenSurfaceData createData(CGLGraphicsConfig gc,145int width, int height, ColorModel cm, Image image, int type) {146return new CGLOffScreenSurfaceData(null, gc, width, height, image, cm,147type);148}149150public static CGLGraphicsConfig getGC(CPlatformView pView) {151if (pView != null) {152return (CGLGraphicsConfig)pView.getGraphicsConfiguration();153} else {154// REMIND: this should rarely (never?) happen, but what if155// default config is not CGL?156GraphicsEnvironment env = GraphicsEnvironment157.getLocalGraphicsEnvironment();158GraphicsDevice gd = env.getDefaultScreenDevice();159return (CGLGraphicsConfig) gd.getDefaultConfiguration();160}161}162163public static CGLGraphicsConfig getGC(CGLLayer layer) {164return (CGLGraphicsConfig)layer.getGraphicsConfiguration();165}166167public void validate() {168// Overridden in CGLWindowSurfaceData below169}170171@Override172public int getDefaultScale() {173return scale;174}175176@Override177public boolean copyArea(SunGraphics2D sg2d, int x, int y, int w, int h,178int dx, int dy) {179final int state = sg2d.transformState;180if (state > SunGraphics2D.TRANSFORM_TRANSLATESCALE181|| sg2d.compositeState >= SunGraphics2D.COMP_XOR) {182return false;183}184if (state <= SunGraphics2D.TRANSFORM_ANY_TRANSLATE) {185x += sg2d.transX;186y += sg2d.transY;187} else if (state == SunGraphics2D.TRANSFORM_TRANSLATESCALE) {188final double[] coords = {x, y, x + w, y + h, x + dx, y + dy};189sg2d.transform.transform(coords, 0, coords, 0, 3);190x = (int) Math.ceil(coords[0] - 0.5);191y = (int) Math.ceil(coords[1] - 0.5);192w = ((int) Math.ceil(coords[2] - 0.5)) - x;193h = ((int) Math.ceil(coords[3] - 0.5)) - y;194dx = ((int) Math.ceil(coords[4] - 0.5)) - x;195dy = ((int) Math.ceil(coords[5] - 0.5)) - y;196}197oglRenderPipe.copyArea(sg2d, x, y, w, h, dx, dy);198return true;199}200201protected native void clearWindow();202203public static class CGLWindowSurfaceData extends CGLSurfaceData {204205public CGLWindowSurfaceData(CPlatformView pView,206CGLGraphicsConfig gc) {207super(pView, gc, gc.getColorModel(), WINDOW, 0, 0);208}209210@Override211public SurfaceData getReplacement() {212return pView.getSurfaceData();213}214215@Override216public Rectangle getBounds() {217Rectangle r = pView.getBounds();218return new Rectangle(0, 0, r.width, r.height);219}220221/**222* Returns destination Component associated with this SurfaceData.223*/224@Override225public Object getDestination() {226return pView.getDestination();227}228229public void validate() {230OGLRenderQueue rq = OGLRenderQueue.getInstance();231rq.lock();232try {233rq.flushAndInvokeNow(new Runnable() {234public void run() {235Rectangle peerBounds = pView.getBounds();236validate(0, 0, peerBounds.width, peerBounds.height, pView.isOpaque());237}238});239} finally {240rq.unlock();241}242}243244@Override245public void invalidate() {246super.invalidate();247clearWindow();248}249}250251/**252* A surface which implements an intermediate buffer between253* the Java2D flusher thread and the AppKit thread.254*255* This surface serves as a buffer attached to a CGLLayer and256* the layer redirects all painting to the buffer's graphics.257*/258public static class CGLLayerSurfaceData extends CGLSurfaceData {259260private CGLLayer layer;261262public CGLLayerSurfaceData(CGLLayer layer, CGLGraphicsConfig gc,263int width, int height) {264super(layer, gc, gc.getColorModel(), FBOBJECT, width, height);265this.layer = layer;266initSurface(this.width, this.height);267}268269@Override270public SurfaceData getReplacement() {271return layer.getSurfaceData();272}273274@Override275boolean isOnScreen() {276return true;277}278279@Override280public Rectangle getBounds() {281return new Rectangle(width, height);282}283284@Override285public Object getDestination() {286return layer.getDestination();287}288289@Override290public int getTransparency() {291return layer.getTransparency();292}293294@Override295public void invalidate() {296super.invalidate();297clearWindow();298}299}300301/**302* A surface which implements a v-synced flip back-buffer with COPIED303* FlipContents.304*305* This surface serves as a back-buffer to the outside world, while it is306* actually an offscreen surface. When the BufferStrategy this surface307* belongs to is showed, it is first copied to the real private308* FLIP_BACKBUFFER, which is then flipped.309*/310public static class CGLVSyncOffScreenSurfaceData extends311CGLOffScreenSurfaceData {312private CGLOffScreenSurfaceData flipSurface;313314public CGLVSyncOffScreenSurfaceData(CPlatformView pView,315CGLGraphicsConfig gc, int width, int height, Image image,316ColorModel cm, int type) {317super(pView, gc, width, height, image, cm, type);318flipSurface = CGLSurfaceData.createData(pView, image,319FLIP_BACKBUFFER);320}321322public SurfaceData getFlipSurface() {323return flipSurface;324}325326@Override327public void flush() {328flipSurface.flush();329super.flush();330}331}332333public static class CGLOffScreenSurfaceData extends CGLSurfaceData {334private Image offscreenImage;335336public CGLOffScreenSurfaceData(CPlatformView pView,337CGLGraphicsConfig gc, int width, int height, Image image,338ColorModel cm, int type) {339super(pView, gc, cm, type, width, height);340offscreenImage = image;341initSurface(this.width, this.height);342}343344@Override345public SurfaceData getReplacement() {346return restoreContents(offscreenImage);347}348349@Override350public Rectangle getBounds() {351if (type == FLIP_BACKBUFFER) {352Rectangle r = pView.getBounds();353return new Rectangle(0, 0, r.width, r.height);354} else {355return new Rectangle(width, height);356}357}358359/**360* Returns destination Image associated with this SurfaceData.361*/362@Override363public Object getDestination() {364return offscreenImage;365}366}367368// Mac OS X specific APIs for JOGL/Java2D bridge...369370// given a surface create and attach GL context, then return it371private native static long createCGLContextOnSurface(CGLSurfaceData sd,372long sharedContext);373374public static long createOGLContextOnSurface(Graphics g, long sharedContext) {375SurfaceData sd = ((SunGraphics2D) g).surfaceData;376if ((sd instanceof CGLSurfaceData) == true) {377CGLSurfaceData cglsd = (CGLSurfaceData) sd;378return createCGLContextOnSurface(cglsd, sharedContext);379} else {380return 0L;381}382}383384// returns whether or not the makeCurrent operation succeeded385native static boolean makeCGLContextCurrentOnSurface(CGLSurfaceData sd,386long ctx);387388public static boolean makeOGLContextCurrentOnSurface(Graphics g, long ctx) {389SurfaceData sd = ((SunGraphics2D) g).surfaceData;390if ((ctx != 0L) && ((sd instanceof CGLSurfaceData) == true)) {391CGLSurfaceData cglsd = (CGLSurfaceData) sd;392return makeCGLContextCurrentOnSurface(cglsd, ctx);393} else {394return false;395}396}397398// additional cleanup399private native static void destroyCGLContext(long ctx);400401public static void destroyOGLContext(long ctx) {402if (ctx != 0L) {403destroyCGLContext(ctx);404}405}406}407408409