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.StreamConfiguration;
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  * Marshaler for {@code android.scaler.availableStreamConfigurations} custom class
30  * {@link StreamConfiguration}
31  *
32  * <p>Data is stored as {@code (format, width, height, input?)} tuples (int32).</p>
33  */
34 public class MarshalQueryableStreamConfiguration
35         implements MarshalQueryable<StreamConfiguration> {
36     private static final int SIZE = SIZEOF_INT32 * 4;
37 
38     private class MarshalerStreamConfiguration extends Marshaler<StreamConfiguration> {
MarshalerStreamConfiguration(TypeReference<StreamConfiguration> typeReference, int nativeType)39         protected MarshalerStreamConfiguration(TypeReference<StreamConfiguration> typeReference,
40                 int nativeType) {
41             super(MarshalQueryableStreamConfiguration.this, typeReference, nativeType);
42         }
43 
44         @Override
marshal(StreamConfiguration value, ByteBuffer buffer)45         public void marshal(StreamConfiguration value, ByteBuffer buffer) {
46             buffer.putInt(value.getFormat());
47             buffer.putInt(value.getWidth());
48             buffer.putInt(value.getHeight());
49             buffer.putInt(value.isInput() ? 1 : 0);
50         }
51 
52         @Override
unmarshal(ByteBuffer buffer)53         public StreamConfiguration unmarshal(ByteBuffer buffer) {
54             int format = buffer.getInt();
55             int width = buffer.getInt();
56             int height = buffer.getInt();
57             boolean input = buffer.getInt() != 0;
58 
59             return new StreamConfiguration(format, width, height, input);
60         }
61 
62         @Override
getNativeSize()63         public int getNativeSize() {
64             return SIZE;
65         }
66 
67     }
68 
69     @Override
createMarshaler( TypeReference<StreamConfiguration> managedType, int nativeType)70     public Marshaler<StreamConfiguration> createMarshaler(
71             TypeReference<StreamConfiguration> managedType, int nativeType) {
72         return new MarshalerStreamConfiguration(managedType, nativeType);
73     }
74 
75     @Override
isTypeMappingSupported(TypeReference<StreamConfiguration> managedType, int nativeType)76     public boolean isTypeMappingSupported(TypeReference<StreamConfiguration> managedType,
77             int nativeType) {
78         return nativeType == TYPE_INT32 && managedType.getType().equals(StreamConfiguration.class);
79     }
80 }
81