blob: fece8babb400966d77d222f0937fcad1d6bac1fc [file] [log] [blame]
John Reckfe5dfca2019-01-17 17:01:32 -08001/*
2 * Copyright (C) 2019 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.hwui;
18
19import android.app.Activity;
20import android.graphics.Color;
21import android.graphics.HardwareRenderer;
22import android.graphics.Paint;
23import android.graphics.RecordingCanvas;
24import android.graphics.RenderNode;
25import android.os.Bundle;
26import android.util.Log;
27import android.view.SurfaceHolder;
28
29public class CustomRenderer extends Activity {
30 private RenderNode mContent = new RenderNode("CustomRenderer");
31 private HardwareRenderer mRenderer = new HardwareRenderer();
32
33 @Override
34 protected void onCreate(Bundle savedInstanceState) {
35 super.onCreate(savedInstanceState);
36 getWindow().takeSurface(mSurfaceCallbacks);
37 }
38
39 private SurfaceHolder.Callback2 mSurfaceCallbacks = new SurfaceHolder.Callback2() {
40
41 @Override
42 public void surfaceRedrawNeeded(SurfaceHolder holder) {
43 }
44
45 @Override
46 public void surfaceCreated(SurfaceHolder holder) {
47 }
48
49 @Override
50 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
51 mContent.setLeftTopRightBottom(0, 0, width, height);
John Recke57475e2019-02-20 17:39:52 -080052 RecordingCanvas canvas = mContent.beginRecording();
John Reckfe5dfca2019-01-17 17:01:32 -080053 canvas.drawColor(Color.WHITE);
54 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
55 paint.setColor(Color.BLACK);
56 paint.setTextAlign(Paint.Align.CENTER);
57 paint.setTextSize(Math.min(width, height) * .05f);
58 canvas.drawText("Hello custom renderer!", width / 2, height / 2, paint);
59 mContent.endRecording();
60
61 mRenderer.setContentRoot(mContent);
62 mRenderer.setSurface(holder.getSurface());
63 mRenderer.createRenderRequest()
64 .setVsyncTime(System.nanoTime())
65 .setFrameCommitCallback(Runnable::run, () -> {
66 Log.d("CustomRenderer", "Frame committed!");
67 })
68 .syncAndDraw();
69 }
70
71 @Override
72 public void surfaceDestroyed(SurfaceHolder holder) {
73 mRenderer.destroy();
74 }
75 };
76}