• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 
18 package android.filterfw.core;
19 
20 import android.annotation.UnsupportedAppUsage;
21 import android.filterfw.core.FrameFormat;
22 import android.filterfw.core.FrameManager;
23 import android.graphics.Bitmap;
24 
25 import java.nio.ByteBuffer;
26 
27 /**
28  * @hide
29  */
30 public abstract class Frame {
31 
32     public final static int NO_BINDING = 0;
33 
34     public final static long TIMESTAMP_NOT_SET = -2;
35     public final static long TIMESTAMP_UNKNOWN = -1;
36 
37     private FrameFormat mFormat;
38     private FrameManager mFrameManager;
39     private boolean mReadOnly = false;
40     private boolean mReusable = false;
41     private int mRefCount = 1;
42     private int mBindingType = NO_BINDING;
43     private long mBindingId = 0;
44     private long mTimestamp = TIMESTAMP_NOT_SET;
45 
Frame(FrameFormat format, FrameManager frameManager)46     Frame(FrameFormat format, FrameManager frameManager) {
47         mFormat = format.mutableCopy();
48         mFrameManager = frameManager;
49     }
50 
Frame(FrameFormat format, FrameManager frameManager, int bindingType, long bindingId)51     Frame(FrameFormat format, FrameManager frameManager, int bindingType, long bindingId) {
52         mFormat = format.mutableCopy();
53         mFrameManager = frameManager;
54         mBindingType = bindingType;
55         mBindingId = bindingId;
56     }
57 
58     @UnsupportedAppUsage
getFormat()59     public FrameFormat getFormat() {
60         return mFormat;
61     }
62 
getCapacity()63     public int getCapacity() {
64         return getFormat().getSize();
65     }
66 
isReadOnly()67     public boolean isReadOnly() {
68         return mReadOnly;
69     }
70 
getBindingType()71     public int getBindingType() {
72         return mBindingType;
73     }
74 
getBindingId()75     public long getBindingId() {
76         return mBindingId;
77     }
78 
setObjectValue(Object object)79     public void setObjectValue(Object object) {
80         assertFrameMutable();
81 
82         // Attempt to set the value using a specific setter (which may be more optimized), and
83         // fall back to the setGenericObjectValue(...) in case of no match.
84         if (object instanceof int[]) {
85             setInts((int[])object);
86         } else if (object instanceof float[]) {
87             setFloats((float[])object);
88         } else if (object instanceof ByteBuffer) {
89             setData((ByteBuffer)object);
90         } else if (object instanceof Bitmap) {
91             setBitmap((Bitmap)object);
92         } else {
93             setGenericObjectValue(object);
94         }
95     }
96 
getObjectValue()97     public abstract Object getObjectValue();
98 
99     @UnsupportedAppUsage
setInts(int[] ints)100     public abstract void setInts(int[] ints);
101 
getInts()102     public abstract int[] getInts();
103 
setFloats(float[] floats)104     public abstract void setFloats(float[] floats);
105 
getFloats()106     public abstract float[] getFloats();
107 
setData(ByteBuffer buffer, int offset, int length)108     public abstract void setData(ByteBuffer buffer, int offset, int length);
109 
setData(ByteBuffer buffer)110     public void setData(ByteBuffer buffer) {
111         setData(buffer, 0, buffer.limit());
112     }
113 
setData(byte[] bytes, int offset, int length)114     public void setData(byte[] bytes, int offset, int length) {
115         setData(ByteBuffer.wrap(bytes, offset, length));
116     }
117 
getData()118     public abstract ByteBuffer getData();
119 
setBitmap(Bitmap bitmap)120     public abstract void setBitmap(Bitmap bitmap);
121 
122     @UnsupportedAppUsage
getBitmap()123     public abstract Bitmap getBitmap();
124 
125     @UnsupportedAppUsage
setTimestamp(long timestamp)126     public void setTimestamp(long timestamp) {
127         mTimestamp = timestamp;
128     }
129 
130     @UnsupportedAppUsage
getTimestamp()131     public long getTimestamp() {
132         return mTimestamp;
133     }
134 
setDataFromFrame(Frame frame)135     public void setDataFromFrame(Frame frame) {
136         setData(frame.getData());
137     }
138 
requestResize(int[] newDimensions)139     protected boolean requestResize(int[] newDimensions) {
140         return false;
141     }
142 
getRefCount()143     public int getRefCount() {
144         return mRefCount;
145     }
146 
147     @UnsupportedAppUsage
release()148     public Frame release() {
149         if (mFrameManager != null) {
150             return mFrameManager.releaseFrame(this);
151         } else {
152             return this;
153         }
154     }
155 
retain()156     public Frame retain() {
157         if (mFrameManager != null) {
158             return mFrameManager.retainFrame(this);
159         } else {
160             return this;
161         }
162     }
163 
getFrameManager()164     public FrameManager getFrameManager() {
165         return mFrameManager;
166     }
167 
assertFrameMutable()168     protected void assertFrameMutable() {
169         if (isReadOnly()) {
170             throw new RuntimeException("Attempting to modify read-only frame!");
171         }
172     }
173 
setReusable(boolean reusable)174     protected void setReusable(boolean reusable) {
175         mReusable = reusable;
176     }
177 
setFormat(FrameFormat format)178     protected void setFormat(FrameFormat format) {
179         mFormat = format.mutableCopy();
180     }
181 
setGenericObjectValue(Object value)182     protected void setGenericObjectValue(Object value) {
183         throw new RuntimeException(
184             "Cannot set object value of unsupported type: " + value.getClass());
185     }
186 
convertBitmapToRGBA(Bitmap bitmap)187     protected static Bitmap convertBitmapToRGBA(Bitmap bitmap) {
188         if (bitmap.getConfig() == Bitmap.Config.ARGB_8888) {
189             return bitmap;
190         } else {
191             Bitmap result = bitmap.copy(Bitmap.Config.ARGB_8888, false);
192             if (result == null) {
193                 throw new RuntimeException("Error converting bitmap to RGBA!");
194             } else if (result.getRowBytes() != result.getWidth() * 4) {
195                 throw new RuntimeException("Unsupported row byte count in bitmap!");
196             }
197             return result;
198         }
199     }
200 
reset(FrameFormat newFormat)201     protected void reset(FrameFormat newFormat) {
202         mFormat = newFormat.mutableCopy();
203         mReadOnly = false;
204         mRefCount = 1;
205     }
206 
207     /**
208      * Called just before a frame is stored, such as when storing to a cache or context.
209      */
onFrameStore()210     protected void onFrameStore() {
211     }
212 
213     /**
214      * Called when a frame is fetched from an internal store such as a cache.
215      */
onFrameFetch()216     protected void onFrameFetch() {
217     }
218 
219     // Core internal methods ///////////////////////////////////////////////////////////////////////
hasNativeAllocation()220     protected abstract boolean hasNativeAllocation();
221 
releaseNativeAllocation()222     protected abstract void releaseNativeAllocation();
223 
incRefCount()224     final int incRefCount() {
225         ++mRefCount;
226         return mRefCount;
227     }
228 
decRefCount()229     final int decRefCount() {
230         --mRefCount;
231         return mRefCount;
232     }
233 
isReusable()234     final boolean isReusable() {
235         return mReusable;
236     }
237 
markReadOnly()238     final void markReadOnly() {
239         mReadOnly = true;
240     }
241 
242 }
243