graphics_2d.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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.
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
25}  // namespace
26
27Graphics2D::Graphics2D() : Resource() {
28}
29
30Graphics2D::Graphics2D(const Graphics2D& other)
31    : Resource(other),
32      size_(other.size_) {
33}
34
35Graphics2D::Graphics2D(const InstanceHandle& instance,
36                       const Size& size,
37                       bool is_always_opaque)
38    : Resource() {
39  if (!has_interface<PPB_Graphics2D_1_0>())
40    return;
41  PassRefFromConstructor(get_interface<PPB_Graphics2D_1_0>()->Create(
42      instance.pp_instance(),
43      &size.pp_size(),
44      PP_FromBool(is_always_opaque)));
45  if (!is_null()) {
46    // Only save the size if allocation succeeded.
47    size_ = size;
48  }
49}
50
51Graphics2D::~Graphics2D() {
52}
53
54Graphics2D& Graphics2D::operator=(const Graphics2D& other) {
55  Resource::operator=(other);
56  size_ = other.size_;
57  return *this;
58}
59
60void Graphics2D::PaintImageData(const ImageData& image,
61                                const Point& top_left) {
62  if (!has_interface<PPB_Graphics2D_1_0>())
63    return;
64  get_interface<PPB_Graphics2D_1_0>()->PaintImageData(pp_resource(),
65                                                      image.pp_resource(),
66                                                      &top_left.pp_point(),
67                                                      NULL);
68}
69
70void Graphics2D::PaintImageData(const ImageData& image,
71                                const Point& top_left,
72                                const Rect& src_rect) {
73  if (!has_interface<PPB_Graphics2D_1_0>())
74    return;
75  get_interface<PPB_Graphics2D_1_0>()->PaintImageData(pp_resource(),
76                                                      image.pp_resource(),
77                                                      &top_left.pp_point(),
78                                                      &src_rect.pp_rect());
79}
80
81void Graphics2D::Scroll(const Rect& clip, const Point& amount) {
82  if (!has_interface<PPB_Graphics2D_1_0>())
83    return;
84  get_interface<PPB_Graphics2D_1_0>()->Scroll(pp_resource(),
85                                              &clip.pp_rect(),
86                                              &amount.pp_point());
87}
88
89void Graphics2D::ReplaceContents(ImageData* image) {
90  if (!has_interface<PPB_Graphics2D_1_0>())
91    return;
92  get_interface<PPB_Graphics2D_1_0>()->ReplaceContents(pp_resource(),
93                                                       image->pp_resource());
94
95  // On success, reset the image data. This is to help prevent people
96  // from continuing to use the resource which will result in artifacts.
97  *image = ImageData();
98}
99
100int32_t Graphics2D::Flush(const CompletionCallback& cc) {
101  if (!has_interface<PPB_Graphics2D_1_0>())
102    return cc.MayForce(PP_ERROR_NOINTERFACE);
103  return get_interface<PPB_Graphics2D_1_0>()->Flush(
104      pp_resource(), cc.pp_completion_callback());
105}
106
107}  // namespace pp
108