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.util.Size;
19 import android.hardware.camera2.marshal.Marshaler;
20 import android.hardware.camera2.marshal.MarshalQueryable;
21 import android.hardware.camera2.utils.TypeReference;
22 
23 import static android.hardware.camera2.impl.CameraMetadataNative.*;
24 import static android.hardware.camera2.marshal.MarshalHelpers.*;
25 
26 import java.nio.ByteBuffer;
27 
28 /**
29  * Marshal {@link Size} to/from {@code TYPE_INT32}
30  */
31 public class MarshalQueryableSize implements MarshalQueryable<Size> {
32     private static final int SIZE = SIZEOF_INT32 * 2;
33 
34     private class MarshalerSize extends Marshaler<Size> {
MarshalerSize(TypeReference<Size> typeReference, int nativeType)35         protected MarshalerSize(TypeReference<Size> typeReference, int nativeType) {
36             super(MarshalQueryableSize.this, typeReference, nativeType);
37         }
38 
39         @Override
marshal(Size value, ByteBuffer buffer)40         public void marshal(Size value, ByteBuffer buffer) {
41             buffer.putInt(value.getWidth());
42             buffer.putInt(value.getHeight());
43         }
44 
45         @Override
unmarshal(ByteBuffer buffer)46         public Size unmarshal(ByteBuffer buffer) {
47             int width = buffer.getInt();
48             int height = buffer.getInt();
49 
50             return new Size(width, height);
51         }
52 
53         @Override
getNativeSize()54         public int getNativeSize() {
55             return SIZE;
56         }
57     }
58 
59     @Override
createMarshaler(TypeReference<Size> managedType, int nativeType)60     public Marshaler<Size> createMarshaler(TypeReference<Size> managedType, int nativeType) {
61         return new MarshalerSize(managedType, nativeType);
62     }
63 
64     @Override
isTypeMappingSupported(TypeReference<Size> managedType, int nativeType)65     public boolean isTypeMappingSupported(TypeReference<Size> managedType, int nativeType) {
66         return nativeType == TYPE_INT32 && (Size.class.equals(managedType.getType()));
67     }
68 }
69