Eino-Ville Talvala | b267554 | 2012-12-12 13:29:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
| 17 | package android.hardware.photography; |
| 18 | |
| 19 | import android.os.Parcelable; |
| 20 | import android.os.Parcel; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame^] | 21 | import android.util.Log; |
| 22 | |
Eino-Ville Talvala | b267554 | 2012-12-12 13:29:45 -0800 | [diff] [blame] | 23 | import java.util.HashMap; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | /** |
| 27 | * The base class for camera controls and information. |
| 28 | * |
| 29 | * This class defines the basic key/value map used for querying for camera |
| 30 | * characteristics or capture results, and for setting camera request |
| 31 | * parameters. |
| 32 | * |
| 33 | * @see CameraDevice |
| 34 | * @see CameraManager |
| 35 | * @see CameraProperties |
| 36 | **/ |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame^] | 37 | public class CameraMetadata implements Parcelable, AutoCloseable { |
Eino-Ville Talvala | b267554 | 2012-12-12 13:29:45 -0800 | [diff] [blame] | 38 | |
| 39 | public CameraMetadata() { |
| 40 | mMetadataMap = new HashMap<Key<?>, Object>(); |
Eino-Ville Talvala | b267554 | 2012-12-12 13:29:45 -0800 | [diff] [blame] | 41 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame^] | 42 | mMetadataPtr = nativeAllocate(); |
| 43 | if (mMetadataPtr == 0) { |
| 44 | throw new OutOfMemoryError("Failed to allocate native CameraMetadata"); |
| 45 | } |
Eino-Ville Talvala | b267554 | 2012-12-12 13:29:45 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | public static final Parcelable.Creator<CameraMetadata> CREATOR = |
| 49 | new Parcelable.Creator<CameraMetadata>() { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame^] | 50 | @Override |
Eino-Ville Talvala | b267554 | 2012-12-12 13:29:45 -0800 | [diff] [blame] | 51 | public CameraMetadata createFromParcel(Parcel in) { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame^] | 52 | CameraMetadata metadata = new CameraMetadata(); |
| 53 | metadata.readFromParcel(in); |
| 54 | return metadata; |
Eino-Ville Talvala | b267554 | 2012-12-12 13:29:45 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame^] | 57 | @Override |
Eino-Ville Talvala | b267554 | 2012-12-12 13:29:45 -0800 | [diff] [blame] | 58 | public CameraMetadata[] newArray(int size) { |
| 59 | return new CameraMetadata[size]; |
| 60 | } |
| 61 | }; |
| 62 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame^] | 63 | private static final String TAG = "CameraMetadataJV"; |
| 64 | |
Eino-Ville Talvala | b267554 | 2012-12-12 13:29:45 -0800 | [diff] [blame] | 65 | /** |
| 66 | * Set a camera metadata field to a value. The field definitions can be |
| 67 | * found in {@link CameraProperties}, {@link CaptureResult}, and |
| 68 | * {@link CaptureRequest}. |
| 69 | * |
| 70 | * @param key the metadata field to write. |
| 71 | * @param value the value to set the field to, which must be of a matching |
| 72 | * type to the key. |
| 73 | */ |
| 74 | public <T> void set(Key<T> key, T value) { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame^] | 75 | Log.e(TAG, "Not fully implemented yet"); |
| 76 | |
Eino-Ville Talvala | b267554 | 2012-12-12 13:29:45 -0800 | [diff] [blame] | 77 | mMetadataMap.put(key, value); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get a camera metadata field value. The field definitions can be |
| 82 | * found in {@link CameraProperties}, {@link CaptureResult}, and |
| 83 | * {@link CaptureRequest}. |
| 84 | * |
| 85 | * @param key the metadata field to read. |
| 86 | * @return the value of that key, or {@code null} if the field is not set. |
| 87 | */ |
| 88 | @SuppressWarnings("unchecked") |
| 89 | public <T> T get(Key<T> key) { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame^] | 90 | Log.e(TAG, "Not fully implemented yet"); |
| 91 | |
Eino-Ville Talvala | b267554 | 2012-12-12 13:29:45 -0800 | [diff] [blame] | 92 | return (T) mMetadataMap.get(key); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public int describeContents() { |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void writeToParcel(Parcel dest, int flags) { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame^] | 102 | nativeWriteToParcel(dest); |
| 103 | } |
Eino-Ville Talvala | b267554 | 2012-12-12 13:29:45 -0800 | [diff] [blame] | 104 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame^] | 105 | /** |
| 106 | * Expand this object from a Parcel. |
| 107 | * @param in The Parcel from which the object should be read |
| 108 | */ |
| 109 | public void readFromParcel(Parcel in) { |
| 110 | nativeReadFromParcel(in); |
Eino-Ville Talvala | b267554 | 2012-12-12 13:29:45 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | public static class Key<T> { |
| 114 | public Key(String name) { |
| 115 | if (name == null) { |
| 116 | throw new NullPointerException("Key needs a valid name"); |
| 117 | } |
| 118 | mName = name; |
| 119 | } |
| 120 | |
| 121 | public final String getName() { |
| 122 | return mName; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public final int hashCode() { |
| 127 | return mName.hashCode(); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | @SuppressWarnings("unchecked") |
| 132 | public final boolean equals(Object o) { |
| 133 | if (this == o) { |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | if (!(o instanceof Key)) { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | Key lhs = (Key) o; |
| 142 | |
| 143 | return mName.equals(lhs.mName); |
| 144 | } |
| 145 | |
| 146 | private final String mName; |
| 147 | } |
| 148 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame^] | 149 | private final Map<Key<?>, Object> mMetadataMap; |
| 150 | |
| 151 | /** |
| 152 | * @hide |
| 153 | */ |
| 154 | private long mMetadataPtr; // native CameraMetadata* |
| 155 | |
| 156 | private native long nativeAllocate(); |
| 157 | private native synchronized void nativeWriteToParcel(Parcel dest); |
| 158 | private native synchronized void nativeReadFromParcel(Parcel source); |
| 159 | private native synchronized void nativeSwap(CameraMetadata other) throws NullPointerException; |
| 160 | private native synchronized void nativeClose(); |
| 161 | private native synchronized boolean nativeIsEmpty(); |
| 162 | private native synchronized int nativeGetEntryCount(); |
| 163 | private static native void nativeClassInit(); |
| 164 | |
| 165 | /** |
| 166 | * <p>Perform a 0-copy swap of the internal metadata with another object.</p> |
| 167 | * |
| 168 | * <p>Useful to convert a CameraMetadata into e.g. a CaptureRequest.</p> |
| 169 | * |
| 170 | * @param other metadata to swap with |
| 171 | * @throws NullPointerException if other was null |
| 172 | * @hide |
| 173 | */ |
| 174 | public void swap(CameraMetadata other) { |
| 175 | nativeSwap(other); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * @hide |
| 180 | */ |
| 181 | public int getEntryCount() { |
| 182 | return nativeGetEntryCount(); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Does this metadata contain at least 1 entry? |
| 187 | * |
| 188 | * @hide |
| 189 | */ |
| 190 | public boolean isEmpty() { |
| 191 | return nativeIsEmpty(); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * <p>Closes this object, and releases all native resources associated with it.</p> |
| 196 | * |
| 197 | * <p>Calling any other public method after this will result in an IllegalStateException |
| 198 | * being thrown.</p> |
| 199 | */ |
| 200 | @Override |
| 201 | public void close() throws Exception { |
| 202 | // this sets mMetadataPtr to 0 |
| 203 | nativeClose(); |
| 204 | mMetadataPtr = 0; // set it to 0 again to prevent eclipse from making this field final |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Whether or not {@link #close} has already been called (at least once) on this object. |
| 209 | * @hide |
| 210 | */ |
| 211 | public boolean isClosed() { |
| 212 | synchronized (this) { |
| 213 | return mMetadataPtr == 0; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | protected void finalize() throws Throwable { |
| 219 | try { |
| 220 | close(); |
| 221 | } finally { |
| 222 | super.finalize(); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * We use a class initializer to allow the native code to cache some field offsets |
| 228 | */ |
| 229 | static { |
| 230 | System.loadLibrary("media_jni"); |
| 231 | nativeClassInit(); |
| 232 | } |
Eino-Ville Talvala | b267554 | 2012-12-12 13:29:45 -0800 | [diff] [blame] | 233 | } |