BackgroundPlugin.cpp revision 08581f1c06284d2e890d5ddb7f5590d8ebf22146
1/* 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 ANPCanvasInterfaceV0 gCanvasI; 38extern ANPLogInterfaceV0 gLogI; 39extern ANPPaintInterfaceV0 gPaintI; 40extern ANPSurfaceInterfaceV0 gSurfaceI; 41extern ANPTypefaceInterfaceV0 gTypefaceI; 42 43#define ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0])) 44 45static uint32_t getMSecs() { 46 struct timeval tv; 47 gettimeofday(&tv, NULL); 48 return (uint32_t) (tv.tv_sec * 1000 + tv.tv_usec / 1000 ); // microseconds to milliseconds 49} 50 51/////////////////////////////////////////////////////////////////////////////// 52 53BackgroundPlugin::BackgroundPlugin(NPP inst) : SurfaceSubPlugin(inst) { 54 55 // initialize the drawing surface 56 m_surface = NULL; 57 m_vm = NULL; 58 59 //initialize bitmap transparency variables 60 mFinishedStageOne = false; 61 mFinishedStageTwo = false; 62 mFinishedStageThree = false; 63 64 // test basic plugin functionality 65 test_logging(); // android logging 66 test_timers(); // plugin timers 67 test_bitmaps(); // android bitmaps 68 test_domAccess(); 69 test_javascript(); 70} 71 72BackgroundPlugin::~BackgroundPlugin() { } 73 74bool BackgroundPlugin::supportsDrawingModel(ANPDrawingModel model) { 75 return (model == kSurface_ANPDrawingModel); 76} 77 78bool BackgroundPlugin::isFixedSurface() { 79 return false; 80} 81 82void BackgroundPlugin::surfaceCreated(JNIEnv* env, jobject surface) { 83 env->GetJavaVM(&m_vm); 84 m_surface = env->NewGlobalRef(surface); 85} 86 87void BackgroundPlugin::surfaceChanged(int format, int width, int height) { 88 drawPlugin(width, height); 89} 90 91void BackgroundPlugin::surfaceDestroyed() { 92 JNIEnv* env = NULL; 93 if (m_surface && m_vm->GetEnv((void**) &env, JNI_VERSION_1_4) == JNI_OK) { 94 env->DeleteGlobalRef(m_surface); 95 m_surface = NULL; 96 } 97} 98 99void BackgroundPlugin::drawPlugin(int surfaceWidth, int surfaceHeight) { 100 101 // get the plugin's dimensions according to the DOM 102 PluginObject *obj = (PluginObject*) inst()->pdata; 103 const int W = obj->window->width; 104 const int H = obj->window->height; 105 106 // compute the current zoom level 107 const float zoomFactorW = static_cast<float>(surfaceWidth) / W; 108 const float zoomFactorH = static_cast<float>(surfaceHeight) / H; 109 110 // check to make sure the zoom level is uniform 111 if (zoomFactorW + .01 < zoomFactorH && zoomFactorW - .01 > zoomFactorH) 112 gLogI.log(inst(), kError_ANPLogType, " ------ %p zoom is out of sync (%f,%f)", 113 inst(), zoomFactorW, zoomFactorH); 114 115 // scale the variables based on the zoom level 116 const int fontSize = (int)(zoomFactorW * 16); 117 const int leftMargin = (int)(zoomFactorW * 10); 118 119 // lock the surface 120 ANPBitmap bitmap; 121 JNIEnv* env = NULL; 122 if (!m_surface || m_vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK || 123 !gSurfaceI.lock(env, m_surface, &bitmap, NULL)) { 124 gLogI.log(inst(), kError_ANPLogType, " ------ %p unable to lock the plugin", inst()); 125 return; 126 } 127 128 // create a canvas 129 ANPCanvas* canvas = gCanvasI.newCanvas(&bitmap); 130 gCanvasI.drawColor(canvas, 0xFFFFFFFF); 131 132 ANPPaint* paint = gPaintI.newPaint(); 133 gPaintI.setFlags(paint, gPaintI.getFlags(paint) | kAntiAlias_ANPPaintFlag); 134 gPaintI.setColor(paint, 0xFFFF0000); 135 gPaintI.setTextSize(paint, fontSize); 136 137 ANPTypeface* tf = gTypefaceI.createFromName("serif", kItalic_ANPTypefaceStyle); 138 gPaintI.setTypeface(paint, tf); 139 gTypefaceI.unref(tf); 140 141 ANPFontMetrics fm; 142 gPaintI.getFontMetrics(paint, &fm); 143 144 gPaintI.setColor(paint, 0xFF0000FF); 145 const char c[] = "This is a background plugin."; 146 gCanvasI.drawText(canvas, c, sizeof(c)-1, leftMargin, -fm.fTop, paint); 147 148 // clean up variables and unlock the surface 149 gPaintI.deletePaint(paint); 150 gCanvasI.deleteCanvas(canvas); 151 gSurfaceI.unlock(env, m_surface); 152} 153 154int16 BackgroundPlugin::handleEvent(const ANPEvent* evt) { 155 switch (evt->eventType) { 156 case kDraw_ANPEventType: 157 gLogI.log(inst(), kError_ANPLogType, " ------ %p the plugin did not request draw events", inst()); 158 break; 159 case kLifecycle_ANPEventType: 160 if (evt->data.lifecycle.action == kOnLoad_ANPLifecycleAction) { 161 gLogI.log(inst(), kDebug_ANPLogType, " ------ %p the plugin received an onLoad event", inst()); 162 return 1; 163 } 164 break; 165 case kTouch_ANPEventType: 166 gLogI.log(inst(), kError_ANPLogType, " ------ %p the plugin did not request touch events", inst()); 167 break; 168 case kKey_ANPEventType: 169 gLogI.log(inst(), kError_ANPLogType, " ------ %p the plugin did not request key events", inst()); 170 break; 171 default: 172 break; 173 } 174 return 0; // unknown or unhandled event 175} 176 177/////////////////////////////////////////////////////////////////////////////// 178// LOGGING TESTS 179/////////////////////////////////////////////////////////////////////////////// 180 181 182void BackgroundPlugin::test_logging() { 183 NPP instance = this->inst(); 184 185 //LOG_ERROR(instance, " ------ %p Testing Log Error", instance); 186 gLogI.log(instance, kError_ANPLogType, " ------ %p Testing Log Error", instance); 187 gLogI.log(instance, kWarning_ANPLogType, " ------ %p Testing Log Warning", instance); 188 gLogI.log(instance, kDebug_ANPLogType, " ------ %p Testing Log Debug", instance); 189} 190 191/////////////////////////////////////////////////////////////////////////////// 192// TIMER TESTS 193/////////////////////////////////////////////////////////////////////////////// 194 195#define TIMER_INTERVAL 50 196static void timer_oneshot(NPP instance, uint32 timerID); 197static void timer_repeat(NPP instance, uint32 timerID); 198static void timer_neverfires(NPP instance, uint32 timerID); 199static void timer_latency(NPP instance, uint32 timerID); 200 201void BackgroundPlugin::test_timers() { 202 NPP instance = this->inst(); 203 204 //Setup the testing counters 205 mTimerRepeatCount = 5; 206 mTimerLatencyCount = 5; 207 208 // test for bogus timerID 209 browser->unscheduletimer(instance, 999999); 210 // test one-shot 211 browser->scheduletimer(instance, 100, false, timer_oneshot); 212 // test repeat 213 browser->scheduletimer(instance, 50, true, timer_repeat); 214 // test timer latency 215 browser->scheduletimer(instance, TIMER_INTERVAL, true, timer_latency); 216 mStartTime = mPrevTime = getMSecs(); 217 // test unschedule immediately 218 uint32 id = browser->scheduletimer(instance, 100, false, timer_neverfires); 219 browser->unscheduletimer(instance, id); 220 // test double unschedule (should be no-op) 221 browser->unscheduletimer(instance, id); 222 223} 224 225static void timer_oneshot(NPP instance, uint32 timerID) { 226 gLogI.log(instance, kDebug_ANPLogType, "-------- oneshot timer\n"); 227} 228 229static void timer_repeat(NPP instance, uint32 timerID) { 230 BackgroundPlugin *obj = ((BackgroundPlugin*) ((PluginObject*) instance->pdata)->activePlugin); 231 232 gLogI.log(instance, kDebug_ANPLogType, "-------- repeat timer %d\n", 233 obj->mTimerRepeatCount); 234 if (--obj->mTimerRepeatCount == 0) { 235 browser->unscheduletimer(instance, timerID); 236 } 237} 238 239static void timer_neverfires(NPP instance, uint32 timerID) { 240 gLogI.log(instance, kError_ANPLogType, "-------- timer_neverfires!!!\n"); 241} 242 243static void timer_latency(NPP instance, uint32 timerID) { 244 BackgroundPlugin *obj = ((BackgroundPlugin*) ((PluginObject*) instance->pdata)->activePlugin); 245 246 obj->mTimerLatencyCurrentCount += 1; 247 248 uint32_t now = getMSecs(); 249 uint32_t interval = now - obj->mPrevTime; 250 uint32_t dur = now - obj->mStartTime; 251 uint32_t expectedDur = obj->mTimerLatencyCurrentCount * TIMER_INTERVAL; 252 int32_t drift = dur - expectedDur; 253 int32_t avgDrift = drift / obj->mTimerLatencyCurrentCount; 254 255 obj->mPrevTime = now; 256 257 gLogI.log(instance, kDebug_ANPLogType, 258 "-------- latency test: [%3d] interval %d expected %d, total %d expected %d, drift %d avg %d\n", 259 obj->mTimerLatencyCurrentCount, interval, TIMER_INTERVAL, dur, 260 expectedDur, drift, avgDrift); 261 262 if (--obj->mTimerLatencyCount == 0) { 263 browser->unscheduletimer(instance, timerID); 264 } 265} 266 267/////////////////////////////////////////////////////////////////////////////// 268// BITMAP TESTS 269/////////////////////////////////////////////////////////////////////////////// 270 271static void test_formats(NPP instance); 272 273void BackgroundPlugin::test_bitmaps() { 274 test_formats(this->inst()); 275} 276 277static void test_formats(NPP instance) { 278 279 // TODO pull names from enum in npapi instead of hardcoding them 280 static const struct { 281 ANPBitmapFormat fFormat; 282 const char* fName; 283 } gRecs[] = { 284 { kUnknown_ANPBitmapFormat, "unknown" }, 285 { kRGBA_8888_ANPBitmapFormat, "8888" }, 286 { kRGB_565_ANPBitmapFormat, "565" }, 287 }; 288 289 ANPPixelPacking packing; 290 for (size_t i = 0; i < ARRAY_COUNT(gRecs); i++) { 291 if (gBitmapI.getPixelPacking(gRecs[i].fFormat, &packing)) { 292 gLogI.log(instance, kDebug_ANPLogType, 293 "pixel format [%d] %s has packing ARGB [%d %d] [%d %d] [%d %d] [%d %d]\n", 294 gRecs[i].fFormat, gRecs[i].fName, 295 packing.AShift, packing.ABits, 296 packing.RShift, packing.RBits, 297 packing.GShift, packing.GBits, 298 packing.BShift, packing.BBits); 299 } else { 300 gLogI.log(instance, kDebug_ANPLogType, 301 "pixel format [%d] %s has no packing\n", 302 gRecs[i].fFormat, gRecs[i].fName); 303 } 304 } 305} 306 307void BackgroundPlugin::test_bitmap_transparency(const ANPEvent* evt) { 308 NPP instance = this->inst(); 309 310 // check default & set transparent 311 if (!mFinishedStageOne) { 312 313 gLogI.log(instance, kDebug_ANPLogType, "BEGIN: testing bitmap transparency"); 314 315 //check to make sure it is not transparent 316 if (evt->data.draw.data.bitmap.format == kRGBA_8888_ANPBitmapFormat) { 317 gLogI.log(instance, kError_ANPLogType, "bitmap default format is transparent"); 318 } 319 320 //make it transparent (any non-null value will set it to true) 321 bool value = true; 322 NPError err = browser->setvalue(instance, NPPVpluginTransparentBool, &value); 323 if (err != NPERR_NO_ERROR) { 324 gLogI.log(instance, kError_ANPLogType, "Error setting transparency."); 325 } 326 327 mFinishedStageOne = true; 328 browser->invalidaterect(instance, NULL); 329 } 330 // check transparent & set opaque 331 else if (!mFinishedStageTwo) { 332 333 //check to make sure it is transparent 334 if (evt->data.draw.data.bitmap.format != kRGBA_8888_ANPBitmapFormat) { 335 gLogI.log(instance, kError_ANPLogType, "bitmap did not change to transparent format"); 336 } 337 338 //make it opaque 339 NPError err = browser->setvalue(instance, NPPVpluginTransparentBool, NULL); 340 if (err != NPERR_NO_ERROR) { 341 gLogI.log(instance, kError_ANPLogType, "Error setting transparency."); 342 } 343 344 mFinishedStageTwo = true; 345 } 346 // check opaque 347 else if (!mFinishedStageThree) { 348 349 //check to make sure it is not transparent 350 if (evt->data.draw.data.bitmap.format == kRGBA_8888_ANPBitmapFormat) { 351 gLogI.log(instance, kError_ANPLogType, "bitmap default format is transparent"); 352 } 353 354 gLogI.log(instance, kDebug_ANPLogType, "END: testing bitmap transparency"); 355 356 mFinishedStageThree = true; 357 } 358} 359 360/////////////////////////////////////////////////////////////////////////////// 361// DOM TESTS 362/////////////////////////////////////////////////////////////////////////////// 363 364void BackgroundPlugin::test_domAccess() { 365 NPP instance = this->inst(); 366 367 gLogI.log(instance, kDebug_ANPLogType, " ------ %p Testing DOM Access", instance); 368 369 // Get the plugin's DOM object 370 NPObject* windowObject = NULL; 371 browser->getvalue(instance, NPNVWindowNPObject, &windowObject); 372 373 if (!windowObject) 374 gLogI.log(instance, kError_ANPLogType, " ------ %p Unable to retrieve DOM Window", instance); 375 376 // Retrieve a property from the plugin's DOM object 377 NPIdentifier topIdentifier = browser->getstringidentifier("top"); 378 NPVariant topObjectVariant; 379 browser->getproperty(instance, windowObject, topIdentifier, &topObjectVariant); 380 381 if (topObjectVariant.type != NPVariantType_Object) 382 gLogI.log(instance, kError_ANPLogType, " ------ %p Invalid Variant type for DOM Property: %d,%d", instance, topObjectVariant.type, NPVariantType_Object); 383} 384 385 386/////////////////////////////////////////////////////////////////////////////// 387// JAVASCRIPT TESTS 388/////////////////////////////////////////////////////////////////////////////// 389 390 391void BackgroundPlugin::test_javascript() { 392 NPP instance = this->inst(); 393 394 gLogI.log(instance, kDebug_ANPLogType, " ------ %p Testing JavaScript Access", instance); 395 396 // Get the plugin's DOM object 397 NPObject* windowObject = NULL; 398 browser->getvalue(instance, NPNVWindowNPObject, &windowObject); 399 400 if (!windowObject) 401 gLogI.log(instance, kError_ANPLogType, " ------ %p Unable to retrieve DOM Window", instance); 402 403 // create a string (JS code) that is stored in memory allocated by the browser 404 const char* jsString = "1200 + 34"; 405 void* stringMem = browser->memalloc(strlen(jsString)); 406 memcpy(stringMem, jsString, strlen(jsString)); 407 408 // execute the javascript in the plugin's DOM object 409 NPString script = { (char*)stringMem, strlen(jsString) }; 410 NPVariant scriptVariant; 411 if (!browser->evaluate(instance, windowObject, &script, &scriptVariant)) 412 gLogI.log(instance, kError_ANPLogType, " ------ %p Unable to eval the JS.", instance); 413 414 if (scriptVariant.type == NPVariantType_Int32) { 415 if (scriptVariant.value.intValue != 1234) 416 gLogI.log(instance, kError_ANPLogType, " ------ %p Invalid Value for JS Return: %d,1234", instance, scriptVariant.value.intValue); 417 } else { 418 gLogI.log(instance, kError_ANPLogType, " ------ %p Invalid Variant type for JS Return: %d,%d", instance, scriptVariant.type, NPVariantType_Int32); 419 } 420 421 // free the memory allocated within the browser 422 browser->memfree(stringMem); 423} 424