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 */
16package android.hardware.camera2.marshal.impl;
17
18import android.hardware.camera2.marshal.Marshaler;
19import android.hardware.camera2.marshal.MarshalQueryable;
20import android.hardware.camera2.params.StreamConfiguration;
21import android.hardware.camera2.utils.TypeReference;
22
23import static android.hardware.camera2.impl.CameraMetadataNative.*;
24import static android.hardware.camera2.marshal.MarshalHelpers.*;
25
26import 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 */
34public class MarshalQueryableStreamConfiguration
35        implements MarshalQueryable<StreamConfiguration> {
36    private static final int SIZE = SIZEOF_INT32 * 4;
37
38    private class MarshalerStreamConfiguration extends Marshaler<StreamConfiguration> {
39        protected MarshalerStreamConfiguration(TypeReference<StreamConfiguration> typeReference,
40                int nativeType) {
41            super(MarshalQueryableStreamConfiguration.this, typeReference, nativeType);
42        }
43
44        @Override
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
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
63        public int getNativeSize() {
64            return SIZE;
65        }
66
67    }
68
69    @Override
70    public Marshaler<StreamConfiguration> createMarshaler(
71            TypeReference<StreamConfiguration> managedType, int nativeType) {
72        return new MarshalerStreamConfiguration(managedType, nativeType);
73    }
74
75    @Override
76    public boolean isTypeMappingSupported(TypeReference<StreamConfiguration> managedType,
77            int nativeType) {
78        return nativeType == TYPE_INT32 && managedType.getType().equals(StreamConfiguration.class);
79    }
80}
81