[Chromoting] Run jscompile over JS for HTML files (GN-only)
[chromium-blink-merge.git] / ppapi / cpp / image_data.cc
blobd9738964795eb698d8d612a1b2964f1164594415
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.
5 #include "ppapi/cpp/image_data.h"
7 #include <string.h> // Needed for memset.
9 #include <algorithm>
11 #include "ppapi/cpp/instance_handle.h"
12 #include "ppapi/cpp/module.h"
13 #include "ppapi/cpp/module_impl.h"
15 namespace pp {
17 namespace {
19 template <> const char* interface_name<PPB_ImageData_1_0>() {
20 return PPB_IMAGEDATA_INTERFACE_1_0;
23 } // namespace
25 ImageData::ImageData() : data_(NULL) {
26 memset(&desc_, 0, sizeof(PP_ImageDataDesc));
29 ImageData::ImageData(const ImageData& other)
30 : Resource(other),
31 desc_(other.desc_),
32 data_(other.data_) {
35 ImageData::ImageData(PassRef, PP_Resource resource)
36 : Resource(PASS_REF, resource),
37 data_(NULL) {
38 memset(&desc_, 0, sizeof(PP_ImageDataDesc));
39 InitData();
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));
49 if (!has_interface<PPB_ImageData_1_0>())
50 return;
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();
58 ImageData& ImageData::operator=(const ImageData& other) {
59 Resource::operator=(other);
60 desc_ = other.desc_;
61 data_ = other.data_;
62 return *this;
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);
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]);
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));
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();
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;
100 *this = ImageData();
103 } // namespace pp