Make HardwareRenderer public API
Bug: 123661129 Test: HardwareRenderer CTS tests Change-Id: Ic7ff69c9489d00e3f525eec761a84d06cf81be7a
This commit is contained in:
@@ -1028,8 +1028,28 @@
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
android:name="PositionListenerActivity"
|
||||
android:label="RenderNode/PositionListener"
|
||||
android:name="PositionListenerActivity"
|
||||
android:label="RenderNode/PositionListener"
|
||||
android:screenOrientation="fullSensor">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="com.android.test.hwui.TEST" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
android:name="CustomRenderer"
|
||||
android:label="HardwareRenderer/HelloTakeSurface"
|
||||
android:screenOrientation="fullSensor">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="com.android.test.hwui.TEST" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
android:name="MyLittleTextureView"
|
||||
android:label="HardwareRenderer/MyLittleTextureView"
|
||||
android:screenOrientation="fullSensor">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright (C) 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.test.hwui;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.HardwareRenderer;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.RecordingCanvas;
|
||||
import android.graphics.RenderNode;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.SurfaceHolder;
|
||||
|
||||
public class CustomRenderer extends Activity {
|
||||
private RenderNode mContent = new RenderNode("CustomRenderer");
|
||||
private HardwareRenderer mRenderer = new HardwareRenderer();
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
getWindow().takeSurface(mSurfaceCallbacks);
|
||||
}
|
||||
|
||||
private SurfaceHolder.Callback2 mSurfaceCallbacks = new SurfaceHolder.Callback2() {
|
||||
|
||||
@Override
|
||||
public void surfaceRedrawNeeded(SurfaceHolder holder) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
||||
mContent.setLeftTopRightBottom(0, 0, width, height);
|
||||
RecordingCanvas canvas = mContent.startRecording();
|
||||
canvas.drawColor(Color.WHITE);
|
||||
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
paint.setColor(Color.BLACK);
|
||||
paint.setTextAlign(Paint.Align.CENTER);
|
||||
paint.setTextSize(Math.min(width, height) * .05f);
|
||||
canvas.drawText("Hello custom renderer!", width / 2, height / 2, paint);
|
||||
mContent.endRecording();
|
||||
|
||||
mRenderer.setContentRoot(mContent);
|
||||
mRenderer.setSurface(holder.getSurface());
|
||||
mRenderer.createRenderRequest()
|
||||
.setVsyncTime(System.nanoTime())
|
||||
.setFrameCommitCallback(Runnable::run, () -> {
|
||||
Log.d("CustomRenderer", "Frame committed!");
|
||||
})
|
||||
.syncAndDraw();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||
mRenderer.destroy();
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright (C) 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.test.hwui;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.ColorSpace;
|
||||
import android.graphics.HardwareRenderer;
|
||||
import android.graphics.Outline;
|
||||
import android.graphics.PixelFormat;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.RenderNode;
|
||||
import android.hardware.HardwareBuffer;
|
||||
import android.media.Image;
|
||||
import android.media.ImageReader;
|
||||
import android.os.Bundle;
|
||||
import android.widget.ImageView;
|
||||
|
||||
public class MyLittleTextureView extends Activity {
|
||||
private RenderNode mContent = new RenderNode("CustomRenderer");
|
||||
private HardwareRenderer mRenderer = new HardwareRenderer();
|
||||
private ImageView mImageView;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
mImageView = new ImageView(this);
|
||||
mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
|
||||
setContentView(mImageView);
|
||||
|
||||
ImageReader reader = ImageReader.newInstance(100, 100, PixelFormat.RGBA_8888, 3,
|
||||
HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE | HardwareBuffer.USAGE_GPU_COLOR_OUTPUT);
|
||||
mRenderer.setSurface(reader.getSurface());
|
||||
mRenderer.setLightSourceAlpha(0.0f, 1.0f);
|
||||
mRenderer.setLightSourceGeometry(100 / 2f, 0f, 800.0f, 20.0f);
|
||||
mContent.setLeftTopRightBottom(0, 0, 100, 100);
|
||||
|
||||
Rect childRect = new Rect(25, 25, 65, 65);
|
||||
RenderNode childNode = new RenderNode("shadowCaster");
|
||||
childNode.setLeftTopRightBottom(childRect.left, childRect.top,
|
||||
childRect.right, childRect.bottom);
|
||||
Outline outline = new Outline();
|
||||
outline.setRect(new Rect(0, 0, childRect.width(), childRect.height()));
|
||||
outline.setAlpha(1f);
|
||||
childNode.setOutline(outline);
|
||||
{
|
||||
Canvas canvas = childNode.startRecording();
|
||||
canvas.drawColor(Color.BLUE);
|
||||
}
|
||||
childNode.endRecording();
|
||||
childNode.setElevation(20f);
|
||||
|
||||
{
|
||||
Canvas canvas = mContent.startRecording();
|
||||
canvas.drawColor(Color.WHITE);
|
||||
canvas.enableZ();
|
||||
canvas.drawRenderNode(childNode);
|
||||
canvas.disableZ();
|
||||
}
|
||||
mContent.endRecording();
|
||||
mRenderer.setContentRoot(mContent);
|
||||
mRenderer.createRenderRequest()
|
||||
.setWaitForPresent(true)
|
||||
.syncAndDraw();
|
||||
Image image = reader.acquireNextImage();
|
||||
Bitmap bitmap = Bitmap.wrapHardwareBuffer(image.getHardwareBuffer(),
|
||||
ColorSpace.get(ColorSpace.Named.SRGB));
|
||||
mImageView.setImageBitmap(bitmap);
|
||||
image.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user