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 51 UploadingPainter(GlassScene view) { 52 super(view); 53 } 54 55 void disposeRTTexture() { 56 if (rttexture != null) { 57 rttexture.dispose(); 58 rttexture = null; 59 } 60 if (resolveRTT != null) { 61 resolveRTT.dispose(); 62 resolveRTT = null; 63 } 64 } 65 66 @Override 67 public float getPixelScaleFactor() { 68 return sceneState.getRenderScale(); 69 } 70 71 @Override public void run() { 72 renderLock.lock(); 73 74 boolean errored = false; 75 try { 76 if (!validateStageGraphics()) { 77 if (QuantumToolkit.verbose) { 78 System.err.println("UploadingPainter: validateStageGraphics failed"); 79 } 80 paintImpl(null); 81 return; 82 } 83 84 if (factory == null) { 85 factory = GraphicsPipeline.getDefaultResourceFactory(); 86 } 87 if (factory == null || !factory.isDeviceReady()) { 88 return; 89 } 90 91 float scale = getPixelScaleFactor(); 92 int bufWidth = sceneState.getRenderWidth(); 93 int bufHeight = sceneState.getRenderHeight(); 94 95 // Repaint everything on pen scale or view size change because 96 // texture contents are no longer correct. 97 // Repaint everything on new texture dimensions because otherwise 98 // our upload logic below may fail with index out of bounds. 99 boolean needsReset = (penScale != scale || 100 penWidth != viewWidth || 101 penHeight != viewHeight || 102 rttexture == null || 103 rttexture.getContentWidth() != bufWidth || 104 rttexture.getContentHeight() != bufHeight); 105 106 if (!needsReset) { 107 rttexture.lock(); 108 if (rttexture.isSurfaceLost()) { 109 rttexture.unlock(); 110 sceneState.getScene().entireSceneNeedsRepaint(); 111 needsReset = true; 112 } 113 } 114 115 if (needsReset) { 116 disposeRTTexture(); 117 rttexture = factory.createRTTexture(bufWidth, bufHeight, WrapMode.CLAMP_NOT_NEEDED, 118 sceneState.isMSAA()); 119 if (rttexture == null) { 120 return; 121 } 122 penScale = scale; 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 int outWidth = sceneState.getOutputWidth(); 138 int outHeight = sceneState.getOutputHeight(); 139 float outScale = sceneState.getOutputScale(); 140 RTTexture rtt; 141 if (rttexture.isMSAA() || outWidth != bufWidth || outHeight != bufHeight) { 142 rtt = resolveRenderTarget(g, outWidth, outHeight); 143 } else { 144 rtt = rttexture; 145 } 146 147 Pixels pix = pixelSource.getUnusedPixels(outWidth, outHeight, outScale); 148 IntBuffer bits = (IntBuffer) pix.getPixels(); 149 150 int rawbits[] = rtt.getPixels(); 151 152 if (rawbits != null) { 153 bits.put(rawbits, 0, outWidth * outHeight); 154 } else { 155 if (!rtt.readPixels(bits)) { 156 /* device lost */ 157 sceneState.getScene().entireSceneNeedsRepaint(); 158 disposeRTTexture(); 159 pix = null; 160 } 161 } 162 163 if (rttexture != null) { 164 rttexture.unlock(); 165 } 166 167 if (pix != null) { | 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 penScaleX, penScaleY; 50 51 UploadingPainter(GlassScene view) { 52 super(view); 53 } 54 55 void disposeRTTexture() { 56 if (rttexture != null) { 57 rttexture.dispose(); 58 rttexture = null; 59 } 60 if (resolveRTT != null) { 61 resolveRTT.dispose(); 62 resolveRTT = null; 63 } 64 } 65 66 @Override 67 public float getPixelScaleFactorX() { 68 return sceneState.getRenderScaleX(); 69 } 70 71 @Override 72 public float getPixelScaleFactorY() { 73 return sceneState.getRenderScaleY(); 74 } 75 76 @Override public void run() { 77 renderLock.lock(); 78 79 boolean errored = false; 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 scalex = getPixelScaleFactorX(); 97 float scaley = getPixelScaleFactorY(); 98 int bufWidth = sceneState.getRenderWidth(); 99 int bufHeight = sceneState.getRenderHeight(); 100 101 // Repaint everything on pen scale or view size change because 102 // texture contents are no longer correct. 103 // Repaint everything on new texture dimensions because otherwise 104 // our upload logic below may fail with index out of bounds. 105 boolean needsReset = (penScaleX != scalex || 106 penScaleY != scaley || 107 penWidth != viewWidth || 108 penHeight != viewHeight || 109 rttexture == null || 110 rttexture.getContentWidth() != bufWidth || 111 rttexture.getContentHeight() != bufHeight); 112 113 if (!needsReset) { 114 rttexture.lock(); 115 if (rttexture.isSurfaceLost()) { 116 rttexture.unlock(); 117 sceneState.getScene().entireSceneNeedsRepaint(); 118 needsReset = true; 119 } 120 } 121 122 if (needsReset) { 123 disposeRTTexture(); 124 rttexture = factory.createRTTexture(bufWidth, bufHeight, WrapMode.CLAMP_NOT_NEEDED, 125 sceneState.isMSAA()); 126 if (rttexture == null) { 127 return; 128 } 129 penScaleX = scalex; 130 penScaleY = scaley; 131 penWidth = viewWidth; 132 penHeight = viewHeight; 133 freshBackBuffer = true; 134 } 135 Graphics g = rttexture.createGraphics(); 136 if (g == null) { 137 disposeRTTexture(); 138 sceneState.getScene().entireSceneNeedsRepaint(); 139 return; 140 } 141 g.scale(scalex, scaley); 142 paintImpl(g); 143 freshBackBuffer = false; 144 145 int outWidth = sceneState.getOutputWidth(); 146 int outHeight = sceneState.getOutputHeight(); 147 float outScaleX = sceneState.getOutputScaleX(); 148 float outScaleY = sceneState.getOutputScaleY(); 149 RTTexture rtt; 150 if (rttexture.isMSAA() || outWidth != bufWidth || outHeight != bufHeight) { 151 rtt = resolveRenderTarget(g, outWidth, outHeight); 152 } else { 153 rtt = rttexture; 154 } 155 156 Pixels pix = pixelSource.getUnusedPixels(outWidth, outHeight, outScaleX, outScaleY); 157 IntBuffer bits = (IntBuffer) pix.getPixels(); 158 159 int rawbits[] = rtt.getPixels(); 160 161 if (rawbits != null) { 162 bits.put(rawbits, 0, outWidth * outHeight); 163 } else { 164 if (!rtt.readPixels(bits)) { 165 /* device lost */ 166 sceneState.getScene().entireSceneNeedsRepaint(); 167 disposeRTTexture(); 168 pix = null; 169 } 170 } 171 172 if (rttexture != null) { 173 rttexture.unlock(); 174 } 175 176 if (pix != null) { |