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 */
16package com.android.test.uibench;
17
18import android.graphics.Canvas;
19import android.graphics.Color;
20import android.graphics.ColorFilter;
21import android.graphics.Paint;
22import android.graphics.PixelFormat;
23import android.graphics.Rect;
24import android.graphics.drawable.Drawable;
25import android.os.Bundle;
26import android.support.v7.app.AppCompatActivity;
27
28/**
29 * Test Canvas.saveLayer performance by interleaving drawText/drawRect with saveLayer.
30 * This test will be used to measure if drawing interleaved layers at the beginning of a frame will
31 * decrease FBO switching overhead (this is a future optimization in SkiaGL rendering pipeline).
32 */
33public class SaveLayerInterleaveActivity extends AppCompatActivity {
34
35    @Override
36    protected void onCreate(Bundle savedInstanceState) {
37        super.onCreate(savedInstanceState);
38        getWindow().setBackgroundDrawable(new Drawable() {
39            private final Paint mBluePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
40            private final Paint mGreenPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
41
42            @Override
43            public void setAlpha(int alpha) {
44            }
45
46            @Override
47            public int getOpacity() {
48                return PixelFormat.OPAQUE;
49            }
50
51            @Override
52            public void setColorFilter(ColorFilter colorFilter) {
53            }
54
55            @Override
56            public void draw(Canvas canvas) {
57                canvas.drawColor(Color.RED);
58
59                Rect bounds = getBounds();
60                int regions = 20;
61                int smallRectHeight = (bounds.height()/regions);
62                int padding = smallRectHeight / 4;
63                int top = bounds.top;
64                mBluePaint.setColor(Color.BLUE);
65                mBluePaint.setTextSize(padding);
66                mGreenPaint.setColor(Color.GREEN);
67                mGreenPaint.setTextSize(padding);
68
69                //interleave drawText and drawRect with saveLayer ops
70                for (int i = 0; i < regions; i++, top += smallRectHeight) {
71                    canvas.saveLayer(bounds.left, top, bounds.right, top + padding,
72                            mBluePaint);
73                    canvas.drawColor(Color.YELLOW);
74                    canvas.drawText("offscreen line "+ i, bounds.left, top + padding,
75                            mBluePaint);
76                    canvas.restore();
77
78                    Rect partX = new Rect(bounds.left, top + padding,
79                            bounds.right,top + smallRectHeight - padding);
80                    canvas.drawRect(partX, mBluePaint);
81                    canvas.drawText("onscreen line "+ i, bounds.left,
82                            top + smallRectHeight - padding, mGreenPaint);
83                }
84
85                invalidateSelf();
86            }
87        });
88    }
89}
90