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