Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/opengl/OGLSurfaceData.java
38918 views
/*1* Copyright (c) 2003, 2012, 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.AlphaComposite;28import java.awt.Composite;29import java.awt.GraphicsConfiguration;30import java.awt.GraphicsEnvironment;31import java.awt.Rectangle;32import java.awt.Transparency;33import java.awt.image.ColorModel;34import java.awt.image.Raster;35import sun.awt.SunHints;36import sun.awt.image.PixelConverter;37import sun.java2d.pipe.hw.AccelSurface;38import sun.java2d.SunGraphics2D;39import sun.java2d.SurfaceData;40import sun.java2d.SurfaceDataProxy;41import sun.java2d.loops.CompositeType;42import sun.java2d.loops.GraphicsPrimitive;43import sun.java2d.loops.MaskFill;44import sun.java2d.loops.SurfaceType;45import sun.java2d.pipe.ParallelogramPipe;46import sun.java2d.pipe.PixelToParallelogramConverter;47import sun.java2d.pipe.RenderBuffer;48import sun.java2d.pipe.TextPipe;49import static sun.java2d.pipe.BufferedOpCodes.*;50import static sun.java2d.opengl.OGLContext.OGLContextCaps.*;5152/**53* This class describes an OpenGL "surface", that is, a region of pixels54* managed via OpenGL. An OGLSurfaceData can be tagged with one of three55* different SurfaceType objects for the purpose of registering loops, etc.56* This diagram shows the hierarchy of OGL SurfaceTypes:57*58* Any59* / \60* OpenGLSurface OpenGLTexture61* |62* OpenGLSurfaceRTT63*64* OpenGLSurface65* This kind of surface can be rendered to using OpenGL APIs. It is also66* possible to copy an OpenGLSurface to another OpenGLSurface (or to itself).67* This is typically accomplished by calling MakeContextCurrent(dstSD, srcSD)68* and then calling glCopyPixels() (although there are other techniques to69* achieve the same goal).70*71* OpenGLTexture72* This kind of surface cannot be rendered to using OpenGL (in the same sense73* as in OpenGLSurface). However, it is possible to upload a region of pixels74* to an OpenGLTexture object via glTexSubImage2D(). One can also copy a75* surface of type OpenGLTexture to an OpenGLSurface by binding the texture76* to a quad and then rendering it to the destination surface (this process77* is known as "texture mapping").78*79* OpenGLSurfaceRTT80* This kind of surface can be thought of as a sort of hybrid between81* OpenGLSurface and OpenGLTexture, in that one can render to this kind of82* surface as if it were of type OpenGLSurface, but the process of copying83* this kind of surface to another is more like an OpenGLTexture. (Note that84* "RTT" stands for "render-to-texture".)85*86* In addition to these SurfaceType variants, we have also defined some87* constants that describe in more detail the type of underlying OpenGL88* surface. This table helps explain the relationships between those89* "type" constants and their corresponding SurfaceType:90*91* OGL Type Corresponding SurfaceType92* -------- -------------------------93* WINDOW OpenGLSurface94* PBUFFER OpenGLSurface95* TEXTURE OpenGLTexture96* FLIP_BACKBUFFER OpenGLSurface97* FBOBJECT OpenGLSurfaceRTT98*/99public abstract class OGLSurfaceData extends SurfaceData100implements AccelSurface {101102/**103* OGL-specific surface types104*105* @see sun.java2d.pipe.hw.AccelSurface106*/107public static final int PBUFFER = RT_PLAIN;108public static final int FBOBJECT = RT_TEXTURE;109110/**111* Pixel formats112*/113public static final int PF_INT_ARGB = 0;114public static final int PF_INT_ARGB_PRE = 1;115public static final int PF_INT_RGB = 2;116public static final int PF_INT_RGBX = 3;117public static final int PF_INT_BGR = 4;118public static final int PF_INT_BGRX = 5;119public static final int PF_USHORT_565_RGB = 6;120public static final int PF_USHORT_555_RGB = 7;121public static final int PF_USHORT_555_RGBX = 8;122public static final int PF_BYTE_GRAY = 9;123public static final int PF_USHORT_GRAY = 10;124public static final int PF_3BYTE_BGR = 11;125126/**127* SurfaceTypes128*/129private static final String DESC_OPENGL_SURFACE = "OpenGL Surface";130private static final String DESC_OPENGL_SURFACE_RTT =131"OpenGL Surface (render-to-texture)";132private static final String DESC_OPENGL_TEXTURE = "OpenGL Texture";133134static final SurfaceType OpenGLSurface =135SurfaceType.Any.deriveSubType(DESC_OPENGL_SURFACE,136PixelConverter.ArgbPre.instance);137static final SurfaceType OpenGLSurfaceRTT =138OpenGLSurface.deriveSubType(DESC_OPENGL_SURFACE_RTT);139static final SurfaceType OpenGLTexture =140SurfaceType.Any.deriveSubType(DESC_OPENGL_TEXTURE);141142/** This will be true if the fbobject system property has been enabled. */143private static boolean isFBObjectEnabled;144145/** This will be true if the lcdshader system property has been enabled.*/146private static boolean isLCDShaderEnabled;147148/** This will be true if the biopshader system property has been enabled.*/149private static boolean isBIOpShaderEnabled;150151/** This will be true if the gradshader system property has been enabled.*/152private static boolean isGradShaderEnabled;153154private OGLGraphicsConfig graphicsConfig;155protected int type;156// these fields are set from the native code when the surface is157// initialized158private int nativeWidth, nativeHeight;159160protected static OGLRenderer oglRenderPipe;161protected static PixelToParallelogramConverter oglTxRenderPipe;162protected static ParallelogramPipe oglAAPgramPipe;163protected static OGLTextRenderer oglTextPipe;164protected static OGLDrawImage oglImagePipe;165166protected native boolean initTexture(long pData,167boolean isOpaque, boolean texNonPow2,168boolean texRect,169int width, int height);170protected native boolean initFBObject(long pData,171boolean isOpaque, boolean texNonPow2,172boolean texRect,173int width, int height);174protected native boolean initFlipBackbuffer(long pData);175protected abstract boolean initPbuffer(long pData, long pConfigInfo,176boolean isOpaque,177int width, int height);178179private native int getTextureTarget(long pData);180private native int getTextureID(long pData);181182static {183if (!GraphicsEnvironment.isHeadless()) {184// fbobject currently enabled by default; use "false" to disable185String fbo = (String)java.security.AccessController.doPrivileged(186new sun.security.action.GetPropertyAction(187"sun.java2d.opengl.fbobject"));188isFBObjectEnabled = !"false".equals(fbo);189190// lcdshader currently enabled by default; use "false" to disable191String lcd = (String)java.security.AccessController.doPrivileged(192new sun.security.action.GetPropertyAction(193"sun.java2d.opengl.lcdshader"));194isLCDShaderEnabled = !"false".equals(lcd);195196// biopshader currently enabled by default; use "false" to disable197String biop = (String)java.security.AccessController.doPrivileged(198new sun.security.action.GetPropertyAction(199"sun.java2d.opengl.biopshader"));200isBIOpShaderEnabled = !"false".equals(biop);201202// gradshader currently enabled by default; use "false" to disable203String grad = (String)java.security.AccessController.doPrivileged(204new sun.security.action.GetPropertyAction(205"sun.java2d.opengl.gradshader"));206isGradShaderEnabled = !"false".equals(grad);207208OGLRenderQueue rq = OGLRenderQueue.getInstance();209oglImagePipe = new OGLDrawImage();210oglTextPipe = new OGLTextRenderer(rq);211oglRenderPipe = new OGLRenderer(rq);212if (GraphicsPrimitive.tracingEnabled()) {213oglTextPipe = oglTextPipe.traceWrap();214//The wrapped oglRenderPipe will wrap the AA pipe as well...215//oglAAPgramPipe = oglRenderPipe.traceWrap();216}217oglAAPgramPipe = oglRenderPipe.getAAParallelogramPipe();218oglTxRenderPipe =219new PixelToParallelogramConverter(oglRenderPipe,220oglRenderPipe,2211.0, 0.25, true);222223OGLBlitLoops.register();224OGLMaskFill.register();225OGLMaskBlit.register();226}227}228229protected OGLSurfaceData(OGLGraphicsConfig gc,230ColorModel cm, int type)231{232super(getCustomSurfaceType(type), cm);233this.graphicsConfig = gc;234this.type = type;235setBlitProxyKey(gc.getProxyKey());236}237238@Override239public SurfaceDataProxy makeProxyFor(SurfaceData srcData) {240return OGLSurfaceDataProxy.createProxy(srcData, graphicsConfig);241}242243/**244* Returns the appropriate SurfaceType corresponding to the given OpenGL245* surface type constant (e.g. TEXTURE -> OpenGLTexture).246*/247private static SurfaceType getCustomSurfaceType(int oglType) {248switch (oglType) {249case TEXTURE:250return OpenGLTexture;251case FBOBJECT:252return OpenGLSurfaceRTT;253case PBUFFER:254default:255return OpenGLSurface;256}257}258259/**260* Note: This should only be called from the QFT under the AWT lock.261* This method is kept separate from the initSurface() method below just262* to keep the code a bit cleaner.263*/264private void initSurfaceNow(int width, int height) {265boolean isOpaque = (getTransparency() == Transparency.OPAQUE);266boolean success = false;267268switch (type) {269case PBUFFER:270success = initPbuffer(getNativeOps(),271graphicsConfig.getNativeConfigInfo(),272isOpaque,273width, height);274break;275276case TEXTURE:277success = initTexture(getNativeOps(),278isOpaque, isTexNonPow2Available(),279isTexRectAvailable(),280width, height);281break;282283case FBOBJECT:284success = initFBObject(getNativeOps(),285isOpaque, isTexNonPow2Available(),286isTexRectAvailable(),287width, height);288break;289290case FLIP_BACKBUFFER:291success = initFlipBackbuffer(getNativeOps());292break;293294default:295break;296}297298if (!success) {299throw new OutOfMemoryError("can't create offscreen surface");300}301}302303/**304* Initializes the appropriate OpenGL offscreen surface based on the value305* of the type parameter. If the surface creation fails for any reason,306* an OutOfMemoryError will be thrown.307*/308protected void initSurface(final int width, final int height) {309OGLRenderQueue rq = OGLRenderQueue.getInstance();310rq.lock();311try {312switch (type) {313case TEXTURE:314case PBUFFER:315case FBOBJECT:316// need to make sure the context is current before317// creating the texture (or pbuffer, or fbobject)318OGLContext.setScratchSurface(graphicsConfig);319break;320default:321break;322}323rq.flushAndInvokeNow(new Runnable() {324public void run() {325initSurfaceNow(width, height);326}327});328} finally {329rq.unlock();330}331}332333/**334* Returns the OGLContext for the GraphicsConfig associated with this335* surface.336*/337public final OGLContext getContext() {338return graphicsConfig.getContext();339}340341/**342* Returns the OGLGraphicsConfig associated with this surface.343*/344final OGLGraphicsConfig getOGLGraphicsConfig() {345return graphicsConfig;346}347348/**349* Returns one of the surface type constants defined above.350*/351public final int getType() {352return type;353}354355/**356* If this surface is backed by a texture object, returns the target357* for that texture (either GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE_ARB).358* Otherwise, this method will return zero.359*/360public final int getTextureTarget() {361return getTextureTarget(getNativeOps());362}363364/**365* If this surface is backed by a texture object, returns the texture ID366* for that texture.367* Otherwise, this method will return zero.368*/369public final int getTextureID() {370return getTextureID(getNativeOps());371}372373/**374* Returns native resource of specified {@code resType} associated with375* this surface.376*377* Specifically, for {@code OGLSurfaceData} this method returns the378* the following:379* <pre>380* TEXTURE - texture id381* </pre>382*383* Note: the resource returned by this method is only valid on the rendering384* thread.385*386* @return native resource of specified type or 0L if387* such resource doesn't exist or can not be retrieved.388* @see sun.java2d.pipe.hw.AccelSurface#getNativeResource389*/390public long getNativeResource(int resType) {391if (resType == TEXTURE) {392return getTextureID();393}394return 0L;395}396397public Raster getRaster(int x, int y, int w, int h) {398throw new InternalError("not implemented yet");399}400401/**402* For now, we can only render LCD text if:403* - the fragment shader extension is available, and404* - the source color is opaque, and405* - blending is SrcOverNoEa or disabled406* - and the destination is opaque407*408* Eventually, we could enhance the native OGL text rendering code409* and remove the above restrictions, but that would require significantly410* more code just to support a few uncommon cases.411*/412public boolean canRenderLCDText(SunGraphics2D sg2d) {413return414graphicsConfig.isCapPresent(CAPS_EXT_LCD_SHADER) &&415sg2d.surfaceData.getTransparency() == Transparency.OPAQUE &&416sg2d.paintState <= SunGraphics2D.PAINT_OPAQUECOLOR &&417(sg2d.compositeState <= SunGraphics2D.COMP_ISCOPY ||418(sg2d.compositeState <= SunGraphics2D.COMP_ALPHA && canHandleComposite(sg2d.composite)));419}420421private boolean canHandleComposite(Composite c) {422if (c instanceof AlphaComposite) {423AlphaComposite ac = (AlphaComposite)c;424425return ac.getRule() == AlphaComposite.SRC_OVER && ac.getAlpha() >= 1f;426}427return false;428}429430public void validatePipe(SunGraphics2D sg2d) {431TextPipe textpipe;432boolean validated = false;433434// OGLTextRenderer handles both AA and non-AA text, but435// only works with the following modes:436// (Note: For LCD text we only enter this code path if437// canRenderLCDText() has already validated that the mode is438// CompositeType.SrcNoEa (opaque color), which will be subsumed439// by the CompositeType.SrcNoEa (any color) test below.)440441if (/* CompositeType.SrcNoEa (any color) */442(sg2d.compositeState <= SunGraphics2D.COMP_ISCOPY &&443sg2d.paintState <= SunGraphics2D.PAINT_ALPHACOLOR) ||444445/* CompositeType.SrcOver (any color) */446(sg2d.compositeState == SunGraphics2D.COMP_ALPHA &&447sg2d.paintState <= SunGraphics2D.PAINT_ALPHACOLOR &&448(((AlphaComposite)sg2d.composite).getRule() ==449AlphaComposite.SRC_OVER)) ||450451/* CompositeType.Xor (any color) */452(sg2d.compositeState == SunGraphics2D.COMP_XOR &&453sg2d.paintState <= SunGraphics2D.PAINT_ALPHACOLOR))454{455textpipe = oglTextPipe;456} else {457// do this to initialize textpipe correctly; we will attempt458// to override the non-text pipes below459super.validatePipe(sg2d);460textpipe = sg2d.textpipe;461validated = true;462}463464PixelToParallelogramConverter txPipe = null;465OGLRenderer nonTxPipe = null;466467if (sg2d.antialiasHint != SunHints.INTVAL_ANTIALIAS_ON) {468if (sg2d.paintState <= SunGraphics2D.PAINT_ALPHACOLOR) {469if (sg2d.compositeState <= SunGraphics2D.COMP_XOR) {470txPipe = oglTxRenderPipe;471nonTxPipe = oglRenderPipe;472}473} else if (sg2d.compositeState <= SunGraphics2D.COMP_ALPHA) {474if (OGLPaints.isValid(sg2d)) {475txPipe = oglTxRenderPipe;476nonTxPipe = oglRenderPipe;477}478// custom paints handled by super.validatePipe() below479}480} else {481if (sg2d.paintState <= SunGraphics2D.PAINT_ALPHACOLOR) {482if (graphicsConfig.isCapPresent(CAPS_PS30) &&483(sg2d.imageComp == CompositeType.SrcOverNoEa ||484sg2d.imageComp == CompositeType.SrcOver))485{486if (!validated) {487super.validatePipe(sg2d);488validated = true;489}490PixelToParallelogramConverter aaConverter =491new PixelToParallelogramConverter(sg2d.shapepipe,492oglAAPgramPipe,4931.0/8.0, 0.499,494false);495sg2d.drawpipe = aaConverter;496sg2d.fillpipe = aaConverter;497sg2d.shapepipe = aaConverter;498} else if (sg2d.compositeState == SunGraphics2D.COMP_XOR) {499// install the solid pipes when AA and XOR are both enabled500txPipe = oglTxRenderPipe;501nonTxPipe = oglRenderPipe;502}503}504// other cases handled by super.validatePipe() below505}506507if (txPipe != null) {508if (sg2d.transformState >= SunGraphics2D.TRANSFORM_TRANSLATESCALE) {509sg2d.drawpipe = txPipe;510sg2d.fillpipe = txPipe;511} else if (sg2d.strokeState != SunGraphics2D.STROKE_THIN) {512sg2d.drawpipe = txPipe;513sg2d.fillpipe = nonTxPipe;514} else {515sg2d.drawpipe = nonTxPipe;516sg2d.fillpipe = nonTxPipe;517}518// Note that we use the transforming pipe here because it519// will examine the shape and possibly perform an optimized520// operation if it can be simplified. The simplifications521// will be valid for all STROKE and TRANSFORM types.522sg2d.shapepipe = txPipe;523} else {524if (!validated) {525super.validatePipe(sg2d);526}527}528529// install the text pipe based on our earlier decision530sg2d.textpipe = textpipe;531532// always override the image pipe with the specialized OGL pipe533sg2d.imagepipe = oglImagePipe;534}535536@Override537protected MaskFill getMaskFill(SunGraphics2D sg2d) {538if (sg2d.paintState > SunGraphics2D.PAINT_ALPHACOLOR) {539/*540* We can only accelerate non-Color MaskFill operations if541* all of the following conditions hold true:542* - there is an implementation for the given paintState543* - the current Paint can be accelerated for this destination544* - multitexturing is available (since we need to modulate545* the alpha mask texture with the paint texture)546*547* In all other cases, we return null, in which case the548* validation code will choose a more general software-based loop.549*/550if (!OGLPaints.isValid(sg2d) ||551!graphicsConfig.isCapPresent(CAPS_MULTITEXTURE))552{553return null;554}555}556return super.getMaskFill(sg2d);557}558559public boolean copyArea(SunGraphics2D sg2d,560int x, int y, int w, int h, int dx, int dy)561{562if (sg2d.transformState < SunGraphics2D.TRANSFORM_TRANSLATESCALE &&563sg2d.compositeState < SunGraphics2D.COMP_XOR)564{565x += sg2d.transX;566y += sg2d.transY;567568oglRenderPipe.copyArea(sg2d, x, y, w, h, dx, dy);569570return true;571}572return false;573}574575public void flush() {576invalidate();577OGLRenderQueue rq = OGLRenderQueue.getInstance();578rq.lock();579try {580// make sure we have a current context before581// disposing the native resources (e.g. texture object)582OGLContext.setScratchSurface(graphicsConfig);583584RenderBuffer buf = rq.getBuffer();585rq.ensureCapacityAndAlignment(12, 4);586buf.putInt(FLUSH_SURFACE);587buf.putLong(getNativeOps());588589// this call is expected to complete synchronously, so flush now590rq.flushNow();591} finally {592rq.unlock();593}594}595596/**597* Disposes the native resources associated with the given OGLSurfaceData598* (referenced by the pData parameter). This method is invoked from599* the native Dispose() method from the Disposer thread when the600* Java-level OGLSurfaceData object is about to go away. Note that we601* also pass a reference to the OGLGraphicsConfig602* for the purposes of making a context current.603*/604static void dispose(long pData, OGLGraphicsConfig gc) {605OGLRenderQueue rq = OGLRenderQueue.getInstance();606rq.lock();607try {608// make sure we have a current context before609// disposing the native resources (e.g. texture object)610OGLContext.setScratchSurface(gc);611612RenderBuffer buf = rq.getBuffer();613rq.ensureCapacityAndAlignment(12, 4);614buf.putInt(DISPOSE_SURFACE);615buf.putLong(pData);616617// this call is expected to complete synchronously, so flush now618rq.flushNow();619} finally {620rq.unlock();621}622}623624static void swapBuffers(long window) {625OGLRenderQueue rq = OGLRenderQueue.getInstance();626rq.lock();627try {628RenderBuffer buf = rq.getBuffer();629rq.ensureCapacityAndAlignment(12, 4);630buf.putInt(SWAP_BUFFERS);631buf.putLong(window);632rq.flushNow();633} finally {634rq.unlock();635}636}637638/**639* Returns true if OpenGL textures can have non-power-of-two dimensions640* when using the basic GL_TEXTURE_2D target.641*/642boolean isTexNonPow2Available() {643return graphicsConfig.isCapPresent(CAPS_TEXNONPOW2);644}645646/**647* Returns true if OpenGL textures can have non-power-of-two dimensions648* when using the GL_TEXTURE_RECTANGLE_ARB target (only available when the649* GL_ARB_texture_rectangle extension is present).650*/651boolean isTexRectAvailable() {652return graphicsConfig.isCapPresent(CAPS_EXT_TEXRECT);653}654655public Rectangle getNativeBounds() {656OGLRenderQueue rq = OGLRenderQueue.getInstance();657rq.lock();658try {659return new Rectangle(nativeWidth, nativeHeight);660} finally {661rq.unlock();662}663}664665/**666* Returns true if the surface is an on-screen window surface or667* a FBO texture attached to an on-screen CALayer.668*669* Needed by Mac OS X port.670*/671boolean isOnScreen() {672return getType() == WINDOW;673}674}675676677