2 * Copyright 2014 JogAmp Community. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
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.
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.
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.
28 package jogamp.nativewindow;
30 import javax.media.nativewindow.NativeWindowFactory;
31 import javax.media.nativewindow.ScalableSurface;
34 * Basic {@link ScalableSurface} utility to validate and compute pixel-scale values.
36 public class SurfaceScaleUtils {
38 private static final int[] PlatformMaxPixelScale;
39 private static final boolean PlatformUniformPixelScale;
40 private static final boolean PlatformPixelScaleSupported;
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;
48 PlatformMaxPixelScale = new int[] { ScalableSurface.IDENTITY_PIXELSCALE, ScalableSurface.IDENTITY_PIXELSCALE };
49 PlatformUniformPixelScale = false;
50 PlatformPixelScaleSupported = false;
55 * Compute a new valid pixelScale to be used by {@link NativeSurface} implementations,
56 * based on the given request and surface's pixelScale
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
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];
73 result[0] = surfPixelScaleSafeX;
74 result[1] = surfPixelScaleSafeY;
76 result[0] = ScalableSurface.IDENTITY_PIXELSCALE;
77 result[1] = ScalableSurface.IDENTITY_PIXELSCALE;
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)");
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[])}
98 * To be used by {@link ScalableSurface#setSurfaceScale(int[])} implementations.
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
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];
120 if( reqPixelScale[0] >= PlatformMaxPixelScale[0] ) {
121 result[0] = PlatformMaxPixelScale[0];
123 result[0] = reqPixelScale[0];
125 if( reqPixelScale[1] >= PlatformMaxPixelScale[1] ) {
126 result[1] = PlatformMaxPixelScale[1];
128 result[1] = reqPixelScale[1];
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)");
139 * Replaces {@link ScalableSurface#AUTOMAX_PIXELSCALE} with {@link #getPlatformMaxPixelScale(int[])},
140 * for each component.
142 * @param pixelScale int[2] value array to be tested and replaced
144 public static void replaceAutoMaxWithPlatformMax(final int[] pixelScale) {
145 if( ScalableSurface.AUTOMAX_PIXELSCALE == pixelScale[0] ) {
146 pixelScale[0] = PlatformMaxPixelScale[0];
148 if( ScalableSurface.AUTOMAX_PIXELSCALE == pixelScale[1] ) {
149 pixelScale[1] = PlatformMaxPixelScale[1];
154 * Returns the maximum platform pixelScale
156 public static int[] getPlatformMaxPixelScale(final int[] result) {
157 System.arraycopy(PlatformMaxPixelScale, 0, result, 0, 2);
162 * Returns true if platform pixelScale is uniform, i.e. same scale factor for x- and y-direction, otherwise false.
164 public static boolean isPlatformPixelScaleUniform() {
165 return PlatformUniformPixelScale;
169 * Returns whether the platform supports pixelScale
171 public static boolean isPlatformPixelScaleSupported() {
172 return PlatformPixelScaleSupported;