Home | History | Annotate | Download | only in testinfrastructure
      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 com.android.cts.uirendering.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 
     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                 0.0f, 0.0f, width, 0.0f, width * 2, 0.0f, width * 3, 0.0f,
    103                 0.0f, height, width, height, width * 2, height, width * 4, height,
    104                 0.0f, height * 2, width, height * 2, width * 2, height * 2, width * 3, height * 2,
    105                 0.0f, height * 4, width, height * 4, width * 2, height * 4, width * 4, height * 4,
    106         };
    107 
    108         bitmapColors = new int[]{
    109                 0xffff0000, 0xff00ff00, 0xff0000ff, 0xffff0000,
    110                 0xff0000ff, 0xffff0000, 0xff00ff00, 0xff00ff00,
    111                 0xff00ff00, 0xff0000ff, 0xffff0000, 0xff00ff00,
    112                 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00ff0000,
    113         };
    114 
    115         // Use a repeating gradient with many colors to test the non simple case.
    116         radGradient = new RadialGradient(drawWidth / 4.0f, drawHeight / 4.0f, 4.0f,
    117                 bitmapColors, null, Shader.TileMode.REPEAT);
    118 
    119         nestedComposeShader = new ComposeShader(radGradient, composeShader,
    120                 PorterDuff.Mode.MULTIPLY);
    121 
    122         doubleGradientComposeShader = new ComposeShader(radGradient, vertGradient,
    123                 PorterDuff.Mode.MULTIPLY);
    124     }
    125 
    126     public static ResourceModifier instance() {
    127         return sInstance;
    128     }
    129 
    130     public static void init(Resources resources) {
    131         sInstance = new ResourceModifier(resources);
    132     }
    133 }
    134