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