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.graphics.Rect;
19import android.hardware.camera2.marshal.Marshaler;
20import android.hardware.camera2.marshal.MarshalQueryable;
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 Rect} to/from {@link #TYPE_INT32}
30 */
31public class MarshalQueryableRect implements MarshalQueryable<Rect> {
32    private static final int SIZE = SIZEOF_INT32 * 4;
33
34    private class MarshalerRect extends Marshaler<Rect> {
35        protected MarshalerRect(TypeReference<Rect> typeReference,
36                int nativeType) {
37            super(MarshalQueryableRect.this, typeReference, nativeType);
38        }
39
40        @Override
41        public void marshal(Rect value, ByteBuffer buffer) {
42            buffer.putInt(value.left);
43            buffer.putInt(value.top);
44            buffer.putInt(value.width());
45            buffer.putInt(value.height());
46        }
47
48        @Override
49        public Rect unmarshal(ByteBuffer buffer) {
50            int left = buffer.getInt();
51            int top = buffer.getInt();
52            int width = buffer.getInt();
53            int height = buffer.getInt();
54
55            int right = left + width;
56            int bottom = top + height;
57
58            return new Rect(left, top, right, bottom);
59        }
60
61        @Override
62        public int getNativeSize() {
63            return SIZE;
64        }
65    }
66
67    @Override
68    public Marshaler<Rect> createMarshaler(TypeReference<Rect> managedType, int nativeType) {
69        return new MarshalerRect(managedType, nativeType);
70    }
71
72    @Override
73    public boolean isTypeMappingSupported(TypeReference<Rect> managedType, int nativeType) {
74        return nativeType == TYPE_INT32 && (Rect.class.equals(managedType.getType()));
75    }
76
77}
78