1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2018 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Distorted is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.library.main;
|
21
|
|
22
|
import android.opengl.GLES31;
|
23
|
import java.nio.Buffer;
|
24
|
|
25
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
26
|
/**
|
27
|
* Implements OpenGL buffer object such as GL_ARRAY_BUFFER or GL_TRANSFORM_FEEDBACK_BUFFER.
|
28
|
* Main point: deal with Android lifecycle and recreate the buffer on loss of OpenGL context.
|
29
|
* <p>
|
30
|
* Not part of public API, do not document (public only because has to be used in Meshes)
|
31
|
*
|
32
|
* @y.exclude
|
33
|
*/
|
34
|
public class InternalBuffer extends InternalObject
|
35
|
{
|
36
|
public int[] mIndex;
|
37
|
|
38
|
private int mTarget, mSize, mUsage;
|
39
|
private Buffer mBuffer;
|
40
|
|
41
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
42
|
|
43
|
public InternalBuffer(int target, int usage)
|
44
|
{
|
45
|
super(InternalObject.TYPE_USER);
|
46
|
|
47
|
mIndex = new int[1];
|
48
|
mTarget = target;
|
49
|
mUsage = usage;
|
50
|
mBuffer = null;
|
51
|
mSize = 0;
|
52
|
|
53
|
recreate();
|
54
|
}
|
55
|
|
56
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
57
|
// mark for creation only now, when we do have all data ready, in order to avoid the situation
|
58
|
// when create() would be called before this.
|
59
|
|
60
|
public void setData(int size, Buffer buffer)
|
61
|
{
|
62
|
mSize = size;
|
63
|
mBuffer = buffer;
|
64
|
|
65
|
markForCreation();
|
66
|
}
|
67
|
|
68
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
69
|
// must be called from a thread holding OpenGL Context
|
70
|
|
71
|
void create()
|
72
|
{
|
73
|
if( mIndex[0]<0 )
|
74
|
{
|
75
|
GLES31.glGenBuffers( 1, mIndex, 0);
|
76
|
GLES31.glBindBuffer( mTarget, mIndex[0]);
|
77
|
GLES31.glBufferData( mTarget, mSize, mBuffer, mUsage);
|
78
|
GLES31.glBindBuffer( mTarget, 0);
|
79
|
}
|
80
|
}
|
81
|
|
82
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
83
|
// must be called from a thread holding OpenGL Context
|
84
|
|
85
|
void delete()
|
86
|
{
|
87
|
if( mIndex[0]>=0 )
|
88
|
{
|
89
|
GLES31.glDeleteBuffers(1, mIndex, 0);
|
90
|
mIndex[0] = -1;
|
91
|
}
|
92
|
}
|
93
|
|
94
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
95
|
|
96
|
void recreate()
|
97
|
{
|
98
|
mIndex[0] = -1;
|
99
|
}
|
100
|
|
101
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
102
|
// debugging only
|
103
|
|
104
|
String printDetails()
|
105
|
{
|
106
|
return getClass().getSimpleName();
|
107
|
}
|
108
|
}
|