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