1/*
2 * Copyright (C) 2012 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 com.android.test.hwuicompare;
18
19import com.android.test.hwuicompare.R;
20
21import android.content.res.Resources;
22import android.graphics.Bitmap;
23import android.graphics.BitmapFactory;
24import android.graphics.BitmapShader;
25import android.graphics.Color;
26import android.graphics.ComposeShader;
27import android.graphics.LinearGradient;
28import android.graphics.PorterDuff;
29import android.graphics.RadialGradient;
30import android.graphics.SweepGradient;
31import android.graphics.Matrix;
32import android.graphics.Shader;
33
34public class ResourceModifiers {
35        public final BitmapShader mRepeatShader;
36        public final BitmapShader mTranslatedShader;
37        public final BitmapShader mScaledShader;
38        private final int mTexWidth;
39        private final int mTexHeight;
40        private final float mDrawWidth;
41        private final float mDrawHeight;
42        public final LinearGradient mHorGradient;
43        public final LinearGradient mDiagGradient;
44        public final LinearGradient mVertGradient;
45        public final RadialGradient mRadGradient;
46        public final SweepGradient mSweepGradient;
47        public final ComposeShader mComposeShader;
48        public final ComposeShader mBadComposeShader;
49        public final ComposeShader mAnotherBadComposeShader;
50        public final Bitmap mBitmap;
51        private final Matrix mMtx1;
52        private final Matrix mMtx2;
53        private final Matrix mMtx3;
54
55        public final float[] mBitmapVertices;
56        public final int[] mBitmapColors;
57
58        private static ResourceModifiers sInstance = null;
59        public static ResourceModifiers instance() { return sInstance; }
60        public static void init(Resources resources) {
61            sInstance = new ResourceModifiers(resources);
62        }
63
64        public ResourceModifiers(Resources resources) {
65            mBitmap = BitmapFactory.decodeResource(resources, R.drawable.sunset1);
66            mTexWidth = mBitmap.getWidth();
67            mTexHeight = mBitmap.getHeight();
68
69            mDrawWidth = resources.getDimensionPixelSize(R.dimen.layer_width);
70            mDrawHeight = resources.getDimensionPixelSize(R.dimen.layer_height);
71
72            mRepeatShader = new BitmapShader(mBitmap, Shader.TileMode.REPEAT,
73                    Shader.TileMode.REPEAT);
74
75            mTranslatedShader = new BitmapShader(mBitmap, Shader.TileMode.REPEAT,
76                    Shader.TileMode.REPEAT);
77            mMtx1 = new Matrix();
78            mMtx1.setTranslate(mTexWidth / 2.0f, mTexHeight / 2.0f);
79            mMtx1.postRotate(45, 0, 0);
80            mTranslatedShader.setLocalMatrix(mMtx1);
81
82            mScaledShader = new BitmapShader(mBitmap, Shader.TileMode.MIRROR,
83                    Shader.TileMode.MIRROR);
84            mMtx2 = new Matrix();
85            mMtx2.setScale(0.5f, 0.5f);
86            mScaledShader.setLocalMatrix(mMtx2);
87
88            mHorGradient = new LinearGradient(0.0f, 0.0f, 1.0f, 0.0f,
89                    Color.RED, Color.GREEN, Shader.TileMode.CLAMP);
90            mMtx3 = new Matrix();
91            mMtx3.setScale(mDrawHeight, 1.0f);
92            mMtx3.postRotate(-90.0f);
93            mMtx3.postTranslate(0.0f, mDrawHeight);
94            mHorGradient.setLocalMatrix(mMtx3);
95
96            mDiagGradient = new LinearGradient(0.0f, 0.0f, mDrawWidth / 2.0f, mDrawHeight / 2.0f,
97                    Color.BLUE, Color.RED, Shader.TileMode.CLAMP);
98
99            mVertGradient = new LinearGradient(0.0f, 0.0f, 0.0f, mDrawHeight / 2.0f,
100                    Color.YELLOW, Color.MAGENTA, Shader.TileMode.MIRROR);
101
102            mSweepGradient = new SweepGradient(mDrawWidth / 2.0f, mDrawHeight / 2.0f,
103                    Color.YELLOW, Color.MAGENTA);
104
105            mComposeShader = new ComposeShader(mRepeatShader, mHorGradient,
106                    PorterDuff.Mode.MULTIPLY);
107
108            final float width = mBitmap.getWidth() / 8.0f;
109            final float height = mBitmap.getHeight() / 8.0f;
110
111            mBitmapVertices = new float[] {
112                0.0f, 0.0f, width, 0.0f, width * 2, 0.0f, width * 3, 0.0f,
113                0.0f, height, width, height, width * 2, height, width * 4, height,
114                0.0f, height * 2, width, height * 2, width * 2, height * 2, width * 3, height * 2,
115                0.0f, height * 4, width, height * 4, width * 2, height * 4, width * 4, height * 4,
116            };
117
118            mBitmapColors = new int[] {
119                0xffff0000, 0xff00ff00, 0xff0000ff, 0xffff0000,
120                0xff0000ff, 0xffff0000, 0xff00ff00, 0xff00ff00,
121                0xff00ff00, 0xff0000ff, 0xffff0000, 0xff00ff00,
122                0x00ff0000, 0x0000ff00, 0x000000ff, 0x00ff0000,
123            };
124
125            // Use a repeating gradient with many colors to test the non simple case.
126            mRadGradient = new RadialGradient(mDrawWidth / 4.0f, mDrawHeight / 4.0f, 4.0f,
127                    mBitmapColors, null, Shader.TileMode.REPEAT);
128
129            mBadComposeShader = new ComposeShader(mRadGradient, mComposeShader,
130                    PorterDuff.Mode.MULTIPLY);
131
132            mAnotherBadComposeShader = new ComposeShader(mRadGradient, mVertGradient,
133                    PorterDuff.Mode.MULTIPLY);
134        }
135
136}
137