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
18package android.filterpacks.base;
19
20import android.filterfw.core.Filter;
21import android.filterfw.core.FilterContext;
22import android.filterfw.core.Frame;
23import android.filterfw.core.FrameFormat;
24import android.filterfw.core.GenerateFieldPort;
25import android.filterfw.core.GLFrame;
26import android.filterfw.core.MutableFrameFormat;
27import android.filterfw.format.ImageFormat;
28
29import java.util.Set;
30
31/**
32 * @hide
33 */
34public class GLTextureSource extends Filter {
35
36    @GenerateFieldPort(name = "texId")
37    private int mTexId;
38
39    @GenerateFieldPort(name = "width")
40    private int mWidth;
41
42    @GenerateFieldPort(name = "height")
43    private int mHeight;
44
45    @GenerateFieldPort(name = "repeatFrame", hasDefault = true)
46    private boolean mRepeatFrame = false;
47
48    /* This timestamp will be used for all output frames from this source.  They
49     * represent nanoseconds, and should be positive and monotonically
50     * increasing.  Set to Frame.TIMESTAMP_UNKNOWN if timestamps are not
51     * meaningful for these textures.
52     */
53    @GenerateFieldPort(name = "timestamp", hasDefault = true)
54    private long mTimestamp = Frame.TIMESTAMP_UNKNOWN;
55
56    private Frame mFrame;
57
58    public GLTextureSource(String name) {
59        super(name);
60    }
61
62    @Override
63    public void setupPorts() {
64        addOutputPort("frame", ImageFormat.create(ImageFormat.COLORSPACE_RGBA,
65                                                  FrameFormat.TARGET_GPU));
66    }
67
68    @Override
69    public void fieldPortValueUpdated(String name, FilterContext context) {
70        // Release frame, so that it is recreated during the next process call
71        if (mFrame != null) {
72            mFrame.release();
73            mFrame = null;
74        }
75    }
76
77    @Override
78    public void process(FilterContext context) {
79        // Generate frame if not generated already
80        if (mFrame == null) {
81            FrameFormat outputFormat = ImageFormat.create(mWidth, mHeight,
82                                                          ImageFormat.COLORSPACE_RGBA,
83                                                          FrameFormat.TARGET_GPU);
84            mFrame = context.getFrameManager().newBoundFrame(outputFormat,
85                                                             GLFrame.EXISTING_TEXTURE_BINDING,
86                                                             mTexId);
87            mFrame.setTimestamp(mTimestamp);
88        }
89
90        // Push output
91        pushOutput("frame", mFrame);
92
93        if (!mRepeatFrame) {
94            // Close output port as we are done here
95            closeOutputPort("frame");
96        }
97    }
98
99    @Override
100    public void tearDown(FilterContext context) {
101        if (mFrame != null) {
102            mFrame.release();
103        }
104    }
105
106}
107