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