Project

General

Profile

Download (9.59 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / main / DistortedScreen.java @ b48a9939

1 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
2 9b94626c Leszek Koltunski
// Copyright 2016 Leszek Koltunski                                                               //
3 c5369f1b leszek
//                                                                                               //
4 46b572b5 Leszek Koltunski
// This file is part of Distorted.                                                               //
5 c5369f1b leszek
//                                                                                               //
6 46b572b5 Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 c5369f1b leszek
// 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 46b572b5 Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 c5369f1b leszek
// 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 46b572b5 Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 fe82a979 Leszek Koltunski
package org.distorted.library.main;
21 c5369f1b leszek
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23 806ca386 leszek
24 86e99907 leszek
import android.graphics.Bitmap;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27 b7074bc6 Leszek Koltunski
import android.opengl.GLES30;
28 806ca386 leszek
29 81b1577b Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
30 1b059065 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectScale;
31 e1e94682 Leszek Koltunski
import org.distorted.library.mesh.MeshQuad;
32 86e99907 leszek
import org.distorted.library.type.Static3D;
33
34 c5369f1b leszek
/**
35
 * Class which represents the Screen.
36
 * <p>
37
 * User is able to render to it just like to a DistortedFramebuffer.
38
 */
39 040cd18c Leszek Koltunski
public class DistortedScreen extends DistortedFramebuffer
40 c5369f1b leszek
  {
41 86e99907 leszek
  ///// DEBUGGING ONLY /////////////////////////
42
  private static final int NUM_FRAMES  = 100;
43 aa6e92f2 Leszek Koltunski
  private static final float DEBUG_SCR_FRAC = 0.15f;
44
  private static final float DEBUG_FRAC     = 0.5f;
45 4aa38649 Leszek Koltunski
46 b8f8ef5c Leszek Koltunski
  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 86e99907 leszek
50 b8f8ef5c Leszek Koltunski
  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 86e99907 leszek
  private Paint mPaint;
59 b8f8ef5c Leszek Koltunski
  private String debugString;
60 86e99907 leszek
  private long lastTime=0;
61
  private long[] durations;
62
  private int currDuration;
63 b8f8ef5c Leszek Koltunski
  private int frameNumber;
64 b6947445 Leszek Koltunski
  private static final Static3D mMoveVector = new Static3D(0,0,0);
65
  private static final MatrixEffectMove mMoveEffect = new MatrixEffectMove(mMoveVector);
66 040cd18c Leszek Koltunski
  ///// END DEBUGGING //////////////////////////
67 86e99907 leszek
68 5f35f1cb Leszek Koltunski
  private int mQueueSize;
69 9b94626c Leszek Koltunski
  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 9d845904 Leszek Koltunski
73 c840136b Leszek Koltunski
  private int mDebugWidth, mDebugHeight, mDebugGap, mDebugTextColor, mDebugBackColor;
74
75 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
76 040cd18c Leszek Koltunski
// PUBLIC API
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78
/**
79
 * Create a new Screen. Initially 1x1 in size.
80
 * <p>
81 9ecac8cd Leszek Koltunski
 * Has to be followed by a 'resizeFBO()' to set the size.
82 040cd18c Leszek Koltunski
 */
83
  public DistortedScreen()
84
    {
85 9ec374e8 Leszek Koltunski
    super(DistortedLibrary.WAIT_FOR_FBO_QUEUE_SIZE,1,BOTH_DEPTH_STENCIL, TYPE_SYST, STORAGE_PRIVATE,1,1);
86 b8f8ef5c Leszek Koltunski
    mDebugMode = DEBUG_MODE_NONE;
87 9b94626c Leszek Koltunski
    mCurRenderedFBO = 0;
88
    mToBeBlittedFBO = 0;
89
    mFirstCircle = true;
90 b8f8ef5c Leszek Koltunski
    mDebugAllocated = false;
91 5f35f1cb Leszek Koltunski
    mQueueSize = -1;
92 040cd18c Leszek Koltunski
    }
93 c5369f1b leszek
94 26a4e5f6 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
95 040cd18c Leszek Koltunski
/**
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 b8f8ef5c Leszek Koltunski
    if( mDebugMode!=DEBUG_MODE_NONE )
106 040cd18c Leszek Koltunski
      {
107
      if( lastTime==0 ) lastTime = time;
108 26a4e5f6 leszek
109 b8f8ef5c Leszek Koltunski
      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 040cd18c Leszek Koltunski
124 aa6e92f2 Leszek Koltunski
      if( !mDebugAllocated )
125 c840136b Leszek Koltunski
        {
126
        mDebugAllocated = true;
127
        debugString = "";
128
129 b0ac5b29 Leszek Koltunski
        if( mDebugWidth<=0 || mDebugHeight<=0 )
130
          {
131
          mDebugWidth = (int)(mWidth*DEBUG_SCR_FRAC);
132
          mDebugHeight= (int)(mWidth*DEBUG_SCR_FRAC*DEBUG_FRAC);
133
          }
134
135 c840136b Leszek Koltunski
        debugBitmap = Bitmap.createBitmap( mDebugWidth, mDebugHeight, Bitmap.Config.ARGB_8888);
136
        debugMesh = new MeshQuad();
137
        debugTexture = new DistortedTexture();
138
        debugTexture.setTexture(debugBitmap);
139
        debugCanvas = new Canvas(debugBitmap);
140
        debugEffects = new DistortedEffects();
141
        debugEffects.apply( new MatrixEffectScale( new Static3D(mDebugWidth,mDebugHeight,1) ) );
142
        debugEffects.apply(mMoveEffect);
143
144
        mPaint = new Paint();
145
        mPaint.setAntiAlias(true);
146
        mPaint.setTextAlign(Paint.Align.CENTER);
147
        mPaint.setTextSize(0.7f*mDebugHeight);
148
        }
149
150
      mPaint.setColor(mDebugBackColor);
151
      debugCanvas.drawRect(0, 0, mDebugWidth, mDebugHeight, mPaint);
152
      mPaint.setColor(mDebugTextColor);
153
      debugCanvas.drawText(debugString, 0.5f*mDebugWidth, 0.75f*mDebugHeight, mPaint);
154 b8f8ef5c Leszek Koltunski
      debugTexture.setTexture(debugBitmap);
155 040cd18c Leszek Koltunski
156 c840136b Leszek Koltunski
      mMoveVector.set( (-mWidth+mDebugWidth)*0.5f +mDebugGap, (mHeight-mDebugHeight)*0.5f -mDebugGap, 0);
157 7bebb196 Leszek Koltunski
158 040cd18c Leszek Koltunski
      lastTime = time;
159
      }
160
161 9b94626c Leszek Koltunski
    int numrender = super.render(time,mCurRenderedFBO);
162 040cd18c Leszek Koltunski
163 b7074bc6 Leszek Koltunski
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
164 2301cb2f Leszek Koltunski
165 7602a827 Leszek Koltunski
    // workaround for the Mali issues: blit the framebuffer we have computed DistortedLibrary.FBO_QUEUE_SIZE
166 2301cb2f Leszek Koltunski
    // frames ago. Looks like FBO_QUEUE_SIZE=2 solves the issue already but I decided to play it safe and
167
    // make it equal to 3.
168
    // This of course introduces a delay and uses more memory, but it does not appear to have any effect
169
    // on speed. Maybe a slight positive effect if any!
170 9b94626c Leszek Koltunski
    setAsInput(mToBeBlittedFBO,0);
171 2301cb2f Leszek Koltunski
172 b7074bc6 Leszek Koltunski
    GLES30.glColorMask(true,true,true,true);
173
    GLES30.glDepthMask(false);
174
    GLES30.glDisable(GLES30.GL_STENCIL_TEST);
175
    GLES30.glDisable(GLES30.GL_DEPTH_TEST);
176
    GLES30.glDisable(GLES30.GL_BLEND);
177 040cd18c Leszek Koltunski
178 7602a827 Leszek Koltunski
    DistortedLibrary.blitPriv(this);
179 040cd18c Leszek Koltunski
180 b8f8ef5c Leszek Koltunski
    if( mDebugMode!=DEBUG_MODE_NONE && debugTexture.setAsInput())
181 040cd18c Leszek Koltunski
      {
182 835b197e Leszek Koltunski
      DistortedLibrary.drawPriv(debugEffects, debugMesh, this, time,0);
183 040cd18c Leszek Koltunski
      }
184
185 5f35f1cb Leszek Koltunski
    if( mQueueSize<=0 )
186
      {
187
      mQueueSize = DistortedLibrary.getQueueSize();
188
      }
189
190
    if( ++mCurRenderedFBO>=mQueueSize )
191 9b94626c Leszek Koltunski
      {
192
      mCurRenderedFBO = 0;
193
      if (mFirstCircle) mFirstCircle = false;
194
      }
195 5f35f1cb Leszek Koltunski
    if( !mFirstCircle && ++mToBeBlittedFBO>=mQueueSize )
196 9b94626c Leszek Koltunski
      {
197
      mToBeBlittedFBO=0;
198
      }
199 b48a9939 Leszek Koltunski
/*
200 fca614bf Leszek Koltunski
    int err = GLES30.glGetError();
201
202
    if( err!=GLES30.GL_NO_ERROR )
203
      {
204
      android.util.Log.e("D", "DISTORTED: OpenGL error "+err);
205
      }
206 b48a9939 Leszek Koltunski
*/
207 040cd18c Leszek Koltunski
    return numrender+1;
208
    }
209
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211
/**
212
 * Make the library show Frames Per Second in the upper-left corner.
213
 * <p>
214
 */
215
  public void showFPS()
216 26a4e5f6 leszek
    {
217 c840136b Leszek Koltunski
    int width     = (int)(mWidth*DEBUG_SCR_FRAC);
218
    int height    = (int)(mWidth*DEBUG_SCR_FRAC*DEBUG_FRAC);
219
    int gap       = 5;
220
    int textColor = 0xffffffff;
221
    int backColor = 0xff000000;
222
223
    showFPS(width,height,gap,textColor,backColor);
224
    }
225
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227
/**
228
 * Make the library show Frames Per Second in the upper-left corner.
229
 * Set appropriate params.
230
 * <p>
231
 */
232
  public void showFPS(int width, int height, int gap, int textColor, int backColor)
233
    {
234
    mDebugWidth     = width;
235
    mDebugHeight    = height;
236
    mDebugGap       = gap;
237
    mDebugBackColor = backColor;
238
    mDebugTextColor = textColor;
239
240 b8f8ef5c Leszek Koltunski
    if( mDebugMode!=DEBUG_MODE_FPS )
241 01b9a241 Leszek Koltunski
      {
242 b8f8ef5c Leszek Koltunski
      mDebugMode = DEBUG_MODE_FPS;
243 01b9a241 Leszek Koltunski
244
      durations = new long[NUM_FRAMES + 1];
245
      currDuration = 0;
246
247 7bebb196 Leszek Koltunski
      for (int i=0; i<NUM_FRAMES+1; i++) durations[i] = 16;  // Assume FPS will be
248
      durations[NUM_FRAMES] = NUM_FRAMES * 16;               // close to 1000/16 ~ 60
249 01b9a241 Leszek Koltunski
      }
250 c5369f1b leszek
    }
251 b8f8ef5c Leszek Koltunski
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253
/**
254
 * Make the library show current frame number in the upper-left corner.
255
 * <p>
256
 */
257
  public void showFrameNumber()
258
    {
259
    if( mDebugMode!=DEBUG_MODE_FRAME )
260
      {
261
      mDebugMode = DEBUG_MODE_FRAME;
262
      frameNumber = 0;
263
      }
264
    }
265 9b94626c Leszek Koltunski
  }