1 /*
2  * Copyright (C) 2014 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 android.hardware.camera2.marshal.impl;
17 
18 import android.hardware.camera2.marshal.Marshaler;
19 import android.hardware.camera2.marshal.MarshalQueryable;
20 import android.hardware.camera2.params.MeteringRectangle;
21 import android.hardware.camera2.utils.TypeReference;
22 
23 import java.nio.ByteBuffer;
24 
25 import static android.hardware.camera2.impl.CameraMetadataNative.*;
26 import static android.hardware.camera2.marshal.MarshalHelpers.*;
27 
28 /**
29  * Marshal {@link MeteringRectangle} to/from {@link #TYPE_INT32}
30  */
31 public class MarshalQueryableMeteringRectangle implements MarshalQueryable<MeteringRectangle> {
32     private static final int SIZE = SIZEOF_INT32 * 5;
33 
34     /** (xmin, ymin, xmax, ymax, weight) */
35     private class MarshalerMeteringRectangle extends Marshaler<MeteringRectangle> {
MarshalerMeteringRectangle(TypeReference<MeteringRectangle> typeReference, int nativeType)36         protected MarshalerMeteringRectangle(TypeReference<MeteringRectangle> typeReference,
37                 int nativeType) {
38             super(MarshalQueryableMeteringRectangle.this, typeReference, nativeType);
39         }
40 
41         @Override
marshal(MeteringRectangle value, ByteBuffer buffer)42         public void marshal(MeteringRectangle value, ByteBuffer buffer) {
43             int xMin = value.getX();
44             int yMin = value.getY();
45             int xMax = xMin + value.getWidth();
46             int yMax = yMin + value.getHeight();
47             int weight = value.getMeteringWeight();
48 
49             buffer.putInt(xMin);
50             buffer.putInt(yMin);
51             buffer.putInt(xMax);
52             buffer.putInt(yMax);
53             buffer.putInt(weight);
54         }
55 
56         @Override
unmarshal(ByteBuffer buffer)57         public MeteringRectangle unmarshal(ByteBuffer buffer) {
58             int xMin = buffer.getInt();
59             int yMin = buffer.getInt();
60             int xMax = buffer.getInt();
61             int yMax = buffer.getInt();
62             int weight = buffer.getInt();
63 
64             int width = xMax - xMin;
65             int height = yMax - yMin;
66 
67             return new MeteringRectangle(xMin, yMin, width, height, weight);
68         }
69 
70         @Override
getNativeSize()71         public int getNativeSize() {
72             return SIZE;
73         }
74     }
75 
76     @Override
createMarshaler( TypeReference<MeteringRectangle> managedType, int nativeType)77     public Marshaler<MeteringRectangle> createMarshaler(
78             TypeReference<MeteringRectangle> managedType, int nativeType) {
79         return new MarshalerMeteringRectangle(managedType, nativeType);
80     }
81 
82     @Override
isTypeMappingSupported( TypeReference<MeteringRectangle> managedType, int nativeType)83     public boolean isTypeMappingSupported(
84             TypeReference<MeteringRectangle> managedType, int nativeType) {
85         return nativeType == TYPE_INT32 && MeteringRectangle.class.equals(managedType.getType());
86     }
87 
88 }
89