summaryrefslogtreecommitdiffstats
path: root/ppapi/utility
diff options
context:
space:
mode:
Diffstat (limited to 'ppapi/utility')
-rw-r--r--ppapi/utility/DEPS15
-rw-r--r--ppapi/utility/README.txt2
-rw-r--r--ppapi/utility/completion_callback_factory.h564
-rw-r--r--ppapi/utility/graphics/paint_aggregator.cc279
-rw-r--r--ppapi/utility/graphics/paint_aggregator.h174
-rw-r--r--ppapi/utility/graphics/paint_manager.cc203
-rw-r--r--ppapi/utility/graphics/paint_manager.h297
-rw-r--r--ppapi/utility/non_thread_safe_ref_count.h60
8 files changed, 1594 insertions, 0 deletions
diff --git a/ppapi/utility/DEPS b/ppapi/utility/DEPS
new file mode 100644
index 0000000..768eadb
--- /dev/null
+++ b/ppapi/utility/DEPS
@@ -0,0 +1,15 @@
+# ppapi/cpp should not be dependent on other parts of chromium; it should stay
+# browser-neutral as much as possible.
+include_rules = [
+ "-base",
+ "-build",
+ "-ipc",
+ "-ppapi",
+ "+ppapi/c",
+ "-ppapi/c/private",
+ "-ppapi/c/trusted",
+ "+ppapi/cpp",
+ "-ppapi/cpp/private",
+ "-ppapi/cpp/trusted",
+ "+ppapi/utility",
+]
diff --git a/ppapi/utility/README.txt b/ppapi/utility/README.txt
new file mode 100644
index 0000000..f2bb80d
--- /dev/null
+++ b/ppapi/utility/README.txt
@@ -0,0 +1,2 @@
+The classes in ppapi/utility are helper classes that provide higher-level
+features on top of the C++ wrappers. Using these classes is optional.
diff --git a/ppapi/utility/completion_callback_factory.h b/ppapi/utility/completion_callback_factory.h
new file mode 100644
index 0000000..b656024
--- /dev/null
+++ b/ppapi/utility/completion_callback_factory.h
@@ -0,0 +1,564 @@
+// Copyright (c) 2011 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.
+
+#ifndef PPAPI_UTILITY_COMPLETION_CALLBACK_FACTORY_H_
+#define PPAPI_UTILITY_COMPLETION_CALLBACK_FACTORY_H_
+
+#include "ppapi/cpp/completion_callback.h"
+#include "ppapi/utility/non_thread_safe_ref_count.h"
+
+namespace pp {
+
+/// CompletionCallbackFactory<T> may be used to create CompletionCallback
+/// objects that are bound to member functions.
+///
+/// If a factory is destroyed, then any pending callbacks will be cancelled
+/// preventing any bound member functions from being called. The CancelAll()
+/// method allows pending callbacks to be cancelled without destroying the
+/// factory.
+///
+/// <strong>Note: </strong><code>CompletionCallbackFactory<T></code> isn't
+/// thread safe, but you can make it more thread-friendly by passing a
+/// thread-safe refcounting class as the second template element. However, it
+/// only guarantees safety for creating a callback from another thread, the
+/// callback itself needs to execute on the same thread as the thread that
+/// creates/destroys the factory. With this restriction, it is safe to create
+/// the <code>CompletionCallbackFactory</code> on the main thread, create
+/// callbacks from any thread and pass them to CallOnMainThread().
+///
+/// <strong>Example: </strong>
+///
+/// @code
+///
+/// class MyHandler {
+/// public:
+/// // If an compiler warns on following using |this| in the initializer
+/// // list, use PP_ALLOW_THIS_IN_INITIALIZER_LIST macro.
+/// MyHandler() : factory_(this), offset_(0) {
+/// }
+///
+/// void ProcessFile(const FileRef& file) {
+/// CompletionCallback cc = factory_.NewRequiredCallback(
+/// &MyHandler::DidOpen);
+/// int32_t rv = fio_.Open(file, PP_FileOpenFlag_Read, cc);
+/// CHECK(rv == PP_OK_COMPLETIONPENDING);
+/// }
+///
+/// private:
+/// CompletionCallback NewCallback() {
+/// return factory_.NewCallback(&MyHandler::DidCompleteIO);
+/// }
+///
+/// void DidOpen(int32_t result) {
+/// if (result == PP_OK) {
+/// // The file is open, and we can begin reading.
+/// offset_ = 0;
+/// ReadMore();
+/// } else {
+/// // Failed to open the file with error given by 'result'.
+/// }
+/// }
+///
+/// void DidRead(int32_t result) {
+/// if (result > 0) {
+/// // buf_ now contains 'result' number of bytes from the file.
+/// ProcessBytes(buf_, result);
+/// offset_ += result;
+/// ReadMore();
+/// } else {
+/// // Done reading (possibly with an error given by 'result').
+/// }
+/// }
+///
+/// void ReadMore() {
+/// CompletionCallback cc =
+/// factory_.NewOptionalCallback(&MyHandler::DidRead);
+/// int32_t rv = fio_.Read(offset_, buf_, sizeof(buf_),
+/// cc.pp_completion_callback());
+/// if (rv != PP_OK_COMPLETIONPENDING)
+/// cc.Run(rv);
+/// }
+///
+/// void ProcessBytes(const char* bytes, int32_t length) {
+/// // Do work ...
+/// }
+///
+/// pp::CompletionCallbackFactory<MyHandler> factory_;
+/// pp::FileIO fio_;
+/// char buf_[4096];
+/// int64_t offset_;
+/// };
+///
+/// @endcode
+///
+template <typename T, typename RefCount = NonThreadSafeRefCount>
+class CompletionCallbackFactory {
+ public:
+
+ /// This constructor creates a <code>CompletionCallbackFactory</code>
+ /// bound to an object. If the constructor is called without an argument,
+ /// the default value of <code>NULL</code> is used. The user then must call
+ /// Initialize() to initialize the object.
+ ///
+ /// param[in] object Optional parameter. An object whose member functions
+ /// are to be bound to CompletionCallbacks created by this
+ /// <code>CompletionCallbackFactory</code>. The default value of this
+ /// parameter is <code>NULL</code>.
+ explicit CompletionCallbackFactory(T* object = NULL)
+ : object_(object) {
+ InitBackPointer();
+ }
+
+ /// Destructor.
+ ~CompletionCallbackFactory() {
+ ResetBackPointer();
+ }
+
+ /// CancelAll() cancels all <code>CompletionCallbacks</code> allocated from
+ /// this factory.
+ void CancelAll() {
+ ResetBackPointer();
+ InitBackPointer();
+ }
+ /// Initialize() binds the <code>CallbackFactory</code> to a particular
+ /// object. Use this when the object is not available at
+ /// <code>CallbackFactory</code> creation, and the <code>NULL</code> default
+ /// is passed to the constructor. The object may only be initialized once,
+ /// either by the constructor, or by a call to Initialize().
+ ///
+ /// @param[in] object The object whose member functions are to be bound to
+ /// the <code>CompletionCallback</code> created by this
+ /// <code>CompletionCallbackFactory</code>.
+ void Initialize(T* object) {
+ PP_DCHECK(object);
+ PP_DCHECK(!object_); // May only initialize once!
+ object_ = object;
+ }
+
+ /// GetObject() returns the object that was passed at initialization to
+ /// Intialize().
+ ///
+ /// @return the object passed to the constructor or Intialize().
+ T* GetObject() {
+ return object_;
+ }
+
+ /// NewCallback allocates a new, single-use <code>CompletionCallback</code>.
+ /// The <code>CompletionCallback</code> must be run in order for the memory
+ /// allocated by the methods to be freed.
+ /// NewCallback() is equivalent to NewRequiredCallback() below.
+ ///
+ /// @param[in] method The method to be invoked upon completion of the
+ /// operation.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method>
+ CompletionCallback NewCallback(Method method) {
+ PP_DCHECK(object_);
+ return NewCallbackHelper(Dispatcher0<Method>(method));
+ }
+
+ /// NewRequiredCallback() allocates a new, single-use
+ /// <code>CompletionCallback</code> that will always run. The
+ /// <code>CompletionCallback</code> must be run in order for the memory
+ /// allocated by the methods to be freed.
+ ///
+ /// @param[in] method The method to be invoked upon completion of the
+ /// operation.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method>
+ CompletionCallback NewRequiredCallback(Method method) {
+ CompletionCallback cc = NewCallback(method);
+ cc.set_flags(cc.flags() & ~PP_COMPLETIONCALLBACK_FLAG_OPTIONAL);
+ return cc;
+ }
+
+ /// NewOptionalCallback() allocates a new, single-use
+ /// <code>CompletionCallback</code> that might not run if the method
+ /// taking it can complete synchronously. Thus, if after passing the
+ /// CompletionCallback to a Pepper method, the method does not return
+ /// PP_OK_COMPLETIONPENDING, then you should manually call the
+ /// CompletionCallback's Run method, or memory will be leaked.
+ ///
+ /// @param[in] method The method to be invoked upon completion of the
+ /// operation.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method>
+ CompletionCallback NewOptionalCallback(Method method) {
+ CompletionCallback cc = NewCallback(method);
+ cc.set_flags(cc.flags() | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL);
+ return cc;
+ }
+
+ /// NewCallback() allocates a new, single-use <code>CompletionCallback</code>.
+ /// The <code>CompletionCallback</code> must be run in order for the memory
+ /// allocated by the methods to be freed.
+ /// NewCallback() is equivalent to NewRequiredCallback() below.
+ ///
+ /// @param[in] method The method to be invoked upon completion of the
+ /// operation. Method should be of type:
+ /// <code>void (T::*)(int32_t result, const A& a)</code>
+ ///
+ /// @param[in] a Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method, typename A>
+ CompletionCallback NewCallback(Method method, const A& a) {
+ PP_DCHECK(object_);
+ return NewCallbackHelper(Dispatcher1<Method, A>(method, a));
+ }
+
+ /// NewRequiredCallback() allocates a new, single-use
+ /// <code>CompletionCallback</code> that will always run. The
+ /// <code>CompletionCallback</code> must be run in order for the memory
+ /// allocated by the methods to be freed.
+ ///
+ /// @param[in] method The method to be invoked upon completion of the
+ /// operation. Method should be of type:
+ /// <code>void (T::*)(int32_t result, const A& a)</code>
+ ///
+ /// @param[in] a Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method, typename A>
+ CompletionCallback NewRequiredCallback(Method method, const A& a) {
+ CompletionCallback cc = NewCallback(method, a);
+ cc.set_flags(cc.flags() & ~PP_COMPLETIONCALLBACK_FLAG_OPTIONAL);
+ return cc;
+ }
+
+ /// NewOptionalCallback() allocates a new, single-use
+ /// <code>CompletionCallback</code> that might not run if the method
+ /// taking it can complete synchronously. Thus, if after passing the
+ /// CompletionCallback to a Pepper method, the method does not return
+ /// PP_OK_COMPLETIONPENDING, then you should manually call the
+ /// CompletionCallback's Run method, or memory will be leaked.
+ ///
+ /// @param[in] method The method to be invoked upon completion of the
+ /// operation. Method should be of type:
+ /// <code>void (T::*)(int32_t result, const A& a)</code>
+ ///
+ /// @param[in] a Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method, typename A>
+ CompletionCallback NewOptionalCallback(Method method, const A& a) {
+ CompletionCallback cc = NewCallback(method, a);
+ cc.set_flags(cc.flags() | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL);
+ return cc;
+ }
+
+ /// NewCallback() allocates a new, single-use
+ /// <code>CompletionCallback</code>.
+ /// The <code>CompletionCallback</code> must be run in order for the memory
+ /// allocated by the methods to be freed.
+ /// NewCallback() is equivalent to NewRequiredCallback() below.
+ ///
+ /// @param method The method taking the callback. Method should be of type:
+ /// <code>void (T::*)(int32_t result, const A& a, const B& b)</code>
+ ///
+ /// @param[in] a Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @param[in] b Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method, typename A, typename B>
+ CompletionCallback NewCallback(Method method, const A& a, const B& b) {
+ PP_DCHECK(object_);
+ return NewCallbackHelper(Dispatcher2<Method, A, B>(method, a, b));
+ }
+
+ /// NewRequiredCallback() allocates a new, single-use
+ /// <code>CompletionCallback</code> that will always run. The
+ /// <code>CompletionCallback</code> must be run in order for the memory
+ /// allocated by the methods to be freed.
+ ///
+ /// @param method The method taking the callback. Method should be of type:
+ /// <code>void (T::*)(int32_t result, const A& a, const B& b)</code>
+ ///
+ /// @param[in] a Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @param[in] b Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method, typename A, typename B>
+ CompletionCallback NewRequiredCallback(Method method, const A& a,
+ const B& b) {
+ CompletionCallback cc = NewCallback(method, a, b);
+ cc.set_flags(cc.flags() & ~PP_COMPLETIONCALLBACK_FLAG_OPTIONAL);
+ return cc;
+ }
+
+ /// NewOptionalCallback() allocates a new, single-use
+ /// <code>CompletionCallback</code> that might not run if the method
+ /// taking it can complete synchronously. Thus, if after passing the
+ /// CompletionCallback to a Pepper method, the method does not return
+ /// PP_OK_COMPLETIONPENDING, then you should manually call the
+ /// CompletionCallback's Run method, or memory will be leaked.
+ ///
+ /// @param[in] method The method taking the callback. Method should be of
+ /// type:
+ /// <code>void (T::*)(int32_t result, const A& a, const B& b)</code>
+ ///
+ /// @param[in] a Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @param[in] b Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method, typename A, typename B>
+ CompletionCallback NewOptionalCallback(Method method, const A& a,
+ const B& b) {
+ CompletionCallback cc = NewCallback(method, a, b);
+ cc.set_flags(cc.flags() | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL);
+ return cc;
+ }
+
+ /// NewCallback() allocates a new, single-use
+ /// <code>CompletionCallback</code>.
+ /// The <code>CompletionCallback</code> must be run in order for the memory
+ /// allocated by the methods to be freed.
+ /// NewCallback() is equivalent to NewRequiredCallback() below.
+ ///
+ /// @param method The method taking the callback. Method should be of type:
+ /// <code>
+ /// void (T::*)(int32_t result, const A& a, const B& b, const C& c)
+ /// </code>
+ ///
+ /// @param[in] a Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @param[in] b Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @param[in] c Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method, typename A, typename B, typename C>
+ CompletionCallback NewCallback(Method method, const A& a, const B& b,
+ const C& c) {
+ PP_DCHECK(object_);
+ return NewCallbackHelper(Dispatcher3<Method, A, B, C>(method, a, b, c));
+ }
+
+ /// NewRequiredCallback() allocates a new, single-use
+ /// <code>CompletionCallback</code> that will always run. The
+ /// <code>CompletionCallback</code> must be run in order for the memory
+ /// allocated by the methods to be freed.
+ ///
+ /// @param method The method taking the callback. Method should be of type:
+ /// <code>
+ /// void (T::*)(int32_t result, const A& a, const B& b, const C& c)
+ /// </code>
+ ///
+ /// @param[in] a Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @param[in] b Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @param[in] c Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method, typename A, typename B, typename C>
+ CompletionCallback NewRequiredCallback(Method method, const A& a,
+ const B& b, const C& c) {
+ CompletionCallback cc = NewCallback(method, a, b, c);
+ cc.set_flags(cc.flags() & ~PP_COMPLETIONCALLBACK_FLAG_OPTIONAL);
+ return cc;
+ }
+
+ /// NewOptionalCallback() allocates a new, single-use
+ /// <code>CompletionCallback</code> that might not run if the method
+ /// taking it can complete synchronously. Thus, if after passing the
+ /// CompletionCallback to a Pepper method, the method does not return
+ /// PP_OK_COMPLETIONPENDING, then you should manually call the
+ /// CompletionCallback's Run method, or memory will be leaked.
+ ///
+ /// @param[in] method The method taking the callback. Method should be of
+ /// type:
+ /// <code>
+ /// void (T::*)(int32_t result, const A& a, const B& b, const C& c)
+ /// </code>
+ ///
+ /// @param[in] a Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @param[in] b Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @param[in] c Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method, typename A, typename B, typename C>
+ CompletionCallback NewOptionalCallback(Method method, const A& a,
+ const B& b, const C& c) {
+ CompletionCallback cc = NewCallback(method, a, b, c);
+ cc.set_flags(cc.flags() | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL);
+ return cc;
+ }
+
+ private:
+ class BackPointer {
+ public:
+ typedef CompletionCallbackFactory<T, RefCount> FactoryType;
+
+ BackPointer(FactoryType* factory)
+ : factory_(factory) {
+ }
+
+ void AddRef() {
+ ref_.AddRef();
+ }
+
+ void Release() {
+ if (ref_.Release() == 0)
+ delete this;
+ }
+
+ void DropFactory() {
+ factory_ = NULL;
+ }
+
+ T* GetObject() {
+ return factory_ ? factory_->GetObject() : NULL;
+ }
+
+ private:
+ RefCount ref_;
+ FactoryType* factory_;
+ };
+
+ template <typename Dispatcher>
+ class CallbackData {
+ public:
+ CallbackData(BackPointer* back_pointer, const Dispatcher& dispatcher)
+ : back_pointer_(back_pointer),
+ dispatcher_(dispatcher) {
+ back_pointer_->AddRef();
+ }
+
+ ~CallbackData() {
+ back_pointer_->Release();
+ }
+
+ static void Thunk(void* user_data, int32_t result) {
+ Self* self = static_cast<Self*>(user_data);
+ T* object = self->back_pointer_->GetObject();
+ if (object)
+ self->dispatcher_(object, result);
+ delete self;
+ }
+
+ private:
+ typedef CallbackData<Dispatcher> Self;
+ BackPointer* back_pointer_;
+ Dispatcher dispatcher_;
+ };
+
+ template <typename Method>
+ class Dispatcher0 {
+ public:
+ Dispatcher0(Method method) : method_(method) {
+ }
+ void operator()(T* object, int32_t result) {
+ (object->*method_)(result);
+ }
+ private:
+ Method method_;
+ };
+
+ template <typename Method, typename A>
+ class Dispatcher1 {
+ public:
+ Dispatcher1(Method method, const A& a)
+ : method_(method),
+ a_(a) {
+ }
+ void operator()(T* object, int32_t result) {
+ (object->*method_)(result, a_);
+ }
+ private:
+ Method method_;
+ A a_;
+ };
+
+ template <typename Method, typename A, typename B>
+ class Dispatcher2 {
+ public:
+ Dispatcher2(Method method, const A& a, const B& b)
+ : method_(method),
+ a_(a),
+ b_(b) {
+ }
+ void operator()(T* object, int32_t result) {
+ (object->*method_)(result, a_, b_);
+ }
+ private:
+ Method method_;
+ A a_;
+ B b_;
+ };
+
+ template <typename Method, typename A, typename B, typename C>
+ class Dispatcher3 {
+ public:
+ Dispatcher3(Method method, const A& a, const B& b, const C& c)
+ : method_(method),
+ a_(a),
+ b_(b),
+ c_(c) {
+ }
+ void operator()(T* object, int32_t result) {
+ (object->*method_)(result, a_, b_, c_);
+ }
+ private:
+ Method method_;
+ A a_;
+ B b_;
+ C c_;
+ };
+
+ void InitBackPointer() {
+ back_pointer_ = new BackPointer(this);
+ back_pointer_->AddRef();
+ }
+
+ void ResetBackPointer() {
+ back_pointer_->DropFactory();
+ back_pointer_->Release();
+ }
+
+ template <typename Dispatcher>
+ CompletionCallback NewCallbackHelper(const Dispatcher& dispatcher) {
+ PP_DCHECK(object_); // Expects a non-null object!
+ return CompletionCallback(
+ &CallbackData<Dispatcher>::Thunk,
+ new CallbackData<Dispatcher>(back_pointer_, dispatcher));
+ }
+
+ // Disallowed:
+ CompletionCallbackFactory(const CompletionCallbackFactory&);
+ CompletionCallbackFactory& operator=(const CompletionCallbackFactory&);
+
+ T* object_;
+ BackPointer* back_pointer_;
+};
+
+} // namespace pp
+
+#endif // PPAPI_UTILITY_COMPLETION_CALLBACK_FACTORY_H_
diff --git a/ppapi/utility/graphics/paint_aggregator.cc b/ppapi/utility/graphics/paint_aggregator.cc
new file mode 100644
index 0000000..d4fc721
--- /dev/null
+++ b/ppapi/utility/graphics/paint_aggregator.cc
@@ -0,0 +1,279 @@
+// Copyright (c) 2011 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/utility/graphics/paint_aggregator.h"
+
+#include <algorithm>
+
+#include "ppapi/cpp/logging.h"
+
+// ----------------------------------------------------------------------------
+// ALGORITHM NOTES
+//
+// We attempt to maintain a scroll rect in the presence of invalidations that
+// are contained within the scroll rect. If an invalidation crosses a scroll
+// rect, then we just treat the scroll rect as an invalidation rect.
+//
+// For invalidations performed prior to scrolling and contained within the
+// scroll rect, we offset the invalidation rects to account for the fact that
+// the consumer will perform scrolling before painting.
+//
+// We only support scrolling along one axis at a time. A diagonal scroll will
+// therefore be treated as an invalidation.
+// ----------------------------------------------------------------------------
+
+namespace pp {
+
+PaintAggregator::PaintUpdate::PaintUpdate() : has_scroll(false) {}
+
+PaintAggregator::PaintUpdate::~PaintUpdate() {}
+
+PaintAggregator::InternalPaintUpdate::InternalPaintUpdate() {}
+
+PaintAggregator::InternalPaintUpdate::~InternalPaintUpdate() {}
+
+Rect PaintAggregator::InternalPaintUpdate::GetScrollDamage() const {
+ // Should only be scrolling in one direction at a time.
+ PP_DCHECK(!(scroll_delta.x() && scroll_delta.y()));
+
+ Rect damaged_rect;
+
+ // Compute the region we will expose by scrolling, and paint that into a
+ // shared memory section.
+ if (scroll_delta.x()) {
+ int32_t dx = scroll_delta.x();
+ damaged_rect.set_y(scroll_rect.y());
+ damaged_rect.set_height(scroll_rect.height());
+ if (dx > 0) {
+ damaged_rect.set_x(scroll_rect.x());
+ damaged_rect.set_width(dx);
+ } else {
+ damaged_rect.set_x(scroll_rect.right() + dx);
+ damaged_rect.set_width(-dx);
+ }
+ } else {
+ int32_t dy = scroll_delta.y();
+ damaged_rect.set_x(scroll_rect.x());
+ damaged_rect.set_width(scroll_rect.width());
+ if (dy > 0) {
+ damaged_rect.set_y(scroll_rect.y());
+ damaged_rect.set_height(dy);
+ } else {
+ damaged_rect.set_y(scroll_rect.bottom() + dy);
+ damaged_rect.set_height(-dy);
+ }
+ }
+
+ // In case the scroll offset exceeds the width/height of the scroll rect
+ return scroll_rect.Intersect(damaged_rect);
+}
+
+Rect PaintAggregator::InternalPaintUpdate::GetPaintBounds() const {
+ Rect bounds;
+ for (size_t i = 0; i < paint_rects.size(); ++i)
+ bounds = bounds.Union(paint_rects[i]);
+ return bounds;
+}
+
+PaintAggregator::PaintAggregator()
+ : max_redundant_paint_to_scroll_area_(0.8f),
+ max_paint_rects_(10) {
+}
+
+bool PaintAggregator::HasPendingUpdate() const {
+ return !update_.scroll_rect.IsEmpty() || !update_.paint_rects.empty();
+}
+
+void PaintAggregator::ClearPendingUpdate() {
+ update_ = InternalPaintUpdate();
+}
+
+PaintAggregator::PaintUpdate PaintAggregator::GetPendingUpdate() const {
+ // Convert the internal paint update to the external one, which includes a
+ // bit more precomputed info for the caller.
+ PaintUpdate ret;
+ ret.scroll_delta = update_.scroll_delta;
+ ret.scroll_rect = update_.scroll_rect;
+ ret.has_scroll = ret.scroll_delta.x() != 0 || ret.scroll_delta.y() != 0;
+
+ ret.paint_rects.reserve(update_.paint_rects.size() + 1);
+ for (size_t i = 0; i < update_.paint_rects.size(); i++)
+ ret.paint_rects.push_back(update_.paint_rects[i]);
+
+ ret.paint_bounds = update_.GetPaintBounds();
+
+ // Also include the scroll damage (if any) in the paint rects.
+ if (ret.has_scroll) {
+ PP_Rect scroll_damage = update_.GetScrollDamage();
+ ret.paint_rects.push_back(scroll_damage);
+ ret.paint_bounds = ret.paint_bounds.Union(scroll_damage);
+ }
+
+ return ret;
+}
+
+void PaintAggregator::InvalidateRect(const Rect& rect) {
+ // Combine overlapping paints using smallest bounding box.
+ for (size_t i = 0; i < update_.paint_rects.size(); ++i) {
+ const Rect& existing_rect = update_.paint_rects[i];
+ if (existing_rect.Contains(rect)) // Optimize out redundancy.
+ return;
+ if (rect.Intersects(existing_rect) || rect.SharesEdgeWith(existing_rect)) {
+ // Re-invalidate in case the union intersects other paint rects.
+ Rect combined_rect = existing_rect.Union(rect);
+ update_.paint_rects.erase(update_.paint_rects.begin() + i);
+ InvalidateRect(combined_rect);
+ return;
+ }
+ }
+
+ // Add a non-overlapping paint.
+ update_.paint_rects.push_back(rect);
+
+ // If the new paint overlaps with a scroll, then it forces an invalidation of
+ // the scroll. If the new paint is contained by a scroll, then trim off the
+ // scroll damage to avoid redundant painting.
+ if (!update_.scroll_rect.IsEmpty()) {
+ if (ShouldInvalidateScrollRect(rect)) {
+ InvalidateScrollRect();
+ } else if (update_.scroll_rect.Contains(rect)) {
+ update_.paint_rects[update_.paint_rects.size() - 1] =
+ rect.Subtract(update_.GetScrollDamage());
+ if (update_.paint_rects[update_.paint_rects.size() - 1].IsEmpty())
+ update_.paint_rects.erase(update_.paint_rects.end() - 1);
+ }
+ }
+
+ if (update_.paint_rects.size() > max_paint_rects_)
+ CombinePaintRects();
+}
+
+void PaintAggregator::ScrollRect(const Rect& clip_rect, const Point& amount) {
+ // We only support scrolling along one axis at a time.
+ if (amount.x() != 0 && amount.y() != 0) {
+ InvalidateRect(clip_rect);
+ return;
+ }
+
+ // We can only scroll one rect at a time.
+ if (!update_.scroll_rect.IsEmpty() && update_.scroll_rect != clip_rect) {
+ InvalidateRect(clip_rect);
+ return;
+ }
+
+ // Again, we only support scrolling along one axis at a time. Make sure this
+ // update doesn't scroll on a different axis than any existing one.
+ if ((amount.x() && update_.scroll_delta.y()) ||
+ (amount.y() && update_.scroll_delta.x())) {
+ InvalidateRect(clip_rect);
+ return;
+ }
+
+ // The scroll rect is new or isn't changing (though the scroll amount may
+ // be changing).
+ update_.scroll_rect = clip_rect;
+ update_.scroll_delta += amount;
+
+ // We might have just wiped out a pre-existing scroll.
+ if (update_.scroll_delta == Point()) {
+ update_.scroll_rect = Rect();
+ return;
+ }
+
+ // Adjust any contained paint rects and check for any overlapping paints.
+ for (size_t i = 0; i < update_.paint_rects.size(); ++i) {
+ if (update_.scroll_rect.Contains(update_.paint_rects[i])) {
+ update_.paint_rects[i] = ScrollPaintRect(update_.paint_rects[i], amount);
+ // The rect may have been scrolled out of view.
+ if (update_.paint_rects[i].IsEmpty()) {
+ update_.paint_rects.erase(update_.paint_rects.begin() + i);
+ i--;
+ }
+ } else if (update_.scroll_rect.Intersects(update_.paint_rects[i])) {
+ InvalidateScrollRect();
+ return;
+ }
+ }
+
+ // If the new scroll overlaps too much with contained paint rects, then force
+ // an invalidation of the scroll.
+ if (ShouldInvalidateScrollRect(Rect()))
+ InvalidateScrollRect();
+}
+
+Rect PaintAggregator::ScrollPaintRect(const Rect& paint_rect,
+ const Point& amount) const {
+ Rect result = paint_rect;
+
+ result.Offset(amount);
+ result = update_.scroll_rect.Intersect(result);
+
+ // Subtract out the scroll damage rect to avoid redundant painting.
+ return result.Subtract(update_.GetScrollDamage());
+}
+
+bool PaintAggregator::ShouldInvalidateScrollRect(const Rect& rect) const {
+ if (!rect.IsEmpty()) {
+ if (!update_.scroll_rect.Intersects(rect))
+ return false;
+
+ if (!update_.scroll_rect.Contains(rect))
+ return true;
+ }
+
+ // Check if the combined area of all contained paint rects plus this new
+ // rect comes too close to the area of the scroll_rect. If so, then we
+ // might as well invalidate the scroll rect.
+
+ int paint_area = rect.size().GetArea();
+ for (size_t i = 0; i < update_.paint_rects.size(); ++i) {
+ const Rect& existing_rect = update_.paint_rects[i];
+ if (update_.scroll_rect.Contains(existing_rect))
+ paint_area += existing_rect.size().GetArea();
+ }
+ int scroll_area = update_.scroll_rect.size().GetArea();
+ if (float(paint_area) / float(scroll_area) > max_redundant_paint_to_scroll_area_)
+ return true;
+
+ return false;
+}
+
+void PaintAggregator::InvalidateScrollRect() {
+ Rect scroll_rect = update_.scroll_rect;
+ update_.scroll_rect = Rect();
+ update_.scroll_delta = Point();
+ InvalidateRect(scroll_rect);
+}
+
+void PaintAggregator::CombinePaintRects() {
+ // Combine paint rects down to at most two rects: one inside the scroll_rect
+ // and one outside the scroll_rect. If there is no scroll_rect, then just
+ // use the smallest bounding box for all paint rects.
+ //
+ // NOTE: This is a fairly simple algorithm. We could get fancier by only
+ // combining two rects to get us under the max_paint_rects limit, but if we
+ // reach this method then it means we're hitting a rare case, so there's no
+ // need to over-optimize it.
+ //
+ if (update_.scroll_rect.IsEmpty()) {
+ Rect bounds = update_.GetPaintBounds();
+ update_.paint_rects.clear();
+ update_.paint_rects.push_back(bounds);
+ } else {
+ Rect inner, outer;
+ for (size_t i = 0; i < update_.paint_rects.size(); ++i) {
+ const Rect& existing_rect = update_.paint_rects[i];
+ if (update_.scroll_rect.Contains(existing_rect)) {
+ inner = inner.Union(existing_rect);
+ } else {
+ outer = outer.Union(existing_rect);
+ }
+ }
+ update_.paint_rects.clear();
+ update_.paint_rects.push_back(inner);
+ update_.paint_rects.push_back(outer);
+ }
+}
+
+} // namespace pp
diff --git a/ppapi/utility/graphics/paint_aggregator.h b/ppapi/utility/graphics/paint_aggregator.h
new file mode 100644
index 0000000..74e24da
--- /dev/null
+++ b/ppapi/utility/graphics/paint_aggregator.h
@@ -0,0 +1,174 @@
+// Copyright (c) 2011 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.
+
+#ifndef PPAPI_UTILITY_GRAPHICS_PAINT_AGGREGATOR_H_
+#define PPAPI_UTILITY_GRAPHICS_PAINT_AGGREGATOR_H_
+
+#include <stddef.h>
+#include <vector>
+
+#include "ppapi/cpp/point.h"
+#include "ppapi/cpp/rect.h"
+
+/// @file
+/// This file defines the API to aggregate multiple invalidation and scroll
+/// commands to produce a scroll and repaint sequence.
+namespace pp {
+
+/// This class is responsible for aggregating multiple invalidation and scroll
+/// commands to produce a scroll and repaint sequence. You can use this manually
+/// to track your updates, but most applications will use the PaintManager to
+/// additionally handle the necessary callbacks on top of the PaintAggregator
+/// functionality.
+///
+/// Refer to <code>http://code.google.com/p/ppapi/wiki/2DPaintingModel</code>
+/// for further information.
+class PaintAggregator {
+ public:
+ struct PaintUpdate {
+ /// Default constructor for creating an is_null() <code>PaintUpdate</code>
+ /// object.
+ PaintUpdate();
+
+ /// Destructor.
+ ~PaintUpdate();
+
+ /// True if there is a scroll applied. This indicates that the scroll delta
+ /// and scroll_rect are nonzero (just as a convenience).
+ bool has_scroll;
+
+ /// The amount to scroll by. Either the X or Y may be nonzero to indicate a
+ /// scroll in that direction, but there will never be a scroll in both
+ /// directions at the same time (this will be converted to a paint of the
+ /// region instead).
+ ///
+ /// If there is no scroll, this will be (0, 0).
+ Point scroll_delta;
+
+ /// The rectangle that should be scrolled by the scroll_delta. If there is
+ /// no scroll, this will be (0, 0, 0, 0). We only track one scroll command
+ /// at once. If there are multiple ones, they will be converted to
+ /// invalidates.
+ Rect scroll_rect;
+
+ /// A list of all the individual dirty rectangles. This is an aggregated
+ /// list of all invalidate calls. Different rectangles may be unified to
+ /// produce a minimal list with no overlap that is more efficient to paint.
+ /// This list also contains the region exposed by any scroll command.
+ std::vector<Rect> paint_rects;
+
+ /// The union of all paint_rects.
+ Rect paint_bounds;
+ };
+
+ /// Default constructor.
+ PaintAggregator();
+
+ /// Setter function setting the max ratio of paint rect area to scroll rect
+ /// area that we will tolerate before downgrading the scroll into a repaint.
+ ///
+ /// If the combined area of paint rects contained within the scroll
+ /// rect grows too large, then we might as well just treat
+ /// the scroll rect as a paint rect.
+ ///
+ /// @param[in] area The max ratio of paint rect area to scroll rect area that
+ /// we will tolerate before downgrading the scroll into a repaint.
+ void set_max_redundant_paint_to_scroll_area(float area) {
+ max_redundant_paint_to_scroll_area_ = area;
+ }
+
+ /// Setter function for setting the maximum number of paint rects. If we
+ /// exceed this limit, then we'll start combining paint rects (see
+ /// CombinePaintRects). This limiting can be important since there is
+ /// typically some overhead in deciding what to paint. If your module is fast
+ /// at doing these computations, raise this threshold, if your module is
+ /// slow, lower it (probably requires some tuning to find the right value).
+ ///
+ /// @param[in] max_rects The maximum number of paint rects.
+ void set_max_paint_rects(size_t max_rects) {
+ max_paint_rects_ = max_rects;
+ }
+
+ /// This function determines if there is a pending update. There is a
+ /// PendingUpdate if InvalidateRect or ScrollRect were called and
+ /// ClearPendingUpdate was not called.
+ ///
+ /// @return true if there is a pending update, otherwise false.
+ bool HasPendingUpdate() const;
+
+ /// This function clears a pending update.
+ void ClearPendingUpdate();
+
+ /// This function gets a pending update.
+ ///
+ /// @return A PaintUpdate containing the pending update.
+ PaintUpdate GetPendingUpdate() const;
+
+ /// This function invalidates the rect so it will be repainted.
+ ///
+ /// @param[in] rect A rect to be repainted.
+ void InvalidateRect(const Rect& rect);
+
+ /// This function adds a pending scroll update.
+ ///
+ /// @param[in] clip_rect The rect to scroll.
+ /// @param[in] amount A Point amount to scroll <code>rect</code>.
+ void ScrollRect(const Rect& clip_rect, const Point& amount);
+
+ private:
+ // This structure is an internal version of PaintUpdate. It's different in
+ // two respects:
+ //
+ // - The scroll damange (area exposed by the scroll operation, if any) is
+ // maintained separately from the dirty rects generated by calling
+ // InvalidateRect. We need to know this distinction for some operations.
+ //
+ // - The paint bounds union is computed on the fly so we don't have to keep
+ // a rectangle up-to-date as we do different operations.
+ class InternalPaintUpdate {
+ public:
+ InternalPaintUpdate();
+ ~InternalPaintUpdate();
+
+ // Computes the rect damaged by scrolling within |scroll_rect| by
+ // |scroll_delta|. This rect must be repainted. It is not included in
+ // paint_rects or in the rect returned by GetPaintBounds.
+ Rect GetScrollDamage() const;
+
+ // Returns the smallest rect containing all paint rects, not including the
+ // scroll damage rect.
+ Rect GetPaintBounds() const;
+
+ Point scroll_delta;
+ Rect scroll_rect;
+
+ // Does not include the scroll damage rect.
+ std::vector<Rect> paint_rects;
+ };
+
+ Rect ScrollPaintRect(const Rect& paint_rect, const Point& amount) const;
+ bool ShouldInvalidateScrollRect(const Rect& rect) const;
+ void InvalidateScrollRect();
+ void CombinePaintRects();
+
+ InternalPaintUpdate update_;
+
+ // If the combined area of paint rects contained within the scroll rect grows
+ // too large, then we might as well just treat the scroll rect as a paint
+ // rect. This constant sets the max ratio of paint rect area to scroll rect
+ // area that we will tolerate before downgrading the scroll into a repaint.
+ float max_redundant_paint_to_scroll_area_;
+
+ // The maximum number of paint rects. If we exceed this limit, then we'll
+ // start combining paint rects (see CombinePaintRects). This limiting can be
+ // important since there is typically some overhead in deciding what to
+ // paint. If your plugin is fast at doing these computations, raise this
+ // threshold, if your plugin is slow, lower it (probably requires some
+ // tuning to find the right value).
+ size_t max_paint_rects_;
+};
+
+} // namespace pp
+
+#endif // PPAPI_UTILITY_PAINT_AGGREGATOR_H_
diff --git a/ppapi/utility/graphics/paint_manager.cc b/ppapi/utility/graphics/paint_manager.cc
new file mode 100644
index 0000000..1141ac6
--- /dev/null
+++ b/ppapi/utility/graphics/paint_manager.cc
@@ -0,0 +1,203 @@
+// Copyright (c) 2011 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/utility/graphics/paint_manager.h"
+
+#include "ppapi/c/pp_errors.h"
+#include "ppapi/cpp/instance.h"
+#include "ppapi/cpp/logging.h"
+#include "ppapi/cpp/module.h"
+
+namespace pp {
+
+PaintManager::PaintManager()
+ : instance_(NULL),
+ client_(NULL),
+ is_always_opaque_(false),
+ callback_factory_(NULL),
+ manual_callback_pending_(false),
+ flush_pending_(false),
+ has_pending_resize_(false) {
+ // Set the callback object outside of the initializer list to avoid a
+ // compiler warning about using "this" in an initializer list.
+ callback_factory_.Initialize(this);
+}
+
+PaintManager::PaintManager(Instance* instance,
+ Client* client,
+ bool is_always_opaque)
+ : instance_(instance),
+ client_(client),
+ is_always_opaque_(is_always_opaque),
+ callback_factory_(NULL),
+ manual_callback_pending_(false),
+ flush_pending_(false),
+ has_pending_resize_(false) {
+ // Set the callback object outside of the initializer list to avoid a
+ // compiler warning about using "this" in an initializer list.
+ callback_factory_.Initialize(this);
+
+ // You can not use a NULL client pointer.
+ PP_DCHECK(client);
+}
+
+PaintManager::~PaintManager() {
+}
+
+void PaintManager::Initialize(Instance* instance,
+ Client* client,
+ bool is_always_opaque) {
+ PP_DCHECK(!instance_ && !client_); // Can't initialize twice.
+ instance_ = instance;
+ client_ = client;
+ is_always_opaque_ = is_always_opaque;
+}
+
+void PaintManager::SetSize(const Size& new_size) {
+ if (GetEffectiveSize() == new_size)
+ return;
+
+ has_pending_resize_ = true;
+ pending_size_ = new_size;
+
+ Invalidate();
+}
+
+void PaintManager::Invalidate() {
+ // You must call SetSize before using.
+ PP_DCHECK(!graphics_.is_null() || has_pending_resize_);
+
+ EnsureCallbackPending();
+ aggregator_.InvalidateRect(Rect(GetEffectiveSize()));
+}
+
+void PaintManager::InvalidateRect(const Rect& rect) {
+ // You must call SetSize before using.
+ PP_DCHECK(!graphics_.is_null() || has_pending_resize_);
+
+ // Clip the rect to the device area.
+ Rect clipped_rect = rect.Intersect(Rect(GetEffectiveSize()));
+ if (clipped_rect.IsEmpty())
+ return; // Nothing to do.
+
+ EnsureCallbackPending();
+ aggregator_.InvalidateRect(clipped_rect);
+}
+
+void PaintManager::ScrollRect(const Rect& clip_rect, const Point& amount) {
+ // You must call SetSize before using.
+ PP_DCHECK(!graphics_.is_null() || has_pending_resize_);
+
+ EnsureCallbackPending();
+ aggregator_.ScrollRect(clip_rect, amount);
+}
+
+Size PaintManager::GetEffectiveSize() const {
+ return has_pending_resize_ ? pending_size_ : graphics_.size();
+}
+
+void PaintManager::EnsureCallbackPending() {
+ // The best way for us to do the next update is to get a notification that
+ // a previous one has completed. So if we're already waiting for one, we
+ // don't have to do anything differently now.
+ if (flush_pending_)
+ return;
+
+ // If no flush is pending, we need to do a manual call to get back to the
+ // main thread. We may have one already pending, or we may need to schedule.
+ if (manual_callback_pending_)
+ return;
+
+ Module::Get()->core()->CallOnMainThread(
+ 0,
+ callback_factory_.NewCallback(&PaintManager::OnManualCallbackComplete),
+ 0);
+ manual_callback_pending_ = true;
+}
+
+void PaintManager::DoPaint() {
+ PP_DCHECK(aggregator_.HasPendingUpdate());
+
+ // Make a copy of the pending update and clear the pending update flag before
+ // actually painting. A plugin might cause invalidates in its Paint code, and
+ // we want those to go to the *next* paint.
+ PaintAggregator::PaintUpdate update = aggregator_.GetPendingUpdate();
+ aggregator_.ClearPendingUpdate();
+
+ // Apply any pending resize. Setting the graphics to this class must happen
+ // before asking the plugin to paint in case it requests the Graphics2D during
+ // painting. However, the bind must not happen until afterward since we don't
+ // want to have an unpainted device bound. The needs_binding flag tells us
+ // whether to do this later.
+ bool needs_binding = false;
+ if (has_pending_resize_) {
+ graphics_ = Graphics2D(instance_, pending_size_, is_always_opaque_);
+ needs_binding = true;
+
+ // Since we're binding a new one, all of the callbacks have been canceled.
+ manual_callback_pending_ = false;
+ flush_pending_ = false;
+ callback_factory_.CancelAll();
+
+ // This must be cleared before calling into the plugin since it may do
+ // additional invalidation or sizing operations.
+ has_pending_resize_ = false;
+ pending_size_ = Size();
+ }
+
+ // Apply any scroll before asking the client to paint.
+ if (update.has_scroll)
+ graphics_.Scroll(update.scroll_rect, update.scroll_delta);
+
+ if (client_->OnPaint(graphics_, update.paint_rects, update.paint_bounds)) {
+ // Something was painted, schedule a flush.
+ int32_t result = graphics_.Flush(
+ callback_factory_.NewOptionalCallback(&PaintManager::OnFlushComplete));
+
+ // If you trigger this assertion, then your plugin has called Flush()
+ // manually. When using the PaintManager, you should not call Flush, it
+ // will handle that for you because it needs to know when it can do the
+ // next paint by implementing the flush callback.
+ //
+ // Another possible cause of this assertion is re-using devices. If you
+ // use one device, swap it with another, then swap it back, we won't know
+ // that we've already scheduled a Flush on the first device. It's best to
+ // not re-use devices in this way.
+ PP_DCHECK(result != PP_ERROR_INPROGRESS);
+
+ if (result == PP_OK_COMPLETIONPENDING) {
+ flush_pending_ = true;
+ } else {
+ PP_DCHECK(result == PP_OK); // Catch all other errors in debug mode.
+ }
+ }
+
+ if (needs_binding)
+ instance_->BindGraphics(graphics_);
+}
+
+void PaintManager::OnFlushComplete(int32_t) {
+ PP_DCHECK(flush_pending_);
+ flush_pending_ = false;
+
+ // If more paints were enqueued while we were waiting for the flush to
+ // complete, execute them now.
+ if (aggregator_.HasPendingUpdate())
+ DoPaint();
+}
+
+void PaintManager::OnManualCallbackComplete(int32_t) {
+ PP_DCHECK(manual_callback_pending_);
+ manual_callback_pending_ = false;
+
+ // Just because we have a manual callback doesn't mean there are actually any
+ // invalid regions. Even though we only schedule this callback when something
+ // is pending, a Flush callback could have come in before this callback was
+ // executed and that could have cleared the queue.
+ if (aggregator_.HasPendingUpdate() && !flush_pending_)
+ DoPaint();
+}
+
+
+} // namespace pp
diff --git a/ppapi/utility/graphics/paint_manager.h b/ppapi/utility/graphics/paint_manager.h
new file mode 100644
index 0000000..c3787e8
--- /dev/null
+++ b/ppapi/utility/graphics/paint_manager.h
@@ -0,0 +1,297 @@
+// Copyright (c) 2011 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.
+
+#ifndef PPAPI_UTILITY_GRAPHICS_PAINT_MANAGER_H_
+#define PPAPI_UTILITY_GRAPHICS_PAINT_MANAGER_H_
+
+#include <vector>
+
+#include "ppapi/cpp/graphics_2d.h"
+#include "ppapi/utility/completion_callback_factory.h"
+#include "ppapi/utility/graphics/paint_aggregator.h"
+
+/// @file
+/// This file defines the API to convert the "plugin push" model of painting
+/// in PPAPI to a paint request at a later time.
+
+namespace pp {
+
+class Graphics2D;
+class Instance;
+class Point;
+class Rect;
+
+/// This class converts the "instance push" model of painting in PPAPI to a
+/// paint request at a later time. Usage is that you call Invalidate and
+/// Scroll, and implement the Client interface. Your OnPaint handler will
+/// then get called with coalesced paint events.
+///
+/// This class is basically a <code>PaintAggregator</code> that groups updates,
+/// plus management of callbacks for scheduling paints.
+///
+/// <strong>Example:</strong>
+///
+/// <code>
+///
+/// class MyClass : public pp::Instance, public PaintManager::Client {
+/// public:
+/// MyClass() {
+/// paint_manager_.Initialize(this, this, false);
+/// }
+///
+/// void ViewChanged(const pp::Rect& position, const pp::Rect& clip) {
+/// paint_manager_.SetSize(position.size());
+/// }
+///
+/// void DoSomething() {
+/// // This function does something like respond to an event that causes
+/// // the screen to need updating.
+/// paint_manager_.InvalidateRect(some_rect);
+/// }
+///
+/// // Implementation of PaintManager::Client
+/// virtual bool OnPaint(pp::Graphics2D& device,
+/// const pp::PaintUpdate& update) {
+/// // If our app needed scrolling, we would apply that first here.
+///
+/// // Then we would either repaint the area returned by GetPaintBounds or
+/// // iterate through all the paint_rects.
+///
+/// // The caller will call Flush() for us, so don't do that here.
+/// return true;
+/// }
+///
+/// private:
+/// pp::PaintManager paint_manager_;
+/// };
+/// </code>
+class PaintManager {
+ public:
+ class Client {
+ public:
+ /// OnPaint() paints the given invalid area of the instance to the given
+ /// graphics device. Returns true if anything was painted.
+ ///
+ /// You are given the list of rects to paint in <code>paint_rects</code>,
+ /// and the union of all of these rects in <code>paint_bounds</code>. You
+ /// only have to paint the area inside each of the
+ /// <code>paint_rects</code>, but can paint more if you want (some apps may
+ /// just want to paint the union).
+ ///
+ /// Do not call Flush() on the graphics device, this will be done
+ /// automatically if you return true from this function since the
+ /// <code>PaintManager</code> needs to handle the callback.
+ ///
+ /// It is legal for you to cause invalidates inside of Paint which will
+ /// then get executed as soon as the Flush for this update has completed.
+ /// However, this is not very nice to the host system since it will spin the
+ /// CPU, possibly updating much faster than necessary. It is best to have a
+ /// 1/60 second timer to do an invalidate instead. This will limit your
+ /// animation to the slower of 60Hz or "however fast Flush can complete."
+ ///
+ /// @param[in] graphics A <code>Graphics2D</code> to be painted.
+ /// @param[in] paint_rects A list of rects to paint.
+ /// @param[in] paint_bounds A union of the rects to paint.
+ ///
+ /// @return true if successful, otherwise false.
+ virtual bool OnPaint(Graphics2D& graphics,
+ const std::vector<Rect>& paint_rects,
+ const Rect& paint_bounds) = 0;
+
+ protected:
+ // You shouldn't be doing deleting through this interface.
+ virtual ~Client() {}
+ };
+
+ /// Default constructor for creating an is_null() <code>PaintManager</code>
+ /// object. If you use this version of the constructor, you must call
+ /// Initialize() below.
+ PaintManager();
+
+ /// A constructor to create a new <code>PaintManager</code> with an instance
+ /// and client.
+ ///
+ /// <strong>Note:</strong> You will need to call SetSize() before this class
+ /// will do anything. Normally you do this from the <code>ViewChanged</code>
+ /// method of your instance.
+ ///
+ /// @param instance The instance using this paint manager to do its
+ /// painting. Painting will automatically go to this instance and you don't
+ /// have to manually bind any device context (this is all handled by the
+ /// paint manager).
+ ///
+ /// @param client A non-owning pointer and must remain valid (normally the
+ /// object implementing the Client interface will own the paint manager).
+ ///
+ /// @param is_always_opaque A flag passed to the device contexts that this
+ /// class creates. Set this to true if your instance always draws an opaque
+ /// image to the device. This is used as a hint to the browser that it does
+ /// not need to do alpha blending, which speeds up painting. If you generate
+ /// non-opqaue pixels or aren't sure, set this to false for more general
+ /// blending.
+ ///
+ /// If you set is_always_opaque, your alpha channel should always be set to
+ /// 0xFF or there may be painting artifacts. Being opaque will allow the
+ /// browser to do a memcpy rather than a blend to paint the plugin, and this
+ /// means your alpha values will get set on the page backing store. If these
+ /// values are incorrect, it could mess up future blending. If you aren't
+ /// sure, it is always correct to specify that it it not opaque.
+ PaintManager(Instance* instance, Client* client, bool is_always_opaque);
+
+ /// Destructor.
+ ~PaintManager();
+
+ /// Initialize() must be called if you are using the 0-arg constructor.
+ ///
+ /// @param instance The instance using this paint manager to do its
+ /// painting. Painting will automatically go to this instance and you don't
+ /// have to manually bind any device context (this is all handled by the
+ /// paint manager).
+ /// @param client A non-owning pointer and must remain valid (normally the
+ /// object implementing the Client interface will own the paint manager).
+ /// @param is_always_opaque A flag passed to the device contexts that this
+ /// class creates. Set this to true if your instance always draws an opaque
+ /// image to the device. This is used as a hint to the browser that it does
+ /// not need to do alpha blending, which speeds up painting. If you generate
+ /// non-opqaue pixels or aren't sure, set this to false for more general
+ /// blending.
+ ///
+ /// If you set <code>is_always_opaque</code>, your alpha channel should
+ /// always be set to <code>0xFF</code> or there may be painting artifacts.
+ /// Being opaque will allow the browser to do a memcpy rather than a blend
+ /// to paint the plugin, and this means your alpha values will get set on the
+ /// page backing store. If these values are incorrect, it could mess up
+ /// future blending. If you aren't sure, it is always correct to specify that
+ /// it it not opaque.
+ void Initialize(Instance* instance, Client* client, bool is_always_opaque);
+
+ /// Setter function setting the max ratio of paint rect area to scroll rect
+ /// area that we will tolerate before downgrading the scroll into a repaint.
+ ///
+ /// If the combined area of paint rects contained within the scroll
+ /// rect grows too large, then we might as well just treat
+ /// the scroll rect as a paint rect.
+ ///
+ /// @param[in] area The max ratio of paint rect area to scroll rect area that
+ /// we will tolerate before downgrading the scroll into a repaint.
+ void set_max_redundant_paint_to_scroll_area(float area) {
+ aggregator_.set_max_redundant_paint_to_scroll_area(area);
+ }
+
+ /// Setter function for setting the maximum number of paint rects. If we
+ /// exceed this limit, then we'll start combining paint rects (refer to
+ /// CombinePaintRects() for further information). This limiting can be
+ /// important since there is typically some overhead in deciding what to
+ /// paint. If your module is fast at doing these computations, raise this
+ /// threshold, if your module is slow, lower it (probably requires some
+ /// tuning to find the right value).
+ ///
+ /// @param[in] max_rects The maximum number of paint rects.
+ void set_max_paint_rects(size_t max_rects) {
+ aggregator_.set_max_paint_rects(max_rects);
+ }
+
+ /// SetSize() sets the size of the instance. If the size is the same as the
+ /// previous call, this will be a NOP. If the size has changed, a new device
+ /// will be allocated to the given size and a paint to that device will be
+ /// scheduled.
+ ///
+ /// This function is intended to be called from <code>ViewChanged</code> with
+ /// the size of the instance. Since it tracks the old size and only allocates
+ /// when the size changes, you can always call this function without worrying
+ /// about whether the size changed or ViewChanged() is called for another
+ /// reason (like the position changed).
+ ///
+ /// @param new_size The new size for the instance.
+ void SetSize(const Size& new_size);
+
+ /// This function provides access to the underlying device in case you need
+ /// it. If you have done a SetSize(), note that the graphics context won't be
+ /// updated until right before the next call to OnPaint().
+ ///
+ /// <strong>Note:</strong> If you call Flush on this device the paint manager
+ /// will get very confused, don't do this!
+ const Graphics2D& graphics() const { return graphics_; }
+
+ /// This function provides access to the underlying device in case you need
+ /// it. If you have done a SetSize(), note that the graphics context won't be
+ /// updated until right before the next call to OnPaint().
+ ///
+ /// <strong>Note:</strong> If you call Flush on this device the paint manager
+ /// will get very confused, don't do this!
+ Graphics2D& graphics() { return graphics_; }
+
+ /// Invalidate() invalidate the entire instance.
+ void Invalidate();
+
+ /// InvalidateRect() Invalidate the provided rect.
+ ///
+ /// @param[in] rect The <code>Rect</code> to be invalidated.
+ void InvalidateRect(const Rect& rect);
+
+ /// ScrollRect() scrolls the provided <code>clip_rect</code> by the
+ /// <code>amount</code> argument.
+ ///
+ /// @param clip_rect The clip rectangle to scroll.
+ /// @param amount The amount to scroll <code>clip_rect</code>.
+ void ScrollRect(const Rect& clip_rect, const Point& amount);
+
+ /// GetEffectiveSize() returns the size of the graphics context for the
+ /// next paint operation. This is the pending size if a resize is pending
+ /// (the instance has called SetSize() but we haven't actually painted it
+ /// yet), or the current size of no resize is pending.
+ ///
+ /// @return The effective size.
+ Size GetEffectiveSize() const;
+
+ private:
+ // Disallow copy and assign (these are unimplemented).
+ PaintManager(const PaintManager&);
+ PaintManager& operator=(const PaintManager&);
+
+ // Makes sure there is a callback that will trigger a paint at a later time.
+ // This will be either a Flush callback telling us we're allowed to generate
+ // more data, or, if there's no flush callback pending, a manual call back
+ // to the message loop via ExecuteOnMainThread.
+ void EnsureCallbackPending();
+
+ // Does the client paint and executes a Flush if necessary.
+ void DoPaint();
+
+ // Callback for asynchronous completion of Flush.
+ void OnFlushComplete(int32_t);
+
+ // Callback for manual scheduling of paints when there is no flush callback
+ // pending.
+ void OnManualCallbackComplete(int32_t);
+
+ Instance* instance_;
+
+ // Non-owning pointer. See the constructor.
+ Client* client_;
+
+ bool is_always_opaque_;
+
+ CompletionCallbackFactory<PaintManager> callback_factory_;
+
+ // This graphics device will be is_null() if no graphics has been manually
+ // set yet.
+ Graphics2D graphics_;
+
+ PaintAggregator aggregator_;
+
+ // See comment for EnsureCallbackPending for more on how these work.
+ bool manual_callback_pending_;
+ bool flush_pending_;
+
+ // When we get a resize, we don't bind right away (see SetSize). The
+ // has_pending_resize_ tells us that we need to do a resize for the next
+ // paint operation. When true, the new size is in pending_size_.
+ bool has_pending_resize_;
+ Size pending_size_;
+};
+
+} // namespace pp
+
+#endif // PPAPI_UTILITY_GRAPHICS_PAINT_MANAGER_H_
diff --git a/ppapi/utility/non_thread_safe_ref_count.h b/ppapi/utility/non_thread_safe_ref_count.h
new file mode 100644
index 0000000..db00b7d
--- /dev/null
+++ b/ppapi/utility/non_thread_safe_ref_count.h
@@ -0,0 +1,60 @@
+// Copyright (c) 2011 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.
+
+#ifndef PPAPI_UTILITY_NON_THREAD_SAFE_REF_COUNT_H_
+#define PPAPI_UTILITY_NON_THREAD_SAFE_REF_COUNT_H_
+
+#include "ppapi/cpp/core.h"
+#include "ppapi/cpp/logging.h"
+#include "ppapi/cpp/module.h"
+
+/// @file
+/// This file defines the APIs for maintaining a reference counter.
+namespace pp {
+
+/// A simple reference counter that is not thread-safe. <strong>Note:</strong>
+/// in Debug mode, it checks that it is either called on the main thread, or
+/// always called on another thread.
+class NonThreadSafeRefCount {
+ public:
+ /// Default constructor. In debug mode, this checks that the object is being
+ /// created on the main thread.
+ NonThreadSafeRefCount()
+ : ref_(0) {
+#ifndef NDEBUG
+ is_main_thread_ = Module::Get()->core()->IsMainThread();
+#endif
+ }
+
+ /// Destructor.
+ ~NonThreadSafeRefCount() {
+ PP_DCHECK(is_main_thread_ == Module::Get()->core()->IsMainThread());
+ }
+
+ /// AddRef() increments the reference counter.
+ ///
+ /// @return An int32_t with the incremented reference counter.
+ int32_t AddRef() {
+ PP_DCHECK(is_main_thread_ == Module::Get()->core()->IsMainThread());
+ return ++ref_;
+ }
+
+ /// Release() decrements the reference counter.
+ ///
+ /// @return An int32_t with the decremeneted reference counter.
+ int32_t Release() {
+ PP_DCHECK(is_main_thread_ == Module::Get()->core()->IsMainThread());
+ return --ref_;
+ }
+
+ private:
+ int32_t ref_;
+#ifndef NDEBUG
+ bool is_main_thread_;
+#endif
+};
+
+} // namespace pp
+
+#endif // PPAPI_UTILITY_NON_THREAD_SAFE_REF_COUNT_H_