Project

General

Profile

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

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

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 int FPS_W       = 120;
44
  private static final int FPS_H       =  70;
45

    
46
  private boolean mShowFPS;
47

    
48
  private MeshQuad fpsMesh;
49
  private DistortedTexture fpsTexture;
50
  private DistortedEffects fpsEffects;
51
  private Canvas fpsCanvas;
52
  private Bitmap fpsBitmap;
53
  private Paint mPaint;
54
  private String fpsString;
55
  private long lastTime=0;
56
  private long[] durations;
57
  private int currDuration;
58
  private static Static3D mMoveVector = new Static3D(0,0,0);
59
  private static MatrixEffectMove mMoveEffect = new MatrixEffectMove(mMoveVector);
60
  ///// END DEBUGGING //////////////////////////
61

    
62
  private int mCurRenderedFBO;    // During the first FBO_QUEUE_SIZE frames, we blit the very first
63
  private int mToBeBlittedFBO;    // FBO one we have rendered. Then, we keep blitting the one we
64
  private boolean mFirstCircle;   // rendered FBO_QUEUE_SIZE ago.
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
// PUBLIC API
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69
/**
70
 * Create a new Screen. Initially 1x1 in size.
71
 * <p>
72
 * Has to be followed by a 'resizeFBO()' to set the size.
73
 */
74
  public DistortedScreen()
75
    {
76
    super(DistortedLibrary.FBO_QUEUE_SIZE,1,BOTH_DEPTH_STENCIL, TYPE_SYST, 1,1);
77
    mShowFPS = false;
78
    mCurRenderedFBO = 0;
79
    mToBeBlittedFBO = 0;
80
    mFirstCircle = true;
81
    }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
/**
85
 * Draws all the attached children to this OutputSurface.
86
 * <p>
87
 * Must be called from a thread holding OpenGL Context.
88
 *
89
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
90
 * @return Number of objects rendered.
91
 */
92
  public int render(long time)
93
    {
94
    if( mShowFPS )
95
      {
96
      if( lastTime==0 ) lastTime = time;
97

    
98
      currDuration++;
99
      if (currDuration >= NUM_FRAMES) currDuration = 0;
100
      durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
101
      durations[currDuration] = time - lastTime;
102

    
103
      fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
104

    
105
      mPaint.setColor(0xffffffff);
106
      fpsCanvas.drawRect(0, 0, FPS_W, FPS_H, mPaint);
107
      mPaint.setColor(0xff000000);
108
      fpsCanvas.drawText(fpsString, 0.5f*FPS_W, 0.75f*FPS_H, mPaint);
109
      fpsTexture.setTexture(fpsBitmap);
110

    
111
      mMoveVector.set( (-mWidth+FPS_W)/2 +5, (mHeight-FPS_H)/2 -5, 0);
112

    
113
      lastTime = time;
114
      }
115

    
116
    int numrender = super.render(time,mCurRenderedFBO);
117

    
118
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
119

    
120
    // workaround for the Mali issues: blit the framebuffer we have computed DistortedLibrary.FBO_QUEUE_SIZE
121
    // frames ago. Looks like FBO_QUEUE_SIZE=2 solves the issue already but I decided to play it safe and
122
    // make it equal to 3.
123
    // This of course introduces a delay and uses more memory, but it does not appear to have any effect
124
    // on speed. Maybe a slight positive effect if any!
125
    setAsInput(mToBeBlittedFBO,0);
126

    
127
    GLES30.glColorMask(true,true,true,true);
128
    GLES30.glDepthMask(false);
129
    GLES30.glDisable(GLES30.GL_STENCIL_TEST);
130
    GLES30.glDisable(GLES30.GL_DEPTH_TEST);
131
    GLES30.glDisable(GLES30.GL_BLEND);
132

    
133
    DistortedLibrary.blitPriv(this);
134

    
135
    if( mShowFPS && fpsTexture.setAsInput())
136
      {
137
      DistortedLibrary.drawPriv(fpsEffects, fpsMesh, this, time);
138
      }
139

    
140
    if( ++mCurRenderedFBO>= DistortedLibrary.FBO_QUEUE_SIZE )
141
      {
142
      mCurRenderedFBO = 0;
143
      if (mFirstCircle) mFirstCircle = false;
144
      }
145
    if( !mFirstCircle && ++mToBeBlittedFBO>= DistortedLibrary.FBO_QUEUE_SIZE )
146
      {
147
      mToBeBlittedFBO=0;
148
      }
149

    
150
    return numrender+1;
151
    }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
/**
155
 * Make the library show Frames Per Second in the upper-left corner.
156
 * <p>
157
 */
158
  public void showFPS()
159
    {
160
    if( !mShowFPS )
161
      {
162
      mShowFPS = true;
163
      fpsString = "";
164
      fpsBitmap = Bitmap.createBitmap(FPS_W, FPS_H, Bitmap.Config.ARGB_8888);
165
      fpsMesh = new MeshQuad();
166
      fpsTexture = new DistortedTexture();
167
      fpsTexture.setTexture(fpsBitmap);
168
      fpsCanvas = new Canvas(fpsBitmap);
169
      fpsEffects = new DistortedEffects();
170
      fpsEffects.apply( new MatrixEffectScale( new Static3D(FPS_W,FPS_H,1) ) );
171
      fpsEffects.apply(mMoveEffect);
172

    
173
      mPaint = new Paint();
174
      mPaint.setAntiAlias(true);
175
      mPaint.setTextAlign(Paint.Align.CENTER);
176
      mPaint.setTextSize(0.7f * FPS_H);
177

    
178
      durations = new long[NUM_FRAMES + 1];
179
      currDuration = 0;
180

    
181
      for (int i=0; i<NUM_FRAMES+1; i++) durations[i] = 16;  // Assume FPS will be
182
      durations[NUM_FRAMES] = NUM_FRAMES * 16;               // close to 1000/16 ~ 60
183
      }
184
    }
185
  }
(5-5/14)