• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Canvas;
22 import android.graphics.Color;
23 import android.graphics.ColorSpace;
24 import android.graphics.HardwareRenderer;
25 import android.graphics.Outline;
26 import android.graphics.PixelFormat;
27 import android.graphics.Rect;
28 import android.graphics.RenderNode;
29 import android.hardware.HardwareBuffer;
30 import android.media.Image;
31 import android.media.ImageReader;
32 import android.os.Bundle;
33 import android.widget.ImageView;
34 
35 public class MyLittleTextureView extends Activity {
36     private RenderNode mContent = new RenderNode("CustomRenderer");
37     private HardwareRenderer mRenderer = new HardwareRenderer();
38     private ImageView mImageView;
39 
40     @Override
onCreate(Bundle savedInstanceState)41     protected void onCreate(Bundle savedInstanceState) {
42         super.onCreate(savedInstanceState);
43         mImageView = new ImageView(this);
44         mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
45         setContentView(mImageView);
46 
47         ImageReader reader = ImageReader.newInstance(100, 100, PixelFormat.RGBA_8888, 3,
48                 HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE | HardwareBuffer.USAGE_GPU_COLOR_OUTPUT);
49         mRenderer.setSurface(reader.getSurface());
50         mRenderer.setLightSourceAlpha(0.0f, 1.0f);
51         mRenderer.setLightSourceGeometry(100 / 2f, 0f, 800.0f, 20.0f);
52         mContent.setLeftTopRightBottom(0, 0, 100, 100);
53 
54         Rect childRect = new Rect(25, 25, 65, 65);
55         RenderNode childNode = new RenderNode("shadowCaster");
56         childNode.setLeftTopRightBottom(childRect.left, childRect.top,
57                 childRect.right, childRect.bottom);
58         Outline outline = new Outline();
59         outline.setRect(new Rect(0, 0, childRect.width(), childRect.height()));
60         outline.setAlpha(1f);
61         childNode.setOutline(outline);
62         {
63             Canvas canvas = childNode.beginRecording();
64             canvas.drawColor(Color.BLUE);
65         }
66         childNode.endRecording();
67         childNode.setElevation(20f);
68 
69         {
70             Canvas canvas = mContent.beginRecording();
71             canvas.drawColor(Color.WHITE);
72             canvas.enableZ();
73             canvas.drawRenderNode(childNode);
74             canvas.disableZ();
75         }
76         mContent.endRecording();
77         mRenderer.setContentRoot(mContent);
78         mRenderer.createRenderRequest()
79                 .setWaitForPresent(true)
80                 .syncAndDraw();
81         Image image = reader.acquireNextImage();
82         Bitmap bitmap = Bitmap.wrapHardwareBuffer(image.getHardwareBuffer(),
83                 ColorSpace.get(ColorSpace.Named.SRGB));
84         mImageView.setImageBitmap(bitmap);
85         image.close();
86     }
87 }
88