1 /*
   2  * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package sun.awt.windows;
  26 
  27 import java.awt.AlphaComposite;
  28 import java.awt.Color;
  29 import java.awt.Graphics2D;
  30 import java.awt.GraphicsConfiguration;
  31 import java.awt.Image;
  32 import java.awt.Window;
  33 import java.awt.geom.AffineTransform;
  34 import java.awt.image.BufferedImage;
  35 import java.awt.image.DataBufferInt;
  36 import java.awt.image.VolatileImage;
  37 import java.security.AccessController;
  38 import sun.awt.image.BufImgSurfaceData;
  39 import sun.java2d.DestSurfaceProvider;
  40 import sun.java2d.InvalidPipeException;
  41 import sun.java2d.Surface;
  42 import sun.java2d.pipe.RenderQueue;
  43 import sun.java2d.pipe.BufferedContext;
  44 import sun.java2d.pipe.hw.AccelGraphicsConfig;
  45 import sun.java2d.pipe.hw.AccelSurface;
  46 import sun.security.action.GetPropertyAction;
  47 
  48 import static java.awt.image.VolatileImage.*;
  49 import sun.java2d.pipe.Region;
  50 import static sun.java2d.pipe.hw.AccelSurface.*;
  51 import static sun.java2d.pipe.hw.ContextCapabilities.*;
  52 
  53 /**
  54  * This class handles the updates of the non-opaque windows.
  55  * The window associated with the peer is updated either given an image or
  56  * the window is repainted to an internal buffer which is then used to update
  57  * the window.
  58  *
  59  * Note: this class does not attempt to be thread safe, it is expected to be
  60  * called from a single thread (EDT).
  61  */
  62 abstract class TranslucentWindowPainter {
  63 
  64     protected Window window;
  65     protected WWindowPeer peer;
  66 
  67     // REMIND: we probably would want to remove this later
  68     private static final boolean forceOpt  =
  69         Boolean.valueOf(AccessController.doPrivileged(
  70             new GetPropertyAction("sun.java2d.twp.forceopt", "false")));
  71     private static final boolean forceSW  =
  72         Boolean.valueOf(AccessController.doPrivileged(
  73             new GetPropertyAction("sun.java2d.twp.forcesw", "false")));
  74 
  75     /**
  76      * Creates an instance of the painter for particular peer.
  77      */
  78     public static TranslucentWindowPainter createInstance(WWindowPeer peer) {
  79         GraphicsConfiguration gc = peer.getGraphicsConfiguration();
  80         if (!forceSW && gc instanceof AccelGraphicsConfig) {
  81             String gcName = gc.getClass().getSimpleName();
  82             AccelGraphicsConfig agc = (AccelGraphicsConfig)gc;
  83             // this is a heuristic to check that we have a pcix board
  84             // (those have higher transfer rate from gpu to cpu)
  85             if ((agc.getContextCapabilities().getCaps() & CAPS_PS30) != 0 ||
  86                 forceOpt)
  87             {
  88                 // we check for name to avoid loading classes unnecessarily if
  89                 // a pipeline isn't enabled
  90                 if (gcName.startsWith("D3D")) {
  91                     return new VIOptD3DWindowPainter(peer);
  92                 } else if (forceOpt && gcName.startsWith("WGL")) {
  93                     // on some boards (namely, ATI, even on pcix bus) ogl is
  94                     // very slow reading pixels back so for now it is disabled
  95                     // unless forced
  96                     return new VIOptWGLWindowPainter(peer);
  97                 }
  98             }
  99         }
 100         return new BIWindowPainter(peer);
 101     }
 102 
 103     protected TranslucentWindowPainter(WWindowPeer peer) {
 104         this.peer = peer;
 105         this.window = (Window)peer.getTarget();
 106     }
 107 
 108     /**
 109      * Creates (if needed), clears (if requested) and returns the buffer
 110      * for this painter.
 111      */
 112     protected abstract Image getBackBuffer(boolean clear);
 113 
 114     /**
 115      * Updates the window associated with this painter with the contents
 116      * of the passed image.
 117      * The image can not be null, and NPE will be thrown if it is.
 118      */
 119     protected abstract boolean update(Image bb);
 120 
 121     /**
 122      * Flushes the resources associated with the painter. They will be
 123      * recreated as needed.
 124      */
 125     public abstract void flush();
 126 
 127     /**
 128      * Updates the window associated with the painter.
 129      *
 130      * @param repaint indicates if the window should be completely repainted
 131      * to the back buffer using {@link java.awt.Window#paintAll} before update.
 132      */
 133     public void updateWindow(boolean repaint) {
 134         boolean done = false;
 135         Image bb = getBackBuffer(repaint);
 136         while (!done) {
 137             if (repaint) {
 138                 Graphics2D g = (Graphics2D)bb.getGraphics();
 139                 try {
 140                     window.paintAll(g);
 141                 } finally {
 142                     g.dispose();
 143                 }
 144             }
 145 
 146             done = update(bb);
 147             if (!done) {
 148                 repaint = true;
 149                 bb = getBackBuffer(true);
 150             }
 151         }
 152     }
 153 
 154     private static final Image clearImage(Image bb) {
 155         Graphics2D g = (Graphics2D)bb.getGraphics();
 156         int w = bb.getWidth(null);
 157         int h = bb.getHeight(null);
 158 
 159         g.setComposite(AlphaComposite.Src);
 160         g.setColor(new Color(0, 0, 0, 0));
 161         g.fillRect(0, 0, w, h);
 162 
 163         return bb;
 164     }
 165 
 166     /**
 167      * A painter which uses BufferedImage as the internal buffer. The window
 168      * is painted into this buffer, and the contents then are uploaded
 169      * into the layered window.
 170      *
 171      * This painter handles all types of images passed to its paint(Image)
 172      * method (VI, BI, regular Images).
 173      */
 174     private static class BIWindowPainter extends TranslucentWindowPainter {
 175         private BufferedImage backBuffer;
 176 
 177         protected BIWindowPainter(WWindowPeer peer) {
 178             super(peer);
 179         }
 180 
 181         @Override
 182         protected Image getBackBuffer(boolean clear) {
 183             int w = window.getWidth();
 184             int h = window.getHeight();
 185             if (backBuffer == null ||
 186                 backBuffer.getWidth() != w ||
 187                 backBuffer.getHeight() != h)
 188             {
 189                 flush();
 190                 backBuffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
 191             }
 192             return clear ? (BufferedImage)clearImage(backBuffer) : backBuffer;
 193         }
 194 
 195         @Override
 196         protected boolean update(Image bb) {
 197             VolatileImage viBB = null;
 198 
 199             if (bb instanceof BufferedImage) {
 200                 BufferedImage bi = (BufferedImage)bb;
 201                 int data[] =
 202                     ((DataBufferInt)bi.getRaster().getDataBuffer()).getData();
 203                 peer.updateWindowImpl(data, bi.getWidth(), bi.getHeight());
 204                 return true;
 205             } else if (bb instanceof VolatileImage) {
 206                 viBB = (VolatileImage)bb;
 207                 if (bb instanceof DestSurfaceProvider) {
 208                     Surface s = ((DestSurfaceProvider)bb).getDestSurface();
 209                     if (s instanceof BufImgSurfaceData) {
 210                         // the image is probably lost, upload the data from the
 211                         // backup surface to avoid creating another heap-based
 212                         // image (the parent's buffer)
 213                         int w = viBB.getWidth();
 214                         int h = viBB.getHeight();
 215                         BufImgSurfaceData bisd = (BufImgSurfaceData)s;
 216                         int data[] = ((DataBufferInt)bisd.getRaster(0,0,w,h).
 217                             getDataBuffer()).getData();
 218                         peer.updateWindowImpl(data, w, h);
 219                         return true;
 220                     }
 221                 }
 222             }
 223 
 224             // copy the passed image into our own buffer, then upload
 225             BufferedImage bi = (BufferedImage)clearImage(backBuffer);
 226 
 227             int data[] =
 228                 ((DataBufferInt)bi.getRaster().getDataBuffer()).getData();
 229             peer.updateWindowImpl(data, bi.getWidth(), bi.getHeight());
 230 
 231             return (viBB != null ? !viBB.contentsLost() : true);
 232         }
 233 
 234         @Override
 235         public void flush() {
 236             if (backBuffer != null) {
 237                 backBuffer.flush();
 238                 backBuffer = null;
 239             }
 240         }
 241     }
 242 
 243     /**
 244      * A version of the painter which uses VolatileImage as the internal buffer.
 245      * The window is painted into this VI and then copied into the parent's
 246      * Java heap-based buffer (which is then uploaded to the layered window)
 247      */
 248     private static class VIWindowPainter extends BIWindowPainter {
 249         private VolatileImage viBB;
 250 
 251         protected VIWindowPainter(WWindowPeer peer) {
 252             super(peer);
 253         }
 254 
 255         @Override
 256         protected Image getBackBuffer(boolean clear) {
 257             int w = window.getWidth();
 258             int h = window.getHeight();
 259             GraphicsConfiguration gc = peer.getGraphicsConfiguration();
 260 
 261             if (viBB == null || viBB.getWidth() != w || viBB.getHeight() != h ||
 262                 viBB.validate(gc) == IMAGE_INCOMPATIBLE)
 263             {
 264                 flush();
 265 
 266                 if (gc instanceof AccelGraphicsConfig) {
 267                     AccelGraphicsConfig agc = ((AccelGraphicsConfig)gc);
 268                     viBB = agc.createCompatibleVolatileImage(w, h,
 269                                                              TRANSLUCENT,
 270                                                              RT_PLAIN);
 271                 }
 272                 if (viBB == null) {
 273                     viBB = gc.createCompatibleVolatileImage(w, h, TRANSLUCENT);
 274                 }
 275                 viBB.validate(gc);
 276             }
 277 
 278             return clear ? clearImage(viBB) : viBB;
 279         }
 280 
 281         @Override
 282         public void flush() {
 283             if (viBB != null) {
 284                 viBB.flush();
 285                 viBB = null;
 286             }
 287         }
 288     }
 289 
 290     /**
 291      * Optimized version of hw painter. Uses VolatileImages for the
 292      * buffer, and uses an optimized path to pull the data from those into
 293      * the layered window, bypassing Java heap-based image.
 294      */
 295     private abstract static class VIOptWindowPainter extends VIWindowPainter {
 296 
 297         protected VIOptWindowPainter(WWindowPeer peer) {
 298             super(peer);
 299         }
 300 
 301         protected abstract boolean updateWindowAccel(long psdops, int w, int h);
 302 
 303         @Override
 304         protected boolean update(Image bb) {
 305             if (bb instanceof DestSurfaceProvider) {
 306                 Surface s = ((DestSurfaceProvider)bb).getDestSurface();
 307                 if (s instanceof AccelSurface) {
 308                     AffineTransform tx = ((Graphics2D) bb.getGraphics()).
 309                             getTransform();
 310                     final int w = Region.
 311                             clipScale(bb.getWidth(null), tx.getScaleX());
 312                     final int h = Region.
 313                             clipScale(bb.getHeight(null), tx.getScaleY());
 314                     final boolean arr[] = { false };
 315                     final AccelSurface as = (AccelSurface)s;
 316                     RenderQueue rq = as.getContext().getRenderQueue();
 317                     rq.lock();
 318                     try {
 319                         BufferedContext.validateContext(as);
 320                         rq.flushAndInvokeNow(new Runnable() {
 321                             @Override
 322                             public void run() {
 323                                 long psdops = as.getNativeOps();
 324                                 arr[0] = updateWindowAccel(psdops, w, h);
 325                             }
 326                         });
 327                     } catch (InvalidPipeException e) {
 328                         // ignore, false will be returned
 329                     } finally {
 330                         rq.unlock();
 331                     }
 332                     return arr[0];
 333                 }
 334             }
 335             return super.update(bb);
 336         }
 337     }
 338 
 339     private static class VIOptD3DWindowPainter extends VIOptWindowPainter {
 340 
 341         protected VIOptD3DWindowPainter(WWindowPeer peer) {
 342             super(peer);
 343         }
 344 
 345         @Override
 346         protected boolean updateWindowAccel(long psdops, int w, int h) {
 347             // note: this method is executed on the toolkit thread, no sync is
 348             // necessary at the native level, and a pointer to peer can be used
 349             return sun.java2d.d3d.D3DSurfaceData.
 350                 updateWindowAccelImpl(psdops, peer.getData(), w, h);
 351         }
 352     }
 353 
 354     private static class VIOptWGLWindowPainter extends VIOptWindowPainter {
 355 
 356         protected VIOptWGLWindowPainter(WWindowPeer peer) {
 357             super(peer);
 358         }
 359 
 360         @Override
 361         protected boolean updateWindowAccel(long psdops, int w, int h) {
 362             // note: part of this method which deals with GDI will be on the
 363             // toolkit thread
 364             return sun.java2d.opengl.WGLSurfaceData.
 365                 updateWindowAccelImpl(psdops, peer, w, h);
 366         }
 367     }
 368 }