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.
11 #include "ppapi/cpp/instance_handle.h"
12 #include "ppapi/cpp/module.h"
13 #include "ppapi/cpp/module_impl.h"
19 template <> const char* interface_name
<PPB_ImageData_1_0
>() {
20 return PPB_IMAGEDATA_INTERFACE_1_0
;
25 ImageData::ImageData() : data_(NULL
) {
26 memset(&desc_
, 0, sizeof(PP_ImageDataDesc
));
29 ImageData::ImageData(const ImageData
& other
)
35 ImageData::ImageData(PassRef
, PP_Resource resource
)
36 : Resource(PASS_REF
, resource
),
38 memset(&desc_
, 0, sizeof(PP_ImageDataDesc
));
42 ImageData::ImageData(const InstanceHandle
& instance
,
43 PP_ImageDataFormat format
,
47 memset(&desc_
, 0, sizeof(PP_ImageDataDesc
));
49 if (!has_interface
<PPB_ImageData_1_0
>())
52 PassRefFromConstructor(get_interface
<PPB_ImageData_1_0
>()->Create(
53 instance
.pp_instance(), format
, &size
.pp_size(),
54 PP_FromBool(init_to_zero
)));
58 ImageData
& ImageData::operator=(const ImageData
& other
) {
59 Resource::operator=(other
);
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
73 return reinterpret_cast<uint32_t*>(
74 &static_cast<char*>(data())[coord
.y() * stride() + coord
.x() * 4]);
78 bool ImageData::IsImageDataFormatSupported(PP_ImageDataFormat format
) {
79 if (!has_interface
<PPB_ImageData_1_0
>())
81 return PP_ToBool(get_interface
<PPB_ImageData_1_0
>()->
82 IsImageDataFormatSupported(format
));
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
>())
95 if (!get_interface
<PPB_ImageData_1_0
>()->Describe(pp_resource(), &desc_
) ||
96 !(data_
= get_interface
<PPB_ImageData_1_0
>()->Map(pp_resource())))