Update Animation Plugin to use OpenGL.
Change-Id: Ib20fa69422a0dae804c95f71b70a65027ad8e9cf
diff --git a/samples/BrowserPlugin/jni/animation/AnimationPlugin.cpp b/samples/BrowserPlugin/jni/animation/AnimationPlugin.cpp
index 6e93fb6..58999ad 100644
--- a/samples/BrowserPlugin/jni/animation/AnimationPlugin.cpp
+++ b/samples/BrowserPlugin/jni/animation/AnimationPlugin.cpp
@@ -33,6 +33,7 @@
extern ANPCanvasInterfaceV0 gCanvasI;
extern ANPPaintInterfaceV0 gPaintI;
extern ANPPathInterfaceV0 gPathI;
+extern ANPSystemInterfaceV0 gSystemI;
extern ANPWindowInterfaceV0 gWindowI;
static uint16_t rnd16(float x, int inset) {
@@ -43,118 +44,74 @@
return static_cast<uint16_t>(ix);
}
-static void inval(NPP instance, const ANPRectF& r, bool doAA) {
- const int inset = doAA ? -1 : 0;
-
- NPRect inval;
- inval.left = rnd16(r.left, inset);
- inval.top = rnd16(r.top, inset);
- inval.right = rnd16(r.right, -inset);
- inval.bottom = rnd16(r.bottom, -inset);
- browser->invalidaterect(instance, &inval);
-}
-
-static void bounce(float* x, float* dx, const float max) {
- *x += *dx;
- if (*x < 0) {
- *x = 0;
- if (*dx < 0) {
- *dx = -*dx;
- }
- } else if (*x > max) {
- *x = max;
- if (*dx > 0) {
- *dx = -*dx;
- }
- }
-}
///////////////////////////////////////////////////////////////////////////////
-BallAnimation::BallAnimation(NPP inst) : SubPlugin(inst) {
- m_x = m_y = 0;
- m_dx = 7 * SCALE;
- m_dy = 5 * SCALE;
-
- memset(&m_oval, 0, sizeof(m_oval));
-
- m_paint = gPaintI.newPaint();
- gPaintI.setFlags(m_paint, gPaintI.getFlags(m_paint) | kAntiAlias_ANPPaintFlag);
- gPaintI.setColor(m_paint, 0xFFFF0000);
-
+BallAnimation::BallAnimation(NPP inst) : SurfaceSubPlugin(inst) {
//register for touch events
ANPEventFlags flags = kTouch_ANPEventFlag;
NPError err = browser->setvalue(inst, kAcceptEvents_ANPSetValue, &flags);
if (err != NPERR_NO_ERROR) {
gLogI.log(kError_ANPLogType, "Error selecting input events.");
}
+
+ gLogI.log(kError_ANPLogType, "Starting Rendering Thread");
+
+ //start a thread and do your drawing there
+ m_renderingThread = new AnimationThread(inst);
+ m_renderingThread->incStrong(inst);
+ m_renderingThread->run("AnimationThread");
}
BallAnimation::~BallAnimation() {
- gPaintI.deletePaint(m_paint);
+ m_renderingThread->requestExitAndWait();
+ destroySurface();
}
bool BallAnimation::supportsDrawingModel(ANPDrawingModel model) {
- return (model == kBitmap_ANPDrawingModel);
+ return (model == kOpenGL_ANPDrawingModel);
}
-void BallAnimation::drawPlugin(const ANPBitmap& bitmap, const ANPRectI& clip) {
+jobject BallAnimation::getSurface() {
- // create a canvas
- ANPCanvas* canvas = gCanvasI.newCanvas(&bitmap);
-
- // clip the canvas
- ANPRectF clipR;
- clipR.left = clip.left;
- clipR.top = clip.top;
- clipR.right = clip.right;
- clipR.bottom = clip.bottom;
- gCanvasI.clipRect(canvas, &clipR);
-
- // setup variables
- PluginObject *obj = (PluginObject*) inst()->pdata;
- const float OW = 20;
- const float OH = 20;
- const int W = obj->window->width;
- const int H = obj->window->height;
-
- // paint the canvas (using the path API)
- gCanvasI.drawColor(canvas, 0xFFFFFFFF);
- {
- ANPPath* path = gPathI.newPath();
-
- float cx = W * 0.5f;
- float cy = H * 0.5f;
- gPathI.moveTo(path, 0, 0);
- gPathI.quadTo(path, cx, cy, W, 0);
- gPathI.quadTo(path, cx, cy, W, H);
- gPathI.quadTo(path, cx, cy, 0, H);
- gPathI.quadTo(path, cx, cy, 0, 0);
-
- gPaintI.setColor(m_paint, 0xFF0000FF);
- gCanvasI.drawPath(canvas, path, m_paint);
-
- ANPRectF bounds;
- memset(&bounds, 0, sizeof(bounds));
- gPathI.getBounds(path, &bounds);
- gPathI.deletePath(path);
+ if (m_surface) {
+ return m_surface;
}
- // draw the oval
- inval(inst(), m_oval, true); // inval the old
- m_oval.left = m_x;
- m_oval.top = m_y;
- m_oval.right = m_x + OW;
- m_oval.bottom = m_y + OH;
- inval(inst(), m_oval, true); // inval the new
- gPaintI.setColor(m_paint, 0xFFFF0000);
- gCanvasI.drawOval(canvas, &m_oval, m_paint);
+ // load the appropriate java class and instantiate it
+ JNIEnv* env = NULL;
+ if (gVM->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
+ gLogI.log(kError_ANPLogType, " ---- getSurface: failed to get env");
+ return NULL;
+ }
- // update the coordinates of the oval
- bounce(&m_x, &m_dx, obj->window->width - OW);
- bounce(&m_y, &m_dy, obj->window->height - OH);
+ const char* className = "com.android.sampleplugin.AnimationSurface";
+ jclass fullScreenClass = gSystemI.loadJavaClass(inst(), className);
- // delete the canvas
- gCanvasI.deleteCanvas(canvas);
+ if(!fullScreenClass) {
+ gLogI.log(kError_ANPLogType, " ---- getSurface: failed to load class");
+ return NULL;
+ }
+
+ jmethodID constructor = env->GetMethodID(fullScreenClass, "<init>", "(Landroid/content/Context;)V");
+ jobject fullScreenSurface = env->NewObject(fullScreenClass, constructor, m_context);
+
+ if(!fullScreenSurface) {
+ gLogI.log(kError_ANPLogType, " ---- getSurface: failed to construct object");
+ return NULL;
+ }
+
+ gLogI.log(kError_ANPLogType, " ---- object %p", fullScreenSurface);
+
+ m_surface = env->NewGlobalRef(fullScreenSurface);
+ return m_surface;
+}
+
+void BallAnimation::destroySurface() {
+ JNIEnv* env = NULL;
+ if (m_surface && gVM->GetEnv((void**) &env, JNI_VERSION_1_4) == JNI_OK) {
+ env->DeleteGlobalRef(m_surface);
+ m_surface = NULL;
+ }
}
void BallAnimation::showEntirePluginOnScreen() {
@@ -179,17 +136,26 @@
switch (evt->eventType) {
case kDraw_ANPEventType:
switch (evt->data.draw.model) {
- case kBitmap_ANPDrawingModel:
- drawPlugin(evt->data.draw.data.bitmap, evt->data.draw.clip);
+ case kOpenGL_ANPDrawingModel: {
+ //send the width and height to the rendering thread
+ int width = evt->data.draw.data.surface.width;
+ int height = evt->data.draw.data.surface.height;
+ gLogI.log(kError_ANPLogType, "New Dimensions (%d,%d)", width, height);
+ m_renderingThread->setDimensions(width, height);
return 1;
+ }
default:
- break; // unknown drawing model
+ return 0; // unknown drawing model
}
case kTouch_ANPEventType:
if (kDown_ANPTouchAction == evt->data.touch.action) {
showEntirePluginOnScreen();
}
- return 1;
+ else if (kDoubleTap_ANPTouchAction == evt->data.touch.action) {
+ browser->geturl(inst(), "javascript:alert('Detected double tap event.')", 0);
+ gWindowI.requestFullScreen(inst());
+ }
+ return 1;
default:
break;
}
diff --git a/samples/BrowserPlugin/jni/animation/AnimationPlugin.h b/samples/BrowserPlugin/jni/animation/AnimationPlugin.h
index 4a5b4e8..870b67c 100644
--- a/samples/BrowserPlugin/jni/animation/AnimationPlugin.h
+++ b/samples/BrowserPlugin/jni/animation/AnimationPlugin.h
@@ -1,5 +1,5 @@
/*
- * Copyright 2008, The Android Open Source Project
+ * Copyright 2010, The Android Open Source Project
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -24,29 +24,25 @@
*/
#include "PluginObject.h"
+#include "AnimationThread.h"
#ifndef pluginGraphics__DEFINED
#define pluginGraphics__DEFINED
-class BallAnimation : public SubPlugin {
+class BallAnimation : public SurfaceSubPlugin {
public:
BallAnimation(NPP inst);
virtual ~BallAnimation();
virtual bool supportsDrawingModel(ANPDrawingModel);
virtual int16_t handleEvent(const ANPEvent* evt);
+
+ virtual jobject getSurface();
private:
- void drawPlugin(const ANPBitmap& bitmap, const ANPRectI& clip);
void showEntirePluginOnScreen();
+ void destroySurface();
- float m_x;
- float m_y;
- float m_dx;
- float m_dy;
-
- ANPRectF m_oval;
- ANPPaint* m_paint;
-
- static const float SCALE = 0.1;
+ jobject m_surface;
+ AnimationThread* m_renderingThread;
};
#endif // pluginGraphics__DEFINED
diff --git a/samples/BrowserPlugin/jni/animation/AnimationThread.cpp b/samples/BrowserPlugin/jni/animation/AnimationThread.cpp
new file mode 100644
index 0000000..9e6342d
--- /dev/null
+++ b/samples/BrowserPlugin/jni/animation/AnimationThread.cpp
@@ -0,0 +1,164 @@
+/*
+ * Copyright 2010, 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 "AnimationThread.h"
+#include "ANPOpenGL_npapi.h"
+
+#include <EGL/egl.h>
+#include <GLES2/gl2.h>
+#include <utils/SystemClock.h>
+
+extern ANPLogInterfaceV0 gLogI;
+extern ANPOpenGLInterfaceV0 gOpenGLI;
+
+AnimationThread::AnimationThread(NPP npp) : RenderingThread(npp) {
+ m_counter = 0;
+ m_lastPrintTime = android::uptimeMillis();
+ m_executionTime = 0;
+ m_idleTime = 0;
+
+ m_x = m_y = 0;
+ m_dx = 0;
+ m_dy = 0;
+
+ memset(&m_oval, 0, sizeof(m_oval));
+
+ m_paint = new SkPaint;
+ m_paint->setAntiAlias(true);
+
+ m_bitmap = constructBitmap(DEFAULT_WIDTH, DEFAULT_HEIGHT);
+ m_canvas = new SkCanvas(*m_bitmap);
+
+ m_startExecutionTime = 0;
+ m_startTime = android::uptimeMillis();
+}
+
+AnimationThread::~AnimationThread() {
+ delete m_paint;
+ delete m_canvas;
+ delete m_bitmap;
+}
+
+SkBitmap* AnimationThread::constructBitmap(int width, int height) {
+ SkBitmap* bitmap = new SkBitmap;
+ bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
+ bitmap->allocPixels();
+ bitmap->eraseColor(0x00000000);
+ return bitmap;
+}
+
+static void bounce(float* x, float* dx, const float max) {
+ *x += *dx;
+ if (*x < 0) {
+ *x = 0;
+ if (*dx < 0) {
+ *dx = -*dx;
+ }
+ } else if (*x > max) {
+ *x = max;
+ if (*dx > 0) {
+ *dx = -*dx;
+ }
+ }
+}
+
+bool AnimationThread::threadLoop() {
+
+ m_startIdleTime = android::uptimeMillis();
+
+ ANPTextureInfo textureInfo = gOpenGLI.lockTexture(m_npp);
+ GLuint textureId = textureInfo.textureId;
+
+ m_idleTime += android::uptimeMillis() - m_startIdleTime;
+ m_startExecutionTime = android::uptimeMillis();
+
+ int width, height;
+ getDimensions(width, height);
+
+ if (width <= 0)
+ width = DEFAULT_WIDTH;
+ if (height <= 0)
+ height = DEFAULT_HEIGHT;
+
+ if (m_bitmap->width() != width || m_bitmap->height() != height) {
+ delete m_canvas;
+ delete m_bitmap;
+ m_bitmap = constructBitmap(width, height);
+ m_canvas = new SkCanvas(*m_bitmap);
+
+ // change the ball's speed to match the size
+ m_dx = width * .005f;
+ m_dy = height * .007f;
+ }
+
+ // setup variables
+ const float OW = width * .125f;
+ const float OH = height * .125f;
+
+ // clear the old oval
+ m_bitmap->eraseColor(0x880000FF);
+
+ // update the coordinates of the oval
+ bounce(&m_x, &m_dx, width - OW);
+ bounce(&m_y, &m_dy, height - OH);
+
+ // draw the new oval
+ m_oval.fLeft = m_x;
+ m_oval.fTop = m_y;
+ m_oval.fRight = m_x + OW;
+ m_oval.fBottom = m_y + OH;
+ m_paint->setColor(0xAAFF0000);
+ m_canvas->drawOval(m_oval, *m_paint);
+
+ if (textureInfo.width == width && textureInfo.height == height) {
+ updateTextureWithBitmap(textureId, *m_bitmap);
+ } else {
+ createTextureWithBitmap(textureId, *m_bitmap);
+ textureInfo.width = width;
+ textureInfo.height = height;
+ textureInfo.internalFormat = GL_RGBA;
+ }
+
+ m_executionTime += android::uptimeMillis() - m_startExecutionTime;
+ m_counter++;
+
+ gOpenGLI.releaseTexture(m_npp, &textureInfo);
+
+ if (android::uptimeMillis() - m_lastPrintTime > 5000) {
+ float fps = m_counter / ((android::uptimeMillis() - m_startTime) / 1000);
+ float spf = ((android::uptimeMillis() - m_startTime)) / m_counter;
+ float lpf = (m_idleTime) / m_counter;
+ float exe = (m_executionTime) / m_counter;
+ gLogI.log(kError_ANPLogType, "TEXT: counter(%d) fps(%f) spf(%f) lock(%f) execution(%f)\n", (int)m_counter, fps, spf, lpf, exe);
+ m_lastPrintTime = android::uptimeMillis();
+
+ m_counter = 0;
+ m_executionTime = 0;
+ m_idleTime = 0;
+ m_startExecutionTime = 0;
+ m_startTime = android::uptimeMillis();
+ }
+
+ return true;
+}
diff --git a/samples/BrowserPlugin/jni/animation/AnimationThread.h b/samples/BrowserPlugin/jni/animation/AnimationThread.h
new file mode 100644
index 0000000..4f74e94
--- /dev/null
+++ b/samples/BrowserPlugin/jni/animation/AnimationThread.h
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2010, 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 "RenderingThread.h"
+#include "SkCanvas.h"
+#include "SkBitmap.h"
+#include "SkRect.h"
+#include "SkPaint.h"
+
+#ifndef AnimationThread__DEFINED
+#define AnimationThread__DEFINED
+
+class AnimationThread : public RenderingThread {
+public:
+ AnimationThread(NPP npp);
+ virtual ~AnimationThread();
+
+private:
+ virtual bool threadLoop();
+ SkBitmap* constructBitmap(int width, int height);
+
+ float m_counter;
+
+ int64_t m_lastPrintTime;
+ int64_t m_executionTime;
+ int64_t m_idleTime;
+ int64_t m_startTime;
+ int64_t m_startExecutionTime;
+ int64_t m_startIdleTime;
+
+ float m_x;
+ float m_y;
+ float m_dx;
+ float m_dy;
+
+ SkRect m_oval;
+ SkPaint* m_paint;
+ SkBitmap* m_bitmap;
+ SkCanvas* m_canvas;
+
+ static const unsigned int DEFAULT_WIDTH = 400;
+ static const unsigned int DEFAULT_HEIGHT = 400;
+};
+
+
+
+#endif // AnimationThread__DEFINED