8169922: SwingMark/TextArea: 2-7% regression on Linux, Mac, Windows in 9-b143

Reviewed-by: flar, serb
This commit is contained in:
Alexander Scherbatiy 2016-12-22 12:09:34 +03:00
parent 4c934b1e6a
commit 4d06c5d6fa
4 changed files with 114 additions and 113 deletions

View File

@ -27,10 +27,7 @@ package javax.swing.text;
import sun.swing.SwingUtilities2;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.security.AccessController;
import java.security.PrivilegedAction;
import javax.swing.JPasswordField;
import static javax.swing.text.PlainView.isFPMethodOverriden;
/**
* Implements a View suitable for use in JPasswordField
@ -332,22 +329,6 @@ public class PasswordView extends FieldView {
static char[] ONE = new char[1];
private final boolean drawEchoCharacterOverridden;
{
final Class<?> CLS = getClass();
final Class<?> INT = Integer.TYPE;
final Class<?> FP = Float.TYPE;
final Class<?> CHAR = Character.TYPE;
drawEchoCharacterOverridden = AccessController
.doPrivileged(new PrivilegedAction<Boolean>() {
@Override
public Boolean run() {
Class<?>[] intTypes = {Graphics.class, INT, INT, CHAR};
Class<?>[] fpTypes = {Graphics2D.class, FP, FP, CHAR};
return isFPMethodOverriden("drawEchoCharacter", CLS, intTypes, fpTypes);
}
});
}
private final boolean drawEchoCharacterOverridden =
getFPMethodOverridden(getClass(), "drawEchoCharacter", FPMethodArgs.GNNC);
}

View File

@ -32,6 +32,8 @@ import java.security.PrivilegedAction;
import java.util.Objects;
import javax.swing.event.*;
import java.lang.reflect.Module;
import java.lang.ref.SoftReference;
import java.util.HashMap;
/**
* Implements View interface for a simple multi-line text view
@ -818,10 +820,45 @@ public class PlainView extends View implements TabExpander {
return w;
}
static boolean isFPMethodOverriden(String method,
Class<?> cls,
Class<?>[] intTypes,
Class<?>[] fpTypes)
static boolean getFPMethodOverridden(Class<?> cls, String method,
FPMethodArgs methodArgs) {
HashMap<FPMethodItem, Boolean> map = null;
boolean initialized = methodsOverriddenMapRef != null
&& (map = methodsOverriddenMapRef.get()) != null;
if (!initialized) {
map = new HashMap<>();
methodsOverriddenMapRef = new SoftReference<>(map);
}
FPMethodItem key = new FPMethodItem(cls, method);
Boolean isFPMethodOverridden = map.get(key);
if (isFPMethodOverridden == null) {
isFPMethodOverridden = checkFPMethodOverridden(cls, method, methodArgs);
map.put(key, isFPMethodOverridden);
}
return isFPMethodOverridden;
}
private static boolean checkFPMethodOverridden(final Class<?> className,
final String methodName,
final FPMethodArgs methodArgs) {
return AccessController
.doPrivileged(new PrivilegedAction<Boolean>() {
@Override
public Boolean run() {
return isFPMethodOverridden(methodName, className,
methodArgs.getMethodArguments(false),
methodArgs.getMethodArguments(true));
}
});
}
private static boolean isFPMethodOverridden(String method,
Class<?> cls,
Class<?>[] intTypes,
Class<?>[] fpTypes)
{
Module thisModule = PlainView.class.getModule();
while (!thisModule.equals(cls.getModule())) {
@ -840,6 +877,57 @@ public class PlainView extends View implements TabExpander {
return true;
}
enum FPMethodArgs {
IGNN,
IIGNN,
GNNII,
GNNC;
public Class<?>[] getMethodArguments(boolean isFPType) {
Class<?> N = (isFPType) ? Float.TYPE : Integer.TYPE;
Class<?> G = (isFPType) ? Graphics2D.class : Graphics.class;
switch (this) {
case IGNN:
return new Class<?>[]{Integer.TYPE, G, N, N};
case IIGNN:
return new Class<?>[]{Integer.TYPE, Integer.TYPE, G, N, N};
case GNNII:
return new Class<?>[]{G, N, N, Integer.TYPE, Integer.TYPE};
case GNNC:
return new Class<?>[]{G, N, N, Character.TYPE};
default:
throw new RuntimeException("Unknown method arguments!");
}
}
}
private static class FPMethodItem {
final Class<?> className;
final String methodName;
public FPMethodItem(Class<?> className, String methodName) {
this.className = className;
this.methodName = methodName;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof FPMethodItem) {
FPMethodItem that = (FPMethodItem) obj;
return this.className.equals(that.className)
&& this.methodName.equals(that.methodName);
}
return false;
}
@Override
public int hashCode() {
return 31 * methodName.hashCode() + className.hashCode();
}
}
// --- member variables -----------------------------------------------
/**
@ -878,46 +966,13 @@ public class PlainView extends View implements TabExpander {
*/
int firstLineOffset;
final boolean drawLineOverridden;
final boolean drawSelectedTextOverridden;
final boolean drawUnselectedTextOverridden;
final boolean useFloatingPointAPI;
{
final Class<?> CLS = getClass();
final Class<?> INT = Integer.TYPE;
final Class<?> FP = Float.TYPE;
drawLineOverridden = AccessController
.doPrivileged(new PrivilegedAction<Boolean>() {
@Override
public Boolean run() {
Class<?>[] intTypes = {INT, Graphics.class, INT, INT};
Class<?>[] fpTypes = {INT, Graphics2D.class, FP, FP};
return isFPMethodOverriden("drawLine", CLS, intTypes, fpTypes);
}
});
drawUnselectedTextOverridden = AccessController
.doPrivileged(new PrivilegedAction<Boolean>() {
@Override
public Boolean run() {
Class<?>[] intTypes = {Graphics.class, INT, INT, INT, INT};
Class<?>[] fpTypes = {Graphics2D.class, FP, FP, INT, INT};
return isFPMethodOverriden("drawUnselectedText", CLS, intTypes, fpTypes);
}
});
drawSelectedTextOverridden = AccessController
.doPrivileged(new PrivilegedAction<Boolean>() {
@Override
public Boolean run() {
Class<?>[] intTypes = {Graphics.class, INT, INT, INT, INT};
Class<?>[] fpTypes = {Graphics2D.class, FP, FP, INT, INT};
return isFPMethodOverriden("drawSelectedText", CLS, intTypes, fpTypes);
}
});
useFloatingPointAPI = drawUnselectedTextOverridden || drawSelectedTextOverridden;
}
private static SoftReference<HashMap<FPMethodItem, Boolean>> methodsOverriddenMapRef;
final boolean drawLineOverridden =
getFPMethodOverridden(getClass(), "drawLine", FPMethodArgs.IGNN);
final boolean drawSelectedTextOverridden =
getFPMethodOverridden(getClass(), "drawSelectedText", FPMethodArgs.GNNII);
final boolean drawUnselectedTextOverridden =
getFPMethodOverridden(getClass(), "drawUnselectedText", FPMethodArgs.GNNII);
final boolean useFloatingPointAPI =
drawUnselectedTextOverridden || drawSelectedTextOverridden;
}

View File

@ -28,10 +28,9 @@ import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.lang.ref.SoftReference;
import java.security.AccessController;
import java.security.PrivilegedAction;
import javax.swing.event.*;
import static javax.swing.text.PlainView.isFPMethodOverriden;
import static javax.swing.text.PlainView.FPMethodArgs.*;
import static javax.swing.text.PlainView.getFPMethodOverridden;
/**
* View of plain text (text with only one font and color)
@ -989,46 +988,12 @@ public class WrappedPlainView extends BoxView implements TabExpander {
SoftReference<int[]> lineCache = null;
}
private final boolean drawLineOverridden;
private final boolean drawSelectedTextOverridden;
private final boolean drawUnselectedTextOverridden;
private final boolean useFloatingPointAPI;
{
final Class<?> CLS = getClass();
final Class<?> INT = Integer.TYPE;
final Class<?> FP = Float.TYPE;
drawLineOverridden = AccessController
.doPrivileged(new PrivilegedAction<Boolean>() {
@Override
public Boolean run() {
Class<?>[] intTypes = {INT, INT, Graphics.class, INT, INT};
Class<?>[] fpTypes = {INT, INT, Graphics2D.class, FP, FP};
return isFPMethodOverriden("drawLine", CLS, intTypes, fpTypes);
}
});
drawUnselectedTextOverridden = AccessController
.doPrivileged(new PrivilegedAction<Boolean>() {
@Override
public Boolean run() {
Class<?>[] intTypes = {Graphics.class, INT, INT, INT, INT};
Class<?>[] fpTypes = {Graphics2D.class, FP, FP, INT, INT};
return isFPMethodOverriden("drawUnselectedText", CLS, intTypes, fpTypes);
}
});
drawSelectedTextOverridden = AccessController
.doPrivileged(new PrivilegedAction<Boolean>() {
@Override
public Boolean run() {
Class<?>[] intTypes = {Graphics.class, INT, INT, INT, INT};
Class<?>[] fpTypes = {Graphics2D.class, FP, FP, INT, INT};
return isFPMethodOverriden("drawSelectedText", CLS, intTypes, fpTypes);
}
});
useFloatingPointAPI = drawUnselectedTextOverridden || drawSelectedTextOverridden;
}
private final boolean drawLineOverridden =
getFPMethodOverridden(getClass(), "drawLine", IIGNN);
private final boolean drawSelectedTextOverridden =
getFPMethodOverridden(getClass(), "drawSelectedText", GNNII);
private final boolean drawUnselectedTextOverridden =
getFPMethodOverridden(getClass(), "drawUnselectedText", GNNII);
private final boolean useFloatingPointAPI =
drawUnselectedTextOverridden || drawSelectedTextOverridden;
}

View File

@ -42,7 +42,7 @@ import javax.swing.text.WrappedPlainView;
/**
* @test
* @bug 8156217
* @bug 8156217 8169922
* @key headful
* @summary Selected text is shifted on HiDPI display
* @run main FPMethodCalledTest