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