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.Bitmap;
21 import android.graphics.Bitmap.Config;
22 import android.graphics.Canvas;
23 import android.graphics.Color;
24 import android.graphics.ColorSpace;
25 import android.graphics.ColorSpace.Named;
26 import android.graphics.HardwareBufferRenderer;
27 import android.graphics.Paint;
28 import android.graphics.PixelFormat;
29 import android.graphics.RenderNode;
30 import android.hardware.HardwareBuffer;
31 import android.os.Bundle;
32 import android.os.Handler;
33 import android.os.Looper;
34 import android.view.ViewGroup;
35 import android.widget.FrameLayout;
36 import android.widget.ImageView;
37 
38 import java.time.Duration;
39 import java.util.concurrent.Executors;
40 
41 public class HardwareBufferRendererActivity extends Activity {
42 
43     private ImageView mImageView;
44 
45     @Override
onCreate(Bundle savedInstanceState)46     protected void onCreate(Bundle savedInstanceState) {
47         super.onCreate(savedInstanceState);
48         mImageView = new ImageView(this);
49         mImageView.setBackgroundColor(Color.MAGENTA);
50         FrameLayout layout = new FrameLayout(this);
51         layout.setBackgroundColor(Color.CYAN);
52         layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
53                 ViewGroup.LayoutParams.MATCH_PARENT));
54         layout.addView(mImageView, new FrameLayout.LayoutParams(100, 100));
55         setContentView(layout);
56 
57         HardwareBuffer buffer = HardwareBuffer.create(100, 100, PixelFormat.RGBA_8888, 1,
58                 HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE | HardwareBuffer.USAGE_GPU_COLOR_OUTPUT);
59         HardwareBufferRenderer renderer = new HardwareBufferRenderer(buffer);
60         RenderNode node = new RenderNode("content");
61         node.setPosition(0, 0, 100, 100);
62 
63         Canvas canvas = node.beginRecording();
64         canvas.drawColor(Color.BLUE);
65 
66         Paint paint = new Paint();
67         paint.setColor(Color.RED);
68         canvas.drawRect(0f, 0f, 50f, 50f, paint);
69         node.endRecording();
70 
71         renderer.setContentRoot(node);
72 
73         ColorSpace colorSpace = ColorSpace.get(Named.SRGB);
74         Handler handler = new Handler(Looper.getMainLooper());
75         renderer.obtainRenderRequest()
76                 .setColorSpace(colorSpace)
77                 .draw(Executors.newSingleThreadExecutor(), result -> {
78                     result.getFence().await(Duration.ofMillis(3000));
79                     handler.post(() -> {
80                         Bitmap bitmap = Bitmap.wrapHardwareBuffer(buffer, colorSpace);
81                         Bitmap copy = bitmap.copy(Config.ARGB_8888, false);
82                         mImageView.setImageBitmap(copy);
83                     });
84                 });
85     }
86 }
87