Debian Codegraph @

tests/HwAccelerationTest/src/com/android/test/hwui/PenStylusActivity.kt

raw
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 com.android.test.hwui
18
19 import android.app.Activity
20 import android.os.Bundle
21 import android.hardware.HardwareBuffer
22 import android.graphics.Canvas
23 import android.graphics.PixelFormat
24 import android.graphics.Rect
25 import android.media.ImageReader
26 import android.view.Surface
27 import java.lang.IllegalArgumentException
28
29 const val USAGE_HWC = 0x800L
30
31 class PenStylusActivity : Activity() {
32
33 override fun onCreate(savedInstanceState: Bundle?) {
34 super.onCreate(savedInstanceState)
35
36 setContentView(FrontBufferedLayer(this))
37 }
38
39 class SingleBufferedCanvas : AutoCloseable {
40 val mHardwareBuffer: HardwareBuffer
41 private var mCanvas: Canvas?
42 private val mImageReader: ImageReader
43 private val mSurface: Surface
44
45 constructor(width: Int, height: Int) {
46 mImageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 1,
47 HardwareBuffer.USAGE_CPU_READ_RARELY or HardwareBuffer.USAGE_CPU_WRITE_RARELY or
48 HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE or USAGE_HWC)
49
50 mSurface = mImageReader.surface
51 mSurface.unlockCanvasAndPost(mSurface.lockCanvas(null))
52 val image = mImageReader.acquireNextImage()
53 mHardwareBuffer = image.hardwareBuffer!!
54 image.close()
55 mCanvas = mSurface.lockCanvas(null)
56 }
57
58 fun lockCanvas(rect: Rect?): Canvas {
59 if (mCanvas != null) {
60 unlockCanvas(mCanvas!!)
61 }
62 mCanvas = mSurface.lockCanvas(rect)
63 return mCanvas!!
64 }
65
66 fun unlockCanvas(canvas: Canvas) {
67 if (this.mCanvas !== canvas) throw IllegalArgumentException()
68 mSurface.unlockCanvasAndPost(canvas)
69 this.mCanvas = null
70 mImageReader.acquireNextImage().close()
71 }
72
73 inline fun update(area: Rect?, func: Canvas.() -> Unit) {
74 val canvas = lockCanvas(area)
75 func(canvas)
76 unlockCanvas(canvas)
77 }
78
79 override fun close() {
80 mHardwareBuffer.close()
81 mSurface.unlockCanvasAndPost(mCanvas)
82 mImageReader.close()
83 }
84 }
85 }