Update Animation Plugin to use OpenGL.

Change-Id: Ib20fa69422a0dae804c95f71b70a65027ad8e9cf
diff --git a/samples/BrowserPlugin/jni/Android.mk b/samples/BrowserPlugin/jni/Android.mk
index d444bb0..b153e37 100644
--- a/samples/BrowserPlugin/jni/Android.mk
+++ b/samples/BrowserPlugin/jni/Android.mk
@@ -30,7 +30,9 @@
 LOCAL_SRC_FILES := \
 	main.cpp \
 	PluginObject.cpp \
+	RenderingThread.cpp \
 	animation/AnimationPlugin.cpp \
+	animation/AnimationThread.cpp \
 	audio/AudioPlugin.cpp \
 	background/BackgroundPlugin.cpp \
 	form/FormPlugin.cpp \
@@ -52,10 +54,16 @@
 	external/webkit/WebCore/bridge \
 	external/webkit/WebCore/plugins \
 	external/webkit/WebCore/platform/android/JavaVM \
-	external/webkit/WebKit/android/plugins
+	external/webkit/WebKit/android/plugins \
+	external/skia/include/core
 
 LOCAL_SHARED_LIBRARIES := \
-	libnativehelper
+	libnativehelper \
+	libutils \
+	libcutils \
+	libEGL \
+	libGLESv2 \
+	libskia
 
 LOCAL_CFLAGS += -fvisibility=hidden 
 LOCAL_PRELINK_MODULE:=false
diff --git a/samples/BrowserPlugin/jni/PluginObject.cpp b/samples/BrowserPlugin/jni/PluginObject.cpp
index dd0fbac..16925c8 100644
--- a/samples/BrowserPlugin/jni/PluginObject.cpp
+++ b/samples/BrowserPlugin/jni/PluginObject.cpp
@@ -45,6 +45,10 @@
     return obj->window->height;
 }
 
+SurfaceSubPlugin::~SurfaceSubPlugin() {
+    setContext(NULL);
+}
+
 bool SurfaceSubPlugin::supportsDrawingModel(ANPDrawingModel model) {
     return (model == kSurface_ANPDrawingModel);
 }
diff --git a/samples/BrowserPlugin/jni/PluginObject.h b/samples/BrowserPlugin/jni/PluginObject.h
index 296749a..e0f4424 100644
--- a/samples/BrowserPlugin/jni/PluginObject.h
+++ b/samples/BrowserPlugin/jni/PluginObject.h
@@ -63,7 +63,7 @@
 class SurfaceSubPlugin : public SubPlugin {
 public:
     SurfaceSubPlugin(NPP inst) : SubPlugin(inst) { m_context = NULL; }
-    virtual ~SurfaceSubPlugin() {}
+    virtual ~SurfaceSubPlugin();
     virtual jobject getSurface() = 0;
     virtual bool supportsDrawingModel(ANPDrawingModel);
 
diff --git a/samples/BrowserPlugin/jni/RenderingThread.cpp b/samples/BrowserPlugin/jni/RenderingThread.cpp
new file mode 100644
index 0000000..7f267ed
--- /dev/null
+++ b/samples/BrowserPlugin/jni/RenderingThread.cpp
@@ -0,0 +1,142 @@
+/*
+ * 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 "ANPOpenGL_npapi.h"
+
+extern ANPLogInterfaceV0       gLogI;
+extern ANPOpenGLInterfaceV0    gOpenGLI;
+
+RenderingThread::RenderingThread(NPP npp) : android::Thread() {
+    m_npp = npp;
+    m_width = -1;
+    m_height = -1;
+    gLogI.log(kError_ANPLogType, "Created Rendering Thread");
+}
+
+android::status_t RenderingThread::readyToRun() {
+
+    gLogI.log(kError_ANPLogType, "in ready to run");
+
+    EGLContext context = gOpenGLI.acquireContext(m_npp);
+
+    gLogI.log(kError_ANPLogType, "context: %p", context);
+
+    if (context == EGL_NO_CONTEXT) {
+        gLogI.log(kError_ANPLogType, "Unable to create EGLContext for a TextureProducer thread");
+        return android::UNKNOWN_ERROR;
+    }
+    return android::NO_ERROR;
+}
+
+void RenderingThread::setDimensions(int width, int height) {
+    android::Mutex::Autolock lock(m_sync);
+    m_width = width;
+    m_height = height;
+}
+
+void RenderingThread::getDimensions(int& width, int& height) {
+    android::Mutex::Autolock lock(m_sync);
+    width = m_width;
+    height = m_height;
+}
+
+void RenderingThread::printGLString(const char *name, GLenum s) {
+    const char *v = (const char *) glGetString(s);
+    gLogI.log(kError_ANPLogType, "GL %s = %s\n", name, v);
+}
+
+void RenderingThread::checkGlError(const char* op) {
+    for (GLint error = glGetError(); error; error
+            = glGetError()) {
+        gLogI.log(kError_ANPLogType, "after %s() glError (0x%x)\n", op, error);
+    }
+}
+
+GLenum RenderingThread::getInternalFormat(SkBitmap::Config config)
+{
+    switch(config) {
+        case SkBitmap::kA8_Config:
+            return GL_ALPHA;
+        case SkBitmap::kARGB_4444_Config:
+            return GL_RGBA;
+        case SkBitmap::kARGB_8888_Config:
+            return GL_RGBA;
+        case SkBitmap::kRGB_565_Config:
+            return GL_RGB;
+        default:
+            return -1;
+    }
+}
+
+GLenum RenderingThread::getType(SkBitmap::Config config)
+{
+    switch(config) {
+        case SkBitmap::kA8_Config:
+            return GL_UNSIGNED_BYTE;
+        case SkBitmap::kARGB_4444_Config:
+            return GL_UNSIGNED_SHORT_4_4_4_4;
+        case SkBitmap::kARGB_8888_Config:
+            return GL_UNSIGNED_BYTE;
+        case SkBitmap::kIndex8_Config:
+            return -1; // No type for compressed data.
+        case SkBitmap::kRGB_565_Config:
+            return GL_UNSIGNED_SHORT_5_6_5;
+        default:
+            return -1;
+    }
+}
+
+void RenderingThread::createTextureWithBitmap(GLuint texture, SkBitmap& bitmap) {
+    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+    glBindTexture(GL_TEXTURE_2D, texture);
+    checkGlError("glBindTexture");
+    SkBitmap::Config config = bitmap.getConfig();
+    int internalformat = getInternalFormat(config);
+    int type = getType(config);
+    bitmap.lockPixels();
+    glTexImage2D(GL_TEXTURE_2D, 0, internalformat, bitmap.width(), bitmap.height(),
+                 0, internalformat, type, bitmap.getPixels());
+    bitmap.unlockPixels();
+    checkGlError("glTexImage2D");
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+}
+
+void RenderingThread::updateTextureWithBitmap(GLuint texture, SkBitmap& bitmap) {
+    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+    glBindTexture(GL_TEXTURE_2D, texture);
+    checkGlError("glBindTexture");
+    SkBitmap::Config config = bitmap.getConfig();
+    int internalformat = getInternalFormat(config);
+    int type = getType(config);
+    bitmap.lockPixels();
+    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap.width(), bitmap.height(),
+                    internalformat, type, bitmap.getPixels());
+    bitmap.unlockPixels();
+    checkGlError("glTexSubImage2D");
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+}
diff --git a/samples/BrowserPlugin/jni/RenderingThread.h b/samples/BrowserPlugin/jni/RenderingThread.h
new file mode 100644
index 0000000..41f0ce8
--- /dev/null
+++ b/samples/BrowserPlugin/jni/RenderingThread.h
@@ -0,0 +1,66 @@
+/*
+ * 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 "android_npapi.h"
+#include "SkCanvas.h"
+#include "SkBitmap.h"
+
+#include <EGL/egl.h>
+#include <GLES2/gl2.h>
+
+#ifndef RenderingThread__DEFINED
+#define RenderingThread__DEFINED
+
+
+class RenderingThread : public android::Thread {
+public:
+    RenderingThread(NPP npp);
+    virtual ~RenderingThread() {};
+    virtual android::status_t readyToRun();
+
+    void setDimensions(int width, int height);
+    void getDimensions(int& width, int& height);
+
+protected:
+    NPP m_npp;
+
+    static void printGLString(const char *name, GLenum s);
+    static void checkGlError(const char* op);
+    static GLenum getInternalFormat(SkBitmap::Config config);
+    static GLenum getType(SkBitmap::Config config);
+    static void createTextureWithBitmap(GLuint texture, SkBitmap& bitmap);
+    static void updateTextureWithBitmap(GLuint texture, SkBitmap& bitmap);
+
+private:
+    virtual bool threadLoop() = 0;
+
+    android::Mutex m_sync;
+    int m_width;
+    int m_height;
+};
+
+
+
+
+#endif // RenderingThread__DEFINED
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
diff --git a/samples/BrowserPlugin/jni/main.cpp b/samples/BrowserPlugin/jni/main.cpp
index 07bae66..a2dd667 100644
--- a/samples/BrowserPlugin/jni/main.cpp
+++ b/samples/BrowserPlugin/jni/main.cpp
@@ -77,6 +77,7 @@
 ANPSystemInterfaceV0        gSystemI;
 ANPTypefaceInterfaceV0      gTypefaceI;
 ANPWindowInterfaceV0        gWindowI;
+ANPOpenGLInterfaceV0        gOpenGLI;
 
 #define ARRAY_COUNT(array)      (sizeof(array) / sizeof(array[0]))
 #define DEBUG_PLUGIN_EVENTS     0
@@ -125,6 +126,7 @@
         { kSystemInterfaceV0_ANPGetValue,       sizeof(gSystemI),   &gSystemI },
         { kTypefaceInterfaceV0_ANPGetValue,     sizeof(gTypefaceI), &gTypefaceI },
         { kWindowInterfaceV0_ANPGetValue,       sizeof(gWindowI),   &gWindowI },
+        { kOpenGLInterfaceV0_ANPGetValue,       sizeof(gOpenGLI),   &gOpenGLI },
     };
     for (size_t i = 0; i < ARRAY_COUNT(gPairs); i++) {
         gPairs[i].i->inSize = gPairs[i].size;
@@ -162,6 +164,7 @@
     if (browser->version >= 14) {
         instance->pdata = browser->createobject (instance, getPluginClass());
         obj = static_cast<PluginObject*>(instance->pdata);
+        obj->pluginType = 0;
     }
     /* END: STANDARD PLUGIN FRAMEWORK */
 
@@ -176,6 +179,9 @@
             else if (!strcmp(argv[i], "Surface")) {
                model = kSurface_ANPDrawingModel;
             }
+            else if (!strcmp(argv[i], "OpenGL")) {
+               model = kOpenGL_ANPDrawingModel;
+            }
             gLogI.log(kDebug_ANPLogType, "------ %p DrawingModel is %d", instance, model);
             break;
         }
@@ -228,7 +234,6 @@
                 obj->pluginType = kVideo_PluginType;
                 obj->activePlugin = new VideoPlugin(instance);
             }
-            gLogI.log(kDebug_ANPLogType, "------ %p PluginType is %d", instance, obj->pluginType);
             break;
         }
     }
@@ -240,6 +245,8 @@
         obj->activePlugin = new BallAnimation(instance);
     }
 
+    gLogI.log(kDebug_ANPLogType, "------ %p PluginType is %d", instance, obj->pluginType);
+
     // check to ensure the pluginType supports the model
     if (!obj->activePlugin->supportsDrawingModel(model)) {
         gLogI.log(kError_ANPLogType, "------ %p Unsupported DrawingModel (%d)", instance, model);
@@ -247,7 +254,7 @@
     }
 
     // if the plugin uses the surface drawing model then set the java context
-    if (model == kSurface_ANPDrawingModel) {
+    if (model == kSurface_ANPDrawingModel || model == kOpenGL_ANPDrawingModel) {
         SurfaceSubPlugin* surfacePlugin = static_cast<SurfaceSubPlugin*>(obj->activePlugin);
 
         jobject context;
@@ -434,7 +441,8 @@
         PluginObject* obj = static_cast<PluginObject*>(instance->pdata);
         if (obj && obj->activePlugin) {
 
-            if(obj->activePlugin->supportsDrawingModel(kSurface_ANPDrawingModel)) {
+            if(obj->activePlugin->supportsDrawingModel(kSurface_ANPDrawingModel)
+                    || obj->activePlugin->supportsDrawingModel(kOpenGL_ANPDrawingModel)) {
                 SurfaceSubPlugin* plugin = static_cast<SurfaceSubPlugin*>(obj->activePlugin);
                 jobject* surface = static_cast<jobject*>(value);
                 *surface = plugin->getSurface();
diff --git a/samples/BrowserPlugin/jni/main.h b/samples/BrowserPlugin/jni/main.h
index 66629e6..ebfb4b2 100644
--- a/samples/BrowserPlugin/jni/main.h
+++ b/samples/BrowserPlugin/jni/main.h
@@ -27,6 +27,7 @@
 #include <npfunctions.h>
 #include <npruntime.h>
 #include "android_npapi.h"
+#include "ANPOpenGL_npapi.h"
 #include "ANPSurface_npapi.h"
 #include "ANPSystem_npapi.h"
 
diff --git a/samples/BrowserPlugin/src/com/android/sampleplugin/AnimationSurface.java b/samples/BrowserPlugin/src/com/android/sampleplugin/AnimationSurface.java
new file mode 100644
index 0000000..e3afab6
--- /dev/null
+++ b/samples/BrowserPlugin/src/com/android/sampleplugin/AnimationSurface.java
@@ -0,0 +1,22 @@
+package com.android.sampleplugin;
+
+import android.content.Context;
+import android.graphics.Color;
+import android.util.Log;
+import android.widget.TextView;
+
+public class AnimationSurface extends TextView {
+
+    public AnimationSurface(Context context) {
+        super(context);
+
+        Log.e("AnimSurface", "IN ANIMATION SURFACE");
+        
+        this.setBackgroundColor(Color.GRAY);
+        this.setTextColor(Color.WHITE);
+        this.setText("This is a full-screen plugin");
+
+        // ensure that the view system is aware that we will be drawing
+        this.setWillNotDraw(false);
+    }
+}