Pepper_47_C++_interfaces
image_data.cc
Go to the documentation of this file.
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 
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 
30  : Resource(other),
31  desc_(other.desc_),
32  data_(other.data_) {
33 }
34 
36  : Resource(PASS_REF, resource),
37  data_(NULL) {
38  memset(&desc_, 0, sizeof(PP_ImageDataDesc));
39  InitData();
40 }
41 
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 
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>()->
83 }
84 
85 // static
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
void PassRefFromConstructor(PP_Resource resource)
Definition: resource.cc:50
void * data() const
Definition: image_data.h:112
const uint32_t * GetAddr32(const Point &coord) const
Definition: image_data.cc:65
PP_ImageDataFormat format() const
Definition: image_data.h:97
const PP_Size & pp_size() const
Definition: size.h:64
A 2 dimensional point with 0,0 being the upper-left starting coordinate.
Definition: point.h:16
static PP_ImageDataFormat GetNativeImageDataFormat()
Definition: image_data.cc:86
int32_t y() const
Definition: point.h:84
A size of an object based on width and height.
Definition: size.h:18
PassRef
Definition: pass_ref.h:17
int32_t x() const
Definition: point.h:72
int32_t stride() const
Definition: image_data.h:107
PP_Resource pp_resource() const
Definition: resource.h:47
static bool IsImageDataFormatSupported(PP_ImageDataFormat format)
Definition: image_data.cc:78
PP_Instance pp_instance() const
A reference counted module resource.
Definition: resource.h:20
Resource & operator=(const Resource &other)
Definition: resource.cc:27
ImageData & operator=(const ImageData &other)
Definition: image_data.cc:58