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 = 100;
|
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 Static3D mMoveVector = new Static3D(0,0,0);
|
65
|
private static 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
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
74
|
// PUBLIC API
|
75
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
76
|
/**
|
77
|
* Create a new Screen. Initially 1x1 in size.
|
78
|
* <p>
|
79
|
* Has to be followed by a 'resizeFBO()' to set the size.
|
80
|
*/
|
81
|
public DistortedScreen()
|
82
|
{
|
83
|
super(DistortedLibrary.WAIT_FOR_FBO_QUEUE_SIZE,1,BOTH_DEPTH_STENCIL, TYPE_SYST, STORAGE_PRIVATE,1,1);
|
84
|
mDebugMode = DEBUG_MODE_NONE;
|
85
|
mCurRenderedFBO = 0;
|
86
|
mToBeBlittedFBO = 0;
|
87
|
mFirstCircle = true;
|
88
|
mDebugAllocated = false;
|
89
|
mQueueSize = -1;
|
90
|
}
|
91
|
|
92
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
93
|
/**
|
94
|
* Draws all the attached children to this OutputSurface.
|
95
|
* <p>
|
96
|
* Must be called from a thread holding OpenGL Context.
|
97
|
*
|
98
|
* @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
|
99
|
* @return Number of objects rendered.
|
100
|
*/
|
101
|
public int render(long time)
|
102
|
{
|
103
|
if( mDebugMode!=DEBUG_MODE_NONE )
|
104
|
{
|
105
|
if( lastTime==0 ) lastTime = time;
|
106
|
|
107
|
if( mDebugMode==DEBUG_MODE_FPS )
|
108
|
{
|
109
|
currDuration++;
|
110
|
if (currDuration >= NUM_FRAMES) currDuration = 0;
|
111
|
durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
|
112
|
durations[currDuration] = time - lastTime;
|
113
|
|
114
|
debugString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
|
115
|
}
|
116
|
else if( mDebugMode==DEBUG_MODE_FRAME )
|
117
|
{
|
118
|
debugString = "" + frameNumber;
|
119
|
frameNumber++;
|
120
|
}
|
121
|
|
122
|
int width = (int)(mWidth*DEBUG_SCR_FRAC);
|
123
|
int height= (int)(mWidth*DEBUG_SCR_FRAC*DEBUG_FRAC);
|
124
|
|
125
|
if( !mDebugAllocated )
|
126
|
{
|
127
|
mDebugAllocated = true;
|
128
|
debugString = "";
|
129
|
|
130
|
debugBitmap = Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888);
|
131
|
debugMesh = new MeshQuad();
|
132
|
debugTexture = new DistortedTexture();
|
133
|
debugTexture.setTexture(debugBitmap);
|
134
|
debugCanvas = new Canvas(debugBitmap);
|
135
|
debugEffects = new DistortedEffects();
|
136
|
debugEffects.apply( new MatrixEffectScale( new Static3D(width,height,1) ) );
|
137
|
debugEffects.apply(mMoveEffect);
|
138
|
|
139
|
mPaint = new Paint();
|
140
|
mPaint.setAntiAlias(true);
|
141
|
mPaint.setTextAlign(Paint.Align.CENTER);
|
142
|
mPaint.setTextSize(0.7f*height);
|
143
|
}
|
144
|
|
145
|
mPaint.setColor(0xffffffff);
|
146
|
debugCanvas.drawRect(0, 0, width, height, mPaint);
|
147
|
mPaint.setColor(0xff000000);
|
148
|
debugCanvas.drawText(debugString, 0.5f*width, 0.75f*height, mPaint);
|
149
|
debugTexture.setTexture(debugBitmap);
|
150
|
|
151
|
mMoveVector.set( (-mWidth+width)*0.5f +5, (mHeight-height)*0.5f -5, 0);
|
152
|
|
153
|
lastTime = time;
|
154
|
}
|
155
|
|
156
|
int numrender = super.render(time,mCurRenderedFBO);
|
157
|
|
158
|
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
|
159
|
|
160
|
// workaround for the Mali issues: blit the framebuffer we have computed DistortedLibrary.FBO_QUEUE_SIZE
|
161
|
// frames ago. Looks like FBO_QUEUE_SIZE=2 solves the issue already but I decided to play it safe and
|
162
|
// make it equal to 3.
|
163
|
// This of course introduces a delay and uses more memory, but it does not appear to have any effect
|
164
|
// on speed. Maybe a slight positive effect if any!
|
165
|
setAsInput(mToBeBlittedFBO,0);
|
166
|
|
167
|
GLES30.glColorMask(true,true,true,true);
|
168
|
GLES30.glDepthMask(false);
|
169
|
GLES30.glDisable(GLES30.GL_STENCIL_TEST);
|
170
|
GLES30.glDisable(GLES30.GL_DEPTH_TEST);
|
171
|
GLES30.glDisable(GLES30.GL_BLEND);
|
172
|
|
173
|
DistortedLibrary.blitPriv(this);
|
174
|
|
175
|
if( mDebugMode!=DEBUG_MODE_NONE && debugTexture.setAsInput())
|
176
|
{
|
177
|
DistortedLibrary.drawPriv(debugEffects, debugMesh, this, time);
|
178
|
}
|
179
|
|
180
|
if( mQueueSize<=0 )
|
181
|
{
|
182
|
mQueueSize = DistortedLibrary.getQueueSize();
|
183
|
}
|
184
|
|
185
|
if( ++mCurRenderedFBO>=mQueueSize )
|
186
|
{
|
187
|
mCurRenderedFBO = 0;
|
188
|
if (mFirstCircle) mFirstCircle = false;
|
189
|
}
|
190
|
if( !mFirstCircle && ++mToBeBlittedFBO>=mQueueSize )
|
191
|
{
|
192
|
mToBeBlittedFBO=0;
|
193
|
}
|
194
|
|
195
|
return numrender+1;
|
196
|
}
|
197
|
|
198
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
199
|
/**
|
200
|
* Make the library show Frames Per Second in the upper-left corner.
|
201
|
* <p>
|
202
|
*/
|
203
|
public void showFPS()
|
204
|
{
|
205
|
if( mDebugMode!=DEBUG_MODE_FPS )
|
206
|
{
|
207
|
mDebugMode = DEBUG_MODE_FPS;
|
208
|
|
209
|
durations = new long[NUM_FRAMES + 1];
|
210
|
currDuration = 0;
|
211
|
|
212
|
for (int i=0; i<NUM_FRAMES+1; i++) durations[i] = 16; // Assume FPS will be
|
213
|
durations[NUM_FRAMES] = NUM_FRAMES * 16; // close to 1000/16 ~ 60
|
214
|
}
|
215
|
}
|
216
|
|
217
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
218
|
/**
|
219
|
* Make the library show current frame number in the upper-left corner.
|
220
|
* <p>
|
221
|
*/
|
222
|
public void showFrameNumber()
|
223
|
{
|
224
|
if( mDebugMode!=DEBUG_MODE_FRAME )
|
225
|
{
|
226
|
mDebugMode = DEBUG_MODE_FRAME;
|
227
|
frameNumber = 0;
|
228
|
}
|
229
|
}
|
230
|
}
|