1 /* 2 * Copyright (c) 2013, 2015, 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 26 package com.sun.prism; 27 28 import com.sun.glass.ui.Application; 29 import com.sun.glass.ui.Pixels; 30 import com.sun.glass.ui.Screen; 31 import com.sun.glass.ui.View; 32 import com.sun.glass.ui.Window; 33 34 /** 35 * PresentableState is intended to provide for a shadow copy of View/Window 36 * state for use off the event thread. It is the task of the invoker of 37 * Prism to make sure that the state is consistent for a rendering probably 38 * by use of the AbstractPainter.renderLock to ensure consistent state. 39 */ 40 public abstract class PresentableState { 41 42 /** The underlying Window and View */ 43 protected Window window; 44 protected View view; 45 46 // Captured state 47 protected int nativeFrameBuffer; 48 protected int windowX, windowY; 49 protected float windowAlpha; 50 protected long nativeWindowHandle; 51 protected long nativeView; 52 protected int viewWidth, viewHeight; 53 protected float renderScale; 54 protected int renderWidth, renderHeight; 55 protected float outputScale; 56 protected int outputWidth, outputHeight; 57 protected int screenHeight; 58 protected int screenWidth; 59 protected boolean isWindowVisible; 60 protected boolean isWindowMinimized; 61 protected static final boolean hasWindowManager = 62 Application.GetApplication().hasWindowManager(); 63 // Between PaintCollector and *Painter, there is a window where 64 // the associated View can be closed. This variable allows us 65 // to shortcut the queued *Painter task. 66 protected boolean isClosed; 67 protected final int pixelFormat = Pixels.getNativeFormat(); 68 69 /** Create a PresentableState based on a View. 70 * 71 * Must be called on the event thread. 72 */ 73 public PresentableState() { 74 } 75 76 /** 77 * The screen relative window X 78 * @return The screen relative window X 79 * 80 * May be called on any thread. 81 */ 82 public int getWindowX() { 83 return windowX; 84 } 85 86 /** 87 * The screen relative window Y 88 * @return The screen relative window Y 89 * 90 * May be called on any thread. 91 */ 92 public int getWindowY() { 93 return windowY; 94 } 95 96 /** 97 * @return the width of the View 98 * 99 * May be called on any thread. 100 */ 101 public int getWidth() { 102 return viewWidth; 103 } 104 105 /** 106 * @return the height of the View 107 * 108 * May be called on any thread. 109 */ 110 public int getHeight() { 111 return viewHeight; 112 } 113 114 public int getRenderWidth() { 115 return renderWidth; 116 } 117 118 public int getRenderHeight() { 119 return renderHeight; 120 } 121 122 public int getOutputWidth() { 123 return outputWidth; 124 } 125 126 public int getOutputHeight() { 127 return outputHeight; 128 } 129 130 /** 131 * @return Screen.getScale 132 * 133 * May be called on any thread 134 */ 135 public float getRenderScale() { 136 return renderScale; 137 } 138 139 public float getOutputScale() { 140 return outputScale; 141 } 142 143 /** 144 * @return the window's alpha level 145 * 146 * May be called on any thread. 147 */ 148 public float getAlpha() { 149 return windowAlpha; 150 } 151 152 /** 153 * @return the native handle of the window represented by this 154 * PresentableState 155 * 156 * May be called on any thread. 157 */ 158 public long getNativeWindow() { 159 return nativeWindowHandle; 160 } 161 162 /** 163 * @return the native handle of the View represented by this 164 * PresentableState 165 * 166 * May be called on any thread. 167 */ 168 public long getNativeView() { 169 return nativeView; 170 } 171 172 /** 173 * @return the current height of the screen 174 * 175 * May be called on any thread. 176 */ 177 public int getScreenHeight() { 178 return screenHeight; 179 } 180 181 /** 182 * @return the current width of the screen 183 * 184 * May be called on any thread. 185 */ 186 public int getScreenWidth() { 187 return screenWidth; 188 } 189 190 /** 191 * @return true if the underlying View is closed, false otherwise 192 * 193 * May be called on any thread. 194 */ 195 public boolean isViewClosed() { 196 return isClosed; 197 } 198 199 /** 200 * @return true if the underlying Window is minimized, false otherwise 201 * 202 * May be called on any thread. 203 */ 204 public boolean isWindowMinimized() { 205 return isWindowMinimized; 206 } 207 208 /** 209 * @return true if the underlying Window is Visible, false otherwise 210 * 211 * May be called on any thread. 212 */ 213 public boolean isWindowVisible() { 214 return isWindowVisible; 215 } 216 217 /** 218 * @return true if the underlying window is managed by a window manager 219 * external to JavaFX 220 * 221 * May be called on any thread. 222 */ 223 public boolean hasWindowManager() { 224 return hasWindowManager; 225 } 226 227 /** 228 * @return the underlying Window 229 * 230 * May be called on any thread. 231 */ 232 public Window getWindow() { 233 return window; 234 } 235 236 public boolean isMSAA() { return false; } 237 238 /** 239 * @return the underlying View 240 * 241 * May be called on any thread. 242 */ 243 public View getView() { 244 return view; 245 } 246 247 /** 248 * @return native pixel format 249 * 250 * May be called on any thread. 251 */ 252 public int getPixelFormat() { 253 return pixelFormat; 254 } 255 256 /** 257 * @return native native frame buffer 258 * 259 * May be called on any thread. 260 */ 261 public int getNativeFrameBuffer() { 262 return nativeFrameBuffer; 263 } 264 265 /** 266 * Locks the underlying view for rendering 267 * 268 * Must be called on Prism renderer thread. 269 */ 270 public void lock() { 271 if (view != null) { 272 view.lock(); 273 nativeFrameBuffer = view.getNativeFrameBuffer(); 274 } 275 } 276 277 /** 278 * Unlocks the underlying view after rendering 279 * 280 * Must be called on Prism renderer thread. 281 */ 282 public void unlock() { 283 if (view != null) view.unlock(); 284 } 285 286 /** 287 * Put the pixels on the screen. 288 * 289 * @param source - the source for the Pixels object to be uploaded 290 */ 291 public void uploadPixels(PixelSource source) { 292 Pixels pixels = source.getLatestPixels(); 293 if (pixels != null) { 294 try { 295 view.uploadPixels(pixels); 296 } finally { 297 source.doneWithPixels(pixels); 298 } 299 } 300 } 301 302 private int scale(int dim, float fromScale, float toScale) { 303 return (fromScale == toScale) 304 ? dim 305 : (int) Math.ceil(dim * toScale / fromScale); 306 } 307 308 protected void update(float viewScale, float renderScale, float outputScale) { 309 this.renderScale = renderScale; 310 this.outputScale = outputScale; 311 if (renderScale == viewScale) { 312 renderWidth = viewWidth; 313 renderHeight = viewHeight; 314 } else { 315 renderWidth = scale(viewWidth, viewScale, renderScale); 316 renderHeight = scale(viewHeight, viewScale, renderScale); 317 } 318 if (outputScale == viewScale) { 319 outputWidth = viewWidth; 320 outputHeight = viewHeight; 321 } else if (outputScale == renderScale) { 322 outputWidth = renderWidth; 323 outputHeight = renderHeight; 324 } else { 325 outputWidth = scale(viewWidth, viewScale, outputScale); 326 outputHeight = scale(viewHeight, viewScale, outputScale); 327 } 328 } 329 330 /** Updates the state of this object based on the current state of its 331 * nativeWindow. 332 * 333 * May only be called from the event thread. 334 */ 335 public void update() { 336 // should only be called on the event thread 337 if (view != null) { 338 viewWidth = view.getWidth(); 339 viewHeight = view.getHeight(); 340 window = view.getWindow(); 341 } else { 342 viewWidth = viewHeight = -1; 343 window = null; 344 } 345 if (window != null) { 346 windowX = window.getX(); 347 windowY = window.getY(); 348 windowAlpha = window.getAlpha(); 349 nativeView = view.getNativeView(); 350 nativeWindowHandle = window.getNativeWindow(); 351 isClosed = view.isClosed(); 352 isWindowVisible = window.isVisible(); 353 isWindowMinimized = window.isMinimized(); 354 update(window.getPlatformScale(), 355 window.getRenderScale(), 356 window.getOutputScale()); 357 Screen screen = window.getScreen(); 358 if (screen != null) { 359 // note only used by Embedded Z order painting 360 // !hasWindowManager so should be safe to ignore 361 // when null, most likely because of "In Browswer" 362 screenHeight = screen.getHeight(); 363 screenWidth = screen.getWidth(); 364 } 365 } else { 366 //TODO - should other variables be cleared? 367 nativeView = -1; 368 nativeWindowHandle = -1; 369 isClosed = true; 370 } 371 } 372 }