1 /* 2 * Copyright (C) 2024 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 package com.android.internal.widget.remotecompose.core.operations.layout.modifiers; 17 18 import android.annotation.NonNull; 19 20 import com.android.internal.widget.remotecompose.core.Operation; 21 import com.android.internal.widget.remotecompose.core.Operations; 22 import com.android.internal.widget.remotecompose.core.PaintContext; 23 import com.android.internal.widget.remotecompose.core.RemoteContext; 24 import com.android.internal.widget.remotecompose.core.WireBuffer; 25 import com.android.internal.widget.remotecompose.core.documentation.DocumentationBuilder; 26 import com.android.internal.widget.remotecompose.core.operations.layout.Component; 27 import com.android.internal.widget.remotecompose.core.operations.utilities.StringSerializer; 28 import com.android.internal.widget.remotecompose.core.serialize.MapSerializer; 29 import com.android.internal.widget.remotecompose.core.serialize.SerializeTags; 30 31 import java.util.List; 32 33 /** Support modifier clip with a rectangle */ 34 public class ClipRectModifierOperation extends DecoratorModifierOperation { 35 public static final String CLASS_NAME = "ClipRectModifierOperation"; 36 private static final int OP_CODE = Operations.MODIFIER_CLIP_RECT; 37 float mWidth; 38 float mHeight; 39 40 @Override paint(@onNull PaintContext context)41 public void paint(@NonNull PaintContext context) { 42 context.clipRect(0f, 0f, mWidth, mHeight); 43 } 44 45 @Override layout( @onNull RemoteContext context, Component component, float width, float height)46 public void layout( 47 @NonNull RemoteContext context, Component component, float width, float height) { 48 this.mWidth = width; 49 this.mHeight = height; 50 } 51 52 @Override serializeToString(int indent, @NonNull StringSerializer serializer)53 public void serializeToString(int indent, @NonNull StringSerializer serializer) { 54 serializer.append(indent, "CLIP_RECT = [" + mWidth + ", " + mHeight + "]"); 55 } 56 57 @Override write(@onNull WireBuffer buffer)58 public void write(@NonNull WireBuffer buffer) { 59 apply(buffer); 60 } 61 62 /** 63 * The name of the class 64 * 65 * @return the name 66 */ 67 @NonNull name()68 public static String name() { 69 return CLASS_NAME; 70 } 71 72 /** 73 * The OP_CODE for this command 74 * 75 * @return the opcode 76 */ id()77 public static int id() { 78 return OP_CODE; 79 } 80 81 /** 82 * Write the operation to the buffer 83 * 84 * @param buffer the WireBuffer 85 */ apply(@onNull WireBuffer buffer)86 public static void apply(@NonNull WireBuffer buffer) { 87 buffer.start(OP_CODE); 88 } 89 90 /** 91 * Read this operation and add it to the list of operations 92 * 93 * @param buffer the buffer to read 94 * @param operations the list of operations that will be added to 95 */ read(@onNull WireBuffer buffer, @NonNull List<Operation> operations)96 public static void read(@NonNull WireBuffer buffer, @NonNull List<Operation> operations) { 97 operations.add(new ClipRectModifierOperation()); 98 } 99 100 /** 101 * Populate the documentation with a description of this operation 102 * 103 * @param doc to append the description to. 104 */ documentation(@onNull DocumentationBuilder doc)105 public static void documentation(@NonNull DocumentationBuilder doc) { 106 doc.operation("Canvas Operations", OP_CODE, CLASS_NAME) 107 .description("Draw the specified round-rect"); 108 } 109 110 @Override serialize(MapSerializer serializer)111 public void serialize(MapSerializer serializer) { 112 serializer 113 .addTags(SerializeTags.MODIFIER) 114 .addType("ClipRectModifierOperation") 115 .add("width", mWidth) 116 .add("height", mHeight); 117 } 118 } 119