Skip to content

Commit 3296ff4

Browse files
committed
8359380: Rework deferral profile logic after JDK-8346465
1 parent bbc5c98 commit 3296ff4

File tree

5 files changed

+60
-69
lines changed

5 files changed

+60
-69
lines changed

src/java.desktop/share/classes/java/awt/color/ICC_Profile.java

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@
5151
import java.util.StringTokenizer;
5252

5353
import sun.awt.AWTAccessor;
54+
import sun.java2d.cmm.BuiltinProfileInfo;
5455
import sun.java2d.cmm.CMSManager;
5556
import sun.java2d.cmm.PCMM;
5657
import sun.java2d.cmm.Profile;
5758
import sun.java2d.cmm.ProfileDataVerifier;
58-
import sun.java2d.cmm.ProfileDeferralInfo;
5959

6060
import static sun.java2d.cmm.ProfileDataVerifier.HEADER_SIZE;
6161

@@ -102,20 +102,11 @@ public sealed class ICC_Profile implements Serializable
102102
private transient volatile Profile cmmProfile;
103103

104104
/**
105-
* Stores some information about {@code ICC_Profile} without causing a
106-
* deferred profile to be loaded. Note that we can defer the loading of
107-
* standard profiles only. If this field is null, then {@link #cmmProfile}
108-
* should be used to access profile information.
105+
* Stores information about a built-in profile without triggering profile
106+
* loading. If this field is null, {@link #cmmProfile} should be used to
107+
* access profile data. If not null, the profile is considered immutable.
109108
*/
110-
private transient volatile ProfileDeferralInfo deferralInfo;
111-
112-
113-
/**
114-
* Set to {@code true} for {@code BuiltInProfile}, {@code false} otherwise.
115-
* This flag is used in {@link #setData(int, byte[])} to prevent modifying
116-
* built-in profiles.
117-
*/
118-
private final transient boolean builtIn;
109+
private transient final BuiltinProfileInfo builtInInfo;
119110

120111
/**
121112
* The lazy registry of singleton profile objects for specific built-in
@@ -124,22 +115,22 @@ public sealed class ICC_Profile implements Serializable
124115
*/
125116
private interface BuiltInProfile {
126117
/*
127-
* ProfileDeferralInfo is used for built-in profile creation only,
118+
* BuiltinProfileInfo is used for built-in profile creation only,
128119
* and all built-in profiles should be constructed using it.
129120
*/
130-
ICC_Profile SRGB = new ICC_ProfileRGB(new ProfileDeferralInfo(
121+
ICC_Profile SRGB = new ICC_ProfileRGB(new BuiltinProfileInfo(
131122
"sRGB.pf", ColorSpace.TYPE_RGB, 3, CLASS_DISPLAY));
132123

133-
ICC_Profile LRGB = new ICC_ProfileRGB(new ProfileDeferralInfo(
124+
ICC_Profile LRGB = new ICC_ProfileRGB(new BuiltinProfileInfo(
134125
"LINEAR_RGB.pf", ColorSpace.TYPE_RGB, 3, CLASS_DISPLAY));
135126

136-
ICC_Profile XYZ = new ICC_Profile(new ProfileDeferralInfo(
127+
ICC_Profile XYZ = new ICC_Profile(new BuiltinProfileInfo(
137128
"CIEXYZ.pf", ColorSpace.TYPE_XYZ, 3, CLASS_ABSTRACT));
138129

139-
ICC_Profile PYCC = new ICC_Profile(new ProfileDeferralInfo(
130+
ICC_Profile PYCC = new ICC_Profile(new BuiltinProfileInfo(
140131
"PYCC.pf", ColorSpace.TYPE_3CLR, 3, CLASS_COLORSPACECONVERSION));
141132

142-
ICC_Profile GRAY = new ICC_ProfileGray(new ProfileDeferralInfo(
133+
ICC_Profile GRAY = new ICC_ProfileGray(new BuiltinProfileInfo(
143134
"GRAY.pf", ColorSpace.TYPE_GRAY, 1, CLASS_DISPLAY));
144135
}
145136

@@ -771,20 +762,19 @@ private interface BuiltInProfile {
771762
*/
772763
ICC_Profile(Profile p) {
773764
cmmProfile = p;
774-
builtIn = false;
765+
builtInInfo = null;
775766
}
776767

777768
/**
778769
* Constructs an {@code ICC_Profile} object whose loading will be deferred.
779770
* The ID will be 0 until the profile is loaded.
780771
*
781772
* <p>
782-
* Note: {@code ProfileDeferralInfo} is used for built-in profile
773+
* Note: {@code BuiltinProfileInfo} is used for built-in profile
783774
* creation only, and all built-in profiles should be constructed using it.
784775
*/
785-
ICC_Profile(ProfileDeferralInfo pdi) {
786-
deferralInfo = pdi;
787-
builtIn = true;
776+
ICC_Profile(BuiltinProfileInfo pdi) {
777+
builtInInfo = pdi;
788778
}
789779

790780
/**
@@ -934,16 +924,14 @@ private Profile cmmProfile() {
934924
if (cmmProfile != null) {
935925
return cmmProfile;
936926
}
937-
var is = getStandardProfileInputStream(deferralInfo.filename);
927+
var is = getStandardProfileInputStream(builtInInfo.filename);
938928
if (is == null) {
939929
return null;
940930
}
941931
try (is) {
942932
byte[] data = getProfileDataFromStream(is);
943933
if (data != null) {
944934
p = cmmProfile = CMSManager.getModule().loadProfile(data);
945-
// from now we cannot use the deferred value, drop it
946-
deferralInfo = null;
947935
}
948936
} catch (CMMException | IOException ignore) {
949937
}
@@ -975,9 +963,8 @@ public int getMinorVersion() {
975963
* @return one of the predefined profile class constants
976964
*/
977965
public int getProfileClass() {
978-
ProfileDeferralInfo info = deferralInfo;
979-
if (info != null) {
980-
return info.profileClass;
966+
if (builtInInfo != null) {
967+
return builtInInfo.profileClass;
981968
}
982969
byte[] theHeader = getData(cmmProfile(), icSigHead);
983970
return getProfileClass(theHeader);
@@ -1012,9 +999,8 @@ private static int getProfileClass(byte[] data) {
1012999
* {@code ColorSpace} class
10131000
*/
10141001
public int getColorSpaceType() {
1015-
ProfileDeferralInfo info = deferralInfo;
1016-
if (info != null) {
1017-
return info.colorSpaceType;
1002+
if (builtInInfo != null) {
1003+
return builtInInfo.colorSpaceType;
10181004
}
10191005
byte[] theHeader = getData(cmmProfile(), icSigHead);
10201006
return getColorSpaceType(theHeader);
@@ -1160,8 +1146,8 @@ static byte[] getData(Profile p, int tagSignature) {
11601146
* @see ColorSpace
11611147
*/
11621148
public void setData(int tagSignature, byte[] tagData) {
1163-
if (builtIn) {
1164-
throw new IllegalArgumentException("Built-in profile cannot be modified");
1149+
if (builtInInfo != null) {
1150+
throw new IllegalArgumentException("Can't modify built-in profile");
11651151
}
11661152

11671153
if (tagSignature == ICC_Profile.icSigHead) {
@@ -1205,9 +1191,8 @@ private static void checkRenderingIntent(byte[] data) {
12051191
* @throws ProfileDataException if color space is in the profile is invalid
12061192
*/
12071193
public int getNumComponents() {
1208-
ProfileDeferralInfo info = deferralInfo;
1209-
if (info != null) {
1210-
return info.numComponents;
1194+
if (builtInInfo != null) {
1195+
return builtInInfo.numComponents;
12111196
}
12121197
byte[] theHeader = getData(cmmProfile(), icSigHead);
12131198
int theColorSpaceSig = intFromBigEndian(theHeader, icHdrColorSpace);

src/java.desktop/share/classes/java/awt/color/ICC_ProfileGray.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737

3838
import java.io.Serial;
3939

40+
import sun.java2d.cmm.BuiltinProfileInfo;
4041
import sun.java2d.cmm.Profile;
41-
import sun.java2d.cmm.ProfileDeferralInfo;
4242

4343
/**
4444
* The {@code ICC_ProfileGray} class is a subclass of the {@code ICC_Profile}
@@ -87,9 +87,9 @@ public final class ICC_ProfileGray extends ICC_Profile {
8787

8888
/**
8989
* Constructs a new {@code ICC_ProfileGray} from a
90-
* {@code ProfileDeferralInfo} object.
90+
* {@code BuiltinProfileInfo} object.
9191
*/
92-
ICC_ProfileGray(ProfileDeferralInfo pdi) {
92+
ICC_ProfileGray(BuiltinProfileInfo pdi) {
9393
super(pdi);
9494
}
9595

src/java.desktop/share/classes/java/awt/color/ICC_ProfileRGB.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737

3838
import java.io.Serial;
3939

40+
import sun.java2d.cmm.BuiltinProfileInfo;
4041
import sun.java2d.cmm.Profile;
41-
import sun.java2d.cmm.ProfileDeferralInfo;
4242

4343
/**
4444
* The {@code ICC_ProfileRGB} class is a subclass of the {@code ICC_Profile}
@@ -122,11 +122,11 @@ public final class ICC_ProfileRGB extends ICC_Profile {
122122

123123
/**
124124
* Constructs a new {@code ICC_ProfileRGB} from a
125-
* {@code ProfileDeferralInfo} object.
125+
* {@code BuiltinProfileInfo} object.
126126
*
127127
* @param pdi
128128
*/
129-
ICC_ProfileRGB(ProfileDeferralInfo pdi) {
129+
ICC_ProfileRGB(BuiltinProfileInfo pdi) {
130130
super(pdi);
131131
}
132132

src/java.desktop/share/classes/sun/java2d/cmm/ProfileDeferralInfo.java renamed to src/java.desktop/share/classes/sun/java2d/cmm/BuiltinProfileInfo.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,15 +26,15 @@
2626
package sun.java2d.cmm;
2727

2828
/**
29-
* A class to pass information about a profile to be loaded from a file to the
30-
* static getInstance(int cspace) method of ICC_Profile. Loading of the profile
31-
* data and initialization of the CMM is to be deferred as long as possible.
29+
* Stores information about a built-in profile used by
30+
* ICC_Profile.getInstance(int cspace) to defer the loading of profile data and
31+
* CMM initialization. Since built-in profiles are immutable, this information
32+
* is always valid.
3233
*/
33-
public final class ProfileDeferralInfo {
34+
public final class BuiltinProfileInfo {
3435

3536
/**
36-
* Need to have this info for ICC_ColorSpace without causing a deferred
37-
* profile to be loaded.
37+
* Used by ICC_ColorSpace without triggering built-in profile loading.
3838
*/
3939
public final int colorSpaceType, numComponents, profileClass;
4040

@@ -43,7 +43,7 @@ public final class ProfileDeferralInfo {
4343
*/
4444
public final String filename;
4545

46-
public ProfileDeferralInfo(String fn, int type, int ncomp, int pclass) {
46+
public BuiltinProfileInfo(String fn, int type, int ncomp, int pclass) {
4747
filename = fn;
4848
colorSpaceType = type;
4949
numComponents = ncomp;

test/jdk/java/awt/color/CheckDefaultProperties.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -34,8 +34,9 @@
3434

3535
/**
3636
* @test
37-
* @bug 8256321
38-
* @summary Verifies profile properties are the same before/after activation
37+
* @bug 8256321 8359380
38+
* @summary Verifies built-in profile properties are the same before and after
39+
* activation and in copies of built-in profiles
3940
*/
4041
public final class CheckDefaultProperties {
4142

@@ -46,21 +47,26 @@ public static void main(String[] args) {
4647
ICC_Profile lrgb = ICC_Profile.getInstance(ColorSpace.CS_LINEAR_RGB);
4748
ICC_Profile pycc = ICC_Profile.getInstance(ColorSpace.CS_PYCC);
4849

49-
// check default values, before profile activation
50-
test(srgb, TYPE_RGB, 3, CLASS_DISPLAY);
51-
test(gray, TYPE_GRAY, 1, CLASS_DISPLAY);
52-
test(xyz, TYPE_XYZ, 3, CLASS_ABSTRACT);
53-
test(lrgb, TYPE_RGB, 3, CLASS_DISPLAY);
54-
test(pycc, TYPE_3CLR, 3, CLASS_COLORSPACECONVERSION);
50+
// checks default values before built-in profiles are activated
51+
test(srgb, gray, xyz, lrgb, pycc);
52+
53+
// activates built-in profiles and creates copies
54+
ICC_Profile srgbCopy = ICC_Profile.getInstance(srgb.getData());
55+
ICC_Profile grayCopy = ICC_Profile.getInstance(gray.getData());
56+
ICC_Profile xyzCopy = ICC_Profile.getInstance(xyz.getData());
57+
ICC_Profile lrgbCopy = ICC_Profile.getInstance(lrgb.getData());
58+
ICC_Profile pyccCopy = ICC_Profile.getInstance(pycc.getData());
5559

56-
// activate profiles
57-
srgb.getData();
58-
gray.getData();
59-
xyz.getData();
60-
lrgb.getData();
61-
pycc.getData();
60+
// checks default values after profile activation
61+
test(srgb, gray, xyz, lrgb, pycc);
62+
63+
// checks default values in copies of the built-in profiles
64+
test(srgbCopy, grayCopy, xyzCopy, lrgbCopy, pyccCopy);
65+
}
6266

63-
// check default values, after profile activation
67+
private static void test(ICC_Profile srgb, ICC_Profile gray,
68+
ICC_Profile xyz, ICC_Profile lrgb,
69+
ICC_Profile pycc) {
6470
test(srgb, TYPE_RGB, 3, CLASS_DISPLAY);
6571
test(gray, TYPE_GRAY, 1, CLASS_DISPLAY);
6672
test(xyz, TYPE_XYZ, 3, CLASS_ABSTRACT);

0 commit comments

Comments
 (0)