Jogamp
Bug 741 HiDPI: Simplify ScalableSurface (2): Add request pixelScale API entry, fixed...
[jogl.git] / src / nativewindow / classes / jogamp / nativewindow / SurfaceScaleUtils.java
CommitLineData
2571ed0b
SG
1/**
2 * Copyright 2014 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package jogamp.nativewindow;
29
30import javax.media.nativewindow.NativeWindowFactory;
31import javax.media.nativewindow.ScalableSurface;
32
33/**
34 * Basic {@link ScalableSurface} utility to validate and compute pixel-scale values.
35 */
36public class SurfaceScaleUtils {
37
38 private static final int[] PlatformMaxPixelScale;
39 private static final boolean PlatformUniformPixelScale;
40 private static final boolean PlatformPixelScaleSupported;
41
42 static {
43 if( NativeWindowFactory.TYPE_MACOSX == NativeWindowFactory.getNativeWindowType(true) ) {
44 PlatformMaxPixelScale = new int[] { jogamp.nativewindow.macosx.OSXUtil.MAX_PIXELSCALE, jogamp.nativewindow.macosx.OSXUtil.MAX_PIXELSCALE };
45 PlatformUniformPixelScale = true;
46 PlatformPixelScaleSupported = true;
47 } else {
48 PlatformMaxPixelScale = new int[] { ScalableSurface.IDENTITY_PIXELSCALE, ScalableSurface.IDENTITY_PIXELSCALE };
49 PlatformUniformPixelScale = false;
50 PlatformPixelScaleSupported = false;
51 }
52 }
53
54 /**
55 * Compute a new valid pixelScale to be used by {@link NativeSurface} implementations,
56 * based on the given request and surface's pixelScale
57 *
58 * @param result int[2] storage for result, maybe same as <code>prePixelScale</code> for in-place
59 * @param prePixelScale previous pixelScale
60 * @param reqPixelScale requested pixelScale, validated via {@link #validateReqPixelScale(int[], int, String)}.
61 * @param surfPixelScaleRaw raw surface pixelScale
62 * @param DEBUG_PREFIX if set, dumps debug info on stderr using this prefix
63 * @return true if pixelScale has changed, otherwise false
64 */
65 public static boolean computePixelScale(int[] result, final int[] prePixelScale, final int[] reqPixelScale, final int[] surfPixelScaleRaw, final String DEBUG_PREFIX) {
66 final int surfPixelScaleSafeX = 0 < surfPixelScaleRaw[0] ? surfPixelScaleRaw[0] : ScalableSurface.IDENTITY_PIXELSCALE;
67 final int surfPixelScaleSafeY = 0 < surfPixelScaleRaw[1] ? surfPixelScaleRaw[1] : ScalableSurface.IDENTITY_PIXELSCALE;
68 final boolean useHiDPI = ScalableSurface.IDENTITY_PIXELSCALE != reqPixelScale[0] || ScalableSurface.IDENTITY_PIXELSCALE != reqPixelScale[1];
69 final int prePixelScaleX = prePixelScale[0];
70 final int prePixelScaleY = prePixelScale[1];
71
72 if( useHiDPI ) {
73 result[0] = surfPixelScaleSafeX;
74 result[1] = surfPixelScaleSafeY;
75 } else {
76 result[0] = ScalableSurface.IDENTITY_PIXELSCALE;
77 result[1] = ScalableSurface.IDENTITY_PIXELSCALE;
78 }
79
80 if( result[0] != prePixelScaleX || result[1] != prePixelScaleY ) {
81 if( null != DEBUG_PREFIX ) {
82 System.err.println(DEBUG_PREFIX+".computePixelScale: useHiDPI "+useHiDPI+", ["+prePixelScaleX+"x"+prePixelScaleY+" (pre), "+
83 reqPixelScale[0]+"x"+reqPixelScale[1]+" (req)] -> "+
84 surfPixelScaleRaw[0]+"x"+surfPixelScaleRaw[1]+" (raw) -> "+
85 surfPixelScaleSafeX+"x"+surfPixelScaleSafeY+" (safe) -> "+
86 result[0]+"x"+result[1]+" (use)");
87 }
88 return true;
89 } else {
90 return false;
91 }
92 }
93
94 /**
95 * Validate the given requested pixelScale value pair, i.e. clip it to the
96 * limits of {@link ScalableSurface#AUTOMAX_PIXELSCALE} and {@link #getPlatformMaxPixelScale(int[])}
97 * <p>
58153310 98 * To be used by {@link ScalableSurface#setSurfaceScale(int[])} implementations.
2571ed0b
SG
99 * </p>
100 *
101 * @param result int[2] storage for result
102 * @param reqPixelScale requested pixelScale
103 * @param DEBUG_PREFIX if set, dumps debug info on stderr using this prefix
104 */
105 public static void validateReqPixelScale(final int[] result, final int[] reqPixelScale, final String DEBUG_PREFIX) {
106 final int minPS = Math.min(reqPixelScale[0], reqPixelScale[1]);
107 if( ScalableSurface.AUTOMAX_PIXELSCALE >= minPS ) {
108 result[0] = ScalableSurface.AUTOMAX_PIXELSCALE;
109 result[1] = ScalableSurface.AUTOMAX_PIXELSCALE;
110 } else if( PlatformUniformPixelScale ) {
111 final int maxPS = Math.max(reqPixelScale[0], reqPixelScale[1]);
112 if( maxPS >= PlatformMaxPixelScale[0] ) {
113 result[0] = PlatformMaxPixelScale[0];
114 result[1] = PlatformMaxPixelScale[1];
115 } else {
116 result[0] = maxPS;
117 result[1] = maxPS;
118 }
119 } else {
120 if( reqPixelScale[0] >= PlatformMaxPixelScale[0] ) {
121 result[0] = PlatformMaxPixelScale[0];
122 } else {
123 result[0] = reqPixelScale[0];
124 }
125 if( reqPixelScale[1] >= PlatformMaxPixelScale[1] ) {
126 result[1] = PlatformMaxPixelScale[1];
127 } else {
128 result[1] = reqPixelScale[1];
129 }
130 }
131 if( null != DEBUG_PREFIX ) {
132 System.err.println(DEBUG_PREFIX+".validateReqPixelScale: ["+reqPixelScale[0]+"x"+reqPixelScale[1]+" (req), "+
133 PlatformMaxPixelScale[0]+"x"+PlatformMaxPixelScale[1]+" (max)] -> "+
134 result[0]+"x"+result[1]+" (valid)");
135 }
136 }
137
138 /**
139 * Replaces {@link ScalableSurface#AUTOMAX_PIXELSCALE} with {@link #getPlatformMaxPixelScale(int[])},
140 * for each component.
141 *
142 * @param pixelScale int[2] value array to be tested and replaced
143 */
144 public static void replaceAutoMaxWithPlatformMax(final int[] pixelScale) {
145 if( ScalableSurface.AUTOMAX_PIXELSCALE == pixelScale[0] ) {
146 pixelScale[0] = PlatformMaxPixelScale[0];
147 }
148 if( ScalableSurface.AUTOMAX_PIXELSCALE == pixelScale[1] ) {
149 pixelScale[1] = PlatformMaxPixelScale[1];
150 }
151 }
152
153 /**
154 * Returns the maximum platform pixelScale
155 */
156 public static int[] getPlatformMaxPixelScale(final int[] result) {
157 System.arraycopy(PlatformMaxPixelScale, 0, result, 0, 2);
158 return result;
159 }
160
161 /**
162 * Returns true if platform pixelScale is uniform, i.e. same scale factor for x- and y-direction, otherwise false.
163 */
164 public static boolean isPlatformPixelScaleUniform() {
165 return PlatformUniformPixelScale;
166 }
167
168 /**
169 * Returns whether the platform supports pixelScale
170 */
171 public static boolean isPlatformPixelScaleSupported() {
172 return PlatformPixelScaleSupported;
173 }
174
175}
http://JogAmp.org git info: FAQ, tutorial and man pages.