Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/java2d/d3d/D3DVolatileSurfaceManager.java
32288 views
/*1* Copyright (c) 2007, 2008, 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.d3d;2627import java.awt.Component;28import java.awt.GraphicsConfiguration;29import java.awt.Image;30import java.awt.Transparency;31import java.awt.image.ColorModel;32import sun.awt.Win32GraphicsConfig;33import sun.awt.image.SunVolatileImage;34import sun.awt.image.SurfaceManager;35import sun.awt.image.VolatileSurfaceManager;36import sun.awt.windows.WComponentPeer;37import sun.java2d.InvalidPipeException;38import sun.java2d.SurfaceData;39import static sun.java2d.pipe.hw.AccelSurface.*;40import static sun.java2d.d3d.D3DContext.D3DContextCaps.*;41import sun.java2d.windows.GDIWindowSurfaceData;4243public class D3DVolatileSurfaceManager44extends VolatileSurfaceManager45{46private boolean accelerationEnabled;47private int restoreCountdown;4849public D3DVolatileSurfaceManager(SunVolatileImage vImg, Object context) {50super(vImg, context);5152/*53* We will attempt to accelerate this image only under the54* following conditions:55* - the image is opaque OR56* - the image is translucent AND57* - the GraphicsConfig supports the FBO extension OR58* - the GraphicsConfig has a stored alpha channel59*/60int transparency = vImg.getTransparency();61D3DGraphicsDevice gd = (D3DGraphicsDevice)62vImg.getGraphicsConfig().getDevice();63accelerationEnabled =64(transparency == Transparency.OPAQUE) ||65(transparency == Transparency.TRANSLUCENT &&66(gd.isCapPresent(CAPS_RT_PLAIN_ALPHA) ||67gd.isCapPresent(CAPS_RT_TEXTURE_ALPHA)));68}6970protected boolean isAccelerationEnabled() {71return accelerationEnabled;72}73public void setAccelerationEnabled(boolean accelerationEnabled) {74this.accelerationEnabled = accelerationEnabled;75}7677/**78* Create a pbuffer-based SurfaceData object (or init the backbuffer79* of an existing window if this is a double buffered GraphicsConfig).80*/81protected SurfaceData initAcceleratedSurface() {82SurfaceData sData;83Component comp = vImg.getComponent();84WComponentPeer peer =85(comp != null) ? (WComponentPeer)comp.getPeer() : null;8687try {88boolean forceback = false;89if (context instanceof Boolean) {90forceback = ((Boolean)context).booleanValue();91}9293if (forceback) {94// peer must be non-null in this case95sData = D3DSurfaceData.createData(peer, vImg);96} else {97D3DGraphicsConfig gc =98(D3DGraphicsConfig)vImg.getGraphicsConfig();99ColorModel cm = gc.getColorModel(vImg.getTransparency());100int type = vImg.getForcedAccelSurfaceType();101// if acceleration type is forced (type != UNDEFINED) then102// use the forced type, otherwise use RT_TEXTURE103if (type == UNDEFINED) {104type = RT_TEXTURE;105}106sData = D3DSurfaceData.createData(gc,107vImg.getWidth(),108vImg.getHeight(),109cm, vImg,110type);111}112} catch (NullPointerException ex) {113sData = null;114} catch (OutOfMemoryError er) {115sData = null;116} catch (InvalidPipeException ipe) {117sData = null;118}119120return sData;121}122123protected boolean isConfigValid(GraphicsConfiguration gc) {124return ((gc == null) || (gc == vImg.getGraphicsConfig()));125}126127/**128* Set the number of iterations for restoreAcceleratedSurface to fail129* before attempting to restore the accelerated surface.130*131* @see #restoreAcceleratedSurface132* @see #handleVItoScreenOp133*/134private synchronized void setRestoreCountdown(int count) {135restoreCountdown = count;136}137138/**139* Note that we create a new surface instead of restoring140* an old one. This will help with D3DContext revalidation.141*/142@Override143protected void restoreAcceleratedSurface() {144synchronized (this) {145if (restoreCountdown > 0) {146restoreCountdown--;147throw new148InvalidPipeException("Will attempt to restore surface " +149" in " + restoreCountdown);150}151}152153SurfaceData sData = initAcceleratedSurface();154if (sData != null) {155sdAccel = sData;156} else {157throw new InvalidPipeException("could not restore surface");158// REMIND: alternatively, we could try this:159// ((D3DSurfaceData)sdAccel).restoreSurface();160}161}162163/**164* We're asked to restore contents by the accelerated surface, which means165* that it had been lost.166*/167@Override168public SurfaceData restoreContents() {169acceleratedSurfaceLost();170return super.restoreContents();171}172173/**174* If the destination surface's peer can potentially handle accelerated175* on-screen rendering then it is likely that the condition which resulted176* in VI to Screen operation is temporary, so this method sets the177* restore countdown in hope that the on-screen accelerated rendering will178* resume. In the meantime the backup surface of the VISM will be used.179*180* The countdown is needed because otherwise we may never break out181* of "do { vi.validate()..} while(vi.lost)" loop since validate() could182* restore the source surface every time and it will get lost again on the183* next copy attempt, and we would never get a chance to use the backup184* surface. By using the countdown we allow the backup surface to be used185* while the screen surface gets sorted out, or if it for some reason can186* never be restored.187*188* If the destination surface's peer could never do accelerated onscreen189* rendering then the acceleration for the SurfaceManager associated with190* the source surface is disabled forever.191*/192static void handleVItoScreenOp(SurfaceData src, SurfaceData dst) {193if (src instanceof D3DSurfaceData &&194dst instanceof GDIWindowSurfaceData)195{196D3DSurfaceData d3dsd = (D3DSurfaceData)src;197SurfaceManager mgr =198SurfaceManager.getManager((Image)d3dsd.getDestination());199if (mgr instanceof D3DVolatileSurfaceManager) {200D3DVolatileSurfaceManager vsm = (D3DVolatileSurfaceManager)mgr;201if (vsm != null) {202d3dsd.setSurfaceLost(true);203204GDIWindowSurfaceData wsd = (GDIWindowSurfaceData)dst;205WComponentPeer p = wsd.getPeer();206if (D3DScreenUpdateManager.canUseD3DOnScreen(p,207(Win32GraphicsConfig)p.getGraphicsConfiguration(),208p.getBackBuffersNum()))209{210// 10 is only chosen to be greater than the number of211// times a sane person would call validate() inside212// a validation loop, and to reduce thrashing between213// accelerated and backup surfaces214vsm.setRestoreCountdown(10);215} else {216vsm.setAccelerationEnabled(false);217}218}219}220}221}222223@Override224public void initContents() {225if (vImg.getForcedAccelSurfaceType() != TEXTURE) {226super.initContents();227}228}229}230231232