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.MeteringRectangle;
21import android.hardware.camera2.utils.TypeReference;
22
23import java.nio.ByteBuffer;
24
25import static android.hardware.camera2.impl.CameraMetadataNative.*;
26import static android.hardware.camera2.marshal.MarshalHelpers.*;
27
28/**
29 * Marshal {@link MeteringRectangle} to/from {@link #TYPE_INT32}
30 */
31public class MarshalQueryableMeteringRectangle implements MarshalQueryable<MeteringRectangle> {
32    private static final int SIZE = SIZEOF_INT32 * 5;
33
34    /** (xmin, ymin, xmax, ymax, weight) */
35    private class MarshalerMeteringRectangle extends Marshaler<MeteringRectangle> {
36        protected MarshalerMeteringRectangle(TypeReference<MeteringRectangle> typeReference,
37                int nativeType) {
38            super(MarshalQueryableMeteringRectangle.this, typeReference, nativeType);
39        }
40
41        @Override
42        public void marshal(MeteringRectangle value, ByteBuffer buffer) {
43            int xMin = value.getX();
44            int yMin = value.getY();
45            int xMax = xMin + value.getWidth();
46            int yMax = yMin + value.getHeight();
47            int weight = value.getMeteringWeight();
48
49            buffer.putInt(xMin);
50            buffer.putInt(yMin);
51            buffer.putInt(xMax);
52            buffer.putInt(yMax);
53            buffer.putInt(weight);
54        }
55
56        @Override
57        public MeteringRectangle unmarshal(ByteBuffer buffer) {
58            int xMin = buffer.getInt();
59            int yMin = buffer.getInt();
60            int xMax = buffer.getInt();
61            int yMax = buffer.getInt();
62            int weight = buffer.getInt();
63
64            int width = xMax - xMin;
65            int height = yMax - yMin;
66
67            return new MeteringRectangle(xMin, yMin, width, height, weight);
68        }
69
70        @Override
71        public int getNativeSize() {
72            return SIZE;
73        }
74    }
75
76    @Override
77    public Marshaler<MeteringRectangle> createMarshaler(
78            TypeReference<MeteringRectangle> managedType, int nativeType) {
79        return new MarshalerMeteringRectangle(managedType, nativeType);
80    }
81
82    @Override
83    public boolean isTypeMappingSupported(
84            TypeReference<MeteringRectangle> managedType, int nativeType) {
85        return nativeType == TYPE_INT32 && MeteringRectangle.class.equals(managedType.getType());
86    }
87
88}
89