1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.uirendering.cts.testclasses;
18 
19 import android.graphics.Bitmap;
20 import android.graphics.BitmapFactory;
21 import android.graphics.Color;
22 import android.graphics.ColorSpace;
23 import android.graphics.Point;
24 import android.support.test.filters.MediumTest;
25 import android.support.test.runner.AndroidJUnit4;
26 import android.uirendering.cts.R;
27 import android.uirendering.cts.bitmapverifiers.BitmapVerifier;
28 import android.uirendering.cts.bitmapverifiers.SamplePointVerifier;
29 import android.uirendering.cts.bitmapverifiers.SamplePointWideGamutVerifier;
30 import android.uirendering.cts.testclasses.view.BitmapView;
31 import android.uirendering.cts.testinfrastructure.ActivityTestBase;
32 import android.view.View;
33 import org.junit.Assert;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.util.Arrays;
41 
42 @MediumTest
43 @RunWith(AndroidJUnit4.class)
44 public class WideColorGamutTests extends ActivityTestBase {
45     private static final ColorSpace DISPLAY_P3 = ColorSpace.get(ColorSpace.Named.DISPLAY_P3);
46     private static final ColorSpace SCRGB = ColorSpace.get(ColorSpace.Named.EXTENDED_SRGB);
47 
48     private static final Point[] POINTS = {
49             new Point(16, 16),
50             new Point(48, 16),
51             new Point(80, 16),
52     };
53 
54     // The colors are defined as found in wide-gamut-test.png, which is Display P3
55     // Since the UI toolkit renders in scRGB, we want to convert here to compare values
56     // directly in the sample point verifier
57     private static final Color[] COLORS = {
58             Color.valueOf(0.937f, 0.000f, 0.000f, 1.0f, DISPLAY_P3).convert(SCRGB),
59             Color.valueOf(1.000f, 0.000f, 0.000f, 1.0f, DISPLAY_P3).convert(SCRGB),
60             Color.valueOf(0.918f, 0.200f, 0.137f, 1.0f, DISPLAY_P3).convert(SCRGB)
61     };
62 
63     private Bitmap mBitmap;
64 
65     @Override
isWideColorGamut()66     protected boolean isWideColorGamut() {
67         return true;
68     }
69 
70     @Before
loadBitmap()71     public void loadBitmap() {
72         try (InputStream in = getActivity().getAssets().open("wide-gamut-test.png")) {
73             mBitmap = BitmapFactory.decodeStream(in);
74         } catch (IOException e) {
75             Assert.fail("Could not load wide-gamut-test.png");
76         }
77     }
78 
79     @SuppressWarnings("SameParameterValue")
getVerifier(Point[] points, Color[] colors, float eps)80     private BitmapVerifier getVerifier(Point[] points, Color[] colors, float eps) {
81         if (getActivity().getWindow().isWideColorGamut()) {
82             return new SamplePointWideGamutVerifier(points, colors, eps);
83         }
84         return new SamplePointVerifier(points,
85                 Arrays.stream(colors).mapToInt(Color::toArgb).toArray(),
86                 (int) (eps * 255.0f + 0.5f));
87     }
88 
89     @Test
testDraw()90     public void testDraw() {
91         createTest()
92                 .addLayout(R.layout.wide_gamut_bitmap_layout, view -> {
93                     BitmapView bv = (BitmapView) view;
94                     bv.setBitmap(mBitmap);
95                 }, true)
96                 .runWithVerifier(getVerifier(POINTS, COLORS, 1e-2f));
97     }
98 
99     @Test
testSaveLayer()100     public void testSaveLayer() {
101         createTest()
102                 .addLayout(R.layout.wide_gamut_bitmap_layout, view -> {
103                     BitmapView bv = (BitmapView) view;
104                     bv.setBitmap(mBitmap);
105                     bv.setSaveLayer(true);
106                 }, true)
107                 .runWithVerifier(getVerifier(POINTS, COLORS, 1e-2f));
108     }
109 
110     @Test
testHardwareLayer()111     public void testHardwareLayer() {
112         createTest()
113                 .addLayout(R.layout.wide_gamut_bitmap_layout, view -> {
114                     BitmapView bv = (BitmapView) view;
115                     bv.setBitmap(mBitmap);
116                     bv.setLayerType(View.LAYER_TYPE_HARDWARE, null);
117                 }, true)
118                 .runWithVerifier(getVerifier(POINTS, COLORS, 1e-2f));
119     }
120 
121     @Test
testSaveLayerInHardwareLayer()122     public void testSaveLayerInHardwareLayer() {
123         createTest()
124                 .addLayout(R.layout.wide_gamut_bitmap_layout, view -> {
125                     BitmapView bv = (BitmapView) view;
126                     bv.setBitmap(mBitmap);
127                     bv.setSaveLayer(true);
128                     bv.setLayerType(View.LAYER_TYPE_HARDWARE, null);
129                 }, true)
130                 .runWithVerifier(getVerifier(POINTS, COLORS, 1e-2f));
131     }
132 }
133