1 /*
   2  * Copyright (c) 2011, 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 
  26 package com.sun.javafx.tk.quantum;
  27 
  28 import com.sun.javafx.logging.PulseLogger;
  29 import static com.sun.javafx.logging.PulseLogger.PULSE_LOGGING_ENABLED;
  30 import com.sun.prism.Graphics;
  31 import com.sun.prism.GraphicsPipeline;
  32 import com.sun.prism.impl.Disposer;
  33 import com.sun.prism.impl.ManagedResource;
  34 
  35 /**
  36  * The PresentingPainter is used when we are rendering to the main screen.
  37  * UploadingPainter is used when we need to render into an offscreen buffer.
  38  */
  39 final class PresentingPainter extends ViewPainter {
  40 
  41     PresentingPainter(ViewScene view) {
  42         super(view);
  43     }
  44 
  45     @Override public void run() {
  46         renderLock.lock();
  47 
  48         boolean locked = false;
  49         boolean valid = false;
  50         boolean errored = false;
  51 
  52         try {
  53             valid = validateStageGraphics();
  54             if (!valid) {
  55                 if (QuantumToolkit.verbose) {
  56                     System.err.println("PresentingPainter: validateStageGraphics failed");
  57                 }
  58                 return;
  59             }
  60             
  61             /*
  62              * As Glass is responsible for creating the rendering contexts,
  63              * locking should be done prior to the Prism calls.
  64              */
  65             sceneState.lock();
  66             locked = true;
  67             
  68             if (factory == null) {
  69                 factory = GraphicsPipeline.getDefaultResourceFactory();
  70             }
  71             if (factory == null || !factory.isDeviceReady()) {
  72                 sceneState.getScene().entireSceneNeedsRepaint();
  73                 return;
  74             }
  75 
  76             if (presentable != null && presentable.lockResources(sceneState)) {
  77                 disposePresentable();
  78             }
  79             if (presentable == null) {
  80                 presentable = factory.createPresentable(sceneState);
  81                 penWidth  = viewWidth;
  82                 penHeight = viewHeight;
  83             }
  84             
  85             if (presentable != null) {
  86                 Graphics g = presentable.createGraphics();
  87 
  88                 ViewScene vs = (ViewScene) sceneState.getScene();
  89                 if (g != null) {
  90                     paintImpl(g);
  91                 }
  92 
  93                 if (PULSE_LOGGING_ENABLED) PulseLogger.newPhase("Presenting");
  94                 if (!presentable.prepare(null)) {
  95                     disposePresentable();
  96                     sceneState.getScene().entireSceneNeedsRepaint();
  97                     return;
  98                 }
  99                 
 100                 /* present for vsync buffer swap */
 101                 if (vs.getDoPresent()) {
 102                     if (!presentable.present()) {
 103                         disposePresentable();
 104                         sceneState.getScene().entireSceneNeedsRepaint();
 105                     }
 106                 }
 107             }
 108         } catch (Throwable th) {
 109             errored = true;
 110             th.printStackTrace(System.err);
 111         } finally {
 112             if (valid) {
 113                 Disposer.cleanUp();
 114             }
 115             if (locked) {
 116                 sceneState.unlock();
 117             }
 118 
 119             ViewScene viewScene = (ViewScene)sceneState.getScene();
 120             viewScene.setPainting(false);
 121 
 122             ManagedResource.freeDisposalRequestedAndCheckResources(errored);
 123 
 124             renderLock.unlock();
 125         }
 126     }
 127 }