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