From 8bc8c9bd128aa1af7df2c5a2c04e1f2eb8eb1fd5 Mon Sep 17 00:00:00 2001
From: "sehr@google.com" <sehr@google.com@0039d316-1c4b-4281-b951-d872f2087c98>
Date: Mon, 11 Jan 2010 19:46:41 +0000
Subject: [PATCH] This CL is for Neb.  It contains his change 501124, with
 conflicts merged. Review URL: http://codereview.chromium.org/547005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@35928 0039d316-1c4b-4281-b951-d872f2087c98
---
 build/all.gyp                                 |  4 +-
 chrome/renderer/render_view.cc                | 10 ++--
 chrome/renderer/webplugin_delegate_pepper.cc  |  2 +-
 .../tools/pepper_test_plugin/event_handler.cc | 53 +++++++++----------
 webkit/tools/pepper_test_plugin/main.cc       | 50 +++++++++--------
 .../pepper_test_plugin/pepper_test_plugin.gyp | 14 +++--
 .../tools/pepper_test_plugin/plugin_object.cc | 44 +++++++++++----
 .../tools/pepper_test_plugin/plugin_object.h  | 10 ++--
 8 files changed, 112 insertions(+), 75 deletions(-)

diff --git a/build/all.gyp b/build/all.gyp
index b6646b7acfda2..5c9a214c70e8e 100644
--- a/build/all.gyp
+++ b/build/all.gyp
@@ -104,8 +104,8 @@
             '../third_party/cld/cld.gyp:*',
             '../third_party/gles2_book/gles2_book.gyp:*',
             '../tools/memory_watcher/memory_watcher.gyp:*',
-            # As of now Skia build has problems with Linux and/or 64 bits.
-            # TODO(sehr,brettw): Make this unconditional.
+            # As of now, build machines do not have GL headers to compile this
+            # TODO(sehr,brettw, neb): Make this unconditional.
             '../webkit/tools/pepper_test_plugin/pepper_test_plugin.gyp:*',
           ],
         }, {
diff --git a/chrome/renderer/render_view.cc b/chrome/renderer/render_view.cc
index f04a110da56cd..da247282c9e94 100644
--- a/chrome/renderer/render_view.cc
+++ b/chrome/renderer/render_view.cc
@@ -2674,21 +2674,21 @@ webkit_glue::WebPluginDelegate* RenderView::CreatePluginDelegate(
     }
   }
   if (in_process_plugin) {
-#if defined(OS_WIN)  // In-proc plugins aren't supported on Linux or Mac.
     if (use_pepper_host) {
       return WebPluginDelegatePepper::Create(
           path,
           *mime_type_to_use,
           AsWeakPtr(),
-          gfx::NativeViewFromId(host_window_));
+          0);
     } else {
+#if defined(OS_WIN)  // In-proc plugins aren't supported on Linux or Mac.
       return WebPluginDelegateImpl::Create(
           path, *mime_type_to_use, gfx::NativeViewFromId(host_window_));
-    }
 #else
-    NOTIMPLEMENTED();
-    return NULL;
+      NOTIMPLEMENTED();
+      return NULL;
 #endif
+    }
   }
 
   return new WebPluginDelegateProxy(*mime_type_to_use, AsWeakPtr());
diff --git a/chrome/renderer/webplugin_delegate_pepper.cc b/chrome/renderer/webplugin_delegate_pepper.cc
index 0686a5698dd52..36808049d4721 100644
--- a/chrome/renderer/webplugin_delegate_pepper.cc
+++ b/chrome/renderer/webplugin_delegate_pepper.cc
@@ -593,7 +593,7 @@ void WebPluginDelegatePepper::PluginDestroyed() {
 
 void WebPluginDelegatePepper::Paint(WebKit::WebCanvas* canvas,
                                     const gfx::Rect& rect) {
-#if defined(OS_WIN)
+#if defined(OS_WIN) || defined(OS_LINUX)
   if (nested_delegate_) {
     // TODO(apatrick): The GPU plugin will render to an offscreen render target.
     //    Need to copy it to the screen here.
diff --git a/webkit/tools/pepper_test_plugin/event_handler.cc b/webkit/tools/pepper_test_plugin/event_handler.cc
index 7884faca51092..65df790faba1d 100644
--- a/webkit/tools/pepper_test_plugin/event_handler.cc
+++ b/webkit/tools/pepper_test_plugin/event_handler.cc
@@ -26,11 +26,8 @@
 #include "webkit/tools/pepper_test_plugin/event_handler.h"
 
 #include <stdio.h>
-#include <string>
+#include <sstream>
 
-#include "base/basictypes.h"
-#include "base/logging.h"
-#include "base/string_util.h"
 #include "webkit/tools/pepper_test_plugin/plugin_object.h"
 
 EventHandler* event_handler = NULL;
@@ -58,7 +55,10 @@ void EventHandler::addText(const char* cstr) {
 }
 
 std::string EventHandler::EventName(double timestamp, int32 type) {
-  std::string str = DoubleToString(timestamp) + ": ";
+  std::stringstream strstr;
+  strstr.setf(std::ios::fixed, std::ios::floatfield);
+  strstr << timestamp << ": ";
+  std::string str(strstr.str());
   switch (type) {
     case NPEventType_MouseDown:
       return str + "MouseDown";
@@ -94,46 +94,43 @@ std::string EventHandler::EventName(double timestamp, int32 type) {
 
 int EventHandler::handle(void* event) {
   NPPepperEvent* npevent = reinterpret_cast<NPPepperEvent*>(event);
-  std::string str = EventName(npevent->timeStampSeconds, npevent->type);
+  std::stringstream str;
+  str << EventName(npevent->timeStampSeconds, npevent->type);
   switch (npevent->type) {
     case NPEventType_MouseDown:
     case NPEventType_MouseUp:
     case NPEventType_MouseMove:
     case NPEventType_MouseEnter:
     case NPEventType_MouseLeave:
-      str += StringPrintf(": mod %x, but: %x, x: %d, y: %d, click: %d",
-                          npevent->u.mouse.modifier,
-                          npevent->u.mouse.button,
-                          npevent->u.mouse.x,
-                          npevent->u.mouse.y,
-                          npevent->u.mouse.clickCount);
+      str << ": mod "  << npevent->u.mouse.modifier
+          << ", but: " << npevent->u.mouse.button
+          << ", x: "   << npevent->u.mouse.x
+          << ", y: "   << npevent->u.mouse.y
+          << ", click: " << npevent->u.mouse.clickCount;
         break;
     case NPEventType_MouseWheel:
-      str += StringPrintf(": mod %x, dx: %f, dy: %f, wtx: %f, wty: %d: sbp %d",
-                          npevent->u.wheel.modifier,
-                          npevent->u.wheel.deltaX,
-                          npevent->u.wheel.deltaY,
-                          npevent->u.wheel.wheelTicksX,
-                          npevent->u.wheel.wheelTicksY,
-                          npevent->u.wheel.scrollByPage);
+      str << ": mod "  << npevent->u.wheel.modifier
+          << ", dx: "  << npevent->u.wheel.deltaX
+          << ", dy: "  << npevent->u.wheel.deltaY
+          << ", wtx: " << npevent->u.wheel.wheelTicksX
+          << ", wty: " << npevent->u.wheel.wheelTicksY
+          << ", sbp:"  << npevent->u.wheel.scrollByPage;
       break;
     case NPEventType_RawKeyDown:
     case NPEventType_KeyDown:
     case NPEventType_KeyUp:
-      str += StringPrintf(": mod %x, key: %x",
-                          npevent->u.key.modifier,
-                          npevent->u.key.normalizedKeyCode);
+      str << ": mod " << npevent->u.key.modifier
+          << ", key: " << npevent->u.key.normalizedKeyCode;
       break;
     case NPEventType_Char:
-      str += StringPrintf(": mod %x, text: ",
-                          npevent->u.character.modifier);
+      str << ": mod " << npevent->u.character.modifier << ", text: ";
       size_t i;
       for (i = 0; i < arraysize(npevent->u.character.text); ++i) {
-        str += StringPrintf("%x ", npevent->u.character.text[i]);
+        str << npevent->u.character.text[i] << ' ';
       }
-      str += ", unmod: ";
+      str << ", unmod: ";
       for (i = 0; i < arraysize(npevent->u.character.unmodifiedText); ++i) {
-          str += StringPrintf("%x ", npevent->u.character.unmodifiedText[i]);
+          str << npevent->u.character.unmodifiedText[i] << ' ';
       }
       break;
     case NPEventType_Minimize:
@@ -144,7 +141,7 @@ int EventHandler::handle(void* event) {
     default:
       break;
   }
-  addText(str.c_str());
+  addText(str.str().c_str());
   return 0;
 }
 
diff --git a/webkit/tools/pepper_test_plugin/main.cc b/webkit/tools/pepper_test_plugin/main.cc
index ef9a64b1949aa..851674cc3c514 100644
--- a/webkit/tools/pepper_test_plugin/main.cc
+++ b/webkit/tools/pepper_test_plugin/main.cc
@@ -34,15 +34,21 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#if defined(INDEPENDENT_PLUGIN)
+#include <iostream>
+#define LOG(x) std::cerr
+#else
 #include "base/logging.h"
 #include "base/string_util.h"
+#endif
+#include "webkit/glue/plugins/nphostapi.h"
 #include "webkit/tools/pepper_test_plugin/plugin_object.h"
 #include "webkit/tools/pepper_test_plugin/event_handler.h"
 
-#ifdef WIN32
-#define NPAPI WINAPI
-#else
-#define NPAPI
+#if __GNUC__ >= 4
+#define EXPORT __attribute__ ((visibility("default")))
+#elif defined(_MSC_VER)
+#define EXPORT __declspec(dllexport)
 #endif
 
 namespace {
@@ -51,7 +57,12 @@ void Log(NPP instance, const char* format, ...) {
   va_list args;
   va_start(args, format);
   std::string message("PLUGIN: ");
+#if defined(INDEPENDENT_PLUGIN)
+  char msgbuf[100];
+  vsnprintf(msgbuf, sizeof(msgbuf), format, args);
+#else
   StringAppendV(&message, format, args);
+#endif
   va_end(args);
 
   NPObject* window_object = 0;
@@ -99,34 +110,27 @@ void Log(NPP instance, const char* format, ...) {
 // Plugin entry points
 extern "C" {
 
-#if defined(OS_WIN)
-//__declspec(dllexport)
-#endif
-NPError NPAPI NP_Initialize(NPNetscapeFuncs* browser_funcs
+EXPORT NPError API_CALL NP_Initialize(NPNetscapeFuncs* browser_funcs
 #if defined(OS_LINUX)
                             , NPPluginFuncs* plugin_funcs
 #endif
                             );
-#if defined(OS_WIN)
-//__declspec(dllexport)
-#endif
-NPError NPAPI NP_GetEntryPoints(NPPluginFuncs* plugin_funcs);
+EXPORT NPError API_CALL NP_GetEntryPoints(NPPluginFuncs* plugin_funcs);
 
-#if defined(OS_WIN)
-//__declspec(dllexport)
-#endif
-void NPAPI NP_Shutdown() {
+EXPORT void API_CALL NP_Shutdown() {
 }
 
 #if defined(OS_LINUX)
-NPError NP_GetValue(NPP instance, NPPVariable variable, void* value);
-const char* NP_GetMIMEDescription();
+EXPORT NPError API_CALL NP_GetValue(NPP instance,
+                                    NPPVariable variable,
+                                    void* value);
+EXPORT const char* API_CALL NP_GetMIMEDescription();
 #endif
 
 }  // extern "C"
 
 // Plugin entry points
-NPError NPAPI NP_Initialize(NPNetscapeFuncs* browser_funcs
+EXPORT NPError API_CALL NP_Initialize(NPNetscapeFuncs* browser_funcs
 #if defined(OS_LINUX)
                             , NPPluginFuncs* plugin_funcs
 #endif
@@ -141,7 +145,7 @@ NPError NPAPI NP_Initialize(NPNetscapeFuncs* browser_funcs
 
 // Entrypoints -----------------------------------------------------------------
 
-NPError NPAPI NP_GetEntryPoints(NPPluginFuncs* plugin_funcs) {
+NPError API_CALL NP_GetEntryPoints(NPPluginFuncs* plugin_funcs) {
   plugin_funcs->version = 11;
   plugin_funcs->size = sizeof(plugin_funcs);
   plugin_funcs->newp = NPP_New;
@@ -271,11 +275,11 @@ NPError NPP_SetValue(NPP instance, NPNVariable variable, void* value) {
 }
 
 #if defined(OS_LINUX)
-NPError NP_GetValue(NPP instance, NPPVariable variable, void* value) {
+NPError API_CALL NP_GetValue(NPP instance, NPPVariable variable, void* value) {
   return NPP_GetValue(instance, variable, value);
 }
 
-const char* NP_GetMIMEDescription() {
-  return "pepper-application/x-pepper-test-plugin pepper test;";
+const char* API_CALL NP_GetMIMEDescription() {
+  return "pepper-application/x-pepper-test-plugin::Pepper Test";
 }
 #endif
diff --git a/webkit/tools/pepper_test_plugin/pepper_test_plugin.gyp b/webkit/tools/pepper_test_plugin/pepper_test_plugin.gyp
index aa3908a8a396e..93837476e74f3 100644
--- a/webkit/tools/pepper_test_plugin/pepper_test_plugin.gyp
+++ b/webkit/tools/pepper_test_plugin/pepper_test_plugin.gyp
@@ -5,8 +5,6 @@
       'target_name': 'pepper_test_plugin',
       'type': 'shared_library',
       'dependencies': [
-        '../../../base/base.gyp:base',
-        '../../../skia/skia.gyp:skia',
         '../../../third_party/npapi/npapi.gyp:npapi',
       ],
       'include_dirs': [
@@ -23,7 +21,17 @@
             'pepper_test_plugin.def',
             'pepper_test_plugin.rc',
           ],
-        }]
+        }],
+        ['OS=="linux" and (target_arch=="x64" or target_arch=="arm")', {
+          # Shared libraries need -fPIC on x86-64
+          'cflags': ['-fPIC'],
+          'defines': ['INDEPENDENT_PLUGIN'],
+        }, {
+          'dependencies': [
+            '../../../base/base.gyp:base',
+            '../../../skia/skia.gyp:skia',
+          ],
+        }],
       ],
       'sources': [
         'command_buffer_pepper.cc',
diff --git a/webkit/tools/pepper_test_plugin/plugin_object.cc b/webkit/tools/pepper_test_plugin/plugin_object.cc
index 59212ac95788c..91b406f10ecca 100644
--- a/webkit/tools/pepper_test_plugin/plugin_object.cc
+++ b/webkit/tools/pepper_test_plugin/plugin_object.cc
@@ -28,12 +28,16 @@
 #include <stdio.h>
 #include <string>
 
+#if defined(INDEPENDENT_PLUGIN)
+#define CHECK(x)
+#else
 #include "base/logging.h"
 #include "gpu/command_buffer/client/gles2_lib.h"
 #include "gpu/command_buffer/client/gles2_demo_cc.h"
 #include "third_party/skia/include/core/SkBitmap.h"
 #include "third_party/skia/include/core/SkCanvas.h"
 #include "third_party/skia/include/effects/SkGradientShader.h"
+#endif
 #include "webkit/tools/pepper_test_plugin/event_handler.h"
 #include "webkit/tools/pepper_test_plugin/test_object.h"
 
@@ -210,7 +214,26 @@ NPClass plugin_class = {
 
 // Bitmap painting -------------------------------------------------------------
 
-void DrawSampleBitmap(SkCanvas& canvas, int width, int height) {
+#if defined(INDEPENDENT_PLUGIN)
+void DrawSampleBitmap(void *region, int width, int height) {
+  int32 *bitmap = reinterpret_cast<int32 *>(region);
+
+  for (int h = 0; h < height; ++h) {
+    uint8 opacity = (h * 0xff) / height;
+    for (int w = 0; w < width; ++w) {
+      // kudos to apatrick for noticing we're using premultiplied alpha
+      uint8 greenness = (w * opacity) / width;
+      *bitmap++ = (opacity << 24) | (greenness << 8);
+    }
+  }
+}
+#else
+void DrawSampleBitmap(void *region, int width, int height) {
+  SkBitmap bitmap;
+  bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
+  bitmap.setPixels(region);
+
+  SkCanvas canvas(bitmap);
   SkRect rect;
   rect.set(SkIntToScalar(0), SkIntToScalar(0),
            SkIntToScalar(width), SkIntToScalar(height));
@@ -233,7 +256,7 @@ void DrawSampleBitmap(SkCanvas& canvas, int width, int height) {
 
   canvas.drawPath(path, paint);
 }
-
+#endif
 void FlushCallback(NPP instance, NPDeviceContext* context,
                    NPError err, void* user_data) {
 }
@@ -290,19 +313,14 @@ void PluginObject::New(NPMIMEType pluginType,
 
 void PluginObject::SetWindow(const NPWindow& window) {
   if (dimensions_ == 2) {
-    size_.set_width(window.width);
-    size_.set_height(window.height);
+    width_ = window.width;
+    height_ = window.height;
 
     NPDeviceContext2DConfig config;
     NPDeviceContext2D context;
     device2d_->initializeContext(npp_, &config, &context);
 
-    SkBitmap bitmap;
-    bitmap.setConfig(SkBitmap::kARGB_8888_Config, window.width, window.height);
-    bitmap.setPixels(context.region);
-
-    SkCanvas canvas(bitmap);
-    DrawSampleBitmap(canvas, window.width, window.height);
+    DrawSampleBitmap(context.region, width_, height_);
 
     // TODO(brettw) figure out why this cast is necessary, the functions seem to
     // match. Could be a calling convention mismatch?
@@ -310,6 +328,7 @@ void PluginObject::SetWindow(const NPWindow& window) {
         reinterpret_cast<NPDeviceFlushContextCallbackPtr>(&FlushCallback);
     device2d_->flushContext(npp_, &context, callback, NULL);
   } else {
+#if !defined(INDEPENDENT_PLUGIN)
     if (!command_buffer_.get()) {
       if (!InitializeCommandBuffer())
         return;
@@ -319,10 +338,12 @@ void PluginObject::SetWindow(const NPWindow& window) {
 
     // Schedule the first call to Draw.
     browser->pluginthreadasynccall(npp_, Draw3DCallback, this);
+#endif
   }
 }
 
 void PluginObject::Draw3D() {
+#if !defined(INDEPENDENT_PLUGIN)
   // Render some stuff.
   gles2::g_gl_impl = gles2_implementation_.get();
   GLFromCPPTestFunction();
@@ -332,9 +353,11 @@ void PluginObject::Draw3D() {
 
   // Schedule another call to Draw.
   browser->pluginthreadasynccall(npp_, Draw3DCallback, this);
+#endif
 }
 
 bool PluginObject::InitializeCommandBuffer() {
+#if !defined(INDEPENDENT_PLUGIN)
   const static int32 kCommandBufferSize = 512 * 1024;
   command_buffer_.reset(new CommandBufferPepper(npp_, browser));
   if (command_buffer_->Initialize(kCommandBufferSize)) {
@@ -360,6 +383,7 @@ bool PluginObject::InitializeCommandBuffer() {
   }
 
   command_buffer_.reset();
+#endif
 
   return false;
 }
diff --git a/webkit/tools/pepper_test_plugin/plugin_object.h b/webkit/tools/pepper_test_plugin/plugin_object.h
index a248f480e63b3..4e9440e44de8c 100644
--- a/webkit/tools/pepper_test_plugin/plugin_object.h
+++ b/webkit/tools/pepper_test_plugin/plugin_object.h
@@ -27,11 +27,12 @@
 #define WEBKIT_TOOLS_PEPPER_TEST_PLUGIN_PLUGIN_OBJECT_H_
 
 #include "base/basictypes.h"
-#include "base/gfx/size.h"
 #include "base/scoped_ptr.h"
+#include "webkit/glue/plugins/nphostapi.h"
+#if !defined(INDEPENDENT_PLUGIN)
 #include "gpu/command_buffer/client/gles2_implementation.h"
 #include "webkit/tools/pepper_test_plugin/command_buffer_pepper.h"
-#include "webkit/glue/plugins/nphostapi.h"
+#endif
 
 extern NPNetscapeFuncs* browser;
 
@@ -59,11 +60,14 @@ class PluginObject {
 
   NPDevice* device2d_;
 
+#if !defined(INDEPENDENT_PLUGIN)
   scoped_ptr<CommandBufferPepper> command_buffer_;
   scoped_ptr<gpu::gles2::GLES2CmdHelper> helper_;
   scoped_ptr<gpu::gles2::GLES2Implementation> gles2_implementation_;
+#endif
 
-  gfx::Size size_;
+  int width_;
+  int height_;
 
   DISALLOW_COPY_AND_ASSIGN(PluginObject);
 };
-- 
GitLab