Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedScreen.java @ 7a5e538a

1 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
2 9b94626c Leszek Koltunski
// Copyright 2016 Leszek Koltunski                                                               //
3 c5369f1b leszek
//                                                                                               //
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 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 e6519ac8 Leszek Koltunski
import android.opengl.GLES31;
28 806ca386 leszek
29 81b1577b Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
30 715e7726 Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
31 6c00149d Leszek Koltunski
import org.distorted.library.mesh.MeshFlat;
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 040cd18c Leszek Koltunski
  private boolean mShowFPS;
43
44 86e99907 leszek
  private static final int NUM_FRAMES  = 100;
45
46 715e7726 Leszek Koltunski
  private MeshBase fpsMesh;
47 86e99907 leszek
  private DistortedTexture fpsTexture;
48
  private DistortedEffects fpsEffects;
49
  private Canvas fpsCanvas;
50
  private Bitmap fpsBitmap;
51
  private Paint mPaint;
52
  private int fpsH, fpsW;
53 040cd18c Leszek Koltunski
  private String fpsString;
54 86e99907 leszek
  private long lastTime=0;
55
  private long[] durations;
56
  private int currDuration;
57 81b1577b Leszek Koltunski
  private static MatrixEffectMove mMoveEffect = new MatrixEffectMove( new Static3D(5,5,0) );
58 040cd18c Leszek Koltunski
  ///// END DEBUGGING //////////////////////////
59 86e99907 leszek
60 9b94626c Leszek Koltunski
  private int mCurRenderedFBO;    // During the first FBO_QUEUE_SIZE frames, we blit the very first
61
  private int mToBeBlittedFBO;    // FBO one we have rendered. Then, we keep blitting the one we
62
  private boolean mFirstCircle;   // rendered FBO_QUEUE_SIZE ago.
63 9d845904 Leszek Koltunski
64 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
65 040cd18c Leszek Koltunski
// PUBLIC API
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
/**
68
 * Create a new Screen. Initially 1x1 in size.
69
 * <p>
70
 * Has to be followed by a 'resize()' to set the size.
71
 */
72
  public DistortedScreen()
73
    {
74 f4d6114c Leszek Koltunski
    super(Distorted.FBO_QUEUE_SIZE,1,BOTH_DEPTH_STENCIL, TYPE_SYST, 1,1);
75 040cd18c Leszek Koltunski
    mShowFPS = false;
76 9b94626c Leszek Koltunski
    mCurRenderedFBO = 0;
77
    mToBeBlittedFBO = 0;
78
    mFirstCircle = true;
79 040cd18c Leszek Koltunski
    }
80 c5369f1b leszek
81 26a4e5f6 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
82 040cd18c Leszek Koltunski
/**
83
 * Draws all the attached children to this OutputSurface.
84
 * <p>
85
 * Must be called from a thread holding OpenGL Context.
86
 *
87
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
88
 * @return Number of objects rendered.
89
 */
90
  public int render(long time)
91
    {
92
    if( mShowFPS )
93
      {
94
      if( lastTime==0 ) lastTime = time;
95 26a4e5f6 leszek
96 040cd18c Leszek Koltunski
      currDuration++;
97
      if (currDuration >= NUM_FRAMES) currDuration = 0;
98
      durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
99
      durations[currDuration] = time - lastTime;
100
101
      fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
102
103
      mPaint.setColor(0xffffffff);
104
      fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
105
      mPaint.setColor(0xff000000);
106
      fpsCanvas.drawText(fpsString, fpsW/2, 0.75f*fpsH, mPaint);
107
      fpsTexture.setTexture(fpsBitmap);
108
109
      lastTime = time;
110
      }
111
112 9b94626c Leszek Koltunski
    int numrender = super.render(time,mCurRenderedFBO);
113 040cd18c Leszek Koltunski
114
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
115 2301cb2f Leszek Koltunski
116
    // workaround for the Mali issues: blit the framebuffer we have computed Distorted.FBO_QUEUE_SIZE
117
    // frames ago. Looks like FBO_QUEUE_SIZE=2 solves the issue already but I decided to play it safe and
118
    // make it equal to 3.
119
    // This of course introduces a delay and uses more memory, but it does not appear to have any effect
120
    // on speed. Maybe a slight positive effect if any!
121 9b94626c Leszek Koltunski
    setAsInput(mToBeBlittedFBO,0);
122 2301cb2f Leszek Koltunski
123 040cd18c Leszek Koltunski
    GLES31.glColorMask(true,true,true,true);
124
    GLES31.glDepthMask(false);
125
    GLES31.glDisable(GLES31.GL_STENCIL_TEST);
126
    GLES31.glDisable(GLES31.GL_DEPTH_TEST);
127
    GLES31.glDisable(GLES31.GL_BLEND);
128
129
    DistortedEffects.blitPriv(this);
130
131
    if( mShowFPS && fpsTexture.setAsInput())
132
      {
133 a13dde77 Leszek Koltunski
      fpsEffects.drawPriv(fpsW / 2.0f, fpsH / 2.0f, fpsMesh, this, time);
134 040cd18c Leszek Koltunski
      }
135
136 9b94626c Leszek Koltunski
    if( ++mCurRenderedFBO>=Distorted.FBO_QUEUE_SIZE )
137
      {
138
      mCurRenderedFBO = 0;
139
      if (mFirstCircle) mFirstCircle = false;
140
      }
141
    if( !mFirstCircle && ++mToBeBlittedFBO>=Distorted.FBO_QUEUE_SIZE )
142
      {
143
      mToBeBlittedFBO=0;
144
      }
145
146 040cd18c Leszek Koltunski
    return numrender+1;
147
    }
148
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
/**
151
 * Make the library show Frames Per Second in the upper-left corner.
152
 * <p>
153
 */
154
  public void showFPS()
155 26a4e5f6 leszek
    {
156 01b9a241 Leszek Koltunski
    if( !mShowFPS )
157
      {
158
      mShowFPS = true;
159
160
      fpsW = 120;
161
      fpsH = 70;
162
163
      fpsString = "";
164
      fpsBitmap = Bitmap.createBitmap(fpsW, fpsH, Bitmap.Config.ARGB_8888);
165
      fpsMesh = new MeshFlat(1, 1);
166
      fpsTexture = new DistortedTexture(fpsW, fpsH);
167
      fpsTexture.setTexture(fpsBitmap);
168
      fpsCanvas = new Canvas(fpsBitmap);
169
      fpsEffects = new DistortedEffects();
170
      fpsEffects.apply(mMoveEffect);
171
172
      mPaint = new Paint();
173
      mPaint.setAntiAlias(true);
174
      mPaint.setTextAlign(Paint.Align.CENTER);
175
      mPaint.setTextSize(0.7f * fpsH);
176
177
      durations = new long[NUM_FRAMES + 1];
178
      currDuration = 0;
179
180
      for (int i = 0; i < NUM_FRAMES + 1; i++) durations[i] = 16;  // Assume FPS will be
181
      durations[NUM_FRAMES] = NUM_FRAMES * 16;              // close to 1000/16 ~ 60
182
      }
183 c5369f1b leszek
    }
184 9b94626c Leszek Koltunski
  }