blob: 0fbb4216f8017f4b412e4863b27d15e8e1300ae5 [file] [log] [blame]
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -04001/*
2 * Copyright 2008, 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
26#include "BackgroundPlugin.h"
27#include "android_npapi.h"
28
29#include <stdio.h>
30#include <sys/time.h>
31#include <time.h>
32#include <math.h>
33#include <string.h>
34
35extern NPNetscapeFuncs* browser;
36extern ANPBitmapInterfaceV0 gBitmapI;
37extern ANPLogInterfaceV0 gLogI;
38extern ANPCanvasInterfaceV0 gCanvasI;
39extern ANPPaintInterfaceV0 gPaintI;
40extern ANPPathInterfaceV0 gPathI;
41extern ANPTypefaceInterfaceV0 gTypefaceI;
42
43extern uint32_t getMSecs();
44
45#define ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0]))
46
47//#define LOG_ERROR(inst, string, params...) gLogI.log(inst, kError_ANPLogType, (log_prefix + string), inst, params)
48
49///////////////////////////////////////////////////////////////////////////////
50
51BackgroundPlugin::BackgroundPlugin(NPP inst) : SubPlugin(inst) {
52
53 m_paint = gPaintI.newPaint();
54 gPaintI.setFlags(m_paint, gPaintI.getFlags(m_paint) | kAntiAlias_ANPPaintFlag);
55 gPaintI.setColor(m_paint, 0xFFFF0000);
56 gPaintI.setTextSize(m_paint, 16);
57
58 ANPTypeface* tf = gTypefaceI.createFromName("serif", kItalic_ANPTypefaceStyle);
59 gPaintI.setTypeface(m_paint, tf);
60 gTypefaceI.unref(tf);
61
62 //initialize variables
63 mFinishedStageOne = false;
64 mFinishedStageTwo = false;
65 mFinishedStageThree = false;
66
67 // test basic plugin functionality
68 test_logging(); // android logging
69 test_timers(); // plugin timers
70 test_bitmaps(); // android bitmaps
71}
72
73BackgroundPlugin::~BackgroundPlugin() {
74}
75
76void BackgroundPlugin::draw(ANPCanvas* canvas) {
77
78 gCanvasI.drawColor(canvas, 0xFFFFFFFF);
79
80 ANPFontMetrics fm;
81 gPaintI.getFontMetrics(m_paint, &fm);
82
83 gPaintI.setColor(m_paint, 0xFF0000FF);
84 const char c[] = "This is a background plugin.";
85 gCanvasI.drawText(canvas, c, sizeof(c)-1, 10, -fm.fTop, m_paint);
86}
87
88static void drawPlugin(SubPlugin* plugin, const ANPBitmap& bitmap, const ANPRectI& clip) {
89
90 ANPCanvas* canvas = gCanvasI.newCanvas(&bitmap);
91
92 ANPRectF clipR;
93 clipR.left = clip.left;
94 clipR.top = clip.top;
95 clipR.right = clip.right;
96 clipR.bottom = clip.bottom;
97 gCanvasI.clipRect(canvas, &clipR);
98
99 plugin->draw(canvas);
100 gCanvasI.deleteCanvas(canvas);
101}
102
103int16 BackgroundPlugin::handleEvent(const ANPEvent* evt) {
104 NPP instance = this->inst();
105
106 switch (evt->eventType) {
107 case kDraw_ANPEventType:
108 switch (evt->data.draw.model) {
109 case kBitmap_ANPDrawingModel:
110 test_bitmap_transparency(evt);
111 drawPlugin(this, evt->data.draw.data.bitmap, evt->data.draw.clip);
112 return 1;
113 default:
114 break; // unknown drawing model
115 }
116 case kTouch_ANPEventType:
117 gLogI.log(instance, kError_ANPLogType, " ------ %p the plugin did not request touch events", instance);
118 case kKey_ANPEventType:
119 gLogI.log(instance, kError_ANPLogType, " ------ %p the plugin did not request key events", instance);
120 default:
121 break;
122 }
123 return 0; // unknown or unhandled event
124}
125
126///////////////////////////////////////////////////////////////////////////////
127// LOGGING TESTS
128///////////////////////////////////////////////////////////////////////////////
129
130
131void BackgroundPlugin::test_logging() {
132 NPP instance = this->inst();
133
134 //LOG_ERROR(instance, " ------ %p Testing Log Error", instance);
135 gLogI.log(instance, kError_ANPLogType, " ------ %p Testing Log Error", instance);
136 gLogI.log(instance, kWarning_ANPLogType, " ------ %p Testing Log Warning", instance);
137 gLogI.log(instance, kDebug_ANPLogType, " ------ %p Testing Log Debug", instance);
138}
139
140///////////////////////////////////////////////////////////////////////////////
141// TIMER TESTS
142///////////////////////////////////////////////////////////////////////////////
143
144#define TIMER_INTERVAL 50
145static void timer_oneshot(NPP instance, uint32 timerID);
146static void timer_repeat(NPP instance, uint32 timerID);
147static void timer_neverfires(NPP instance, uint32 timerID);
148static void timer_latency(NPP instance, uint32 timerID);
149
150void BackgroundPlugin::test_timers() {
151 NPP instance = this->inst();
152
153 //Setup the testing counters
154 mTimerRepeatCount = 5;
155 mTimerLatencyCount = 5;
156
157 // test for bogus timerID
158 browser->unscheduletimer(instance, 999999);
159 // test one-shot
160 browser->scheduletimer(instance, 100, false, timer_oneshot);
161 // test repeat
162 browser->scheduletimer(instance, 50, true, timer_repeat);
163 // test timer latency
164 browser->scheduletimer(instance, TIMER_INTERVAL, true, timer_latency);
165 mStartTime = mPrevTime = getMSecs();
166 // test unschedule immediately
167 uint32 id = browser->scheduletimer(instance, 100, false, timer_neverfires);
168 browser->unscheduletimer(instance, id);
169 // test double unschedule (should be no-op)
170 browser->unscheduletimer(instance, id);
171
172}
173
174static void timer_oneshot(NPP instance, uint32 timerID) {
175 gLogI.log(instance, kDebug_ANPLogType, "-------- oneshot timer\n");
176}
177
178static void timer_repeat(NPP instance, uint32 timerID) {
179 BackgroundPlugin *obj = ((BackgroundPlugin*) ((PluginObject*) instance->pdata)->activePlugin);
180
181 gLogI.log(instance, kDebug_ANPLogType, "-------- repeat timer %d\n",
182 obj->mTimerRepeatCount);
183 if (--obj->mTimerRepeatCount == 0) {
184 browser->unscheduletimer(instance, timerID);
185 }
186}
187
188static void timer_neverfires(NPP instance, uint32 timerID) {
189 gLogI.log(instance, kError_ANPLogType, "-------- timer_neverfires!!!\n");
190}
191
192static void timer_latency(NPP instance, uint32 timerID) {
193 BackgroundPlugin *obj = ((BackgroundPlugin*) ((PluginObject*) instance->pdata)->activePlugin);
194
195 obj->mTimerLatencyCurrentCount += 1;
196
197 uint32_t now = getMSecs();
198 uint32_t interval = now - obj->mPrevTime;
199 uint32_t dur = now - obj->mStartTime;
200 uint32_t expectedDur = obj->mTimerLatencyCurrentCount * TIMER_INTERVAL;
201 int32_t drift = dur - expectedDur;
202 int32_t avgDrift = drift / obj->mTimerLatencyCurrentCount;
203
204 obj->mPrevTime = now;
205
206 gLogI.log(instance, kDebug_ANPLogType,
207 "-------- latency test: [%3d] interval %d expected %d, total %d expected %d, drift %d avg %d\n",
208 obj->mTimerLatencyCurrentCount, interval, TIMER_INTERVAL, dur,
209 expectedDur, drift, avgDrift);
210
211 if (--obj->mTimerLatencyCount == 0) {
212 browser->unscheduletimer(instance, timerID);
213 }
214}
215
216///////////////////////////////////////////////////////////////////////////////
217// BITMAP TESTS
218///////////////////////////////////////////////////////////////////////////////
219
220static void test_formats(NPP instance);
221
222void BackgroundPlugin::test_bitmaps() {
223 test_formats(this->inst());
224}
225
226static void test_formats(NPP instance) {
227
228 // TODO pull names from enum in npapi instead of hardcoding them
229 static const struct {
230 ANPBitmapFormat fFormat;
231 const char* fName;
232 } gRecs[] = {
233 { kUnknown_ANPBitmapFormat, "unknown" },
234 { kRGBA_8888_ANPBitmapFormat, "8888" },
235 { kRGB_565_ANPBitmapFormat, "565" },
236 };
237
238 ANPPixelPacking packing;
239 for (size_t i = 0; i < ARRAY_COUNT(gRecs); i++) {
240 if (gBitmapI.getPixelPacking(gRecs[i].fFormat, &packing)) {
241 gLogI.log(instance, kDebug_ANPLogType,
242 "pixel format [%d] %s has packing ARGB [%d %d] [%d %d] [%d %d] [%d %d]\n",
243 gRecs[i].fFormat, gRecs[i].fName,
244 packing.AShift, packing.ABits,
245 packing.RShift, packing.RBits,
246 packing.GShift, packing.GBits,
247 packing.BShift, packing.BBits);
248 } else {
249 gLogI.log(instance, kDebug_ANPLogType,
250 "pixel format [%d] %s has no packing\n",
251 gRecs[i].fFormat, gRecs[i].fName);
252 }
253 }
254}
255
256void BackgroundPlugin::test_bitmap_transparency(const ANPEvent* evt) {
257 NPP instance = this->inst();
258
259 // check default & set transparent
260 if (!mFinishedStageOne) {
261
262 gLogI.log(instance, kDebug_ANPLogType, "BEGIN: testing bitmap transparency");
263
264 //check to make sure it is not transparent
265 if (evt->data.draw.data.bitmap.format == kRGBA_8888_ANPBitmapFormat) {
266 gLogI.log(instance, kError_ANPLogType, "bitmap default format is transparent");
267 }
268
269 //make it transparent (any non-null value will set it to true)
270 bool value = true;
271 NPError err = browser->setvalue(instance, NPPVpluginTransparentBool, &value);
272 if (err != NPERR_NO_ERROR) {
273 gLogI.log(instance, kError_ANPLogType, "Error setting transparency.");
274 }
275
276 mFinishedStageOne = true;
277 browser->invalidaterect(instance, NULL);
278 }
279 // check transparent & set opaque
280 else if (!mFinishedStageTwo) {
281
282 //check to make sure it is transparent
283 if (evt->data.draw.data.bitmap.format != kRGBA_8888_ANPBitmapFormat) {
284 gLogI.log(instance, kError_ANPLogType, "bitmap did not change to transparent format");
285 }
286
287 //make it opaque
288 NPError err = browser->setvalue(instance, NPPVpluginTransparentBool, NULL);
289 if (err != NPERR_NO_ERROR) {
290 gLogI.log(instance, kError_ANPLogType, "Error setting transparency.");
291 }
292
293 mFinishedStageTwo = true;
294 }
295 // check opaque
296 else if (!mFinishedStageThree) {
297
298 //check to make sure it is not transparent
299 if (evt->data.draw.data.bitmap.format == kRGBA_8888_ANPBitmapFormat) {
300 gLogI.log(instance, kError_ANPLogType, "bitmap default format is transparent");
301 }
302
303 gLogI.log(instance, kDebug_ANPLogType, "END: testing bitmap transparency");
304
305 mFinishedStageThree = true;
306 }
307}