From d049ec168bd86df7d448d7ff86fdc36acb4be713 Mon Sep 17 00:00:00 2001 From: Derek Sollenberger Date: Fri, 7 Aug 2009 11:26:44 -0400 Subject: [PATCH] updating the sample plugin to reflect the latest changes in android_npapi.h --- samples/BrowserPlugin/jni/Android.mk | 2 - samples/BrowserPlugin/jni/PluginObject.h | 1 - .../jni/background/BackgroundPlugin.cpp | 115 ++++++++++++++------- .../jni/background/BackgroundPlugin.h | 6 +- samples/BrowserPlugin/jni/form/FormPlugin.cpp | 1 + samples/BrowserPlugin/jni/main.cpp | 5 - samples/BrowserPlugin/jni/paint/PaintPlugin.cpp | 15 ++- .../BrowserPlugin/jni/surface/SurfacePlugin.cpp | 88 ---------------- samples/BrowserPlugin/jni/surface/SurfacePlugin.h | 42 -------- 9 files changed, 95 insertions(+), 180 deletions(-) delete mode 100644 samples/BrowserPlugin/jni/surface/SurfacePlugin.cpp delete mode 100644 samples/BrowserPlugin/jni/surface/SurfacePlugin.h diff --git a/samples/BrowserPlugin/jni/Android.mk b/samples/BrowserPlugin/jni/Android.mk index c3acbd82..3d1c88f4 100644 --- a/samples/BrowserPlugin/jni/Android.mk +++ b/samples/BrowserPlugin/jni/Android.mk @@ -35,7 +35,6 @@ LOCAL_SRC_FILES := \ background/BackgroundPlugin.cpp \ form/FormPlugin.cpp \ paint/PaintPlugin.cpp \ - surface/SurfacePlugin.cpp \ LOCAL_C_INCLUDES += \ $(LOCAL_PATH) \ @@ -44,7 +43,6 @@ LOCAL_C_INCLUDES += \ $(LOCAL_PATH)/background \ $(LOCAL_PATH)/form \ $(LOCAL_PATH)/paint \ - $(LOCAL_PATH)/surface \ external/webkit/WebCore/bridge \ external/webkit/WebCore/plugins \ external/webkit/WebCore/platform/android/JavaVM \ diff --git a/samples/BrowserPlugin/jni/PluginObject.h b/samples/BrowserPlugin/jni/PluginObject.h index 248a09b0..82f6f481 100644 --- a/samples/BrowserPlugin/jni/PluginObject.h +++ b/samples/BrowserPlugin/jni/PluginObject.h @@ -56,7 +56,6 @@ enum PluginTypes { kForm_PluginType = 4, kText_PluginType = 5, kPaint_PluginType = 6, - kSurface_PluginType = 7, }; typedef uint32_t PluginType; diff --git a/samples/BrowserPlugin/jni/background/BackgroundPlugin.cpp b/samples/BrowserPlugin/jni/background/BackgroundPlugin.cpp index 4ca79ed0..548f0e58 100644 --- a/samples/BrowserPlugin/jni/background/BackgroundPlugin.cpp +++ b/samples/BrowserPlugin/jni/background/BackgroundPlugin.cpp @@ -34,10 +34,10 @@ extern NPNetscapeFuncs* browser; extern ANPBitmapInterfaceV0 gBitmapI; -extern ANPLogInterfaceV0 gLogI; extern ANPCanvasInterfaceV0 gCanvasI; +extern ANPLogInterfaceV0 gLogI; extern ANPPaintInterfaceV0 gPaintI; -extern ANPPathInterfaceV0 gPathI; +extern ANPSurfaceInterfaceV0 gSurfaceI; extern ANPTypefaceInterfaceV0 gTypefaceI; extern uint32_t getMSecs(); @@ -50,16 +50,13 @@ extern uint32_t getMSecs(); BackgroundPlugin::BackgroundPlugin(NPP inst) : SubPlugin(inst) { - m_paint = gPaintI.newPaint(); - gPaintI.setFlags(m_paint, gPaintI.getFlags(m_paint) | kAntiAlias_ANPPaintFlag); - gPaintI.setColor(m_paint, 0xFFFF0000); - gPaintI.setTextSize(m_paint, 16); - - ANPTypeface* tf = gTypefaceI.createFromName("serif", kItalic_ANPTypefaceStyle); - gPaintI.setTypeface(m_paint, tf); - gTypefaceI.unref(tf); + // initialize the drawing surface + m_surfaceReady = false; + m_surface = gSurfaceI.newSurface(inst, kRGBA_ANPSurfaceType, false); + if(!m_surface) + gLogI.log(inst, kError_ANPLogType, "----%p Unable to create RGBA surface", inst); - //initialize variables + //initialize bitmap transparency variables mFinishedStageOne = false; mFinishedStageTwo = false; mFinishedStageThree = false; @@ -73,55 +70,97 @@ BackgroundPlugin::BackgroundPlugin(NPP inst) : SubPlugin(inst) { } BackgroundPlugin::~BackgroundPlugin() { + gSurfaceI.deleteSurface(m_surface); } bool BackgroundPlugin::supportsDrawingModel(ANPDrawingModel model) { - return (model == kBitmap_ANPDrawingModel); + return (model == kSurface_ANPDrawingModel); } -void BackgroundPlugin::drawPlugin(const ANPBitmap& bitmap, const ANPRectI& clip) { - ANPCanvas* canvas = gCanvasI.newCanvas(&bitmap); +void BackgroundPlugin::drawPlugin(int surfaceWidth, int surfaceHeight) { - ANPRectF clipR; - clipR.left = clip.left; - clipR.top = clip.top; - clipR.right = clip.right; - clipR.bottom = clip.bottom; - gCanvasI.clipRect(canvas, &clipR); + // get the plugin's dimensions according to the DOM + PluginObject *obj = (PluginObject*) inst()->pdata; + const int W = obj->window->width; + const int H = obj->window->height; - draw(canvas); - gCanvasI.deleteCanvas(canvas); -} + // compute the current zoom level + const float zoomFactorW = static_cast(surfaceWidth) / W; + const float zoomFactorH = static_cast(surfaceHeight) / H; + + // check to make sure the zoom level is uniform + if (zoomFactorW + .01 < zoomFactorH && zoomFactorW - .01 > zoomFactorH) + gLogI.log(inst(), kError_ANPLogType, " ------ %p zoom is out of sync (%f,%f)", + inst(), zoomFactorW, zoomFactorH); -void BackgroundPlugin::draw(ANPCanvas* canvas) { + // scale the variables based on the zoom level + const int fontSize = (int)(zoomFactorW * 16); + const int leftMargin = (int)(zoomFactorW * 10); + // lock the surface + ANPBitmap bitmap; + if (!m_surfaceReady || !gSurfaceI.lock(m_surface, &bitmap, NULL)) { + gLogI.log(inst(), kError_ANPLogType, " ------ %p unable to lock the plugin", inst()); + return; + } + + // create a canvas + ANPCanvas* canvas = gCanvasI.newCanvas(&bitmap); gCanvasI.drawColor(canvas, 0xFFFFFFFF); + ANPPaint* paint = gPaintI.newPaint(); + gPaintI.setFlags(paint, gPaintI.getFlags(paint) | kAntiAlias_ANPPaintFlag); + gPaintI.setColor(paint, 0xFFFF0000); + gPaintI.setTextSize(paint, fontSize); + + ANPTypeface* tf = gTypefaceI.createFromName("serif", kItalic_ANPTypefaceStyle); + gPaintI.setTypeface(paint, tf); + gTypefaceI.unref(tf); + ANPFontMetrics fm; - gPaintI.getFontMetrics(m_paint, &fm); + gPaintI.getFontMetrics(paint, &fm); - gPaintI.setColor(m_paint, 0xFF0000FF); + gPaintI.setColor(paint, 0xFF0000FF); const char c[] = "This is a background plugin."; - gCanvasI.drawText(canvas, c, sizeof(c)-1, 10, -fm.fTop, m_paint); + gCanvasI.drawText(canvas, c, sizeof(c)-1, leftMargin, -fm.fTop, paint); + + // clean up variables and unlock the surface + gPaintI.deletePaint(paint); + gCanvasI.deleteCanvas(canvas); + gSurfaceI.unlock(m_surface); } int16 BackgroundPlugin::handleEvent(const ANPEvent* evt) { - NPP instance = this->inst(); - switch (evt->eventType) { case kDraw_ANPEventType: - switch (evt->data.draw.model) { - case kBitmap_ANPDrawingModel: - test_bitmap_transparency(evt); - drawPlugin(evt->data.draw.data.bitmap, evt->data.draw.clip); - return 1; - default: - break; // unknown drawing model + gLogI.log(inst(), kError_ANPLogType, " ------ %p the plugin did not request draw events", inst()); + break; + case kSurface_ANPEventType: + switch (evt->data.surface.action) { + case kCreated_ANPSurfaceAction: + m_surfaceReady = true; + return 1; + case kDestroyed_ANPSurfaceAction: + m_surfaceReady = false; + return 1; + case kChanged_ANPSurfaceAction: + drawPlugin(evt->data.surface.data.changed.width, + evt->data.surface.data.changed.height); + return 1; + } + break; + case kLifecycle_ANPEventType: + if (evt->data.lifecycle.action == kOnLoad_ANPLifecycleAction) { + gLogI.log(inst(), kDebug_ANPLogType, " ------ %p the plugin received an onLoad event", inst()); + return 1; } + break; case kTouch_ANPEventType: - gLogI.log(instance, kError_ANPLogType, " ------ %p the plugin did not request touch events", instance); + gLogI.log(inst(), kError_ANPLogType, " ------ %p the plugin did not request touch events", inst()); + break; case kKey_ANPEventType: - gLogI.log(instance, kError_ANPLogType, " ------ %p the plugin did not request key events", instance); + gLogI.log(inst(), kError_ANPLogType, " ------ %p the plugin did not request key events", inst()); + break; default: break; } diff --git a/samples/BrowserPlugin/jni/background/BackgroundPlugin.h b/samples/BrowserPlugin/jni/background/BackgroundPlugin.h index 0d7389bc..ed428b5c 100644 --- a/samples/BrowserPlugin/jni/background/BackgroundPlugin.h +++ b/samples/BrowserPlugin/jni/background/BackgroundPlugin.h @@ -48,10 +48,10 @@ public: bool mFinishedStageThree; // check opaque private: - void draw(ANPCanvas*); - void drawPlugin(const ANPBitmap& bitmap, const ANPRectI& clip); + void drawPlugin(int surfaceWidth, int surfaceHeight); - ANPPaint* m_paint; + bool m_surfaceReady; + ANPSurface* m_surface; void test_logging(); void test_timers(); diff --git a/samples/BrowserPlugin/jni/form/FormPlugin.cpp b/samples/BrowserPlugin/jni/form/FormPlugin.cpp index 589550af..a92d447a 100644 --- a/samples/BrowserPlugin/jni/form/FormPlugin.cpp +++ b/samples/BrowserPlugin/jni/form/FormPlugin.cpp @@ -303,6 +303,7 @@ bool FormPlugin::handleNavigation(ANPKeyCode keyCode) { gLogI.log(instance, kDebug_ANPLogType, "----%p Recvd Nav Key %d", instance, keyCode); if (!m_activeInput) { + gWindowI.showKeyboard(instance, true); switchActiveInput(&m_usernameInput); } else if (m_activeInput == &m_usernameInput) { diff --git a/samples/BrowserPlugin/jni/main.cpp b/samples/BrowserPlugin/jni/main.cpp index 119081c6..b5aea95a 100644 --- a/samples/BrowserPlugin/jni/main.cpp +++ b/samples/BrowserPlugin/jni/main.cpp @@ -33,7 +33,6 @@ #include "BackgroundPlugin.h" #include "FormPlugin.h" #include "PaintPlugin.h" -#include "SurfacePlugin.h" #include "android_npapi.h" NPNetscapeFuncs* browser; @@ -213,10 +212,6 @@ NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc, obj->pluginType = kPaint_PluginType; obj->activePlugin = new PaintPlugin(instance); } - else if (!strcmp(argv[i], "RGBA_Surface")) { - obj->pluginType = kSurface_PluginType; - obj->activePlugin = new SurfacePlugin(instance, kRGBA_ANPSurfaceType); - } gLogI.log(instance, kDebug_ANPLogType, "------ %p PluginType is %d", instance, obj->pluginType); break; } diff --git a/samples/BrowserPlugin/jni/paint/PaintPlugin.cpp b/samples/BrowserPlugin/jni/paint/PaintPlugin.cpp index ba5257cf..62cb079b 100644 --- a/samples/BrowserPlugin/jni/paint/PaintPlugin.cpp +++ b/samples/BrowserPlugin/jni/paint/PaintPlugin.cpp @@ -52,7 +52,7 @@ PaintPlugin::PaintPlugin(NPP inst) : SubPlugin(inst) { // initialize the drawing surface m_surfaceReady = false; - m_surface = gSurfaceI.newSurface(inst, kRGBA_ANPSurfaceType); + m_surface = gSurfaceI.newSurface(inst, kRGBA_ANPSurfaceType, true); if(!m_surface) gLogI.log(inst, kError_ANPLogType, "----%p Unable to create RGBA surface", inst); @@ -219,6 +219,19 @@ int16 PaintPlugin::handleEvent(const ANPEvent* evt) { case kDestroyed_ANPSurfaceAction: m_surfaceReady = false; return 1; + case kChanged_ANPSurfaceAction: + // get the plugin's dimensions according to the DOM + PluginObject *obj = (PluginObject*) inst()->pdata; + const int pW = obj->window->width; + const int pH = obj->window->height; + // get the plugin's surface dimensions + const int sW = evt->data.surface.data.changed.width; + const int sH = evt->data.surface.data.changed.height; + if (pW != sW || pH != sH) + gLogI.log(inst(), kError_ANPLogType, + "----%p Invalid Surface Dimensions (%d,%d):(%d,%d)", + inst(), pW, pH, sW, sH); + return 1; } break; diff --git a/samples/BrowserPlugin/jni/surface/SurfacePlugin.cpp b/samples/BrowserPlugin/jni/surface/SurfacePlugin.cpp deleted file mode 100644 index b3be90a6..00000000 --- a/samples/BrowserPlugin/jni/surface/SurfacePlugin.cpp +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 2009, The Android Open Source Project - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "SurfacePlugin.h" - -#include -#include -#include -#include -#include - -extern NPNetscapeFuncs* browser; -extern ANPLogInterfaceV0 gLogI; -extern ANPPaintInterfaceV0 gPaintI; -extern ANPSurfaceInterfaceV0 gSurfaceI; -extern ANPTypefaceInterfaceV0 gTypefaceI; -extern ANPWindowInterfaceV0 gWindowI; - -/////////////////////////////////////////////////////////////////////////////// - -SurfacePlugin::SurfacePlugin(NPP inst, ANPSurfaceType surfaceType) : SubPlugin(inst) { - - m_surface = gSurfaceI.newSurface(inst, surfaceType); - - if(!m_surface) - gLogI.log(inst, kError_ANPLogType, "----%p Unable to create surface (%d)", inst, surfaceType); -} - -SurfacePlugin::~SurfacePlugin() { - if (m_surface) - gSurfaceI.deleteSurface(m_surface); -} - -bool SurfacePlugin::supportsDrawingModel(ANPDrawingModel model) { - return (model == kSurface_ANPDrawingModel); -} - -void SurfacePlugin::draw() { - NPP instance = this->inst(); - PluginObject *obj = (PluginObject*) instance->pdata; - - ANPBitmap bitmap; - - bool value = gSurfaceI.lock(m_surface, &bitmap, NULL); - gLogI.log(instance, kDebug_ANPLogType, "----%p locking: %b", instance, value); - gSurfaceI.unlock(m_surface); -} - -int16 SurfacePlugin::handleEvent(const ANPEvent* evt) { - NPP instance = this->inst(); - - switch (evt->eventType) { - case kDraw_ANPEventType: - switch (evt->data.draw.model) { - case kSurface_ANPDrawingModel: - if (m_surface) - draw(); - return 1; - default: - break; // unknown drawing model - } - default: - break; - } - return 0; // unknown or unhandled event -} diff --git a/samples/BrowserPlugin/jni/surface/SurfacePlugin.h b/samples/BrowserPlugin/jni/surface/SurfacePlugin.h deleted file mode 100644 index 349f8b10..00000000 --- a/samples/BrowserPlugin/jni/surface/SurfacePlugin.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2009, The Android Open Source Project - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "PluginObject.h" - -#ifndef surfacePlugin__DEFINED -#define surfacePlugin__DEFINED - -class SurfacePlugin : public SubPlugin { -public: - SurfacePlugin(NPP inst, ANPSurfaceType surfaceType); - virtual ~SurfacePlugin(); - virtual bool supportsDrawingModel(ANPDrawingModel); - virtual int16 handleEvent(const ANPEvent* evt); -private: - void draw(); - ANPSurface* m_surface; - -}; -#endif // surfacePlugin__DEFINED -- 2.11.0