summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/gallery3d/glrenderer/GLCanvasTest.java
blob: 416c11414eff2e67cd633c68ee65ebf8adb2a266 (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
 * Copyright (C) 2010 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.gallery3d.glrenderer;

import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;

import junit.framework.TestCase;

import java.util.Arrays;

import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;

public class GLCanvasTest extends TestCase {
    private static final String TAG = "GLCanvasTest";

    private static GLPaint newColorPaint(int color) {
        GLPaint paint = new GLPaint();
        paint.setColor(color);
        return paint;
    }

    @SmallTest
    public void testSetSize() {
        GL11 glStub = new GLStub();
        GLCanvas canvas = new GLES11Canvas(glStub);
        canvas.setSize(100, 200);
        canvas.setSize(1000, 100);
        try {
            canvas.setSize(-1, 100);
            fail();
        } catch (Throwable ex) {
            // expected.
        }
    }

    @SmallTest
    public void testClearBuffer() {
        new ClearBufferTest().run();
    }

    private static class ClearBufferTest extends GLMock {
        void run() {
            GLCanvas canvas = new GLES11Canvas(this);
            assertEquals(0, mGLClearCalled);
            canvas.clearBuffer();
            assertEquals(GL10.GL_COLOR_BUFFER_BIT, mGLClearMask);
            assertEquals(1, mGLClearCalled);
        }
    }

    @SmallTest
    public void testSetColor() {
        new SetColorTest().run();
    }

    // This test assumes we use pre-multipled alpha blending and should
    // set the blending function and color correctly.
    private static class SetColorTest extends GLMock {
        void run() {
            int[] testColors = new int[] {
                0, 0xFFFFFFFF, 0xFF000000, 0x00FFFFFF, 0x80FF8001,
                0x7F010101, 0xFEFEFDFC, 0x017F8081, 0x027F8081, 0x2ADE4C4D
            };

            GLCanvas canvas = new GLES11Canvas(this);
            canvas.setSize(400, 300);
            // Test one color to make sure blend function is set.
            assertEquals(0, mGLColorCalled);
            canvas.drawLine(0, 0, 1, 1, newColorPaint(0x7F804020));
            assertEquals(1, mGLColorCalled);
            assertEquals(0x7F402010, mGLColor);
            assertPremultipliedBlending(this);

            // Test other colors to make sure premultiplication is right
            for (int c : testColors) {
                float a = (c >>> 24) / 255f;
                float r = ((c >> 16) & 0xff) / 255f;
                float g = ((c >> 8) & 0xff) / 255f;
                float b = (c & 0xff) / 255f;
                int pre = makeColor4f(a * r, a * g, a * b, a);

                mGLColorCalled = 0;
                canvas.drawLine(0, 0, 1, 1, newColorPaint(c));
                assertEquals(1, mGLColorCalled);
                assertEquals(pre, mGLColor);
            }
        }
    }

    @SmallTest
    public void testSetGetMultiplyAlpha() {
        GL11 glStub = new GLStub();
        GLCanvas canvas = new GLES11Canvas(glStub);

        canvas.setAlpha(1f);
        assertEquals(1f, canvas.getAlpha());

        canvas.setAlpha(0f);
        assertEquals(0f, canvas.getAlpha());

        canvas.setAlpha(0.5f);
        assertEquals(0.5f, canvas.getAlpha());

        canvas.multiplyAlpha(0.5f);
        assertEquals(0.25f, canvas.getAlpha());

        canvas.multiplyAlpha(0f);
        assertEquals(0f, canvas.getAlpha());

        try {
            canvas.setAlpha(-0.01f);
            fail();
        } catch (Throwable ex) {
            // expected.
        }

        try {
            canvas.setAlpha(1.01f);
            fail();
        } catch (Throwable ex) {
            // expected.
        }
    }

    @SmallTest
    public void testAlpha() {
        new AlphaTest().run();
    }

    private static class AlphaTest extends GLMock {
        void run() {
            GLCanvas canvas = new GLES11Canvas(this);
            canvas.setSize(400, 300);

            assertEquals(0, mGLColorCalled);
            canvas.setAlpha(0.48f);
            canvas.drawLine(0, 0, 1, 1, newColorPaint(0xFF804020));
            assertPremultipliedBlending(this);
            assertEquals(1, mGLColorCalled);
            assertEquals(0x7A3D1F0F, mGLColor);
        }
    }

    @SmallTest
    public void testDrawLine() {
        new DrawLineTest().run();
    }

    // This test assumes the drawLine() function use glDrawArrays() with
    // GL_LINE_STRIP mode to draw the line and the input coordinates are used
    // directly.
    private static class DrawLineTest extends GLMock {
        private int mDrawArrayCalled = 0;
        private final int[] mResult = new int[4];

        @Override
        public void glDrawArrays(int mode, int first, int count) {
            assertNotNull(mGLVertexPointer);
            assertEquals(GL10.GL_LINE_STRIP, mode);
            assertEquals(2, count);
            mGLVertexPointer.bindByteBuffer();

            double[] coord = new double[4];
            mGLVertexPointer.getArrayElement(first, coord);
            mResult[0] = (int) coord[0];
            mResult[1] = (int) coord[1];
            mGLVertexPointer.getArrayElement(first + 1, coord);
            mResult[2] = (int) coord[0];
            mResult[3] = (int) coord[1];
            mDrawArrayCalled++;
        }

        void run() {
            GLCanvas canvas = new GLES11Canvas(this);
            canvas.setSize(400, 300);
            canvas.drawLine(2, 7, 1, 8, newColorPaint(0) /* color */);
            assertTrue(mGLVertexArrayEnabled);
            assertEquals(1, mDrawArrayCalled);

            Log.v(TAG, "result = " + Arrays.toString(mResult));
            int[] answer = new int[] {2, 7, 1, 8};
            for (int i = 0; i < answer.length; i++) {
                assertEquals(answer[i], mResult[i]);
            }
        }
    }

    @SmallTest
    public void testFillRect() {
        new FillRectTest().run();
    }

    // This test assumes the drawLine() function use glDrawArrays() with
    // GL_TRIANGLE_STRIP mode to draw the line and the input coordinates
    // are used directly.
    private static class FillRectTest extends GLMock {
        private int mDrawArrayCalled = 0;
        private final int[] mResult = new int[8];

        @Override
        public void glDrawArrays(int mode, int first, int count) {
            assertNotNull(mGLVertexPointer);
            assertEquals(GL10.GL_TRIANGLE_STRIP, mode);
            assertEquals(4, count);
            mGLVertexPointer.bindByteBuffer();

            double[] coord = new double[4];
            for (int i = 0; i < 4; i++) {
                mGLVertexPointer.getArrayElement(first + i, coord);
                mResult[i * 2 + 0] = (int) coord[0];
                mResult[i * 2 + 1] = (int) coord[1];
            }

            mDrawArrayCalled++;
        }

        void run() {
            GLCanvas canvas = new GLES11Canvas(this);
            canvas.setSize(400, 300);
            canvas.fillRect(2, 7, 1, 8, 0 /* color */);
            assertTrue(mGLVertexArrayEnabled);
            assertEquals(1, mDrawArrayCalled);
            Log.v(TAG, "result = " + Arrays.toString(mResult));

            // These are the four vertics that should be used.
            int[] answer = new int[] {
                2, 7,
                3, 7,
                3, 15,
                2, 15};
            int count[] = new int[4];

            // Count the number of appearances for each vertex.
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    if (answer[i * 2] == mResult[j * 2] &&
                        answer[i * 2 + 1] == mResult[j * 2 + 1]) {
                        count[i]++;
                    }
                }
            }

            // Each vertex should appear exactly once.
            for (int i = 0; i < 4; i++) {
                assertEquals(1, count[i]);
            }
        }
    }

    @SmallTest
    public void testTransform() {
        new TransformTest().run();
    }

    // This test assumes glLoadMatrixf is used to load the model view matrix,
    // and glOrthof is used to load the projection matrix.
    //
    // The projection matrix is set to an orthogonal projection which is the
    // inverse of viewport transform. So the model view matrix maps input
    // directly to screen coordinates (default no scaling, and the y-axis is
    // reversed).
    //
    // The matrix here are all listed in column major order.
    //
    private static class TransformTest extends GLMock {
        private final float[] mModelViewMatrixUsed = new float[16];
        private final float[] mProjectionMatrixUsed = new float[16];

        @Override
        public void glDrawArrays(int mode, int first, int count) {
            copy(mModelViewMatrixUsed, mGLModelViewMatrix);
            copy(mProjectionMatrixUsed, mGLProjectionMatrix);
        }

        private void copy(float[] dest, float[] src) {
            System.arraycopy(src, 0, dest, 0, 16);
        }

        void run() {
            GLCanvas canvas = new GLES11Canvas(this);
            canvas.setSize(40, 50);
            int color = 0;

            // Initial matrix
            canvas.drawLine(0, 0, 1, 1, newColorPaint(color));
            assertMatrixEq(new float[] {
                    1,  0, 0, 0,
                    0, -1, 0, 0,
                    0,  0, 1, 0,
                    0, 50, 0, 1
                    }, mModelViewMatrixUsed);

            assertMatrixEq(new float[] {
                    2f / 40,       0,  0, 0,
                          0, 2f / 50,  0, 0,
                          0,       0, -1, 0,
                         -1,      -1,  0, 1
                    }, mProjectionMatrixUsed);

            // Translation
            canvas.translate(3, 4, 5);
            canvas.drawLine(0, 0, 1, 1, newColorPaint(color));
            assertMatrixEq(new float[] {
                    1,  0, 0, 0,
                    0, -1, 0, 0,
                    0,  0, 1, 0,
                    3, 46, 5, 1
                    }, mModelViewMatrixUsed);
            canvas.save();

            // Scaling
            canvas.scale(0.7f, 0.6f, 0.5f);
            canvas.drawLine(0, 0, 1, 1, newColorPaint(color));
            assertMatrixEq(new float[] {
                    0.7f,     0,    0, 0,
                    0,    -0.6f,    0, 0,
                    0,        0, 0.5f, 0,
                    3,       46,    5, 1
                    }, mModelViewMatrixUsed);

            // Rotation
            canvas.rotate(90, 0, 0, 1);
            canvas.drawLine(0, 0, 1, 1, newColorPaint(color));
            assertMatrixEq(new float[] {
                        0, -0.6f,    0, 0,
                    -0.7f,     0,    0, 0,
                        0,     0, 0.5f, 0,
                        3,    46,    5, 1
                    }, mModelViewMatrixUsed);
            canvas.restore();

            // After restoring to the point just after translation,
            // do rotation again.
            canvas.rotate(180, 1, 0, 0);
            canvas.drawLine(0, 0, 1, 1, newColorPaint(color));
            assertMatrixEq(new float[] {
                    1,  0,  0, 0,
                    0,  1,  0, 0,
                    0,  0, -1, 0,
                    3, 46,  5, 1
                    }, mModelViewMatrixUsed);
        }
    }

    private static void assertPremultipliedBlending(GLMock mock) {
        assertTrue(mock.mGLBlendFuncCalled > 0);
        assertTrue(mock.mGLBlendEnabled);
        assertEquals(GL11.GL_ONE, mock.mGLBlendFuncSFactor);
        assertEquals(GL11.GL_ONE_MINUS_SRC_ALPHA, mock.mGLBlendFuncDFactor);
    }

    private static void assertMatrixEq(float[] expected, float[] actual) {
        try {
            for (int i = 0; i < 16; i++) {
                assertFloatEq(expected[i], actual[i]);
            }
        } catch (Throwable t) {
            Log.v(TAG, "expected = " + Arrays.toString(expected) +
                    ", actual = " + Arrays.toString(actual));
            fail();
        }
    }

    public static void assertFloatEq(float expected, float actual) {
        if (Math.abs(actual - expected) > 1e-6) {
            Log.v(TAG, "expected: " + expected + ", actual: " + actual);
            fail();
        }
    }
}