blob: 9e6342d4582ed4a34a1485c2ed3a3b999bed73e7 [file] [log] [blame]
Derek Sollenberger349bc372011-01-04 16:14:34 -05001/*
2 * Copyright 2010, The Android Open Source Project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25#include "AnimationThread.h"
26#include "ANPOpenGL_npapi.h"
27
28#include <EGL/egl.h>
29#include <GLES2/gl2.h>
30#include <utils/SystemClock.h>
31
32extern ANPLogInterfaceV0 gLogI;
33extern ANPOpenGLInterfaceV0 gOpenGLI;
34
35AnimationThread::AnimationThread(NPP npp) : RenderingThread(npp) {
36 m_counter = 0;
37 m_lastPrintTime = android::uptimeMillis();
38 m_executionTime = 0;
39 m_idleTime = 0;
40
41 m_x = m_y = 0;
42 m_dx = 0;
43 m_dy = 0;
44
45 memset(&m_oval, 0, sizeof(m_oval));
46
47 m_paint = new SkPaint;
48 m_paint->setAntiAlias(true);
49
50 m_bitmap = constructBitmap(DEFAULT_WIDTH, DEFAULT_HEIGHT);
51 m_canvas = new SkCanvas(*m_bitmap);
52
53 m_startExecutionTime = 0;
54 m_startTime = android::uptimeMillis();
55}
56
57AnimationThread::~AnimationThread() {
58 delete m_paint;
59 delete m_canvas;
60 delete m_bitmap;
61}
62
63SkBitmap* AnimationThread::constructBitmap(int width, int height) {
64 SkBitmap* bitmap = new SkBitmap;
65 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
66 bitmap->allocPixels();
67 bitmap->eraseColor(0x00000000);
68 return bitmap;
69}
70
71static void bounce(float* x, float* dx, const float max) {
72 *x += *dx;
73 if (*x < 0) {
74 *x = 0;
75 if (*dx < 0) {
76 *dx = -*dx;
77 }
78 } else if (*x > max) {
79 *x = max;
80 if (*dx > 0) {
81 *dx = -*dx;
82 }
83 }
84}
85
86bool AnimationThread::threadLoop() {
87
88 m_startIdleTime = android::uptimeMillis();
89
90 ANPTextureInfo textureInfo = gOpenGLI.lockTexture(m_npp);
91 GLuint textureId = textureInfo.textureId;
92
93 m_idleTime += android::uptimeMillis() - m_startIdleTime;
94 m_startExecutionTime = android::uptimeMillis();
95
96 int width, height;
97 getDimensions(width, height);
98
99 if (width <= 0)
100 width = DEFAULT_WIDTH;
101 if (height <= 0)
102 height = DEFAULT_HEIGHT;
103
104 if (m_bitmap->width() != width || m_bitmap->height() != height) {
105 delete m_canvas;
106 delete m_bitmap;
107 m_bitmap = constructBitmap(width, height);
108 m_canvas = new SkCanvas(*m_bitmap);
109
110 // change the ball's speed to match the size
111 m_dx = width * .005f;
112 m_dy = height * .007f;
113 }
114
115 // setup variables
116 const float OW = width * .125f;
117 const float OH = height * .125f;
118
119 // clear the old oval
120 m_bitmap->eraseColor(0x880000FF);
121
122 // update the coordinates of the oval
123 bounce(&m_x, &m_dx, width - OW);
124 bounce(&m_y, &m_dy, height - OH);
125
126 // draw the new oval
127 m_oval.fLeft = m_x;
128 m_oval.fTop = m_y;
129 m_oval.fRight = m_x + OW;
130 m_oval.fBottom = m_y + OH;
131 m_paint->setColor(0xAAFF0000);
132 m_canvas->drawOval(m_oval, *m_paint);
133
134 if (textureInfo.width == width && textureInfo.height == height) {
135 updateTextureWithBitmap(textureId, *m_bitmap);
136 } else {
137 createTextureWithBitmap(textureId, *m_bitmap);
138 textureInfo.width = width;
139 textureInfo.height = height;
140 textureInfo.internalFormat = GL_RGBA;
141 }
142
143 m_executionTime += android::uptimeMillis() - m_startExecutionTime;
144 m_counter++;
145
146 gOpenGLI.releaseTexture(m_npp, &textureInfo);
147
148 if (android::uptimeMillis() - m_lastPrintTime > 5000) {
149 float fps = m_counter / ((android::uptimeMillis() - m_startTime) / 1000);
150 float spf = ((android::uptimeMillis() - m_startTime)) / m_counter;
151 float lpf = (m_idleTime) / m_counter;
152 float exe = (m_executionTime) / m_counter;
153 gLogI.log(kError_ANPLogType, "TEXT: counter(%d) fps(%f) spf(%f) lock(%f) execution(%f)\n", (int)m_counter, fps, spf, lpf, exe);
154 m_lastPrintTime = android::uptimeMillis();
155
156 m_counter = 0;
157 m_executionTime = 0;
158 m_idleTime = 0;
159 m_startExecutionTime = 0;
160 m_startTime = android::uptimeMillis();
161 }
162
163 return true;
164}