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.imageproc;
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.GLFrame;
25import android.filterfw.core.NativeProgram;
26import android.filterfw.core.NativeFrame;
27import android.filterfw.core.Program;
28import android.filterfw.core.ShaderProgram;
29import android.filterfw.geometry.Quad;
30import android.filterfw.format.ImageFormat;
31import android.filterfw.format.ObjectFormat;
32
33import android.opengl.GLES20;
34
35/**
36 * @hide
37 */
38public class DrawOverlayFilter extends Filter {
39
40    private ShaderProgram mProgram;
41
42    public DrawOverlayFilter(String name) {
43        super(name);
44    }
45
46    @Override
47    public void setupPorts() {
48        FrameFormat imageFormatMask = ImageFormat.create(ImageFormat.COLORSPACE_RGBA,
49                                                         FrameFormat.TARGET_GPU);
50        addMaskedInputPort("source", imageFormatMask);
51        addMaskedInputPort("overlay", imageFormatMask);
52        addMaskedInputPort("box", ObjectFormat.fromClass(Quad.class, FrameFormat.TARGET_SIMPLE));
53        addOutputBasedOnInput("image", "source");
54    }
55
56    @Override
57    public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) {
58        return inputFormat;
59    }
60
61    @Override
62    public void prepare(FilterContext context) {
63        mProgram = ShaderProgram.createIdentity(context);
64    }
65
66    @Override
67    public void process(FilterContext env) {
68        // Get input frame
69        Frame sourceFrame = pullInput("source");
70        Frame overlayFrame = pullInput("overlay");
71        Frame boxFrame = pullInput("box");
72
73        // Get the box
74        Quad box = (Quad)boxFrame.getObjectValue();
75        box = box.translated(1.0f, 1.0f).scaled(2.0f);
76
77        mProgram.setTargetRegion(box);
78
79        // Create output frame with copy of input
80        Frame output = env.getFrameManager().newFrame(sourceFrame.getFormat());
81        output.setDataFromFrame(sourceFrame);
82
83        // Draw onto output
84        mProgram.process(overlayFrame, output);
85
86        // Push output
87        pushOutput("image", output);
88
89        // Release pushed frame
90        output.release();
91    }
92}
93