summaryrefslogtreecommitdiffstats
path: root/ppapi/examples
diff options
context:
space:
mode:
authorbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-01 16:16:50 +0000
committerbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-01 16:16:50 +0000
commit1758e88fd909ea0ffd49621e8066ffad5627ffdf (patch)
treec304a5eed047cae5665f5af1739d84655fb5815d /ppapi/examples
parente7d8b51953b7d3b2b8a0aba46132305b32f3efce (diff)
downloadchromium_src-1758e88fd909ea0ffd49621e8066ffad5627ffdf.zip
chromium_src-1758e88fd909ea0ffd49621e8066ffad5627ffdf.tar.gz
chromium_src-1758e88fd909ea0ffd49621e8066ffad5627ffdf.tar.bz2
Move PPAPI into the Chrome repo. The old repo was
http://ppapi.googlecode.com/ TEST=none BUG=none git-svn-id: svn://svn.chromium.org/chrome/trunk/src@64613 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi/examples')
-rw-r--r--ppapi/examples/2d/graphics_2d_example.c225
-rw-r--r--ppapi/examples/2d/paint_manager_example.cc157
-rw-r--r--ppapi/examples/2d/scroll.cc119
-rw-r--r--ppapi/examples/audio/audio.cc80
-rw-r--r--ppapi/examples/file_chooser/file_chooser.cc107
-rw-r--r--ppapi/examples/font/simple_font.cc67
-rw-r--r--ppapi/examples/stub/stub.c35
-rw-r--r--ppapi/examples/stub/stub.cc41
8 files changed, 831 insertions, 0 deletions
diff --git a/ppapi/examples/2d/graphics_2d_example.c b/ppapi/examples/2d/graphics_2d_example.c
new file mode 100644
index 0000000..eadfb31
--- /dev/null
+++ b/ppapi/examples/2d/graphics_2d_example.c
@@ -0,0 +1,225 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "ppapi/c/pp_completion_callback.h"
+#include "ppapi/c/pp_errors.h"
+#include "ppapi/c/pp_instance.h"
+#include "ppapi/c/pp_module.h"
+#include "ppapi/c/pp_size.h"
+#include "ppapi/c/pp_var.h"
+#include "ppapi/c/ppb.h"
+#include "ppapi/c/ppb_core.h"
+#include "ppapi/c/ppb_graphics_2d.h"
+#include "ppapi/c/ppb_image_data.h"
+#include "ppapi/c/ppb_instance.h"
+#include "ppapi/c/ppp.h"
+#include "ppapi/c/ppp_instance.h"
+
+PP_Module g_module_id;
+PPB_GetInterface g_get_browser_interface = NULL;
+
+const struct PPB_Core* g_core_interface;
+const struct PPB_Graphics2D* g_graphics_2d_interface;
+const struct PPB_ImageData* g_image_data_interface;
+const struct PPB_Instance* g_instance_interface;
+
+// PPP_Instance implementation -------------------------------------------------
+
+struct InstanceInfo {
+ PP_Instance pp_instance;
+ PP_Size last_size;
+
+ InstanceInfo* next;
+};
+
+// Linked list of all live instances.
+InstanceInfo* all_instances = NULL;
+
+// Returns a refed resource corresponding to the created device context.
+PP_Resource MakeAndBindDeviceContext(PP_Instance instance,
+ const struct PP_Size* size) {
+ PP_Resource device_context;
+
+ device_context = g_graphics_2d_interface->Create(g_module_id, size, false);
+ if (!device_context)
+ return 0;
+
+ if (!g_instance_interface->BindGraphics(instance, device_context)) {
+ g_core_interface->ReleaseResource(device_context);
+ return 0;
+ }
+ return device_context;
+}
+
+void FlushCompletionCallback(void* user_data, int32_t result) {
+ // Don't need to do anything here.
+}
+
+void Repaint(InstanceInfo* instance, const struct PP_Size* size) {
+ PP_Resource image, device_context;
+ PP_ImageDataDesc image_desc;
+ uint32_t* image_data;
+ int num_words, i;
+
+ // Create image data to paint into.
+ image = g_image_data_interface->Create(
+ g_module_id, PP_IMAGEDATAFORMAT_BGRA_PREMUL, size, true);
+ if (!image)
+ return;
+ g_image_data_interface->Describe(image, &image_desc);
+
+ // Fill the image with blue.
+ image_data = (uint32_t*)g_image_data_interface->Map(image);
+ if (!image_data) {
+ g_core_interface->ReleaseResource(image);
+ return;
+ }
+ num_words = image_desc.stride * size->height / 4;
+ for (i = 0; i < num_words; i++)
+ image_data[i] = 0xFF0000FF;
+
+ // Create the device context and paint the image to it.
+ device_context = MakeAndBindDeviceContext(instance->pp_instance, size);
+ if (!device_context) {
+ g_core_interface->ReleaseResource(image);
+ return;
+ }
+
+ g_graphics_2d_interface->ReplaceContents(device_context, image);
+ g_graphics_2d_interface->Flush(device_context,
+ PP_MakeCompletionCallback(&FlushCompletionCallback, NULL));
+
+ g_core_interface->ReleaseResource(device_context);
+ g_core_interface->ReleaseResource(image);
+}
+
+// Returns the info for the given instance, or NULL if it's not found.
+InstanceInfo* FindInstance(PP_Instance instance) {
+ InstanceInfo* cur = all_instances;
+ while (cur) {
+ if (cur->pp_instance == instance)
+ return cur;
+ }
+ return NULL;
+}
+
+bool Instance_New(PP_Instance instance) {
+ InstanceInfo* info = (InstanceInfo*)malloc(sizeof(InstanceInfo));
+ info->pp_instance = instance;
+ info->last_size.width = 0;
+ info->last_size.height = 0;
+
+ // Insert into linked list of live instances.
+ info->next = all_instances;
+ all_instances = info;
+ return true;
+}
+
+void Instance_Delete(PP_Instance instance) {
+ // Find the matching item in the linked list, delete it, and patch the links.
+ InstanceInfo** prev_ptr = &all_instances;
+ InstanceInfo* cur = all_instances;
+ while (cur) {
+ if (instance == cur->pp_instance) {
+ *prev_ptr = cur->next;
+ free(cur);
+ return;
+ }
+ prev_ptr = &cur->next;
+ }
+}
+
+bool Instance_Initialize(PP_Instance pp_instance,
+ uint32_t argc,
+ const char* argn[],
+ const char* argv[]) {
+ return true;
+}
+
+bool Instance_HandleDocumentLoad(PP_Instance pp_instance,
+ PP_Resource pp_url_loader) {
+ return false;
+}
+
+bool Instance_HandleInputEvent(PP_Instance pp_instance,
+ const struct PP_InputEvent* event) {
+ // We don't handle any events.
+ return false;
+}
+
+void Instance_HandleFocusChanged(bool /*has_focus*/) {
+}
+
+PP_Var Instance_GetInstanceObject(PP_Instance pp_instance) {
+ return PP_MakeNull();
+}
+
+void Instance_ViewChanged(PP_Instance pp_instance,
+ const struct PP_Rect* position,
+ const struct PP_Rect* clip) {
+ InstanceInfo* info = FindInstance(pp_instance);
+ if (!info)
+ return;
+
+ if (info->last_size.width != position->size.width ||
+ info->last_size.height != position->size.height) {
+ // Got a resize, repaint the plugin.
+ Repaint(info, &position->size);
+ info->last_size.width = position->size.width;
+ info->last_size.height = position->size.height;
+ }
+}
+
+PP_Var Instance_GetSelectedText(PP_Instance pp_instance,
+ bool html) {
+ return PP_MakeNull();
+}
+
+static PPP_Instance instance_interface = {
+ &Instance_New,
+ &Instance_Delete,
+ &Instance_Initialize,
+ &Instance_HandleDocumentLoad,
+ &Instance_HandleInputEvent,
+ &Instance_HandleFocusChanged,
+ &Instance_GetInstanceObject,
+ &Instance_ViewChanged,
+ &Instance_GetSelectedText,
+};
+
+
+// Global entrypoints ----------------------------------------------------------
+
+PP_EXPORT int32_t PPP_InitializeModule(PP_Module module,
+ PPB_GetInterface get_browser_interface) {
+ // Save the global module information for later.
+ g_module_id = module;
+ g_get_browser_interface = get_browser_interface;
+
+ g_core_interface = (const struct PPB_Core*)
+ get_browser_interface(PPB_CORE_INTERFACE);
+ g_instance_interface = (const struct PPB_Instance*)
+ get_browser_interface(PPB_INSTANCE_INTERFACE);
+ g_image_data_interface = (const struct PPB_ImageData*)
+ get_browser_interface(PPB_IMAGEDATA_INTERFACE);
+ g_graphics_2d_interface = (const struct PPB_Graphics2D*)
+ get_browser_interface(PPB_GRAPHICS_2D_INTERFACE);
+ if (!g_core_interface || !g_instance_interface || !g_image_data_interface ||
+ !g_graphics_2d_interface)
+ return -1;
+
+ return PP_OK;
+}
+
+PP_EXPORT void PPP_ShutdownModule() {
+}
+
+PP_EXPORT const void* PPP_GetInterface(const char* interface_name) {
+ if (strcmp(interface_name, PPP_INSTANCE_INTERFACE) == 0)
+ return &instance_interface;
+ return NULL;
+}
diff --git a/ppapi/examples/2d/paint_manager_example.cc b/ppapi/examples/2d/paint_manager_example.cc
new file mode 100644
index 0000000..5d2bb22
--- /dev/null
+++ b/ppapi/examples/2d/paint_manager_example.cc
@@ -0,0 +1,157 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ppapi/c/pp_input_event.h"
+#include "ppapi/cpp/graphics_2d.h"
+#include "ppapi/cpp/image_data.h"
+#include "ppapi/cpp/instance.h"
+#include "ppapi/cpp/module.h"
+#include "ppapi/cpp/paint_manager.h"
+#include "ppapi/cpp/size.h"
+
+// Number of pixels to each side of the center of the square that we draw.
+static const int kSquareRadius = 2;
+
+// We identify our square by the center point. This computes the rect for the
+// square given that point.
+pp::Rect SquareForPoint(int x, int y) {
+ return PP_MakeRectFromXYWH(x - kSquareRadius, y - kSquareRadius,
+ kSquareRadius * 2 + 1, kSquareRadius * 2 + 1);
+}
+
+static void FillRect(pp::ImageData* image,
+ int left, int top, int width, int height,
+ uint32_t color) {
+ for (int y = std::max(0, top);
+ y < std::min(image->size().height() - 1, top + height);
+ y++) {
+ for (int x = std::max(0, left);
+ x < std::min(image->size().width() - 1, left + width);
+ x++)
+ *image->GetAddr32(pp::Point(x, y)) = color;
+ }
+}
+
+class MyInstance : public pp::Instance, public pp::PaintManager::Client {
+ public:
+ MyInstance(PP_Instance instance)
+ : pp::Instance(instance),
+ paint_manager_(),
+ last_x_(0),
+ last_y_(0) {
+ paint_manager_.Initialize(this, this, false);
+ }
+
+ virtual bool HandleEvent(const PP_InputEvent& event) {
+ switch (event.type) {
+ case PP_INPUTEVENT_TYPE_MOUSEDOWN: {
+ const PP_InputEvent_Mouse& mouse_event = event.u.mouse;
+ // Update the square on a mouse down.
+ if (mouse_event.button == PP_INPUTEVENT_MOUSEBUTTON_LEFT) {
+ UpdateSquare(static_cast<int>(mouse_event.x),
+ static_cast<int>(mouse_event.y));
+ }
+ return true;
+ }
+ case PP_INPUTEVENT_TYPE_MOUSEMOVE: {
+ const PP_InputEvent_Mouse& mouse_event = event.u.mouse;
+ // Update the square on a drag.
+ if (mouse_event.button == PP_INPUTEVENT_MOUSEBUTTON_LEFT) {
+ UpdateSquare(static_cast<int>(mouse_event.x),
+ static_cast<int>(mouse_event.y));
+ }
+ return true;
+ }
+ default:
+ return false;
+ }
+ }
+
+ virtual void ViewChanged(const pp::Rect& position, const pp::Rect& clip) {
+ paint_manager_.SetSize(position.size());
+ }
+
+ // PaintManager::Client implementation.
+ virtual bool OnPaint(pp::Graphics2D& device,
+ const std::vector<pp::Rect>& paint_rects,
+ const pp::Rect& paint_bounds) {
+ // Make an image just large enough to hold all dirty rects. We won't
+ // actually paint all of these pixels below, but rather just the dirty
+ // ones. Since image allocation can be somewhat heavyweight, we wouldn't
+ // want to allocate separate images in the case of multiple dirty rects.
+ pp::ImageData updated_image(PP_IMAGEDATAFORMAT_BGRA_PREMUL,
+ paint_bounds.size(), false);
+
+ // We could repaint everything inside the image we made above. For this
+ // example, that would probably be the easiest thing since updates are
+ // small and typically close to each other. However, for the purposes of
+ // demonstration, here we only actually paint the pixels that changed,
+ // which may be the entire update region, or could be multiple discontigous
+ // regions inside the update region.
+ //
+ // Note that the aggregator used by the paint manager won't give us
+ // multiple regions that overlap, so we don't have to worry about double
+ // painting in this code.
+ for (size_t i = 0; i < paint_rects.size(); i++) {
+ // Since our image is just the invalid region, we need to offset the
+ // areas we paint by that much. This is just a light blue background.
+ FillRect(&updated_image,
+ paint_rects[i].x() - paint_bounds.x(),
+ paint_rects[i].y() - paint_bounds.y(),
+ paint_rects[i].width(),
+ paint_rects[i].height(),
+ 0xFFAAAAFF);
+ }
+
+ // Paint the square black. Because we're lazy, we do this outside of the
+ // loop above.
+ pp::Rect square = SquareForPoint(last_x_, last_y_);
+ FillRect(&updated_image,
+ square.x() - paint_bounds.x(),
+ square.y() - paint_bounds.y(),
+ square.width(),
+ square.height(),
+ 0xFF000000);
+
+ return true;
+ }
+
+ private:
+ void UpdateSquare(int x, int y) {
+ if (x == last_x_ && y == last_y_)
+ return; // Nothing changed.
+
+ // Invalidate the region around the old square which needs to be repainted
+ // because it's no longer there.
+ paint_manager_.InvalidateRect(SquareForPoint(last_x_, last_y_));
+
+ // Update the current position.
+ last_x_ = x;
+ last_y_ = y;
+
+ // Also invalidate the region around the new square.
+ paint_manager_.InvalidateRect(SquareForPoint(last_x_, last_y_));
+ }
+
+ pp::PaintManager paint_manager_;
+
+ int last_x_;
+ int last_y_;
+};
+
+class MyModule : public pp::Module {
+ public:
+ virtual pp::Instance* CreateInstance(PP_Instance instance) {
+ return new MyInstance(instance);
+ }
+};
+
+namespace pp {
+
+// Factory function for your specialization of the Module object.
+Module* CreateModule() {
+ return new MyModule();
+}
+
+} // namespace pp
diff --git a/ppapi/examples/2d/scroll.cc b/ppapi/examples/2d/scroll.cc
new file mode 100644
index 0000000..b570ae1
--- /dev/null
+++ b/ppapi/examples/2d/scroll.cc
@@ -0,0 +1,119 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <math.h>
+
+#include "ppapi/cpp/completion_callback.h"
+#include "ppapi/cpp/graphics_2d.h"
+#include "ppapi/cpp/image_data.h"
+#include "ppapi/cpp/instance.h"
+#include "ppapi/cpp/module.h"
+#include "ppapi/cpp/paint_manager.h"
+#include "ppapi/cpp/rect.h"
+#include "ppapi/cpp/var.h"
+
+static const int kSquareSpacing = 98;
+static const int kSquareSize = 5;
+
+static const int kAdvanceXPerFrame = 0;
+static const int kAdvanceYPerFrame = -3;
+
+void FillRect(pp::ImageData* image, const pp::Rect& rect, uint32_t color) {
+ for (int y = std::max(0, rect.y());
+ y < std::min(image->size().height(), rect.bottom());
+ y++) {
+ for (int x = std::max(0, rect.x());
+ x < std::min(image->size().width(), rect.right());
+ x++)
+ *image->GetAddr32(pp::Point(x, y)) = color;
+ }
+}
+
+class MyInstance : public pp::Instance, public pp::PaintManager::Client {
+ public:
+ MyInstance(PP_Instance instance)
+ : pp::Instance(instance),
+ current_step_(0),
+ kicked_off_(false) {
+ factory_.Initialize(this);
+ paint_manager_.Initialize(this, this, false);
+ }
+
+ virtual void ViewChanged(const pp::Rect& position, const pp::Rect& clip) {
+ paint_manager_.SetSize(position.size());
+ }
+
+ void OnTimer(int32_t) {
+ pp::Module::Get()->core()->CallOnMainThread(
+ 16, factory_.NewCallback(&MyInstance::OnTimer), 0);
+ // The scroll and the invalidate will do the same thing in this example,
+ // but the invalidate will cause a large repaint, whereas the scroll will
+ // be faster and cause a smaller repaint.
+#if 1
+ paint_manager_.ScrollRect(pp::Rect(paint_manager_.graphics().size()),
+ pp::Point(kAdvanceXPerFrame, kAdvanceYPerFrame));
+#else
+ paint_manager_.Invalidate();
+#endif
+ current_step_++;
+ }
+
+ private:
+ // PaintManager::Client implementation.
+ virtual bool OnPaint(pp::Graphics2D& device,
+ const std::vector<pp::Rect>& paint_rects,
+ const pp::Rect& paint_bounds) {
+ if (!kicked_off_) {
+ pp::Module::Get()->core()->CallOnMainThread(
+ 16, factory_.NewCallback(&MyInstance::OnTimer), 0);
+ kicked_off_ = true;
+ }
+
+ // Paint the background.
+ pp::ImageData updated_image(PP_IMAGEDATAFORMAT_BGRA_PREMUL,
+ paint_bounds.size(), false);
+ FillRect(&updated_image, pp::Rect(updated_image.size()), 0xFF8888FF);
+
+ int x_origin = current_step_ * kAdvanceXPerFrame;
+ int y_origin = current_step_ * kAdvanceYPerFrame;
+
+ int x_offset = x_origin % kSquareSpacing;
+ int y_offset = y_origin % kSquareSpacing;
+
+ for (int ys = 0; ys < device.size().height() / kSquareSpacing + 2; ys++) {
+ for (int xs = 0; xs < device.size().width() / kSquareSpacing + 2; xs++) {
+ int x = xs * kSquareSpacing + x_offset - paint_bounds.x();
+ int y = ys * kSquareSpacing + y_offset - paint_bounds.y();
+ FillRect(&updated_image, pp::Rect(x, y, kSquareSize, kSquareSize),
+ 0xFF000000);
+ }
+ }
+ device.PaintImageData(updated_image, paint_bounds.point());
+ return true;
+ }
+
+ pp::CompletionCallbackFactory<MyInstance> factory_;
+
+ pp::PaintManager paint_manager_;
+
+ int current_step_;
+
+ bool kicked_off_;
+};
+
+class MyModule : public pp::Module {
+ public:
+ virtual pp::Instance* CreateInstance(PP_Instance instance) {
+ return new MyInstance(instance);
+ }
+};
+
+namespace pp {
+
+// Factory function for your specialization of the Module object.
+Module* CreateModule() {
+ return new MyModule();
+}
+
+} // namespace pp
diff --git a/ppapi/examples/audio/audio.cc b/ppapi/examples/audio/audio.cc
new file mode 100644
index 0000000..8f80ba4
--- /dev/null
+++ b/ppapi/examples/audio/audio.cc
@@ -0,0 +1,80 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <cmath>
+#include <limits>
+
+#include "ppapi/cpp/dev/audio_dev.h"
+#include "ppapi/cpp/instance.h"
+#include "ppapi/cpp/module.h"
+
+// Separate left and right frequency to make sure we didn't swap L & R.
+// Sounds pretty horrible, though...
+const double frequency_l = 200;
+const double frequency_r = 1000;
+
+// This sample frequency is guaranteed to work.
+const PP_AudioSampleRate_Dev sample_frequency = PP_AUDIOSAMPLERATE_44100;
+const uint32_t sample_count = 4096;
+
+class MyInstance : public pp::Instance {
+ public:
+ explicit MyInstance(PP_Instance instance)
+ : pp::Instance(instance),
+ audio_time_(0) {
+ }
+
+ virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
+ audio_ = pp::Audio_Dev(
+ *this, pp::AudioConfig_Dev(sample_frequency, sample_count),
+ SineWaveCallback, this);
+ return audio_.StartPlayback();
+ }
+
+ private:
+ static void SineWaveCallback(void* samples,
+ size_t buffer_size_in_bytes,
+ void* thiz) {
+ const double th_l = 2 * 3.141592653589 * frequency_l / sample_frequency;
+ const double th_r = 2 * 3.141592653589 * frequency_r / sample_frequency;
+
+ // Store time value to avoid clicks on buffer boundries.
+ size_t t = reinterpret_cast<MyInstance*>(thiz)->audio_time_;
+
+ uint16_t* buf = reinterpret_cast<uint16_t*>(samples);
+ for (size_t buffer_index = 0u;
+ buffer_index < buffer_size_in_bytes;
+ buffer_index += 2) {
+ *buf++ = static_cast<uint16_t>(std::sin(th_l * t)
+ * std::numeric_limits<uint16_t>::max());
+ *buf++ = static_cast<uint16_t>(std::sin(th_r * t++)
+ * std::numeric_limits<uint16_t>::max());
+ }
+ reinterpret_cast<MyInstance*>(thiz)->audio_time_ = t;
+ }
+
+ // Audio resource. Allocated in Init(), freed on destruction.
+ pp::Audio_Dev audio_;
+
+ // Audio buffer time. Used to make prevent sine wave skips on buffer
+ // boundaries.
+ size_t audio_time_;
+};
+
+class MyModule : public pp::Module {
+ public:
+ // Override CreateInstance to create your customized Instance object.
+ virtual pp::Instance* CreateInstance(PP_Instance instance) {
+ return new MyInstance(instance);
+ }
+};
+
+namespace pp {
+
+// Factory function for your specialization of the Module object.
+Module* CreateModule() {
+ return new MyModule();
+}
+
+} // namespace pp
diff --git a/ppapi/examples/file_chooser/file_chooser.cc b/ppapi/examples/file_chooser/file_chooser.cc
new file mode 100644
index 0000000..22af29f
--- /dev/null
+++ b/ppapi/examples/file_chooser/file_chooser.cc
@@ -0,0 +1,107 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ppapi/c/dev/ppb_file_chooser_dev.h"
+#include "ppapi/c/pp_input_event.h"
+#include "ppapi/cpp/completion_callback.h"
+#include "ppapi/cpp/dev/file_chooser_dev.h"
+#include "ppapi/cpp/dev/file_ref_dev.h"
+#include "ppapi/cpp/instance.h"
+#include "ppapi/cpp/module.h"
+#include "ppapi/cpp/var.h"
+
+class MyInstance : public pp::Instance {
+ public:
+ MyInstance(PP_Instance instance)
+ : pp::Instance(instance) {
+ callback_factory_.Initialize(this);
+ }
+
+ virtual bool HandleEvent(const PP_InputEvent& event) {
+ switch (event.type) {
+ case PP_INPUTEVENT_TYPE_MOUSEDOWN: {
+ const PP_InputEvent_Mouse& mouse_event = event.u.mouse;
+ if (mouse_event.button == PP_INPUTEVENT_MOUSEBUTTON_LEFT)
+ ShowFileChooser(false);
+ else if (mouse_event.button == PP_INPUTEVENT_MOUSEBUTTON_RIGHT)
+ ShowFileChooser(true);
+ else
+ return false;
+
+ return true;
+ }
+ default:
+ return false;
+ }
+ }
+
+ private:
+ void ShowFileChooser(bool multi_select) {
+ RecreateConsole();
+
+ PP_FileChooserOptions_Dev options;
+ options.mode = (multi_select ? PP_FILECHOOSERMODE_OPENMULTIPLE :
+ PP_FILECHOOSERMODE_OPEN);
+ options.accept_mime_types = (multi_select ? "" : "plain/text");
+
+ // Deleted in ShowSelectedFileNames().
+ pp::FileChooser_Dev* file_chooser = new pp::FileChooser_Dev(
+ *this, options);
+ file_chooser->Show(callback_factory_.NewCallback(
+ &MyInstance::ShowSelectedFileNames, file_chooser));
+ }
+
+ void ShowSelectedFileNames(int32_t, pp::FileChooser_Dev* file_chooser) {
+ if (!file_chooser)
+ return;
+
+ pp::FileRef_Dev file_ref = file_chooser->GetNextChosenFile();
+ while (!file_ref.is_null()) {
+ Log(file_ref.GetPath());
+ file_ref = file_chooser->GetNextChosenFile();
+ }
+
+ delete file_chooser;
+ }
+
+ void RecreateConsole() {
+ pp::Var doc = GetWindowObject().GetProperty("document");
+ pp::Var body = doc.GetProperty("body");
+ if (!console_.is_undefined())
+ body.Call("removeChild", console_);
+
+ console_ = doc.Call("createElement", "pre");
+ console_.SetProperty("id", "console");
+ console_.GetProperty("style").SetProperty("backgroundColor", "lightgray");
+ body.Call("appendChild", console_);
+ }
+
+ void Log(const pp::Var& var) {
+ pp::Var doc = GetWindowObject().GetProperty("document");
+ console_.Call("appendChild", doc.Call("createTextNode", var));
+ console_.Call("appendChild", doc.Call("createTextNode", "\n"));
+ }
+
+ pp::CompletionCallbackFactory<MyInstance> callback_factory_;
+ pp::Var console_;
+};
+
+class MyModule : public pp::Module {
+ public:
+ MyModule() : pp::Module() {}
+ virtual ~MyModule() {}
+
+ virtual pp::Instance* CreateInstance(PP_Instance instance) {
+ return new MyInstance(instance);
+ }
+};
+
+namespace pp {
+
+// Factory function for your specialization of the Module object.
+Module* CreateModule() {
+ return new MyModule();
+}
+
+} // namespace pp
diff --git a/ppapi/examples/font/simple_font.cc b/ppapi/examples/font/simple_font.cc
new file mode 100644
index 0000000..26c8c2d
--- /dev/null
+++ b/ppapi/examples/font/simple_font.cc
@@ -0,0 +1,67 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ppapi/cpp/completion_callback.h"
+#include "ppapi/cpp/dev/font_dev.h"
+#include "ppapi/cpp/graphics_2d.h"
+#include "ppapi/cpp/image_data.h"
+#include "ppapi/cpp/instance.h"
+#include "ppapi/cpp/module.h"
+#include "ppapi/cpp/rect.h"
+#include "ppapi/cpp/size.h"
+
+static void DummyCompletionCallback(void* /*user_data*/, int32_t /*result*/) {
+}
+
+class MyInstance : public pp::Instance {
+ public:
+ MyInstance(PP_Instance instance)
+ : pp::Instance(instance) {
+ }
+
+ virtual void ViewChanged(const pp::Rect& position, const pp::Rect& clip) {
+ if (position.size() == last_size_)
+ return;
+ last_size_ = position.size();
+
+ pp::ImageData image(PP_IMAGEDATAFORMAT_BGRA_PREMUL, last_size_, true);
+ pp::Graphics2D device(last_size_, false);
+ BindGraphics(device);
+
+ pp::FontDescription_Dev desc;
+ desc.set_family(PP_FONTFAMILY_SANSSERIF);
+ desc.set_size(30);
+ pp::Font_Dev font(desc);
+
+ pp::Rect text_clip(position.size()); // Use entire bounds for clip.
+ font.DrawTextAt(&image,
+ pp::TextRun_Dev("\xD9\x85\xD8\xB1\xD8\xAD\xD8\xA8\xD8\xA7\xE2\x80\x8E",
+ true, true),
+ pp::Point(10, 40), 0xFF008000, clip, false);
+ font.DrawTextAt(&image, pp::TextRun_Dev("Hello"),
+ pp::Point(10, 80), 0xFF000080, text_clip, false);
+
+ device.PaintImageData(image, pp::Point(0, 0));
+ device.Flush(pp::CompletionCallback(&DummyCompletionCallback, NULL));
+ }
+
+ private:
+ pp::Size last_size_;
+};
+
+class MyModule : public pp::Module {
+ public:
+ virtual pp::Instance* CreateInstance(PP_Instance instance) {
+ return new MyInstance(instance);
+ }
+};
+
+namespace pp {
+
+// Factory function for your specialization of the Module object.
+Module* CreateModule() {
+ return new MyModule();
+}
+
+} // namespace pp
diff --git a/ppapi/examples/stub/stub.c b/ppapi/examples/stub/stub.c
new file mode 100644
index 0000000..8786669
--- /dev/null
+++ b/ppapi/examples/stub/stub.c
@@ -0,0 +1,35 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// This is the simplest possible C Pepper plugin that does nothing. If you're
+// using C++, you will want to look at stub.cc which uses the more convenient
+// C++ wrappers.
+
+#include <stddef.h>
+
+#include "ppapi/c/pp_errors.h"
+#include "ppapi/c/pp_module.h"
+#include "ppapi/c/ppb.h"
+#include "ppapi/c/ppp.h"
+
+PP_Module g_module_id;
+PPB_GetInterface g_get_browser_interface = NULL;
+
+PP_EXPORT int32_t PPP_InitializeModule(PP_Module module_id,
+ PPB_GetInterface get_browser_interface) {
+ // Save the global module information for later.
+ g_module_id = module_id;
+ g_get_browser_interface = get_browser_interface;
+
+ return PP_OK;
+}
+
+PP_EXPORT void PPP_ShutdownModule() {
+}
+
+PP_EXPORT const void* PPP_GetInterface(const char* interface_name) {
+ // You will normally implement a getter for at least PPP_INSTANCE_INTERFACE
+ // here.
+ return NULL;
+}
diff --git a/ppapi/examples/stub/stub.cc b/ppapi/examples/stub/stub.cc
new file mode 100644
index 0000000..41628a3
--- /dev/null
+++ b/ppapi/examples/stub/stub.cc
@@ -0,0 +1,41 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ppapi/cpp/instance.h"
+#include "ppapi/cpp/module.h"
+
+// This is the simplest possible C++ Pepper plugin that does nothing.
+
+// This object represents one time the page says <embed>.
+class MyInstance : public pp::Instance {
+ public:
+ explicit MyInstance(PP_Instance instance) : pp::Instance(instance) {}
+ virtual ~MyInstance() {}
+
+ virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
+ return true;
+ }
+};
+
+// This object is the global object representing this plugin library as long
+// as it is loaded.
+class MyModule : public pp::Module {
+ public:
+ MyModule() : pp::Module() {}
+ virtual ~MyModule() {}
+
+ // Override CreateInstance to create your customized Instance object.
+ virtual pp::Instance* CreateInstance(PP_Instance instance) {
+ return new MyInstance(instance);
+ }
+};
+
+namespace pp {
+
+// Factory function for your specialization of the Module object.
+Module* CreateModule() {
+ return new MyModule();
+}
+
+} // namespace pp