1 /* <lambda>null2 * Copyright (C) 2023 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.tools.utils 18 19 import android.tools.common.datatypes.ActiveBuffer 20 import android.tools.common.datatypes.Color 21 import android.tools.common.datatypes.Matrix33 22 import android.tools.common.datatypes.Rect 23 import android.tools.common.datatypes.RectF 24 import android.tools.common.datatypes.Region 25 import android.tools.common.traces.surfaceflinger.Flag 26 import android.tools.common.traces.surfaceflinger.HwcCompositionType 27 import android.tools.common.traces.surfaceflinger.Layer 28 import android.tools.common.traces.surfaceflinger.Transform 29 30 class MockLayerBuilder(private val name: String) { 31 companion object { 32 private var idCounter = 1 33 } 34 35 private val children = mutableListOf<MockLayerBuilder>() 36 private var type = "BufferStateLayer" 37 private val id = idCounter++ 38 private var parentId = -1 39 private var isVisible = true 40 private var absoluteBounds: Rect? = null 41 private var zIndex = 0 42 private var isOpaque = true 43 44 fun addChild(layer: MockLayerBuilder): MockLayerBuilder = apply { this.children.add(layer) } 45 46 fun setContainerLayer(): MockLayerBuilder = apply { 47 this.type = "ContainerLayer" 48 this.isOpaque = false 49 this.isVisible = false 50 } 51 52 fun setVisible(): MockLayerBuilder = apply { this.isVisible = true } 53 54 fun setInvisible(): MockLayerBuilder = apply { this.isVisible = false } 55 56 fun addChildren(rootLayers: Collection<MockLayerBuilder>): MockLayerBuilder = apply { 57 rootLayers.forEach { addChild(it) } 58 } 59 60 fun setAbsoluteBounds(bounds: Rect): MockLayerBuilder = apply { this.absoluteBounds = bounds } 61 62 fun build(): Layer { 63 val absoluteBounds = this.absoluteBounds 64 requireNotNull(absoluteBounds) { "Layer has no bounds set..." } 65 66 val transform = Transform.from(0, Matrix33.identity(0f, 0f)) 67 68 val thisLayer = 69 Layer.from( 70 name, 71 id, 72 parentId, 73 z = zIndex, 74 visibleRegion = if (isVisible) Region.from(absoluteBounds) else Region.EMPTY, 75 activeBuffer = ActiveBuffer.from(absoluteBounds.width, absoluteBounds.height, 1, 1), 76 flags = if (isVisible) 0 else Flag.HIDDEN.value, 77 bounds = absoluteBounds.toRectF(), 78 color = Color.DEFAULT, 79 isOpaque = isVisible && isOpaque, 80 shadowRadius = 0f, 81 cornerRadius = 0f, 82 type = type, 83 screenBounds = absoluteBounds.toRectF(), 84 transform = transform, 85 sourceBounds = absoluteBounds.toRectF(), 86 currFrame = 0, 87 effectiveScalingMode = 0, 88 bufferTransform = transform, 89 hwcCompositionType = HwcCompositionType.INVALID, 90 hwcCrop = RectF.EMPTY, 91 hwcFrame = Rect.EMPTY, 92 crop = absoluteBounds, 93 backgroundBlurRadius = 0, 94 isRelativeOf = false, 95 zOrderRelativeOfId = -1, 96 stackId = 0, 97 requestedTransform = transform, 98 requestedColor = Color.DEFAULT, 99 cornerRadiusCrop = RectF.EMPTY, 100 inputTransform = transform, 101 inputRegion = Region.from(absoluteBounds), 102 excludesCompositionState = false 103 ) 104 105 val layers = mutableListOf<Layer>() 106 layers.add(thisLayer) 107 108 // var indexCount = 1 109 children.forEach { child -> 110 child.parentId = this.id 111 // it.zIndex = this.zIndex + indexCount 112 113 val childAbsoluteBounds = child.absoluteBounds 114 if (childAbsoluteBounds == null) { 115 child.absoluteBounds = this.absoluteBounds 116 } else { 117 child.absoluteBounds = childAbsoluteBounds.intersection(absoluteBounds) 118 } 119 120 thisLayer.addChild(child.build()) 121 } 122 123 return thisLayer 124 } 125 } 126