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.StreamConfigurationDuration;
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 custom class {@link StreamConfigurationDuration} for min-frame and stall durations.
30 *
31 * <p>
32 * Data is stored as {@code (format, width, height, durationNs)} tuples (int64).
33 * </p>
34 */
35public class MarshalQueryableStreamConfigurationDuration
36        implements MarshalQueryable<StreamConfigurationDuration> {
37
38    private static final int SIZE = SIZEOF_INT64 * 4;
39    /**
40     * Values and-ed with this will do an unsigned int to signed long conversion;
41     * in other words the sign bit from the int will not be extended.
42     * */
43    private static final long MASK_UNSIGNED_INT = 0x00000000ffffffffL;
44
45    private class MarshalerStreamConfigurationDuration
46        extends Marshaler<StreamConfigurationDuration> {
47
48        protected MarshalerStreamConfigurationDuration(
49                TypeReference<StreamConfigurationDuration> typeReference, int nativeType) {
50            super(MarshalQueryableStreamConfigurationDuration.this, typeReference, nativeType);
51        }
52
53        @Override
54        public void marshal(StreamConfigurationDuration value, ByteBuffer buffer) {
55            buffer.putLong(value.getFormat() & MASK_UNSIGNED_INT); // unsigned int -> long
56            buffer.putLong(value.getWidth());
57            buffer.putLong(value.getHeight());
58            buffer.putLong(value.getDuration());
59        }
60
61        @Override
62        public StreamConfigurationDuration unmarshal(ByteBuffer buffer) {
63            int format = (int)buffer.getLong();
64            int width = (int)buffer.getLong();
65            int height = (int)buffer.getLong();
66            long durationNs = buffer.getLong();
67
68            return new StreamConfigurationDuration(format, width, height, durationNs);
69        }
70
71        @Override
72        public int getNativeSize() {
73            return SIZE;
74        }
75    }
76
77    @Override
78    public Marshaler<StreamConfigurationDuration> createMarshaler(
79            TypeReference<StreamConfigurationDuration> managedType, int nativeType) {
80        return new MarshalerStreamConfigurationDuration(managedType, nativeType);
81    }
82
83    @Override
84    public boolean isTypeMappingSupported(TypeReference<StreamConfigurationDuration> managedType,
85            int nativeType) {
86        return nativeType == TYPE_INT64 &&
87                (StreamConfigurationDuration.class.equals(managedType.getType()));
88    }
89
90}