1 /*
2  * Copyright (c) 2007, 2012, 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 sun.java2d;
27 
28 import java.awt.Color;
29 import java.awt.Font;
30 import java.awt.Graphics2D;
31 import sun.awt.Win32GraphicsConfig;
32 import sun.awt.windows.WComponentPeer;
33 import sun.java2d.d3d.D3DScreenUpdateManager;
34 import sun.java2d.windows.WindowsFlags;
35 
36 /**
37  * This class handles the creation of on-screen surfaces and
38  * corresponding graphics objects.
39  *
40  * By default it delegates the surface creation to the
41  * particular GraphicsConfiguration classes.
42  */
43 public class ScreenUpdateManager {
44     private static ScreenUpdateManager theInstance;
45 
ScreenUpdateManager()46     protected ScreenUpdateManager() {
47     }
48 
49     /**
50      * Creates a SunGraphics2D object for the surface,
51      * given the parameters.
52      *
53      * @param sd surface data for which a graphics is to be created
54      * @param peer peer which owns the surface
55      * @param fgColor fg color to be used in the graphics
56      * @param bgColor bg color to be used in the graphics
57      * @param font font to be used in the graphics
58      * @return a SunGraphics2D object for rendering to the passed surface
59      */
createGraphics(SurfaceData sd, WComponentPeer peer, Color fgColor, Color bgColor, Font font)60     public synchronized Graphics2D createGraphics(SurfaceData sd,
61             WComponentPeer peer, Color fgColor, Color bgColor, Font font)
62     {
63         return new SunGraphics2D(sd, fgColor, bgColor, font);
64     }
65 
66     /**
67      * Creates and returns the surface for the peer. This surface becomes
68      * managed by this manager. To remove the surface from the managed list
69      * {@code}dropScreenSurface(SurfaceData){@code} will need to be called.
70      *
71      * The default implementation delegates surface creation
72      * to the passed in GraphicsConfiguration object.
73      *
74      * @param gc graphics configuration for which the surface is to be created
75      * @param peer peer for which the onscreen surface is to be created
76      * @param bbNum number of back-buffers requested for this peer
77      * @param isResize whether this surface is being created in response to
78      * a component resize event
79      * @return a SurfaceData to be used for on-screen rendering for this peer.
80      * @see #dropScreenSurface(SurfaceData)
81      */
createScreenSurface(Win32GraphicsConfig gc, WComponentPeer peer, int bbNum, boolean isResize)82     public SurfaceData createScreenSurface(Win32GraphicsConfig gc,
83                                            WComponentPeer peer, int bbNum,
84                                            boolean isResize)
85     {
86         return gc.createSurfaceData(peer, bbNum);
87     }
88 
89     /**
90      * Drops the passed surface from the list of managed surfaces.
91      *
92      * Nothing happens if the surface wasn't managed by this manager.
93      *
94      * @param sd SurfaceData to be removed from the list of managed surfaces
95      */
dropScreenSurface(SurfaceData sd)96     public void dropScreenSurface(SurfaceData sd) {}
97 
98     /**
99      * Returns a replacement SurfaceData for the invalid passed one.
100      *
101      * This method should be used by SurfaceData's created by
102      * the ScreenUpdateManager for providing replacement surfaces.
103      *
104      * @param peer to which the old surface belongs
105      * @param oldsd the old (invalid) surface to get replaced
106      * @return a replacement surface
107      * @see sun.java2d.d3d.D3DSurfaceData.D3DWindowSurfaceData#getReplacement()
108      * @see sun.java2d.windows.GDIWindowSurfaceData#getReplacement()
109      */
getReplacementScreenSurface(WComponentPeer peer, SurfaceData oldsd)110     public SurfaceData getReplacementScreenSurface(WComponentPeer peer,
111                                                    SurfaceData oldsd)
112     {
113         SurfaceData surfaceData = peer.getSurfaceData();
114         if (surfaceData == null || surfaceData.isValid()) {
115             return surfaceData;
116         }
117         peer.replaceSurfaceData();
118         return peer.getSurfaceData();
119     }
120 
121     /**
122      * Returns an (singleton) instance of the screen surfaces
123      * manager class.
124      * @return instance of onscreen surfaces manager
125      */
getInstance()126     public static synchronized ScreenUpdateManager getInstance() {
127         if (theInstance == null) {
128             if (WindowsFlags.isD3DEnabled()) {
129                 theInstance = new D3DScreenUpdateManager();
130             } else {
131                 theInstance = new ScreenUpdateManager();
132             }
133         }
134         return theInstance;
135     }
136 }
137