| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/cpp/image_data.h" | 5 #include "ppapi/cpp/image_data.h" |
| 6 | 6 |
| 7 #include <string.h> // Needed for memset. | 7 #include <string.h> // Needed for memset. |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| 11 #include "ppapi/cpp/instance.h" | 11 #include "ppapi/cpp/instance_handle.h" |
| 12 #include "ppapi/cpp/module.h" | 12 #include "ppapi/cpp/module.h" |
| 13 #include "ppapi/cpp/module_impl.h" | 13 #include "ppapi/cpp/module_impl.h" |
| 14 | 14 |
| 15 namespace pp { | 15 namespace pp { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 template <> const char* interface_name<PPB_ImageData>() { | 19 template <> const char* interface_name<PPB_ImageData>() { |
| 20 return PPB_IMAGEDATA_INTERFACE; | 20 return PPB_IMAGEDATA_INTERFACE; |
| 21 } | 21 } |
| 22 | 22 |
| 23 } // namespace | 23 } // namespace |
| 24 | 24 |
| 25 ImageData::ImageData() : data_(NULL) { | 25 ImageData::ImageData() : data_(NULL) { |
| 26 memset(&desc_, 0, sizeof(PP_ImageDataDesc)); | 26 memset(&desc_, 0, sizeof(PP_ImageDataDesc)); |
| 27 } | 27 } |
| 28 | 28 |
| 29 ImageData::ImageData(const ImageData& other) | 29 ImageData::ImageData(const ImageData& other) |
| 30 : Resource(other), | 30 : Resource(other), |
| 31 desc_(other.desc_), | 31 desc_(other.desc_), |
| 32 data_(other.data_) { | 32 data_(other.data_) { |
| 33 } | 33 } |
| 34 | 34 |
| 35 ImageData::ImageData(PassRef, PP_Resource resource) | 35 ImageData::ImageData(PassRef, PP_Resource resource) |
| 36 : data_(NULL) { | 36 : Resource(PASS_REF, resource), |
| 37 data_(NULL) { |
| 37 memset(&desc_, 0, sizeof(PP_ImageDataDesc)); | 38 memset(&desc_, 0, sizeof(PP_ImageDataDesc)); |
| 38 | 39 InitData(); |
| 39 if (!has_interface<PPB_ImageData>()) | |
| 40 return; | |
| 41 | |
| 42 PassRefAndInitData(resource); | |
| 43 } | 40 } |
| 44 | 41 |
| 45 ImageData::ImageData(Instance* instance, | 42 ImageData::ImageData(const InstanceHandle& instance, |
| 46 PP_ImageDataFormat format, | 43 PP_ImageDataFormat format, |
| 47 const Size& size, | 44 const Size& size, |
| 48 bool init_to_zero) | 45 bool init_to_zero) |
| 49 : data_(NULL) { | 46 : data_(NULL) { |
| 50 memset(&desc_, 0, sizeof(PP_ImageDataDesc)); | 47 memset(&desc_, 0, sizeof(PP_ImageDataDesc)); |
| 51 | 48 |
| 52 if (!has_interface<PPB_ImageData>()) | 49 if (!has_interface<PPB_ImageData>()) |
| 53 return; | 50 return; |
| 54 | 51 |
| 55 PassRefAndInitData(get_interface<PPB_ImageData>()->Create( | 52 PassRefFromConstructor(get_interface<PPB_ImageData>()->Create( |
| 56 instance->pp_instance(), format, &size.pp_size(), | 53 instance.pp_instance(), format, &size.pp_size(), |
| 57 PP_FromBool(init_to_zero))); | 54 PP_FromBool(init_to_zero))); |
| 55 InitData(); |
| 58 } | 56 } |
| 59 | 57 |
| 60 ImageData& ImageData::operator=(const ImageData& other) { | 58 ImageData& ImageData::operator=(const ImageData& other) { |
| 61 Resource::operator=(other); | 59 Resource::operator=(other); |
| 62 desc_ = other.desc_; | 60 desc_ = other.desc_; |
| 63 data_ = other.data_; | 61 data_ = other.data_; |
| 64 return *this; | 62 return *this; |
| 65 } | 63 } |
| 66 | 64 |
| 67 const uint32_t* ImageData::GetAddr32(const Point& coord) const { | 65 const uint32_t* ImageData::GetAddr32(const Point& coord) const { |
| 68 // Prefer evil const casts rather than evil code duplication. | 66 // Prefer evil const casts rather than evil code duplication. |
| 69 return const_cast<ImageData*>(this)->GetAddr32(coord); | 67 return const_cast<ImageData*>(this)->GetAddr32(coord); |
| 70 } | 68 } |
| 71 | 69 |
| 72 uint32_t* ImageData::GetAddr32(const Point& coord) { | 70 uint32_t* ImageData::GetAddr32(const Point& coord) { |
| 73 // If we add more image format types that aren't 32-bit, we'd want to check | 71 // If we add more image format types that aren't 32-bit, we'd want to check |
| 74 // here and fail. | 72 // here and fail. |
| 75 return reinterpret_cast<uint32_t*>( | 73 return reinterpret_cast<uint32_t*>( |
| 76 &static_cast<char*>(data())[coord.y() * stride() + coord.x() * 4]); | 74 &static_cast<char*>(data())[coord.y() * stride() + coord.x() * 4]); |
| 77 } | 75 } |
| 78 | 76 |
| 79 // static | 77 // static |
| 80 PP_ImageDataFormat ImageData::GetNativeImageDataFormat() { | 78 PP_ImageDataFormat ImageData::GetNativeImageDataFormat() { |
| 81 if (!has_interface<PPB_ImageData>()) | 79 if (!has_interface<PPB_ImageData>()) |
| 82 return PP_IMAGEDATAFORMAT_BGRA_PREMUL; // Default to something on failure. | 80 return PP_IMAGEDATAFORMAT_BGRA_PREMUL; // Default to something on failure. |
| 83 return get_interface<PPB_ImageData>()->GetNativeImageDataFormat(); | 81 return get_interface<PPB_ImageData>()->GetNativeImageDataFormat(); |
| 84 } | 82 } |
| 85 | 83 |
| 86 void ImageData::PassRefAndInitData(PP_Resource resource) { | 84 void ImageData::InitData() { |
| 87 PassRefFromConstructor(resource); | 85 if (!has_interface<PPB_ImageData>()) |
| 86 return; |
| 88 if (!get_interface<PPB_ImageData>()->Describe(pp_resource(), &desc_) || | 87 if (!get_interface<PPB_ImageData>()->Describe(pp_resource(), &desc_) || |
| 89 !(data_ = get_interface<PPB_ImageData>()->Map(pp_resource()))) | 88 !(data_ = get_interface<PPB_ImageData>()->Map(pp_resource()))) |
| 90 *this = ImageData(); | 89 *this = ImageData(); |
| 91 } | 90 } |
| 92 | 91 |
| 93 } // namespace pp | 92 } // namespace pp |
| OLD | NEW |