1 // Copyright (c) 2012 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.
5 #include "ppapi/cpp/graphics_2d.h"
7 #include "ppapi/c/pp_errors.h"
8 #include "ppapi/c/ppb_graphics_2d.h"
9 #include "ppapi/cpp/completion_callback.h"
10 #include "ppapi/cpp/image_data.h"
11 #include "ppapi/cpp/instance_handle.h"
12 #include "ppapi/cpp/module.h"
13 #include "ppapi/cpp/module_impl.h"
14 #include "ppapi/cpp/point.h"
15 #include "ppapi/cpp/rect.h"
21 template <> const char* interface_name
<PPB_Graphics2D_1_0
>() {
22 return PPB_GRAPHICS_2D_INTERFACE_1_0
;
25 template <> const char* interface_name
<PPB_Graphics2D_1_1
>() {
26 return PPB_GRAPHICS_2D_INTERFACE_1_1
;
31 Graphics2D::Graphics2D() : Resource() {
34 Graphics2D::Graphics2D(const Graphics2D
& other
)
39 Graphics2D::Graphics2D(const InstanceHandle
& instance
,
41 bool is_always_opaque
)
43 if (has_interface
<PPB_Graphics2D_1_1
>()) {
44 PassRefFromConstructor(get_interface
<PPB_Graphics2D_1_1
>()->Create(
45 instance
.pp_instance(),
47 PP_FromBool(is_always_opaque
)));
48 } else if (has_interface
<PPB_Graphics2D_1_0
>()) {
49 PassRefFromConstructor(get_interface
<PPB_Graphics2D_1_0
>()->Create(
50 instance
.pp_instance(),
52 PP_FromBool(is_always_opaque
)));
57 // Only save the size if allocation succeeded.
62 Graphics2D::~Graphics2D() {
65 Graphics2D
& Graphics2D::operator=(const Graphics2D
& other
) {
66 Resource::operator=(other
);
71 void Graphics2D::PaintImageData(const ImageData
& image
,
72 const Point
& top_left
) {
73 if (has_interface
<PPB_Graphics2D_1_1
>()) {
74 get_interface
<PPB_Graphics2D_1_1
>()->PaintImageData(pp_resource(),
78 } else if (has_interface
<PPB_Graphics2D_1_0
>()) {
79 get_interface
<PPB_Graphics2D_1_0
>()->PaintImageData(pp_resource(),
86 void Graphics2D::PaintImageData(const ImageData
& image
,
87 const Point
& top_left
,
88 const Rect
& src_rect
) {
89 if (has_interface
<PPB_Graphics2D_1_1
>()) {
90 get_interface
<PPB_Graphics2D_1_1
>()->PaintImageData(pp_resource(),
94 } else if (has_interface
<PPB_Graphics2D_1_0
>()) {
95 get_interface
<PPB_Graphics2D_1_0
>()->PaintImageData(pp_resource(),
102 void Graphics2D::Scroll(const Rect
& clip
, const Point
& amount
) {
103 if (has_interface
<PPB_Graphics2D_1_1
>()) {
104 get_interface
<PPB_Graphics2D_1_1
>()->Scroll(pp_resource(),
107 } else if (has_interface
<PPB_Graphics2D_1_0
>()) {
108 get_interface
<PPB_Graphics2D_1_0
>()->Scroll(pp_resource(),
114 void Graphics2D::ReplaceContents(ImageData
* image
) {
115 if (has_interface
<PPB_Graphics2D_1_1
>()) {
116 get_interface
<PPB_Graphics2D_1_1
>()->ReplaceContents(pp_resource(),
117 image
->pp_resource());
118 } else if (has_interface
<PPB_Graphics2D_1_0
>()) {
119 get_interface
<PPB_Graphics2D_1_0
>()->ReplaceContents(pp_resource(),
120 image
->pp_resource());
125 // On success, reset the image data. This is to help prevent people
126 // from continuing to use the resource which will result in artifacts.
127 *image
= ImageData();
130 int32_t Graphics2D::Flush(const CompletionCallback
& cc
) {
131 if (has_interface
<PPB_Graphics2D_1_1
>()) {
132 return get_interface
<PPB_Graphics2D_1_1
>()->Flush(
133 pp_resource(), cc
.pp_completion_callback());
134 } else if (has_interface
<PPB_Graphics2D_1_0
>()) {
135 return get_interface
<PPB_Graphics2D_1_0
>()->Flush(
136 pp_resource(), cc
.pp_completion_callback());
138 return cc
.MayForce(PP_ERROR_NOINTERFACE
);
142 bool Graphics2D::SetScale(float scale
) {
143 if (!has_interface
<PPB_Graphics2D_1_1
>())
145 return PP_ToBool(get_interface
<PPB_Graphics2D_1_1
>()->SetScale(pp_resource(),
149 float Graphics2D::GetScale() {
150 if (!has_interface
<PPB_Graphics2D_1_1
>())
152 return get_interface
<PPB_Graphics2D_1_1
>()->GetScale(pp_resource());