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 
  34 /**
  35  * The PresentingPainter is used when we are rendering to the main screen.
  36  * UploadingPainter is used when we need to render into an offscreen buffer.
  37  */
  38 final class PresentingPainter extends ViewPainter {
  39 
  40     PresentingPainter(ViewScene view) {
  41         super(view);
  42     }
  43 
  44     @Override public void run() {
  45         renderLock.lock();
  46 
  47         boolean locked = false;
  48         boolean valid = false;
  49         boolean errored = false;
  50 
  51         try {
  52             valid = validateStageGraphics();
  53             if (!valid) {
  54                 if (QuantumToolkit.verbose) {
  55                     System.err.println("PresentingPainter: validateStageGraphics failed");
  56                 }
  57                 paintImpl(null);
  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                 freshBackBuffer = true;
  84             }
  85             
  86             if (presentable != null) {
  87                 Graphics g = presentable.createGraphics();
  88 
  89                 ViewScene vs = (ViewScene) sceneState.getScene();
  90                 if (g != null) {
  91                     paintImpl(g);
  92                     freshBackBuffer = false;
  93                 }
  94 
  95                 if (PULSE_LOGGING_ENABLED) {
  96                     PulseLogger.newPhase("Presenting");
  97                 }
  98                 if (!presentable.prepare(null)) {
  99                     disposePresentable();
 100                     sceneState.getScene().entireSceneNeedsRepaint();
 101                     return;
 102                 }
 103                 
 104                 /* present for vsync buffer swap */
 105                 if (vs.getDoPresent()) {
 106                     if (!presentable.present()) {
 107                         disposePresentable();
 108                         sceneState.getScene().entireSceneNeedsRepaint();
 109                     }
 110                 }
 111             }
 112         } catch (Throwable th) {
 113             errored = true;
 114             th.printStackTrace(System.err);
 115         } finally {
 116             Disposer.cleanUp();
 117 
 118             if (locked) {
 119                 sceneState.unlock();
 120             }
 121 
 122             ViewScene viewScene = (ViewScene)sceneState.getScene();
 123             viewScene.setPainting(false);
 124 
 125             factory.getTextureResourcePool().freeDisposalRequestedAndCheckResources(errored);
 126 
 127             renderLock.unlock();
 128         }
 129     }
 130 }