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.Region;
  43 import sun.java2d.pipe.RenderQueue;
  44 import sun.java2d.pipe.BufferedContext;
  45 import sun.java2d.pipe.hw.AccelGraphicsConfig;
  46 import sun.java2d.pipe.hw.AccelSurface;
  47 import sun.security.action.GetPropertyAction;
  48 
  49 import static java.awt.image.VolatileImage.*;
  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             GraphicsConfiguration gc = peer.getGraphicsConfiguration();
 184             AffineTransform transform = gc.getDefaultTransform();
 185             int w = Region.clipRound(
 186                     window.getWidth() * transform.getScaleX());
 187             int h = Region.clipRound(
 188                     window.getHeight() * transform.getScaleY());
 189             if (backBuffer == null ||
 190                 backBuffer.getWidth() != w ||
 191                 backBuffer.getHeight() != h)
 192             {
 193                 flush();
 194                 backBuffer = new BufferedImage(gc, w, h, BufferedImage
 195                         .TYPE_INT_ARGB_PRE);
 196             }
 197             return clear ? (BufferedImage)clearImage(backBuffer) : backBuffer;
 198         }
 199 
 200         @Override
 201         protected boolean update(Image bb) {
 202             VolatileImage viBB = null;
 203 
 204             if (bb instanceof BufferedImage) {
 205                 BufferedImage bi = (BufferedImage)bb;
 206                 int data[] =
 207                     ((DataBufferInt)bi.getRaster().getDataBuffer()).getData();
 208                 peer.updateWindowImpl(data, bi.getWidth(), bi.getHeight());
 209                 return true;
 210             } else if (bb instanceof VolatileImage) {
 211                 viBB = (VolatileImage)bb;
 212                 if (bb instanceof DestSurfaceProvider) {
 213                     Surface s = ((DestSurfaceProvider)bb).getDestSurface();
 214                     if (s instanceof BufImgSurfaceData) {
 215                         // the image is probably lost, upload the data from the
 216                         // backup surface to avoid creating another heap-based
 217                         // image (the parent's buffer)
 218                         int w = viBB.getWidth();
 219                         int h = viBB.getHeight();
 220                         BufImgSurfaceData bisd = (BufImgSurfaceData)s;
 221                         int data[] = ((DataBufferInt)bisd.getRaster(0,0,w,h).
 222                             getDataBuffer()).getData();
 223                         peer.updateWindowImpl(data, w, h);
 224                         return true;
 225                     }
 226                 }
 227             }
 228 
 229             // copy the passed image into our own buffer, then upload
 230             BufferedImage bi = (BufferedImage)clearImage(backBuffer);
 231 
 232             int data[] =
 233                 ((DataBufferInt)bi.getRaster().getDataBuffer()).getData();
 234             peer.updateWindowImpl(data, bi.getWidth(), bi.getHeight());
 235 
 236             return (viBB != null ? !viBB.contentsLost() : true);
 237         }
 238 
 239         @Override
 240         public void flush() {
 241             if (backBuffer != null) {
 242                 backBuffer.flush();
 243                 backBuffer = null;
 244             }
 245         }
 246     }
 247 
 248     /**
 249      * A version of the painter which uses VolatileImage as the internal buffer.
 250      * The window is painted into this VI and then copied into the parent's
 251      * Java heap-based buffer (which is then uploaded to the layered window)
 252      */
 253     private static class VIWindowPainter extends BIWindowPainter {
 254         private VolatileImage viBB;
 255 
 256         protected VIWindowPainter(WWindowPeer peer) {
 257             super(peer);
 258         }
 259 
 260         @Override
 261         protected Image getBackBuffer(boolean clear) {
 262             int w = window.getWidth();
 263             int h = window.getHeight();
 264             GraphicsConfiguration gc = peer.getGraphicsConfiguration();
 265 
 266             if (viBB == null || viBB.getWidth() != w || viBB.getHeight() != h ||
 267                 viBB.validate(gc) == IMAGE_INCOMPATIBLE)
 268             {
 269                 flush();
 270 
 271                 if (gc instanceof AccelGraphicsConfig) {
 272                     AccelGraphicsConfig agc = ((AccelGraphicsConfig)gc);
 273                     viBB = agc.createCompatibleVolatileImage(w, h,
 274                                                              TRANSLUCENT,
 275                                                              RT_PLAIN);
 276                 }
 277                 if (viBB == null) {
 278                     viBB = gc.createCompatibleVolatileImage(w, h, TRANSLUCENT);
 279                 }
 280                 viBB.validate(gc);
 281             }
 282 
 283             return clear ? clearImage(viBB) : viBB;
 284         }
 285 
 286         @Override
 287         public void flush() {
 288             if (viBB != null) {
 289                 viBB.flush();
 290                 viBB = null;
 291             }
 292         }
 293     }
 294 
 295     /**
 296      * Optimized version of hw painter. Uses VolatileImages for the
 297      * buffer, and uses an optimized path to pull the data from those into
 298      * the layered window, bypassing Java heap-based image.
 299      */
 300     private abstract static class VIOptWindowPainter extends VIWindowPainter {
 301 
 302         protected VIOptWindowPainter(WWindowPeer peer) {
 303             super(peer);
 304         }
 305 
 306         protected abstract boolean updateWindowAccel(long psdops, int w, int h);
 307 
 308         @Override
 309         protected boolean update(Image bb) {
 310             if (bb instanceof DestSurfaceProvider) {
 311                 Surface s = ((DestSurfaceProvider)bb).getDestSurface();
 312                 if (s instanceof AccelSurface) {
 313                     final boolean arr[] = { false };
 314                     final AccelSurface as = (AccelSurface)s;
 315                     final int w = as.getBounds().width;
 316                     final int h = as.getBounds().height;
 317                     RenderQueue rq = as.getContext().getRenderQueue();
 318                     rq.lock();
 319                     try {
 320                         BufferedContext.validateContext(as);
 321                         rq.flushAndInvokeNow(new Runnable() {
 322                             @Override
 323                             public void run() {
 324                                 long psdops = as.getNativeOps();
 325                                 arr[0] = updateWindowAccel(psdops, w, h);
 326                             }
 327                         });
 328                     } catch (InvalidPipeException e) {
 329                         // ignore, false will be returned
 330                     } finally {
 331                         rq.unlock();
 332                     }
 333                     return arr[0];
 334                 }
 335             }
 336             return super.update(bb);
 337         }
 338     }
 339 
 340     private static class VIOptD3DWindowPainter extends VIOptWindowPainter {
 341 
 342         protected VIOptD3DWindowPainter(WWindowPeer peer) {
 343             super(peer);
 344         }
 345 
 346         @Override
 347         protected boolean updateWindowAccel(long psdops, int w, int h) {
 348             // note: this method is executed on the toolkit thread, no sync is
 349             // necessary at the native level, and a pointer to peer can be used
 350             return sun.java2d.d3d.D3DSurfaceData.
 351                 updateWindowAccelImpl(psdops, peer.getData(), w, h);
 352         }
 353     }
 354 
 355     private static class VIOptWGLWindowPainter extends VIOptWindowPainter {
 356 
 357         protected VIOptWGLWindowPainter(WWindowPeer peer) {
 358             super(peer);
 359         }
 360 
 361         @Override
 362         protected boolean updateWindowAccel(long psdops, int w, int h) {
 363             // note: part of this method which deals with GDI will be on the
 364             // toolkit thread
 365             return sun.java2d.opengl.WGLSurfaceData.
 366                 updateWindowAccelImpl(psdops, peer, w, h);
 367         }
 368     }
 369 }