modules/graphics/src/main/java/com/sun/javafx/tk/quantum/UploadingPainter.java

Print this page




   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 
  26 package com.sun.javafx.tk.quantum;
  27 
  28 import java.nio.IntBuffer;
  29 import com.sun.glass.ui.Application;
  30 import com.sun.glass.ui.Pixels;
  31 import com.sun.prism.Graphics;
  32 import com.sun.prism.GraphicsPipeline;
  33 import com.sun.prism.RTTexture;
  34 import com.sun.prism.Texture.WrapMode;
  35 import com.sun.prism.impl.BufferUtil;
  36 import com.sun.prism.impl.Disposer;
  37 import com.sun.prism.impl.QueuedPixelSource;
  38 
  39 /**
  40  * UploadingPainter is used when we need to render into an offscreen buffer.
  41  * The PresentingPainter is used when we are rendering to the main screen.
  42  */
  43 final class UploadingPainter extends ViewPainter implements Runnable {
  44 
  45     private Application app = Application.GetApplication();
  46     private RTTexture   rttexture;
  47     // resolveRTT is a temporary render target to "resolve" a msaa render buffer
  48     // into a normal color render target.
  49     private RTTexture   resolveRTT = null;
  50 
  51     private QueuedPixelSource pixelSource = new QueuedPixelSource();

  52     private volatile float pixScaleFactor = 1.0f;
  53 
  54     UploadingPainter(GlassScene view) {
  55         super(view);
  56     }
  57 
  58     void disposeRTTexture() {
  59         if (rttexture != null) {
  60             rttexture.dispose();
  61             rttexture = null;
  62         }
  63         if (resolveRTT != null) {
  64             resolveRTT.dispose();
  65             resolveRTT = null;
  66         }
  67     }
  68     
  69     public void setPixelScaleFactor(float scale) {
  70         pixScaleFactor = scale;
  71     }


  82         try {
  83             if (!validateStageGraphics()) {
  84                 if (QuantumToolkit.verbose) {
  85                     System.err.println("UploadingPainter: validateStageGraphics failed");
  86                 }
  87                 paintImpl(null);
  88                 return;
  89             }
  90             
  91             if (factory == null) {
  92                 factory = GraphicsPipeline.getDefaultResourceFactory();
  93             }
  94             if (factory == null || !factory.isDeviceReady()) {
  95                 return;
  96             }
  97 
  98             float scale = pixScaleFactor;
  99             int bufWidth = Math.round(viewWidth * scale);
 100             int bufHeight = Math.round(viewHeight * scale);
 101 
 102             boolean needsReset = (pixelSource.validate(bufWidth, bufHeight, scale) ||
 103                                   penWidth != viewWidth ||
 104                                   penHeight != viewHeight ||
 105                                   rttexture == null);
 106 
 107             if (!needsReset) {
 108                 rttexture.lock();
 109                 if (rttexture.isSurfaceLost()) {
 110                     rttexture.unlock();
 111                     sceneState.getScene().entireSceneNeedsRepaint();
 112                     needsReset = true;
 113                 }
 114             }
 115 
 116             if (needsReset) {
 117                 disposeRTTexture();
 118                 rttexture = factory.createRTTexture(bufWidth, bufHeight, WrapMode.CLAMP_NOT_NEEDED,
 119                         sceneState.isAntiAliasing());
 120                 if (rttexture == null) {
 121                     return;
 122                 }

 123                 penWidth    = viewWidth;
 124                 penHeight   = viewHeight;
 125                 freshBackBuffer = true;
 126             }
 127             Graphics g = rttexture.createGraphics();
 128             if (g == null) {
 129                 disposeRTTexture();
 130                 sceneState.getScene().entireSceneNeedsRepaint();
 131                 return;
 132             }
 133             g.scale(scale, scale);
 134             paintImpl(g);
 135             freshBackBuffer = false;
 136 
 137             Pixels pix = pixelSource.getUnusedPixels();
 138             IntBuffer bits;
 139             if (pix != null) {
 140                 bits = (IntBuffer) pix.getPixels();
 141             } else {
 142                 bits = BufferUtil.newIntBuffer(bufWidth * bufHeight);
 143                 pix = app.createPixels(bufWidth, bufHeight, bits, scale);
 144             }
 145 
 146             int rawbits[] = rttexture.getPixels();
 147             
 148             if (rawbits != null) {
 149                 bits.put(rawbits, 0, bufWidth * bufHeight);
 150             } else {
 151                 RTTexture rtt = rttexture.isAntiAliasing() ?
 152                     resolveRenderTarget(g) : rttexture;
 153 
 154                 if (!rtt.readPixels(bits)) {
 155                     /* device lost */
 156                     sceneState.getScene().entireSceneNeedsRepaint();
 157                     disposeRTTexture();
 158                     pix = null;
 159                 }
 160             }
 161 
 162             if (rttexture != null) {
 163                 rttexture.unlock();
 164             }




   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 
  26 package com.sun.javafx.tk.quantum;
  27 
  28 import java.nio.IntBuffer;

  29 import com.sun.glass.ui.Pixels;
  30 import com.sun.prism.Graphics;
  31 import com.sun.prism.GraphicsPipeline;
  32 import com.sun.prism.RTTexture;
  33 import com.sun.prism.Texture.WrapMode;

  34 import com.sun.prism.impl.Disposer;
  35 import com.sun.prism.impl.QueuedPixelSource;
  36 
  37 /**
  38  * UploadingPainter is used when we need to render into an offscreen buffer.
  39  * The PresentingPainter is used when we are rendering to the main screen.
  40  */
  41 final class UploadingPainter extends ViewPainter implements Runnable {
  42 

  43     private RTTexture   rttexture;
  44     // resolveRTT is a temporary render target to "resolve" a msaa render buffer
  45     // into a normal color render target.
  46     private RTTexture   resolveRTT = null;
  47 
  48     private QueuedPixelSource pixelSource = new QueuedPixelSource(true);
  49     private float penScale;
  50     private volatile float pixScaleFactor = 1.0f;
  51 
  52     UploadingPainter(GlassScene view) {
  53         super(view);
  54     }
  55 
  56     void disposeRTTexture() {
  57         if (rttexture != null) {
  58             rttexture.dispose();
  59             rttexture = null;
  60         }
  61         if (resolveRTT != null) {
  62             resolveRTT.dispose();
  63             resolveRTT = null;
  64         }
  65     }
  66     
  67     public void setPixelScaleFactor(float scale) {
  68         pixScaleFactor = scale;
  69     }


  80         try {
  81             if (!validateStageGraphics()) {
  82                 if (QuantumToolkit.verbose) {
  83                     System.err.println("UploadingPainter: validateStageGraphics failed");
  84                 }
  85                 paintImpl(null);
  86                 return;
  87             }
  88             
  89             if (factory == null) {
  90                 factory = GraphicsPipeline.getDefaultResourceFactory();
  91             }
  92             if (factory == null || !factory.isDeviceReady()) {
  93                 return;
  94             }
  95 
  96             float scale = pixScaleFactor;
  97             int bufWidth = Math.round(viewWidth * scale);
  98             int bufHeight = Math.round(viewHeight * scale);
  99 
 100             boolean needsReset = (penScale != scale ||
 101                                   penWidth != viewWidth ||
 102                                   penHeight != viewHeight ||
 103                                   rttexture == null);
 104 
 105             if (!needsReset) {
 106                 rttexture.lock();
 107                 if (rttexture.isSurfaceLost()) {
 108                     rttexture.unlock();
 109                     sceneState.getScene().entireSceneNeedsRepaint();
 110                     needsReset = true;
 111                 }
 112             }
 113 
 114             if (needsReset) {
 115                 disposeRTTexture();
 116                 rttexture = factory.createRTTexture(bufWidth, bufHeight, WrapMode.CLAMP_NOT_NEEDED,
 117                         sceneState.isAntiAliasing());
 118                 if (rttexture == null) {
 119                     return;
 120                 }
 121                 penScale    = scale;
 122                 penWidth    = viewWidth;
 123                 penHeight   = viewHeight;
 124                 freshBackBuffer = true;
 125             }
 126             Graphics g = rttexture.createGraphics();
 127             if (g == null) {
 128                 disposeRTTexture();
 129                 sceneState.getScene().entireSceneNeedsRepaint();
 130                 return;
 131             }
 132             g.scale(scale, scale);
 133             paintImpl(g);
 134             freshBackBuffer = false;
 135 
 136             Pixels pix = pixelSource.getUnusedPixels(bufWidth, bufHeight, scale);
 137             IntBuffer bits = (IntBuffer) pix.getPixels();






 138 
 139             int rawbits[] = rttexture.getPixels();
 140             
 141             if (rawbits != null) {
 142                 bits.put(rawbits, 0, bufWidth * bufHeight);
 143             } else {
 144                 RTTexture rtt = rttexture.isAntiAliasing() ?
 145                     resolveRenderTarget(g) : rttexture;
 146 
 147                 if (!rtt.readPixels(bits)) {
 148                     /* device lost */
 149                     sceneState.getScene().entireSceneNeedsRepaint();
 150                     disposeRTTexture();
 151                     pix = null;
 152                 }
 153             }
 154 
 155             if (rttexture != null) {
 156                 rttexture.unlock();
 157             }