1 /* 2 * Copyright (C) 2021 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 android.graphics.cts; 18 19 import static android.graphics.cts.utils.LeakTest.runNotLeakingTest; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import android.graphics.Bitmap; 24 import android.graphics.Canvas; 25 import android.graphics.Color; 26 import android.graphics.HardwareBufferRenderer; 27 import android.graphics.HardwareRenderer; 28 import android.graphics.RenderNode; 29 import android.graphics.SurfaceTexture; 30 import android.hardware.HardwareBuffer; 31 import android.view.PixelCopy; 32 import android.view.Surface; 33 34 import androidx.test.filters.LargeTest; 35 import androidx.test.runner.AndroidJUnit4; 36 37 import com.android.compatibility.common.util.SynchronousPixelCopy; 38 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 42 import java.util.concurrent.Executor; 43 import java.util.function.Consumer; 44 45 @RunWith(AndroidJUnit4.class) 46 public class HardwareRendererTest { 47 48 @Test isDrawingEnabled_defaultsTrue()49 public void isDrawingEnabled_defaultsTrue() { 50 assertThat(HardwareRenderer.isDrawingEnabled()).isTrue(); 51 } 52 53 @Test setDrawingEnabled()54 public void setDrawingEnabled() { 55 HardwareRenderer.setDrawingEnabled(false); 56 57 assertThat(HardwareRenderer.isDrawingEnabled()).isFalse(); 58 59 HardwareRenderer.setDrawingEnabled(true); 60 assertThat(HardwareRenderer.isDrawingEnabled()).isTrue(); 61 } 62 63 @Test lockHardwareCanvasSingleBuffer()64 public void lockHardwareCanvasSingleBuffer() { 65 SurfaceTexture st = new SurfaceTexture(true); 66 st.setDefaultBufferSize(50, 50); 67 Surface surface = new Surface(st); 68 Canvas canvas = surface.lockHardwareCanvas(); 69 canvas.drawColor(Color.BLUE); 70 surface.unlockCanvasAndPost(canvas); 71 PixelCopy.Result result = SynchronousPixelCopy.copySurface(surface); 72 assertThat(result.getStatus()).isEqualTo(PixelCopy.SUCCESS); 73 Bitmap bitmap = result.getBitmap(); 74 assertThat(bitmap).isNotNull(); 75 assertThat(bitmap.getColor(1, 1).toArgb()).isEqualTo(Color.BLUE); 76 } 77 78 @Test 79 @LargeTest hardwareBufferRendererLeakTest()80 public void hardwareBufferRendererLeakTest() { 81 HardwareBuffer buffer = HardwareBuffer.create(128, 128, HardwareBuffer.RGBA_8888, 82 1, HardwareBuffer.USAGE_GPU_COLOR_OUTPUT | HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE); 83 HardwareBufferRenderer renderer = new HardwareBufferRenderer(buffer); 84 RenderNode content = new RenderNode("leaktest"); 85 content.setPosition(0, 0, 128, 128); 86 renderer.setContentRoot(content); 87 88 Executor executor = Runnable::run; 89 Consumer<HardwareBufferRenderer.RenderResult> resultListener = renderResult -> { 90 renderResult.getFence().close(); 91 }; 92 93 runNotLeakingTest(() -> { 94 Canvas canvas = content.beginRecording(); 95 canvas.drawColor(Color.RED); 96 content.endRecording(); 97 renderer.obtainRenderRequest().draw(executor, resultListener); 98 }); 99 } 100 } 101