blob: 1388fe3745292cd05e6c4c0e56fdb34e52bd4281 [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"
Derek Sollenberger349bc372011-01-04 16:14:34 -050026
Derek Sollenberger349bc372011-01-04 16:14:34 -050027#include <utils/SystemClock.h>
Chris Craikaceb2e32011-08-04 17:41:20 -070028#include "ANPNativeWindow_npapi.h"
Derek Sollenberger349bc372011-01-04 16:14:34 -050029
Chris Craikaceb2e32011-08-04 17:41:20 -070030extern ANPLogInterfaceV0 gLogI;
31extern ANPNativeWindowInterfaceV0 gNativeWindowI;
Derek Sollenberger349bc372011-01-04 16:14:34 -050032
33AnimationThread::AnimationThread(NPP npp) : RenderingThread(npp) {
34 m_counter = 0;
35 m_lastPrintTime = android::uptimeMillis();
36 m_executionTime = 0;
37 m_idleTime = 0;
38
39 m_x = m_y = 0;
40 m_dx = 0;
41 m_dy = 0;
42
43 memset(&m_oval, 0, sizeof(m_oval));
44
45 m_paint = new SkPaint;
46 m_paint->setAntiAlias(true);
47
48 m_bitmap = constructBitmap(DEFAULT_WIDTH, DEFAULT_HEIGHT);
49 m_canvas = new SkCanvas(*m_bitmap);
50
51 m_startExecutionTime = 0;
52 m_startTime = android::uptimeMillis();
Chris Craikaceb2e32011-08-04 17:41:20 -070053 m_stallTime = android::uptimeMillis();
54
Derek Sollenberger349bc372011-01-04 16:14:34 -050055}
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() {
Chris Craikaceb2e32011-08-04 17:41:20 -070087 if (android::uptimeMillis() - m_stallTime < MS_PER_FRAME)
88 return true;
89 m_stallTime = android::uptimeMillis();
Derek Sollenberger349bc372011-01-04 16:14:34 -050090
91 m_idleTime += android::uptimeMillis() - m_startIdleTime;
92 m_startExecutionTime = android::uptimeMillis();
93
Chris Craikaceb2e32011-08-04 17:41:20 -070094 bool reCreateFlag = false;
Derek Sollenberger349bc372011-01-04 16:14:34 -050095 int width, height;
96 getDimensions(width, height);
97
98 if (width <= 0)
99 width = DEFAULT_WIDTH;
100 if (height <= 0)
101 height = DEFAULT_HEIGHT;
102
103 if (m_bitmap->width() != width || m_bitmap->height() != height) {
104 delete m_canvas;
105 delete m_bitmap;
106 m_bitmap = constructBitmap(width, height);
107 m_canvas = new SkCanvas(*m_bitmap);
108
109 // change the ball's speed to match the size
110 m_dx = width * .005f;
111 m_dy = height * .007f;
Chris Craikaceb2e32011-08-04 17:41:20 -0700112 reCreateFlag = true;
Derek Sollenberger349bc372011-01-04 16:14:34 -0500113 }
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
Chris Craikaceb2e32011-08-04 17:41:20 -0700134 if (!reCreateFlag) {
135 updateNativeWindow(m_ANW, *m_bitmap);
Derek Sollenberger349bc372011-01-04 16:14:34 -0500136 } else {
Chris Craikaceb2e32011-08-04 17:41:20 -0700137 setupNativeWindow(m_ANW, *m_bitmap);
Derek Sollenberger349bc372011-01-04 16:14:34 -0500138 }
139
140 m_executionTime += android::uptimeMillis() - m_startExecutionTime;
141 m_counter++;
142
Derek Sollenberger349bc372011-01-04 16:14:34 -0500143 if (android::uptimeMillis() - m_lastPrintTime > 5000) {
144 float fps = m_counter / ((android::uptimeMillis() - m_startTime) / 1000);
145 float spf = ((android::uptimeMillis() - m_startTime)) / m_counter;
146 float lpf = (m_idleTime) / m_counter;
147 float exe = (m_executionTime) / m_counter;
148 gLogI.log(kError_ANPLogType, "TEXT: counter(%d) fps(%f) spf(%f) lock(%f) execution(%f)\n", (int)m_counter, fps, spf, lpf, exe);
149 m_lastPrintTime = android::uptimeMillis();
150
151 m_counter = 0;
152 m_executionTime = 0;
153 m_idleTime = 0;
154 m_startExecutionTime = 0;
155 m_startTime = android::uptimeMillis();
156 }
157
Chris Craikaceb2e32011-08-04 17:41:20 -0700158 m_startIdleTime = android::uptimeMillis(); // count delay between frames
Derek Sollenberger349bc372011-01-04 16:14:34 -0500159 return true;
160}