1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2016 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
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
23
|
|
24
|
import android.graphics.Bitmap;
|
25
|
import android.graphics.Canvas;
|
26
|
import android.graphics.Paint;
|
27
|
import android.opengl.GLES30;
|
28
|
|
29
|
import org.distorted.library.effect.MatrixEffectMove;
|
30
|
import org.distorted.library.effect.MatrixEffectScale;
|
31
|
import org.distorted.library.mesh.MeshQuad;
|
32
|
import org.distorted.library.type.Static3D;
|
33
|
|
34
|
/**
|
35
|
* Class which represents the Screen.
|
36
|
* <p>
|
37
|
* User is able to render to it just like to a DistortedFramebuffer.
|
38
|
*/
|
39
|
public class DistortedScreen extends DistortedFramebuffer
|
40
|
{
|
41
|
///// DEBUGGING ONLY /////////////////////////
|
42
|
private static final int NUM_FRAMES = 80;
|
43
|
private static final float DEBUG_SCR_FRAC = 0.15f;
|
44
|
private static final float DEBUG_FRAC = 0.5f;
|
45
|
|
46
|
private static final int DEBUG_MODE_NONE = 0;
|
47
|
private static final int DEBUG_MODE_FPS = 1;
|
48
|
private static final int DEBUG_MODE_FRAME= 2;
|
49
|
|
50
|
private int mDebugMode;
|
51
|
private boolean mDebugAllocated;
|
52
|
|
53
|
private MeshQuad debugMesh;
|
54
|
private DistortedTexture debugTexture;
|
55
|
private DistortedEffects debugEffects;
|
56
|
private Canvas debugCanvas;
|
57
|
private Bitmap debugBitmap;
|
58
|
private Paint mPaint;
|
59
|
private String debugString;
|
60
|
private long lastTime=0;
|
61
|
private long[] durations;
|
62
|
private int currDuration;
|
63
|
private int frameNumber;
|
64
|
private static final Static3D mMoveVector = new Static3D(0,0,0);
|
65
|
private static final MatrixEffectMove mMoveEffect = new MatrixEffectMove(mMoveVector);
|
66
|
///// END DEBUGGING //////////////////////////
|
67
|
|
68
|
private int mQueueSize;
|
69
|
private int mCurRenderedFBO; // During the first FBO_QUEUE_SIZE frames, we blit the very first
|
70
|
private int mToBeBlittedFBO; // FBO one we have rendered. Then, we keep blitting the one we
|
71
|
private boolean mFirstCircle; // rendered FBO_QUEUE_SIZE ago.
|
72
|
|
73
|
private int mDebugWidth, mDebugHeight, mDebugGap, mDebugTextColor, mDebugBackColor;
|
74
|
|
75
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
76
|
// PUBLIC API
|
77
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78
|
/**
|
79
|
* Create a new Screen. Initially 1x1 in size.
|
80
|
* <p>
|
81
|
* Has to be followed by a 'resizeFBO()' to set the size.
|
82
|
*/
|
83
|
public DistortedScreen()
|
84
|
{
|
85
|
super(DistortedLibrary.WAIT_FOR_FBO_QUEUE_SIZE,1,BOTH_DEPTH_STENCIL, TYPE_SYST, STORAGE_PRIVATE,1,1);
|
86
|
mDebugMode = DEBUG_MODE_NONE;
|
87
|
mCurRenderedFBO = 0;
|
88
|
mToBeBlittedFBO = 0;
|
89
|
mFirstCircle = true;
|
90
|
mDebugAllocated = false;
|
91
|
mQueueSize = -1;
|
92
|
}
|
93
|
|
94
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
95
|
/**
|
96
|
* Draws all the attached children to this OutputSurface.
|
97
|
* <p>
|
98
|
* Must be called from a thread holding OpenGL Context.
|
99
|
*
|
100
|
* @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
|
101
|
* @return Number of objects rendered.
|
102
|
*/
|
103
|
public int render(long time)
|
104
|
{
|
105
|
if( mDebugMode!=DEBUG_MODE_NONE )
|
106
|
{
|
107
|
if( lastTime==0 ) lastTime = time;
|
108
|
|
109
|
if( mDebugMode==DEBUG_MODE_FPS )
|
110
|
{
|
111
|
currDuration++;
|
112
|
if (currDuration >= NUM_FRAMES) currDuration = 0;
|
113
|
durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
|
114
|
durations[currDuration] = time - lastTime;
|
115
|
|
116
|
debugString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
|
117
|
}
|
118
|
else if( mDebugMode==DEBUG_MODE_FRAME )
|
119
|
{
|
120
|
debugString = "" + frameNumber;
|
121
|
frameNumber++;
|
122
|
}
|
123
|
|
124
|
if( !mDebugAllocated )
|
125
|
{
|
126
|
mDebugAllocated = true;
|
127
|
debugString = "";
|
128
|
|
129
|
if( mDebugWidth<=0 || mDebugHeight<=0 )
|
130
|
{
|
131
|
mDebugWidth = (int)(mWidth*DEBUG_SCR_FRAC);
|
132
|
mDebugHeight= (int)(mWidth*DEBUG_SCR_FRAC*DEBUG_FRAC);
|
133
|
|
134
|
if( mDebugWidth<=0 || mDebugHeight<=0 )
|
135
|
{
|
136
|
int width = 100;
|
137
|
mDebugWidth = (int)(width*DEBUG_SCR_FRAC);
|
138
|
mDebugHeight= (int)(width*DEBUG_SCR_FRAC*DEBUG_FRAC);
|
139
|
}
|
140
|
}
|
141
|
|
142
|
debugBitmap = Bitmap.createBitmap( mDebugWidth, mDebugHeight, Bitmap.Config.ARGB_8888);
|
143
|
debugMesh = new MeshQuad();
|
144
|
debugTexture = new DistortedTexture();
|
145
|
debugTexture.setTexture(debugBitmap);
|
146
|
debugCanvas = new Canvas(debugBitmap);
|
147
|
debugEffects = new DistortedEffects();
|
148
|
debugEffects.apply( new MatrixEffectScale( new Static3D(mDebugWidth,mDebugHeight,1) ) );
|
149
|
debugEffects.apply(mMoveEffect);
|
150
|
|
151
|
mPaint = new Paint();
|
152
|
mPaint.setAntiAlias(true);
|
153
|
mPaint.setTextAlign(Paint.Align.CENTER);
|
154
|
mPaint.setTextSize(0.7f*mDebugHeight);
|
155
|
}
|
156
|
|
157
|
mPaint.setColor(mDebugBackColor);
|
158
|
debugCanvas.drawRect(0, 0, mDebugWidth, mDebugHeight, mPaint);
|
159
|
mPaint.setColor(mDebugTextColor);
|
160
|
debugCanvas.drawText(debugString, 0.5f*mDebugWidth, 0.75f*mDebugHeight, mPaint);
|
161
|
debugTexture.setTexture(debugBitmap);
|
162
|
|
163
|
mMoveVector.set( (-mWidth+mDebugWidth)*0.5f +mDebugGap, (mHeight-mDebugHeight)*0.5f -mDebugGap, 0);
|
164
|
|
165
|
lastTime = time;
|
166
|
}
|
167
|
|
168
|
int numrender = super.render(time,mCurRenderedFBO);
|
169
|
|
170
|
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
|
171
|
|
172
|
// workaround for the Mali issues: blit the framebuffer we have computed DistortedLibrary.FBO_QUEUE_SIZE
|
173
|
// frames ago. Looks like FBO_QUEUE_SIZE=2 solves the issue already but I decided to play it safe and
|
174
|
// make it equal to 3.
|
175
|
// This of course introduces a delay and uses more memory, but it does not appear to have any effect
|
176
|
// on speed. Maybe a slight positive effect if any!
|
177
|
setAsInput(mToBeBlittedFBO,0);
|
178
|
|
179
|
GLES30.glColorMask(true,true,true,true);
|
180
|
GLES30.glDepthMask(false);
|
181
|
GLES30.glDisable(GLES30.GL_STENCIL_TEST);
|
182
|
GLES30.glDisable(GLES30.GL_DEPTH_TEST);
|
183
|
GLES30.glDisable(GLES30.GL_BLEND);
|
184
|
|
185
|
DistortedLibrary.blitPriv(this);
|
186
|
|
187
|
if( mDebugMode!=DEBUG_MODE_NONE && debugTexture.setAsInput())
|
188
|
{
|
189
|
DistortedLibrary.drawPriv(debugEffects, debugMesh, this, time,0);
|
190
|
}
|
191
|
|
192
|
if( mQueueSize<=0 )
|
193
|
{
|
194
|
mQueueSize = DistortedLibrary.getQueueSize();
|
195
|
}
|
196
|
|
197
|
if( ++mCurRenderedFBO>=mQueueSize )
|
198
|
{
|
199
|
mCurRenderedFBO = 0;
|
200
|
if (mFirstCircle) mFirstCircle = false;
|
201
|
}
|
202
|
if( !mFirstCircle && ++mToBeBlittedFBO>=mQueueSize )
|
203
|
{
|
204
|
mToBeBlittedFBO=0;
|
205
|
}
|
206
|
/*
|
207
|
int err = GLES30.glGetError();
|
208
|
|
209
|
if( err!=GLES30.GL_NO_ERROR )
|
210
|
{
|
211
|
android.util.Log.e("D", "DISTORTED: OpenGL error "+err);
|
212
|
}
|
213
|
*/
|
214
|
return numrender+1;
|
215
|
}
|
216
|
|
217
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
218
|
/**
|
219
|
* Make the library show Frames Per Second in the upper-left corner.
|
220
|
* <p>
|
221
|
*/
|
222
|
public void showFPS()
|
223
|
{
|
224
|
int width = (int)(mWidth*DEBUG_SCR_FRAC);
|
225
|
int height = (int)(mWidth*DEBUG_SCR_FRAC*DEBUG_FRAC);
|
226
|
int gap = 5;
|
227
|
int textColor = 0xffffffff;
|
228
|
int backColor = 0xff000000;
|
229
|
|
230
|
showFPS(width,height,gap,textColor,backColor);
|
231
|
}
|
232
|
|
233
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
234
|
/**
|
235
|
* Make the library show Frames Per Second in the upper-left corner.
|
236
|
* Set appropriate params.
|
237
|
* <p>
|
238
|
*/
|
239
|
public void showFPS(int width, int height, int gap, int textColor, int backColor)
|
240
|
{
|
241
|
mDebugWidth = width;
|
242
|
mDebugHeight = height;
|
243
|
mDebugGap = gap;
|
244
|
mDebugBackColor = backColor;
|
245
|
mDebugTextColor = textColor;
|
246
|
|
247
|
if( mDebugMode!=DEBUG_MODE_FPS )
|
248
|
{
|
249
|
mDebugMode = DEBUG_MODE_FPS;
|
250
|
|
251
|
durations = new long[NUM_FRAMES + 1];
|
252
|
currDuration = 0;
|
253
|
|
254
|
for (int i=0; i<NUM_FRAMES+1; i++) durations[i] = 16; // Assume FPS will be
|
255
|
durations[NUM_FRAMES] = NUM_FRAMES * 16; // close to 1000/16 ~ 60
|
256
|
}
|
257
|
}
|
258
|
|
259
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
260
|
/**
|
261
|
* Make the library show current frame number in the upper-left corner.
|
262
|
* <p>
|
263
|
*/
|
264
|
public void showFrameNumber()
|
265
|
{
|
266
|
if( mDebugMode!=DEBUG_MODE_FRAME )
|
267
|
{
|
268
|
mDebugMode = DEBUG_MODE_FRAME;
|
269
|
frameNumber = 0;
|
270
|
}
|
271
|
}
|
272
|
}
|