1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 #include "ppapi/cpp/graphics_2d.h"
10 #include "ppapi/cpp/image_data.h"
11 #include "ppapi/cpp/instance.h"
12 #include "ppapi/cpp/module.h"
13 #include "ppapi/cpp/rect.h"
14 #include "ppapi/cpp/var.h"
15 #include "ppapi/utility/completion_callback_factory.h"
16 #include "ppapi/utility/graphics/paint_manager.h"
18 static const int kSquareSpacing
= 98;
19 static const int kSquareSize
= 5;
21 static const int kAdvanceXPerFrame
= 0;
22 static const int kAdvanceYPerFrame
= -3;
24 void FillRect(pp::ImageData
* image
, const pp::Rect
& rect
, uint32_t color
) {
25 for (int y
= std::max(0, rect
.y());
26 y
< std::min(image
->size().height(), rect
.bottom());
28 for (int x
= std::max(0, rect
.x());
29 x
< std::min(image
->size().width(), rect
.right());
31 *image
->GetAddr32(pp::Point(x
, y
)) = color
;
35 class MyInstance
: public pp::Instance
, public pp::PaintManager::Client
{
37 MyInstance(PP_Instance instance
)
38 : pp::Instance(instance
),
41 factory_
.Initialize(this);
42 paint_manager_
.Initialize(this, this, false);
45 virtual void DidChangeView(const pp::Rect
& position
, const pp::Rect
& clip
) {
46 paint_manager_
.SetSize(position
.size());
49 void OnTimer(int32_t) {
50 pp::Module::Get()->core()->CallOnMainThread(
51 16, factory_
.NewCallback(&MyInstance::OnTimer
), 0);
52 // The scroll and the invalidate will do the same thing in this example,
53 // but the invalidate will cause a large repaint, whereas the scroll will
54 // be faster and cause a smaller repaint.
56 paint_manager_
.ScrollRect(pp::Rect(paint_manager_
.graphics().size()),
57 pp::Point(kAdvanceXPerFrame
, kAdvanceYPerFrame
));
59 paint_manager_
.Invalidate();
65 // PaintManager::Client implementation.
66 virtual bool OnPaint(pp::Graphics2D
& graphics
,
67 const std::vector
<pp::Rect
>& paint_rects
,
68 const pp::Rect
& paint_bounds
) {
70 pp::Module::Get()->core()->CallOnMainThread(
71 16, factory_
.NewCallback(&MyInstance::OnTimer
), 0);
75 // Paint the background.
76 pp::ImageData
updated_image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL
,
77 paint_bounds
.size(), false);
78 FillRect(&updated_image
, pp::Rect(updated_image
.size()), 0xFF8888FF);
80 int x_origin
= current_step_
* kAdvanceXPerFrame
;
81 int y_origin
= current_step_
* kAdvanceYPerFrame
;
83 int x_offset
= x_origin
% kSquareSpacing
;
84 int y_offset
= y_origin
% kSquareSpacing
;
86 for (int ys
= 0; ys
< graphics
.size().height() / kSquareSpacing
+ 2; ys
++) {
87 for (int xs
= 0; xs
< graphics
.size().width() / kSquareSpacing
+ 2;
89 int x
= xs
* kSquareSpacing
+ x_offset
- paint_bounds
.x();
90 int y
= ys
* kSquareSpacing
+ y_offset
- paint_bounds
.y();
91 FillRect(&updated_image
, pp::Rect(x
, y
, kSquareSize
, kSquareSize
),
95 graphics
.PaintImageData(updated_image
, paint_bounds
.point());
99 pp::CompletionCallbackFactory
<MyInstance
> factory_
;
101 pp::PaintManager paint_manager_
;
108 class MyModule
: public pp::Module
{
110 virtual pp::Instance
* CreateInstance(PP_Instance instance
) {
111 return new MyInstance(instance
);
117 // Factory function for your specialization of the Module object.
118 Module
* CreateModule() {
119 return new MyModule();