1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
// Copyright (c) 2012 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/graphics_2d.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/ppb_graphics_2d.h"
#include "ppapi/cpp/completion_callback.h"
#include "ppapi/cpp/image_data.h"
#include "ppapi/cpp/instance_handle.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/module_impl.h"
#include "ppapi/cpp/point.h"
#include "ppapi/cpp/rect.h"
namespace pp {
namespace {
template <> const char* interface_name<PPB_Graphics2D_1_0>() {
return PPB_GRAPHICS_2D_INTERFACE_1_0;
}
template <> const char* interface_name<PPB_Graphics2D_1_1>() {
return PPB_GRAPHICS_2D_INTERFACE_1_1;
}
} // namespace
Graphics2D::Graphics2D() : Resource() {
}
Graphics2D::Graphics2D(const Graphics2D& other)
: Resource(other),
size_(other.size_) {
}
Graphics2D::Graphics2D(const InstanceHandle& instance,
const Size& size,
bool is_always_opaque)
: Resource() {
if (has_interface<PPB_Graphics2D_1_1>()) {
PassRefFromConstructor(get_interface<PPB_Graphics2D_1_1>()->Create(
instance.pp_instance(),
&size.pp_size(),
PP_FromBool(is_always_opaque)));
} else if (has_interface<PPB_Graphics2D_1_0>()) {
PassRefFromConstructor(get_interface<PPB_Graphics2D_1_0>()->Create(
instance.pp_instance(),
&size.pp_size(),
PP_FromBool(is_always_opaque)));
} else {
return;
}
if (!is_null()) {
// Only save the size if allocation succeeded.
size_ = size;
}
}
Graphics2D::~Graphics2D() {
}
Graphics2D& Graphics2D::operator=(const Graphics2D& other) {
Resource::operator=(other);
size_ = other.size_;
return *this;
}
void Graphics2D::PaintImageData(const ImageData& image,
const Point& top_left) {
if (has_interface<PPB_Graphics2D_1_1>()) {
get_interface<PPB_Graphics2D_1_1>()->PaintImageData(pp_resource(),
image.pp_resource(),
&top_left.pp_point(),
NULL);
} else if (has_interface<PPB_Graphics2D_1_0>()) {
get_interface<PPB_Graphics2D_1_0>()->PaintImageData(pp_resource(),
image.pp_resource(),
&top_left.pp_point(),
NULL);
}
}
void Graphics2D::PaintImageData(const ImageData& image,
const Point& top_left,
const Rect& src_rect) {
if (has_interface<PPB_Graphics2D_1_1>()) {
get_interface<PPB_Graphics2D_1_1>()->PaintImageData(pp_resource(),
image.pp_resource(),
&top_left.pp_point(),
&src_rect.pp_rect());
} else if (has_interface<PPB_Graphics2D_1_0>()) {
get_interface<PPB_Graphics2D_1_0>()->PaintImageData(pp_resource(),
image.pp_resource(),
&top_left.pp_point(),
&src_rect.pp_rect());
}
}
void Graphics2D::Scroll(const Rect& clip, const Point& amount) {
if (has_interface<PPB_Graphics2D_1_1>()) {
get_interface<PPB_Graphics2D_1_1>()->Scroll(pp_resource(),
&clip.pp_rect(),
&amount.pp_point());
} else if (has_interface<PPB_Graphics2D_1_0>()) {
get_interface<PPB_Graphics2D_1_0>()->Scroll(pp_resource(),
&clip.pp_rect(),
&amount.pp_point());
}
}
void Graphics2D::ReplaceContents(ImageData* image) {
if (has_interface<PPB_Graphics2D_1_1>()) {
get_interface<PPB_Graphics2D_1_1>()->ReplaceContents(pp_resource(),
image->pp_resource());
} else if (has_interface<PPB_Graphics2D_1_0>()) {
get_interface<PPB_Graphics2D_1_0>()->ReplaceContents(pp_resource(),
image->pp_resource());
} else {
return;
}
// On success, reset the image data. This is to help prevent people
// from continuing to use the resource which will result in artifacts.
*image = ImageData();
}
int32_t Graphics2D::Flush(const CompletionCallback& cc) {
if (has_interface<PPB_Graphics2D_1_1>()) {
return get_interface<PPB_Graphics2D_1_1>()->Flush(
pp_resource(), cc.pp_completion_callback());
} else if (has_interface<PPB_Graphics2D_1_0>()) {
return get_interface<PPB_Graphics2D_1_0>()->Flush(
pp_resource(), cc.pp_completion_callback());
} else {
return cc.MayForce(PP_ERROR_NOINTERFACE);
}
}
bool Graphics2D::SetScale(float scale) {
if (!has_interface<PPB_Graphics2D_1_1>())
return false;
return PP_ToBool(get_interface<PPB_Graphics2D_1_1>()->SetScale(pp_resource(),
scale));
}
float Graphics2D::GetScale() {
if (!has_interface<PPB_Graphics2D_1_1>())
return 1.0f;
return get_interface<PPB_Graphics2D_1_1>()->GetScale(pp_resource());
}
} // namespace pp
|