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.filterfw.core;
19
20import android.filterfw.core.Frame;
21import android.filterfw.core.FrameFormat;
22import android.filterfw.core.FrameManager;
23import android.filterfw.core.GLFrame;
24import android.filterfw.core.NativeFrame;
25import android.filterfw.core.SimpleFrame;
26import android.filterfw.core.VertexFrame;
27
28/**
29 * @hide
30 */
31public class SimpleFrameManager extends FrameManager {
32
33    public SimpleFrameManager() {
34    }
35
36    @Override
37    public Frame newFrame(FrameFormat format) {
38        return createNewFrame(format);
39    }
40
41    @Override
42    public Frame newBoundFrame(FrameFormat format, int bindingType, long bindingId) {
43        Frame result = null;
44        switch(format.getTarget()) {
45            case FrameFormat.TARGET_GPU: {
46                GLFrame glFrame = new GLFrame(format, this, bindingType, bindingId);
47                glFrame.init(getGLEnvironment());
48                result = glFrame;
49                break;
50            }
51
52            default:
53                throw new RuntimeException("Attached frames are not supported for target type: "
54                    + FrameFormat.targetToString(format.getTarget()) + "!");
55        }
56        return result;
57    }
58
59    private Frame createNewFrame(FrameFormat format) {
60        Frame result = null;
61        switch(format.getTarget()) {
62            case FrameFormat.TARGET_SIMPLE:
63                result = new SimpleFrame(format, this);
64                break;
65
66            case FrameFormat.TARGET_NATIVE:
67                result = new NativeFrame(format, this);
68                break;
69
70            case FrameFormat.TARGET_GPU: {
71                GLFrame glFrame = new GLFrame(format, this);
72                glFrame.init(getGLEnvironment());
73                result = glFrame;
74                break;
75            }
76
77            case FrameFormat.TARGET_VERTEXBUFFER: {
78                result = new VertexFrame(format, this);
79                break;
80            }
81
82            default:
83                throw new RuntimeException("Unsupported frame target type: " +
84                                           FrameFormat.targetToString(format.getTarget()) + "!");
85        }
86        return result;
87    }
88
89    @Override
90    public Frame retainFrame(Frame frame) {
91        frame.incRefCount();
92        return frame;
93    }
94
95    @Override
96    public Frame releaseFrame(Frame frame) {
97        int refCount = frame.decRefCount();
98        if (refCount == 0 && frame.hasNativeAllocation()) {
99            frame.releaseNativeAllocation();
100            return null;
101        } else if (refCount < 0) {
102            throw new RuntimeException("Frame reference count dropped below 0!");
103        }
104        return frame;
105    }
106
107}
108