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.MarshalQueryable;
19import android.hardware.camera2.marshal.Marshaler;
20import android.hardware.camera2.params.BlackLevelPattern;
21import android.hardware.camera2.utils.TypeReference;
22
23import java.nio.ByteBuffer;
24
25import static android.hardware.camera2.impl.CameraMetadataNative.TYPE_INT32;
26import static android.hardware.camera2.marshal.MarshalHelpers.SIZEOF_INT32;
27
28/**
29 * Marshal {@link BlackLevelPattern} to/from {@link #TYPE_INT32} {@code x 4}
30 */
31public class MarshalQueryableBlackLevelPattern implements MarshalQueryable<BlackLevelPattern> {
32    private static final int SIZE = SIZEOF_INT32 * BlackLevelPattern.COUNT;
33
34    private class MarshalerBlackLevelPattern extends Marshaler<BlackLevelPattern> {
35        protected MarshalerBlackLevelPattern(TypeReference<BlackLevelPattern> typeReference,
36                                               int nativeType) {
37            super(MarshalQueryableBlackLevelPattern.this, typeReference, nativeType);
38        }
39
40        @Override
41        public void marshal(BlackLevelPattern value, ByteBuffer buffer) {
42            for (int i = 0; i < BlackLevelPattern.COUNT / 2; ++i) {
43                for (int j = 0; j < BlackLevelPattern.COUNT / 2; ++j) {
44                    buffer.putInt(value.getOffsetForIndex(j, i));
45                }
46            }
47        }
48
49        @Override
50        public BlackLevelPattern unmarshal(ByteBuffer buffer) {
51            int[] channelOffsets = new int[BlackLevelPattern.COUNT];
52            for (int i = 0; i < BlackLevelPattern.COUNT; ++i) {
53                channelOffsets[i] = buffer.getInt();
54            }
55            return new BlackLevelPattern(channelOffsets);
56        }
57
58        @Override
59        public int getNativeSize() {
60            return SIZE;
61        }
62    }
63
64    @Override
65    public Marshaler<BlackLevelPattern> createMarshaler(
66            TypeReference<BlackLevelPattern> managedType, int nativeType) {
67        return new MarshalerBlackLevelPattern(managedType, nativeType);
68    }
69
70    @Override
71    public boolean isTypeMappingSupported(
72            TypeReference<BlackLevelPattern> managedType, int nativeType) {
73        return nativeType == TYPE_INT32 &&
74                (BlackLevelPattern.class.equals(managedType.getType()));
75    }
76}
77