This source file includes following definitions.
- has_pending_resize_
- has_pending_resize_
- Initialize
- SetSize
- Invalidate
- InvalidateRect
- ScrollRect
- GetEffectiveSize
- EnsureCallbackPending
- DoPaint
- OnFlushComplete
- OnManualCallbackComplete
#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) {
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) {
callback_factory_.Initialize(this);
PP_DCHECK(client);
}
PaintManager::~PaintManager() {
}
void PaintManager::Initialize(Instance* instance,
Client* client,
bool is_always_opaque) {
PP_DCHECK(!instance_ && !client_);
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() {
PP_DCHECK(!graphics_.is_null() || has_pending_resize_);
EnsureCallbackPending();
aggregator_.InvalidateRect(Rect(GetEffectiveSize()));
}
void PaintManager::InvalidateRect(const Rect& rect) {
PP_DCHECK(!graphics_.is_null() || has_pending_resize_);
Rect clipped_rect = rect.Intersect(Rect(GetEffectiveSize()));
if (clipped_rect.IsEmpty())
return;
EnsureCallbackPending();
aggregator_.InvalidateRect(clipped_rect);
}
void PaintManager::ScrollRect(const Rect& clip_rect, const Point& amount) {
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() {
if (flush_pending_)
return;
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());
PaintAggregator::PaintUpdate update = aggregator_.GetPendingUpdate();
aggregator_.ClearPendingUpdate();
bool needs_binding = false;
if (has_pending_resize_) {
graphics_ = Graphics2D(instance_, pending_size_, is_always_opaque_);
needs_binding = true;
manual_callback_pending_ = false;
flush_pending_ = false;
callback_factory_.CancelAll();
has_pending_resize_ = false;
pending_size_ = Size();
}
if (update.has_scroll)
graphics_.Scroll(update.scroll_rect, update.scroll_delta);
if (client_->OnPaint(graphics_, update.paint_rects, update.paint_bounds)) {
int32_t result = graphics_.Flush(
callback_factory_.NewOptionalCallback(&PaintManager::OnFlushComplete));
PP_DCHECK(result != PP_ERROR_INPROGRESS);
if (result == PP_OK_COMPLETIONPENDING) {
flush_pending_ = true;
} else {
PP_DCHECK(result == PP_OK);
}
}
if (needs_binding)
instance_->BindGraphics(graphics_);
}
void PaintManager::OnFlushComplete(int32_t result) {
PP_DCHECK(flush_pending_);
flush_pending_ = false;
if (result != PP_OK)
return;
if (aggregator_.HasPendingUpdate())
DoPaint();
}
void PaintManager::OnManualCallbackComplete(int32_t) {
PP_DCHECK(manual_callback_pending_);
manual_callback_pending_ = false;
if (aggregator_.HasPendingUpdate() && !flush_pending_)
DoPaint();
}
}