| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.hardware.camera2.marshal; |
| 17 |
|
| 18 |
import android.annotation.Nullable; |
| 19 |
import android.hardware.camera2.impl.CameraMetadataNative; |
| 20 |
import android.hardware.camera2.utils.TypeReference; |
| 21 |
|
| 22 |
import java.util.ArrayList; |
| 23 |
import java.util.HashMap; |
| 24 |
import java.util.List; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class MarshalRegistry { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public static <T> void registerMarshalQueryable(MarshalQueryable<T> queryable) { |
| 41 |
synchronized(sMarshalLock) { |
| 42 |
sRegisteredMarshalQueryables.add(queryable); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
@SuppressWarnings("unchecked") |
| 59 |
public static <T> Marshaler<T> getMarshaler(TypeReference<T> typeToken, int nativeType) { |
| 60 |
synchronized(sMarshalLock) { |
| 61 |
|
| 62 |
|
| 63 |
MarshalToken<T> marshalToken = new MarshalToken<T>(typeToken, nativeType); |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
Marshaler<T> marshaler = |
| 70 |
(Marshaler<T>) sMarshalerMap.get(marshalToken); |
| 71 |
|
| 72 |
if (marshaler == null) { |
| 73 |
|
| 74 |
if (sRegisteredMarshalQueryables.size() == 0) { |
| 75 |
throw new AssertionError("No available query marshalers registered"); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
for (MarshalQueryable<?> potentialMarshaler : sRegisteredMarshalQueryables) { |
| 80 |
|
| 81 |
MarshalQueryable<T> castedPotential = |
| 82 |
(MarshalQueryable<T>)potentialMarshaler; |
| 83 |
|
| 84 |
if (castedPotential.isTypeMappingSupported(typeToken, nativeType)) { |
| 85 |
marshaler = castedPotential.createMarshaler(typeToken, nativeType); |
| 86 |
break; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
if (marshaler == null) { |
| 91 |
throw new UnsupportedOperationException( |
| 92 |
"Could not find marshaler that matches the requested " + |
| 93 |
"combination of type reference " + |
| 94 |
typeToken + " and native type " + |
| 95 |
MarshalHelpers.toStringNativeType(nativeType)); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
sMarshalerMap.put(marshalToken, marshaler); |
| 100 |
} |
| 101 |
|
| 102 |
return marshaler; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
private static class MarshalToken<T> { |
| 107 |
public MarshalToken(TypeReference<T> typeReference, int nativeType) { |
| 108 |
this.typeReference = typeReference; |
| 109 |
this.nativeType = nativeType; |
| 110 |
this.hash = typeReference.hashCode() ^ nativeType; |
| 111 |
} |
| 112 |
|
| 113 |
final TypeReference<T> typeReference; |
| 114 |
final int nativeType; |
| 115 |
private final int hash; |
| 116 |
|
| 117 |
@Override |
| 118 |
public boolean equals(@Nullable Object other) { |
| 119 |
if (other instanceof MarshalToken<?>) { |
| 120 |
MarshalToken<?> otherToken = (MarshalToken<?>)other; |
| 121 |
return typeReference.equals(otherToken.typeReference) && |
| 122 |
nativeType == otherToken.nativeType; |
| 123 |
} |
| 124 |
|
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public int hashCode() { |
| 130 |
return hash; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
private static final Object sMarshalLock = new Object(); |
| 136 |
|
| 137 |
private static final List<MarshalQueryable<?>> sRegisteredMarshalQueryables = |
| 138 |
new ArrayList<MarshalQueryable<?>>(); |
| 139 |
private static final HashMap<MarshalToken<?>, Marshaler<?>> sMarshalerMap = |
| 140 |
new HashMap<MarshalToken<?>, Marshaler<?>>(); |
| 141 |
|
| 142 |
private MarshalRegistry() { |
| 143 |
throw new AssertionError(); |
| 144 |
} |
| 145 |
} |