Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / ppapi / cpp / image_data.cc
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/image_data.h"
6
7 #include <string.h>  // Needed for memset.
8
9 #include <algorithm>
10
11 #include "ppapi/cpp/instance_handle.h"
12 #include "ppapi/cpp/module.h"
13 #include "ppapi/cpp/module_impl.h"
14
15 namespace pp {
16
17 namespace {
18
19 template <> const char* interface_name<PPB_ImageData_1_0>() {
20   return PPB_IMAGEDATA_INTERFACE_1_0;
21 }
22
23 }  // namespace
24
25 ImageData::ImageData() : data_(NULL) {
26   memset(&desc_, 0, sizeof(PP_ImageDataDesc));
27 }
28
29 ImageData::ImageData(const ImageData& other)
30     : Resource(other),
31       desc_(other.desc_),
32       data_(other.data_) {
33 }
34
35 ImageData::ImageData(PassRef, PP_Resource resource)
36     : Resource(PASS_REF, resource),
37       data_(NULL) {
38   memset(&desc_, 0, sizeof(PP_ImageDataDesc));
39   InitData();
40 }
41
42 ImageData::ImageData(const InstanceHandle& instance,
43                      PP_ImageDataFormat format,
44                      const Size& size,
45                      bool init_to_zero)
46     : data_(NULL) {
47   memset(&desc_, 0, sizeof(PP_ImageDataDesc));
48
49   if (!has_interface<PPB_ImageData_1_0>())
50     return;
51
52   PassRefFromConstructor(get_interface<PPB_ImageData_1_0>()->Create(
53       instance.pp_instance(), format, &size.pp_size(),
54       PP_FromBool(init_to_zero)));
55   InitData();
56 }
57
58 ImageData& ImageData::operator=(const ImageData& other) {
59   Resource::operator=(other);
60   desc_ = other.desc_;
61   data_ = other.data_;
62   return *this;
63 }
64
65 const uint32_t* ImageData::GetAddr32(const Point& coord) const {
66   // Prefer evil const casts rather than evil code duplication.
67   return const_cast<ImageData*>(this)->GetAddr32(coord);
68 }
69
70 uint32_t* ImageData::GetAddr32(const Point& coord) {
71   // If we add more image format types that aren't 32-bit, we'd want to check
72   // here and fail.
73   return reinterpret_cast<uint32_t*>(
74       &static_cast<char*>(data())[coord.y() * stride() + coord.x() * 4]);
75 }
76
77 // static
78 bool ImageData::IsImageDataFormatSupported(PP_ImageDataFormat format) {
79   if (!has_interface<PPB_ImageData_1_0>())
80     return false;
81   return PP_ToBool(get_interface<PPB_ImageData_1_0>()->
82       IsImageDataFormatSupported(format));
83 }
84
85 // static
86 PP_ImageDataFormat ImageData::GetNativeImageDataFormat() {
87   if (!has_interface<PPB_ImageData_1_0>())
88     return PP_IMAGEDATAFORMAT_BGRA_PREMUL;  // Default to something on failure.
89   return get_interface<PPB_ImageData_1_0>()->GetNativeImageDataFormat();
90 }
91
92 void ImageData::InitData() {
93   if (!has_interface<PPB_ImageData_1_0>())
94     return;
95   if (get_interface<PPB_ImageData_1_0>()->Describe(pp_resource(), &desc_)) {
96     data_ = get_interface<PPB_ImageData_1_0>()->Map(pp_resource());
97     if (data_)
98       return;
99   }
100   *this = ImageData();
101 }
102
103 }  // namespace pp