blob: 3a766c83546ef224f9a437bcf156f497d3aba534 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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.
4
5#include "ppapi/cpp/graphics_2d.h"
6
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"
16
17namespace pp {
18
19namespace {
20
21template <> const char* interface_name<PPB_Graphics2D_1_0>() {
22 return PPB_GRAPHICS_2D_INTERFACE_1_0;
23}
24
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010025template <> const char* interface_name<PPB_Graphics2D_1_1>() {
26 return PPB_GRAPHICS_2D_INTERFACE_1_1;
27}
28
Torne (Richard Coles)58218062012-11-14 11:43:16 +000029} // namespace
30
31Graphics2D::Graphics2D() : Resource() {
32}
33
34Graphics2D::Graphics2D(const Graphics2D& other)
35 : Resource(other),
36 size_(other.size_) {
37}
38
39Graphics2D::Graphics2D(const InstanceHandle& instance,
40 const Size& size,
41 bool is_always_opaque)
42 : Resource() {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010043 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 {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000054 return;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010055 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +000056 if (!is_null()) {
57 // Only save the size if allocation succeeded.
58 size_ = size;
59 }
60}
61
62Graphics2D::~Graphics2D() {
63}
64
65Graphics2D& Graphics2D::operator=(const Graphics2D& other) {
66 Resource::operator=(other);
67 size_ = other.size_;
68 return *this;
69}
70
71void Graphics2D::PaintImageData(const ImageData& image,
72 const Point& top_left) {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010073 if (has_interface<PPB_Graphics2D_1_1>()) {
74 get_interface<PPB_Graphics2D_1_1>()->PaintImageData(pp_resource(),
75 image.pp_resource(),
76 &top_left.pp_point(),
77 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 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +000084}
85
86void Graphics2D::PaintImageData(const ImageData& image,
87 const Point& top_left,
88 const Rect& src_rect) {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010089 if (has_interface<PPB_Graphics2D_1_1>()) {
90 get_interface<PPB_Graphics2D_1_1>()->PaintImageData(pp_resource(),
91 image.pp_resource(),
92 &top_left.pp_point(),
93 &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 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000100}
101
102void Graphics2D::Scroll(const Rect& clip, const Point& amount) {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100103 if (has_interface<PPB_Graphics2D_1_1>()) {
104 get_interface<PPB_Graphics2D_1_1>()->Scroll(pp_resource(),
105 &clip.pp_rect(),
106 &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 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000112}
113
114void Graphics2D::ReplaceContents(ImageData* image) {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100115 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 {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000122 return;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100123 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000124
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();
128}
129
130int32_t Graphics2D::Flush(const CompletionCallback& cc) {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100131 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 {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000138 return cc.MayForce(PP_ERROR_NOINTERFACE);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100139 }
140}
141
142bool 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
149float 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());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000153}
154
155} // namespace pp