John Reck | fe5dfca | 2019-01-17 17:01:32 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.test.hwui; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.graphics.Color; |
| 21 | import android.graphics.HardwareRenderer; |
| 22 | import android.graphics.Paint; |
| 23 | import android.graphics.RecordingCanvas; |
| 24 | import android.graphics.RenderNode; |
| 25 | import android.os.Bundle; |
| 26 | import android.util.Log; |
| 27 | import android.view.SurfaceHolder; |
| 28 | |
| 29 | public 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 Reck | e57475e | 2019-02-20 17:39:52 -0800 | [diff] [blame] | 52 | RecordingCanvas canvas = mContent.beginRecording(); |
John Reck | fe5dfca | 2019-01-17 17:01:32 -0800 | [diff] [blame] | 53 | 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 | } |