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