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.content.Context; 19 import android.graphics.Canvas; 20 import android.graphics.Color; 21 import android.graphics.Paint; 22 import android.graphics.Picture; 23 import android.util.AttributeSet; 24 import android.view.View; 25 26 /** 27 * A simple View that uses a CanvasClient to draw its contents 28 */ 29 public class CanvasClientView extends View { 30 private boolean mUsePicture = false; 31 private CanvasClient mCanvasClient; 32 CanvasClientView(Context context)33 public CanvasClientView(Context context) { 34 super(context); 35 } 36 CanvasClientView(Context context, AttributeSet attrs)37 public CanvasClientView(Context context, AttributeSet attrs) { 38 super(context, attrs); 39 } 40 CanvasClientView(Context context, AttributeSet attrs, int defStyleAttr)41 public CanvasClientView(Context context, AttributeSet attrs, int defStyleAttr) { 42 super(context, attrs, defStyleAttr); 43 } 44 setUsePicture(boolean usePicture)45 public void setUsePicture(boolean usePicture) { 46 mUsePicture = usePicture; 47 } 48 setCanvasClient(CanvasClient canvasClient)49 public void setCanvasClient(CanvasClient canvasClient) { 50 mCanvasClient = canvasClient; 51 } 52 53 @Override onDraw(Canvas canvas)54 public void onDraw(Canvas canvas) { 55 if (ActivityTestBase.DEBUG) { 56 String s = canvas.isHardwareAccelerated() ? "HARDWARE" : "SOFTWARE"; 57 Paint paint = new Paint(); 58 paint.setColor(Color.BLACK); 59 paint.setTextSize(20); 60 canvas.drawText(s, 200, 200, paint); 61 } 62 if (mCanvasClient == null) throw new IllegalStateException("Canvas client missing"); 63 64 int saveCount = canvas.save(); 65 canvas.clipRect(0, 0, ActivityTestBase.TEST_WIDTH, ActivityTestBase.TEST_HEIGHT); 66 if (mUsePicture) { 67 Picture picture = new Picture(); 68 Canvas pictureCanvas = picture.beginRecording(ActivityTestBase.TEST_WIDTH, 69 ActivityTestBase.TEST_HEIGHT); 70 mCanvasClient.draw(pictureCanvas, ActivityTestBase.TEST_WIDTH, 71 ActivityTestBase.TEST_HEIGHT); 72 picture.endRecording(); 73 picture.draw(canvas); 74 } else { 75 mCanvasClient.draw(canvas, ActivityTestBase.TEST_WIDTH, ActivityTestBase.TEST_HEIGHT); 76 } 77 canvas.restoreToCount(saveCount); 78 } 79 } 80