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
17package android.uirendering.cts.testclasses;
18
19import android.content.res.AssetManager;
20import android.graphics.Bitmap;
21import android.graphics.BitmapFactory;
22import android.graphics.BitmapShader;
23import android.graphics.Canvas;
24import android.graphics.Paint;
25import android.graphics.Point;
26import android.graphics.Shader;
27import android.support.test.filters.MediumTest;
28import android.support.test.runner.AndroidJUnit4;
29import android.uirendering.cts.R;
30import android.uirendering.cts.bitmapverifiers.SamplePointVerifier;
31import android.uirendering.cts.testinfrastructure.ActivityTestBase;
32
33import org.junit.Test;
34import org.junit.runner.RunWith;
35
36import java.io.IOException;
37import java.io.InputStream;
38
39@MediumTest
40@RunWith(AndroidJUnit4.class)
41public class Rgba16fTests extends ActivityTestBase {
42    @Test
43    public void testTransferFunctions() {
44        createTest()
45                .addCanvasClient("RGBA16F_TransferFunctions", (canvas, width, height) -> {
46                    AssetManager assets = getActivity().getResources().getAssets();
47                    try (InputStream in = assets.open("linear-rgba16f.png")) {
48                        Bitmap bitmap = BitmapFactory.decodeStream(in);
49                        canvas.scale(
50                                width / (float) bitmap.getWidth(),
51                                height / (float) bitmap.getHeight());
52                        canvas.drawBitmap(bitmap, 0, 0, null);
53                    } catch (IOException e) {
54                        throw new RuntimeException("Test failed: ", e);
55                    }
56                }, true)
57                .runWithVerifier(new SamplePointVerifier(
58                        new Point[] { new Point(0, 0) },
59                        new int[] { 0xffbbbbbb }
60                ));
61    }
62
63    @Test
64    public void testAlpha() {
65        createTest()
66                .addCanvasClient("RGBA16F_TransferFunctions", (canvas, width, height) -> {
67                    AssetManager assets = getActivity().getResources().getAssets();
68                    try (InputStream in = assets.open("linear-rgba16f.png")) {
69                        Bitmap bitmap = BitmapFactory.decodeStream(in);
70                        canvas.scale(
71                                width / (float) bitmap.getWidth(),
72                                height / (float) bitmap.getHeight());
73                        Paint p = new Paint();
74                        p.setAlpha(127);
75                        canvas.drawColor(0xffff0000);
76                        canvas.drawBitmap(bitmap, 0, 0, p);
77                    } catch (IOException e) {
78                        throw new RuntimeException("Test failed: ", e);
79                    }
80                }, true)
81                .runWithVerifier(new SamplePointVerifier(
82                        new Point[] { new Point(0, 0) },
83                        new int[] { 0xffdd5d5d }
84                ));
85    }
86
87    @Test
88    public void testMasked() {
89        createTest()
90                .addCanvasClient("RGBA16F_Masked", (canvas, width, height) -> {
91                    AssetManager assets = getActivity().getResources().getAssets();
92                    try (InputStream in = assets.open("linear-rgba16f.png")) {
93                        Bitmap bitmap = BitmapFactory.decodeStream(in);
94
95                        Bitmap res = BitmapFactory.decodeResource(getActivity().getResources(),
96                                R.drawable.alpha_mask);
97                        Bitmap mask = Bitmap.createBitmap(res.getWidth(), res.getHeight(),
98                                Bitmap.Config.ALPHA_8);
99                        Canvas c = new Canvas(mask);
100                        c.drawBitmap(res, 0, 0, null);
101
102                        Paint p = new Paint();
103                        p.setShader(new BitmapShader(bitmap,
104                                Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
105
106                        canvas.scale(
107                                width / (float) bitmap.getWidth(),
108                                height / (float) bitmap.getHeight());
109
110                        canvas.drawBitmap(mask, 0, 0, p);
111                    } catch (IOException e) {
112                        throw new RuntimeException("Test failed: ", e);
113                    }
114                }, true)
115                .runWithVerifier(new SamplePointVerifier(
116                        new Point[] { new Point(0, 0), new Point(31, 31) },
117                        new int[] { 0xffffffff, 0xffbbbbbb }
118                ));
119    }
120
121    @Test
122    public void testTransferFunctionsShader() {
123        createTest()
124                .addCanvasClient("RGBA16F_TransferFunctions_Shader", (canvas, width, height) -> {
125                    AssetManager assets = getActivity().getResources().getAssets();
126                    try (InputStream in = assets.open("linear-rgba16f.png")) {
127                        Bitmap bitmap = BitmapFactory.decodeStream(in);
128                        Paint p = new Paint();
129                        p.setShader(new BitmapShader(bitmap,
130                                Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
131                        canvas.drawRect(0.0f, 0.0f, width, height, p);
132                    } catch (IOException e) {
133                        throw new RuntimeException("Test failed: ", e);
134                    }
135                }, true)
136                .runWithVerifier(new SamplePointVerifier(
137                        new Point[] { new Point(0, 0) },
138                        new int[] { 0xffbbbbbb }
139                ));
140    }
141
142    @Test
143    public void testMirroredTransferFunctions() {
144        createTest()
145                .addCanvasClient("RGBA16F_TransferFunctions_Mirror", (canvas, width, height) -> {
146                    AssetManager assets = getActivity().getResources().getAssets();
147                    // Pure blue in ProPhoto RGB will yield negative R and G values in scRGB,
148                    // as well as a value > 1.0 for B
149                    try (InputStream in = assets.open("prophoto-rgba16f.png")) {
150                        Bitmap bitmap = BitmapFactory.decodeStream(in);
151                        canvas.scale(
152                                width / (float) bitmap.getWidth(),
153                                height / (float) bitmap.getHeight());
154                        canvas.drawBitmap(bitmap, 0, 0, null);
155                    } catch (IOException e) {
156                        throw new RuntimeException("Test failed: ", e);
157                    }
158                }, true)
159                .runWithVerifier(new SamplePointVerifier(
160                        new Point[] { new Point(0, 0) },
161                        new int[] { 0xff0000ff }
162                ));
163    }
164}
165