summaryrefslogtreecommitdiffstats
path: root/src/com/android/dreams/basic/Colors.java
blob: db45dc892c23c010733ebba9f66ca40f82d6051f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dreams.basic;

import android.animation.Animator;
import android.opengl.GLSurfaceView;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.TimeInterpolator;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.net.Uri;
import android.os.BatteryManager;
import android.os.Handler;
import android.provider.Settings;
import android.service.dreams.Dream;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.widget.TextView;
import android.os.SystemClock;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

public class Colors extends Dream {
    // It's so easy to use OpenGLES 2.0!
    GLSurfaceView gl;
    Square mSquare;

    class Square {
        // Straight from the API guide
        private final String vertexShaderCode =
            "attribute vec4 a_position;" +
            "attribute vec4 a_color;" +
            "varying vec4 v_color;" +
            "void main() {" +
            "  gl_Position = a_position;" +
            "  v_color = a_color;" +
            "}";

        private final String fragmentShaderCode =
            "precision mediump float;" +
            "varying vec4 v_color;" +
            "void main() {" +
            "  gl_FragColor = v_color;" +
            "}";

        private final FloatBuffer vertexBuffer;
        private final FloatBuffer colorBuffer;
        private final int mProgram;
        private int mPositionHandle;
        private int mColorHandle;

        private ShortBuffer drawListBuffer;


        // number of coordinates per vertex in this array
        final int COORDS_PER_VERTEX = 3;
        float squareCoords[] = { -1f,  1f, 0f,   // top left
                                 -1f, -1f, 0f,   // bottom left
                                  1f, -1f, 0f,   // bottom right
                                  1f,  1f, 0f }; // top right

        private short drawOrder[] = { 0, 1, 2, 0, 2, 3 }; // order to draw vertices (CCW)
        
        private final float HUES[] = { // reverse order due to CCW winding
                60,  // yellow
                120, // green
                343, // red
                200, // blue
        };
        
        private final int vertexCount = squareCoords.length / COORDS_PER_VERTEX;
        private final int vertexStride = COORDS_PER_VERTEX * 4; // bytes per vertex

        private float cornerFrequencies[] = new float[vertexCount];
        private int cornerRotation;
        
        final int COLOR_PLANES_PER_VERTEX = 4;
        private final int colorStride = COLOR_PLANES_PER_VERTEX * 4; // bytes per vertex

        // Set color with red, green, blue and alpha (opacity) values
        float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };

        public Square() {
            for (int i=0; i<vertexCount; i++) {
                cornerFrequencies[i] = 1f + (float)(Math.random() * 5); 
            }
            cornerRotation = (int)(Math.random() * vertexCount);
            // initialize vertex byte buffer for shape coordinates
            ByteBuffer bb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 4 bytes per float)
                    squareCoords.length * 4);
            bb.order(ByteOrder.nativeOrder());
            vertexBuffer = bb.asFloatBuffer();
            vertexBuffer.put(squareCoords);
            vertexBuffer.position(0);
            
            bb = ByteBuffer.allocateDirect(vertexCount * colorStride);
            bb.order(ByteOrder.nativeOrder());
            colorBuffer = bb.asFloatBuffer();

            // initialize byte buffer for the draw list
            ByteBuffer dlb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 2 bytes per short)
                    drawOrder.length * 2);
            dlb.order(ByteOrder.nativeOrder());
            drawListBuffer = dlb.asShortBuffer();
            drawListBuffer.put(drawOrder);
            drawListBuffer.position(0);

            // prepare shaders and OpenGL program
            int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER,
                                                       vertexShaderCode);
            int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER,
                                                         fragmentShaderCode);

            mProgram = GLES20.glCreateProgram();             // create empty OpenGL Program
            GLES20.glAttachShader(mProgram, vertexShader);   // add the vertex shader to program
            GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
            GLES20.glLinkProgram(mProgram);                  // create OpenGL program executables
        }

        final float[] _tmphsv = new float[3];
        public void draw() {
            // Add program to OpenGL environment
            GLES20.glUseProgram(mProgram);

            // get handle to vertex shader's a_position member
            mPositionHandle = GLES20.glGetAttribLocation(mProgram, "a_position");

            // Enable a handle to the triangle vertices
            GLES20.glEnableVertexAttribArray(mPositionHandle);

            // Prepare the triangle coordinate data
            GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
                                         GLES20.GL_FLOAT, false,
                                         vertexStride, vertexBuffer);

            // same thing for colors
            long now = SystemClock.uptimeMillis();
            colorBuffer.clear();
            final float t = (float)now / 4000f; // set the base period to 4sec
//            android.util.Slog.v("Colors", "t=" + t);
            for(int i=0; i<vertexCount; i++) {
                final float freq = (float) Math.sin(2 * Math.PI * t / cornerFrequencies[i]);
                _tmphsv[0] = HUES[(i + cornerRotation) % vertexCount];
                _tmphsv[1] = 1f;
                _tmphsv[2] = freq * 0.25f + 0.75f;
                final int c = Color.HSVToColor(_tmphsv);
                colorBuffer.put((float)((c & 0xFF0000) >> 16) / 0xFF);
                colorBuffer.put((float)((c & 0x00FF00) >> 8) / 0xFF);
                colorBuffer.put((float)(c & 0x0000FF) / 0xFF);
                colorBuffer.put(/*a*/ 1f);
            }
            colorBuffer.position(0);
            mColorHandle = GLES20.glGetAttribLocation(mProgram, "a_color");
            checkGlError("glGetAttribLocation");
            GLES20.glEnableVertexAttribArray(mColorHandle);
            checkGlError("glEnableVertexAttribArray");
            GLES20.glVertexAttribPointer(mColorHandle, COLOR_PLANES_PER_VERTEX,
                    GLES20.GL_FLOAT, false,
                    colorStride, colorBuffer);
            checkGlError("glVertexAttribPointer");

            // Draw the triangle
            GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, vertexCount);

            // Disable vertex array
            GLES20.glDisableVertexAttribArray(mPositionHandle);
            GLES20.glDisableVertexAttribArray(mColorHandle);
        }

        public int loadShader(int type, String shaderCode){

            // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
            // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
            int shader = GLES20.glCreateShader(type);

            // add the source code to the shader and compile it
            GLES20.glShaderSource(shader, shaderCode);
            GLES20.glCompileShader(shader);

            return shader;
        }

        /**
         * Utility method for debugging OpenGL calls. Provide the name of the call
         * just after making it:
         *
         * <pre>
         * mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
         * MyGLRenderer.checkGlError("glGetUniformLocation");</pre>
         *
         * If the operation is not successful, the check throws an error.
         *
         * @param glOperation - Name of the OpenGL call to check.
         */
        public void checkGlError(String glOperation) {
            int error;
            while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
                Log.e("GL", glOperation + ": glError " + error);
                throw new RuntimeException(String.format("%s: glError 0x%04x", glOperation, error));
            }
        }
    }

    private class ColorsRenderer implements GLSurfaceView.Renderer {
        public void onSurfaceCreated(GL10 unused, EGLConfig config) {
            mSquare = new Square();
            GLES20.glClearColor(0f, 0f, 0f, 1.0f);
        }

        public void onDrawFrame(GL10 unused) {
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

            mSquare.draw();
        }

        public void onSurfaceChanged(GL10 unused, int width, int height) {
            GLES20.glViewport(0, 0, width, height);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    class TheySaidIHadToHaveAGLSurfaceView extends GLSurfaceView {

        public TheySaidIHadToHaveAGLSurfaceView(Context context){
            super(context);

            setEGLContextClientVersion(2);

            setRenderer(new ColorsRenderer());
        }
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        
        lightsOut(); // lights out, fullscreen
        setInteractive(false);
        
        gl = new TheySaidIHadToHaveAGLSurfaceView(Colors.this);
        gl.postDelayed(new Runnable() {
            public void run() {
                Colors.this.setContentView(gl);
            }
        }, 1000);
    }
}