1 /*
   2  * Copyright (c) 2008, 2014, 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             

 258             GraphicsConfiguration gc = peer.getGraphicsConfiguration();
 259          
 260             AffineTransform tx = gc.getDefaultTransform();
 261             int w = Region.clipScale(window.getWidth(), tx.getScaleX());
 262             int h = Region.clipScale(window.getHeight(), tx.getScaleY());
 263             
 264             if (viBB == null || viBB.getWidth() != w || viBB.getHeight() != h ||
 265                 viBB.validate(gc) == IMAGE_INCOMPATIBLE)
 266             {
 267                 flush();
 268 
 269                 if (gc instanceof AccelGraphicsConfig) {
 270                     AccelGraphicsConfig agc = ((AccelGraphicsConfig)gc);
 271                     viBB = agc.createCompatibleVolatileImage(w, h,
 272                                                              TRANSLUCENT,
 273                                                              RT_PLAIN);
 274                 }
 275                 if (viBB == null) {
 276                     viBB = gc.createCompatibleVolatileImage(w, h, TRANSLUCENT);
 277                 }
 278                 viBB.validate(gc);
 279             }
 280 
 281             return clear ? clearImage(viBB) : viBB;
 282         }
 283 
 284         @Override
 285         public void flush() {
 286             if (viBB != null) {
 287                 viBB.flush();
 288                 viBB = null;
 289             }
 290         }
 291     }
 292 
 293     /**
 294      * Optimized version of hw painter. Uses VolatileImages for the
 295      * buffer, and uses an optimized path to pull the data from those into
 296      * the layered window, bypassing Java heap-based image.
 297      */
 298     private abstract static class VIOptWindowPainter extends VIWindowPainter {
 299 
 300         protected VIOptWindowPainter(WWindowPeer peer) {
 301             super(peer);
 302         }
 303 
 304         protected abstract boolean updateWindowAccel(long psdops, int w, int h);
 305 
 306         @Override
 307         protected boolean update(Image bb) {
 308             if (bb instanceof DestSurfaceProvider) {
 309                 Surface s = ((DestSurfaceProvider)bb).getDestSurface();
 310                 if (s instanceof AccelSurface) {
 311                     final int w = bb.getWidth(null);
 312                     final int h = bb.getHeight(null);
 313                     final boolean arr[] = { false };
 314                     final AccelSurface as = (AccelSurface)s;
 315                     RenderQueue rq = as.getContext().getRenderQueue();
 316                     rq.lock();
 317                     try {
 318                         BufferedContext.validateContext(as);
 319                         rq.flushAndInvokeNow(new Runnable() {
 320                             @Override
 321                             public void run() {
 322                                 long psdops = as.getNativeOps();
 323                                 arr[0] = updateWindowAccel(psdops, w, h);
 324                             }
 325                         });
 326                     } catch (InvalidPipeException e) {
 327                         // ignore, false will be returned
 328                     } finally {
 329                         rq.unlock();
 330                     }
 331                     return arr[0];
 332                 }
 333             }
 334             return super.update(bb);
 335         }
 336     }
 337 
 338     private static class VIOptD3DWindowPainter extends VIOptWindowPainter {
 339 
 340         protected VIOptD3DWindowPainter(WWindowPeer peer) {
 341             super(peer);
 342         }
 343 
 344         @Override
 345         protected boolean updateWindowAccel(long psdops, int w, int h) {
 346             // note: this method is executed on the toolkit thread, no sync is
 347             // necessary at the native level, and a pointer to peer can be used
 348             return sun.java2d.d3d.D3DSurfaceData.
 349                 updateWindowAccelImpl(psdops, peer.getData(), w, h);
 350         }
 351     }
 352 
 353     private static class VIOptWGLWindowPainter extends VIOptWindowPainter {
 354 
 355         protected VIOptWGLWindowPainter(WWindowPeer peer) {
 356             super(peer);
 357         }
 358 
 359         @Override
 360         protected boolean updateWindowAccel(long psdops, int w, int h) {
 361             // note: part of this method which deals with GDI will be on the
 362             // toolkit thread
 363             return sun.java2d.opengl.WGLSurfaceData.
 364                 updateWindowAccelImpl(psdops, peer, w, h);
 365         }
 366     }
 367 }
--- EOF ---