1 /*
2   * Copyright (C) 2014 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 package android.uirendering.cts.testinfrastructure;
17 
18 import android.uirendering.cts.R;
19 
20 import android.content.res.Resources;
21 import android.graphics.Bitmap;
22 import android.graphics.BitmapFactory;
23 import android.graphics.BitmapShader;
24 import android.graphics.Color;
25 import android.graphics.ComposeShader;
26 import android.graphics.LinearGradient;
27 import android.graphics.Matrix;
28 import android.graphics.PorterDuff;
29 import android.graphics.RadialGradient;
30 import android.graphics.Shader;
31 import android.graphics.SweepGradient;
32 
33 /**
34  * This will contain the resource-based content for the DisplayModifiers
35  */
36 public class ResourceModifier {
37     private static ResourceModifier sInstance = null;
38 
39     public final BitmapShader repeatShader;
40     public final BitmapShader translatedShader;
41     public final BitmapShader scaledShader;
42     public final LinearGradient horGradient;
43     public final LinearGradient diagGradient;
44     public final LinearGradient vertGradient;
45     public final RadialGradient radGradient;
46     public final SweepGradient sweepGradient;
47     public final ComposeShader composeShader;
48     public final ComposeShader nestedComposeShader;
49     public final ComposeShader doubleGradientComposeShader;
50     public final Bitmap bitmap;
51     public final float[] bitmapVertices;
52     public final int[] bitmapColors;
53 
ResourceModifier(Resources resources)54     public ResourceModifier(Resources resources) {
55         bitmap = BitmapFactory.decodeResource(resources, R.drawable.sunset1);
56         int texWidth = bitmap.getWidth();
57         int texHeight = bitmap.getHeight();
58 
59         int drawWidth = ActivityTestBase.TEST_WIDTH;
60         int drawHeight = ActivityTestBase.TEST_HEIGHT;
61 
62         repeatShader = new BitmapShader(bitmap, Shader.TileMode.REPEAT,
63                 Shader.TileMode.REPEAT);
64 
65         translatedShader = new BitmapShader(bitmap, Shader.TileMode.REPEAT,
66                 Shader.TileMode.REPEAT);
67         Matrix matrix = new Matrix();
68         matrix.setTranslate(texWidth / 2.0f, texHeight / 2.0f);
69         matrix.postRotate(45, 0, 0);
70         translatedShader.setLocalMatrix(matrix);
71 
72         scaledShader = new BitmapShader(bitmap, Shader.TileMode.MIRROR,
73                 Shader.TileMode.MIRROR);
74         matrix = new Matrix();
75         matrix.setScale(0.5f, 0.5f);
76         scaledShader.setLocalMatrix(matrix);
77 
78         horGradient = new LinearGradient(0.0f, 0.0f, 1.0f, 0.0f,
79                 Color.RED, Color.GREEN, Shader.TileMode.CLAMP);
80         matrix = new Matrix();
81         matrix.setScale(drawHeight, 1.0f);
82         matrix.postRotate(-90.0f);
83         matrix.postTranslate(0.0f, drawHeight);
84         horGradient.setLocalMatrix(matrix);
85 
86         diagGradient = new LinearGradient(0.0f, 0.0f, drawWidth / 2.0f, drawHeight / 2.0f,
87                 Color.BLUE, Color.RED, Shader.TileMode.CLAMP);
88 
89         vertGradient = new LinearGradient(0.0f, 0.0f, 0.0f, drawHeight / 2.0f,
90                 Color.YELLOW, Color.MAGENTA, Shader.TileMode.MIRROR);
91 
92         sweepGradient = new SweepGradient(drawWidth / 2.0f, drawHeight / 2.0f,
93                 Color.YELLOW, Color.MAGENTA);
94 
95         composeShader = new ComposeShader(repeatShader, horGradient,
96                 PorterDuff.Mode.MULTIPLY);
97 
98         final float width = bitmap.getWidth() / 8.0f;
99         final float height = bitmap.getHeight() / 8.0f;
100 
101         bitmapVertices = new float[]{
102                 -1.0f, -2.0f, -3.0f, -4.0f,  // these two (points) are skipped by the test
103                 0.0f, 0.0f, width, 0.0f, width * 2, 0.0f, width * 3, 0.0f,
104                 0.0f, height, width, height, width * 2, height, width * 4, height,
105                 0.0f, height * 2, width, height * 2, width * 2, height * 2, width * 3, height * 2,
106                 0.0f, height * 4, width, height * 4, width * 2, height * 4, width * 4, height * 4,
107         };
108 
109         bitmapColors = new int[]{
110                 0xffff0000, 0xff00ff00, 0xff0000ff, 0xffff0000,
111                 0xff0000ff, 0xffff0000, 0xff00ff00, 0xff00ff00,
112                 0xff00ff00, 0xff0000ff, 0xffff0000, 0xff00ff00,
113                 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00ff0000,
114         };
115 
116         // Use a repeating gradient with many colors to test the non simple case.
117         radGradient = new RadialGradient(drawWidth / 4.0f, drawHeight / 4.0f, 4.0f,
118                 bitmapColors, null, Shader.TileMode.REPEAT);
119 
120         nestedComposeShader = new ComposeShader(radGradient, composeShader,
121                 PorterDuff.Mode.MULTIPLY);
122 
123         doubleGradientComposeShader = new ComposeShader(radGradient, vertGradient,
124                 PorterDuff.Mode.MULTIPLY);
125     }
126 
instance()127     public static ResourceModifier instance() {
128         return sInstance;
129     }
130 
init(Resources resources)131     public static void init(Resources resources) {
132         sInstance = new ResourceModifier(resources);
133     }
134 }
135