1 /*
2  * 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 package com.android.internal.widget.remotecompose.core.operations;
17 
18 import com.android.internal.widget.remotecompose.core.CompanionOperation;
19 import com.android.internal.widget.remotecompose.core.Operation;
20 import com.android.internal.widget.remotecompose.core.Operations;
21 import com.android.internal.widget.remotecompose.core.RemoteComposeOperation;
22 import com.android.internal.widget.remotecompose.core.RemoteContext;
23 import com.android.internal.widget.remotecompose.core.WireBuffer;
24 
25 import java.util.List;
26 
27 /**
28  * Describe some basic information for a RemoteCompose document
29  * <p>
30  * It encodes the version of the document (following semantic versioning) as well
31  * as the dimensions of the document in pixels.
32  */
33 public class Header implements RemoteComposeOperation {
34     public static final int MAJOR_VERSION = 0;
35     public static final int MINOR_VERSION = 1;
36     public static final int PATCH_VERSION = 0;
37 
38     int mMajorVersion;
39     int mMinorVersion;
40     int mPatchVersion;
41 
42     int mWidth;
43     int mHeight;
44     long mCapabilities;
45 
46     public static final Companion COMPANION = new Companion();
47 
48     /**
49      * It encodes the version of the document (following semantic versioning) as well
50      * as the dimensions of the document in pixels.
51      *
52      * @param majorVersion the major version of the RemoteCompose document API
53      * @param minorVersion the minor version of the RemoteCompose document API
54      * @param patchVersion the patch version of the RemoteCompose document API
55      * @param width        the width of the RemoteCompose document
56      * @param height       the height of the RemoteCompose document
57      * @param capabilities bitmask field storing needed capabilities (unused for now)
58      */
Header(int majorVersion, int minorVersion, int patchVersion, int width, int height, long capabilities)59     public Header(int majorVersion, int minorVersion, int patchVersion,
60                   int width, int height, long capabilities) {
61         this.mMajorVersion = majorVersion;
62         this.mMinorVersion = minorVersion;
63         this.mPatchVersion = patchVersion;
64         this.mWidth = width;
65         this.mHeight = height;
66         this.mCapabilities = capabilities;
67     }
68 
69     @Override
write(WireBuffer buffer)70     public void write(WireBuffer buffer) {
71         COMPANION.apply(buffer, mWidth, mHeight, mCapabilities);
72     }
73 
74     @Override
toString()75     public String toString() {
76         return "HEADER v" + mMajorVersion + "."
77                 + mMinorVersion + "." + mPatchVersion + ", "
78                 + mWidth + " x " + mHeight + " [" + mCapabilities + "]";
79     }
80 
81     @Override
apply(RemoteContext context)82     public void apply(RemoteContext context) {
83         context.header(mMajorVersion, mMinorVersion, mPatchVersion, mWidth, mHeight, mCapabilities);
84     }
85 
86     @Override
deepToString(String indent)87     public String deepToString(String indent) {
88         return toString();
89     }
90 
91     public static class Companion implements CompanionOperation {
Companion()92         private Companion() {
93         }
94 
95         @Override
name()96         public String name() {
97             return "Header";
98         }
99 
100         @Override
id()101         public int id() {
102             return Operations.HEADER;
103         }
104 
apply(WireBuffer buffer, int width, int height, long capabilities)105         public void apply(WireBuffer buffer, int width, int height, long capabilities) {
106             buffer.start(Operations.HEADER);
107             buffer.writeInt(MAJOR_VERSION); // major version number of the protocol
108             buffer.writeInt(MINOR_VERSION); // minor version number of the protocol
109             buffer.writeInt(PATCH_VERSION); // patch version number of the protocol
110             buffer.writeInt(width);
111             buffer.writeInt(height);
112             buffer.writeLong(capabilities);
113         }
114 
115         @Override
read(WireBuffer buffer, List<Operation> operations)116         public void read(WireBuffer buffer, List<Operation> operations) {
117             int majorVersion = buffer.readInt();
118             int minorVersion = buffer.readInt();
119             int patchVersion = buffer.readInt();
120             int width = buffer.readInt();
121             int height = buffer.readInt();
122             long capabilities = buffer.readLong();
123             Header header = new Header(majorVersion, minorVersion, patchVersion,
124                     width, height, capabilities);
125             operations.add(header);
126         }
127     }
128 }
129