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.graphics.Bitmap;
24
25import java.nio.ByteBuffer;
26
27/**
28 * @hide
29 */
30public class VertexFrame extends Frame {
31
32    private int vertexFrameId = -1;
33
34    VertexFrame(FrameFormat format, FrameManager frameManager) {
35        super(format, frameManager);
36        if (getFormat().getSize() <= 0) {
37            throw new IllegalArgumentException("Initializing vertex frame with zero size!");
38        } else {
39            if (!nativeAllocate(getFormat().getSize())) {
40                throw new RuntimeException("Could not allocate vertex frame!");
41            }
42        }
43    }
44
45    @Override
46    protected synchronized boolean hasNativeAllocation() {
47        return vertexFrameId != -1;
48    }
49
50    @Override
51    protected synchronized void releaseNativeAllocation() {
52        nativeDeallocate();
53        vertexFrameId = -1;
54    }
55
56    @Override
57    public Object getObjectValue() {
58        throw new RuntimeException("Vertex frames do not support reading data!");
59    }
60
61    @Override
62    public void setInts(int[] ints) {
63        assertFrameMutable();
64        if (!setNativeInts(ints)) {
65            throw new RuntimeException("Could not set int values for vertex frame!");
66        }
67    }
68
69    @Override
70    public int[] getInts() {
71        throw new RuntimeException("Vertex frames do not support reading data!");
72    }
73
74    @Override
75    public void setFloats(float[] floats) {
76        assertFrameMutable();
77        if (!setNativeFloats(floats)) {
78            throw new RuntimeException("Could not set int values for vertex frame!");
79        }
80    }
81
82    @Override
83    public float[] getFloats() {
84        throw new RuntimeException("Vertex frames do not support reading data!");
85    }
86
87    @Override
88    public void setData(ByteBuffer buffer, int offset, int length) {
89        assertFrameMutable();
90        byte[] bytes = buffer.array();
91        if (getFormat().getSize() != bytes.length) {
92            throw new RuntimeException("Data size in setData does not match vertex frame size!");
93        } else if (!setNativeData(bytes, offset, length)) {
94            throw new RuntimeException("Could not set vertex frame data!");
95        }
96    }
97
98    @Override
99    public ByteBuffer getData() {
100        throw new RuntimeException("Vertex frames do not support reading data!");
101    }
102
103    @Override
104    public void setBitmap(Bitmap bitmap) {
105        throw new RuntimeException("Unsupported: Cannot set vertex frame bitmap value!");
106    }
107
108    @Override
109    public Bitmap getBitmap() {
110        throw new RuntimeException("Vertex frames do not support reading data!");
111    }
112
113    @Override
114    public void setDataFromFrame(Frame frame) {
115        // TODO: Optimize
116        super.setDataFromFrame(frame);
117    }
118
119    public int getVboId() {
120        return getNativeVboId();
121    }
122
123    @Override
124    public String toString() {
125        return "VertexFrame (" + getFormat() + ") with VBO ID " + getVboId();
126    }
127
128    static {
129        System.loadLibrary("filterfw");
130    }
131
132    private native boolean nativeAllocate(int size);
133
134    private native boolean nativeDeallocate();
135
136    private native boolean setNativeData(byte[] data, int offset, int length);
137
138    private native boolean setNativeInts(int[] ints);
139
140    private native boolean setNativeFloats(float[] floats);
141
142    private native int getNativeVboId();
143}
144