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