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 "webkit/glue/plugins/pepper_image_data.h" | 5 #include "webkit/plugins/ppapi/ppb_image_data_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/scoped_ptr.h" | 11 #include "base/scoped_ptr.h" |
12 #include "skia/ext/platform_canvas.h" | 12 #include "skia/ext/platform_canvas.h" |
13 #include "ppapi/c/pp_instance.h" | 13 #include "ppapi/c/pp_instance.h" |
14 #include "ppapi/c/pp_module.h" | 14 #include "ppapi/c/pp_module.h" |
15 #include "ppapi/c/pp_resource.h" | 15 #include "ppapi/c/pp_resource.h" |
16 #include "ppapi/c/ppb_image_data.h" | 16 #include "ppapi/c/ppb_image_data.h" |
17 #include "ppapi/c/trusted/ppb_image_data_trusted.h" | 17 #include "ppapi/c/trusted/ppb_image_data_trusted.h" |
18 #include "third_party/skia/include/core/SkColorPriv.h" | 18 #include "third_party/skia/include/core/SkColorPriv.h" |
19 #include "webkit/glue/plugins/pepper_common.h" | 19 #include "webkit/plugins/ppapi/common.h" |
20 #include "webkit/glue/plugins/pepper_plugin_instance.h" | 20 #include "webkit/plugins/ppapi/plugin_instance.h" |
21 #include "webkit/glue/plugins/pepper_plugin_module.h" | 21 #include "webkit/plugins/ppapi/plugin_module.h" |
22 | 22 |
23 namespace pepper { | 23 namespace webkit { |
| 24 namespace ppapi { |
24 | 25 |
25 namespace { | 26 namespace { |
26 | 27 |
27 PP_ImageDataFormat GetNativeImageDataFormat() { | 28 PP_ImageDataFormat GetNativeImageDataFormat() { |
28 return ImageData::GetNativeImageDataFormat(); | 29 return PPB_ImageData_Impl::GetNativeImageDataFormat(); |
29 } | 30 } |
30 | 31 |
31 PP_Bool IsImageDataFormatSupported(PP_ImageDataFormat format) { | 32 PP_Bool IsImageDataFormatSupported(PP_ImageDataFormat format) { |
32 return BoolToPPBool(ImageData::IsImageDataFormatSupported(format)); | 33 return BoolToPPBool(PPB_ImageData_Impl::IsImageDataFormatSupported(format)); |
33 } | 34 } |
34 | 35 |
35 PP_Resource Create(PP_Module module_id, | 36 PP_Resource Create(PP_Module module_id, |
36 PP_ImageDataFormat format, | 37 PP_ImageDataFormat format, |
37 const PP_Size* size, | 38 const PP_Size* size, |
38 PP_Bool init_to_zero) { | 39 PP_Bool init_to_zero) { |
39 PluginModule* module = ResourceTracker::Get()->GetModule(module_id); | 40 PluginModule* module = ResourceTracker::Get()->GetModule(module_id); |
40 if (!module) | 41 if (!module) |
41 return 0; | 42 return 0; |
42 | 43 |
43 scoped_refptr<ImageData> data(new ImageData(module)); | 44 scoped_refptr<PPB_ImageData_Impl> data(new PPB_ImageData_Impl(module)); |
44 if (!data->Init(format, | 45 if (!data->Init(format, |
45 size->width, | 46 size->width, |
46 size->height, | 47 size->height, |
47 PPBoolToBool(init_to_zero))) { | 48 PPBoolToBool(init_to_zero))) { |
48 return 0; | 49 return 0; |
49 } | 50 } |
50 | 51 |
51 return data->GetReference(); | 52 return data->GetReference(); |
52 } | 53 } |
53 | 54 |
54 PP_Bool IsImageData(PP_Resource resource) { | 55 PP_Bool IsImageData(PP_Resource resource) { |
55 return BoolToPPBool(!!Resource::GetAs<ImageData>(resource)); | 56 return BoolToPPBool(!!Resource::GetAs<PPB_ImageData_Impl>(resource)); |
56 } | 57 } |
57 | 58 |
58 PP_Bool Describe(PP_Resource resource, PP_ImageDataDesc* desc) { | 59 PP_Bool Describe(PP_Resource resource, PP_ImageDataDesc* desc) { |
59 // Give predictable values on failure. | 60 // Give predictable values on failure. |
60 memset(desc, 0, sizeof(PP_ImageDataDesc)); | 61 memset(desc, 0, sizeof(PP_ImageDataDesc)); |
61 | 62 |
62 scoped_refptr<ImageData> image_data(Resource::GetAs<ImageData>(resource)); | 63 scoped_refptr<PPB_ImageData_Impl> image_data( |
| 64 Resource::GetAs<PPB_ImageData_Impl>(resource)); |
63 if (!image_data) | 65 if (!image_data) |
64 return PP_FALSE; | 66 return PP_FALSE; |
65 image_data->Describe(desc); | 67 image_data->Describe(desc); |
66 return PP_TRUE; | 68 return PP_TRUE; |
67 } | 69 } |
68 | 70 |
69 void* Map(PP_Resource resource) { | 71 void* Map(PP_Resource resource) { |
70 scoped_refptr<ImageData> image_data(Resource::GetAs<ImageData>(resource)); | 72 scoped_refptr<PPB_ImageData_Impl> image_data( |
| 73 Resource::GetAs<PPB_ImageData_Impl>(resource)); |
71 if (!image_data) | 74 if (!image_data) |
72 return NULL; | 75 return NULL; |
73 return image_data->Map(); | 76 return image_data->Map(); |
74 } | 77 } |
75 | 78 |
76 void Unmap(PP_Resource resource) { | 79 void Unmap(PP_Resource resource) { |
77 scoped_refptr<ImageData> image_data(Resource::GetAs<ImageData>(resource)); | 80 scoped_refptr<PPB_ImageData_Impl> image_data( |
| 81 Resource::GetAs<PPB_ImageData_Impl>(resource)); |
78 if (image_data) | 82 if (image_data) |
79 image_data->Unmap(); | 83 image_data->Unmap(); |
80 } | 84 } |
81 | 85 |
82 int32_t GetSharedMemory(PP_Resource resource, | 86 int32_t GetSharedMemory(PP_Resource resource, |
83 int* handle, | 87 int* handle, |
84 uint32_t* byte_count) { | 88 uint32_t* byte_count) { |
85 scoped_refptr<ImageData> image_data(Resource::GetAs<ImageData>(resource)); | 89 scoped_refptr<PPB_ImageData_Impl> image_data( |
| 90 Resource::GetAs<PPB_ImageData_Impl>(resource)); |
86 if (image_data) { | 91 if (image_data) { |
87 *handle = image_data->GetSharedMemoryHandle(byte_count); | 92 *handle = image_data->GetSharedMemoryHandle(byte_count); |
88 return PP_OK; | 93 return PP_OK; |
89 } | 94 } |
90 return PP_ERROR_BADRESOURCE; | 95 return PP_ERROR_BADRESOURCE; |
91 } | 96 } |
92 | 97 |
93 const PPB_ImageData ppb_imagedata = { | 98 const PPB_ImageData ppb_imagedata = { |
94 &GetNativeImageDataFormat, | 99 &GetNativeImageDataFormat, |
95 &IsImageDataFormatSupported, | 100 &IsImageDataFormatSupported, |
96 &Create, | 101 &Create, |
97 &IsImageData, | 102 &IsImageData, |
98 &Describe, | 103 &Describe, |
99 &Map, | 104 &Map, |
100 &Unmap, | 105 &Unmap, |
101 }; | 106 }; |
102 | 107 |
103 const PPB_ImageDataTrusted ppb_imagedata_trusted = { | 108 const PPB_ImageDataTrusted ppb_imagedata_trusted = { |
104 &GetSharedMemory, | 109 &GetSharedMemory, |
105 }; | 110 }; |
106 | 111 |
107 } // namespace | 112 } // namespace |
108 | 113 |
109 ImageData::ImageData(PluginModule* module) | 114 PPB_ImageData_Impl::PPB_ImageData_Impl(PluginModule* module) |
110 : Resource(module), | 115 : Resource(module), |
111 format_(PP_IMAGEDATAFORMAT_BGRA_PREMUL), | 116 format_(PP_IMAGEDATAFORMAT_BGRA_PREMUL), |
112 width_(0), | 117 width_(0), |
113 height_(0) { | 118 height_(0) { |
114 } | 119 } |
115 | 120 |
116 ImageData::~ImageData() { | 121 PPB_ImageData_Impl::~PPB_ImageData_Impl() { |
117 } | 122 } |
118 | 123 |
119 // static | 124 // static |
120 const PPB_ImageData* ImageData::GetInterface() { | 125 const PPB_ImageData* PPB_ImageData_Impl::GetInterface() { |
121 return &ppb_imagedata; | 126 return &ppb_imagedata; |
122 } | 127 } |
123 | 128 |
124 // static | 129 // static |
125 const PPB_ImageDataTrusted* ImageData::GetTrustedInterface() { | 130 const PPB_ImageDataTrusted* PPB_ImageData_Impl::GetTrustedInterface() { |
126 return &ppb_imagedata_trusted; | 131 return &ppb_imagedata_trusted; |
127 } | 132 } |
128 | 133 |
129 // static | 134 // static |
130 PP_ImageDataFormat ImageData::GetNativeImageDataFormat() { | 135 PP_ImageDataFormat PPB_ImageData_Impl::GetNativeImageDataFormat() { |
131 if (SK_B32_SHIFT == 0) | 136 if (SK_B32_SHIFT == 0) |
132 return PP_IMAGEDATAFORMAT_BGRA_PREMUL; | 137 return PP_IMAGEDATAFORMAT_BGRA_PREMUL; |
133 else if (SK_R32_SHIFT == 0) | 138 else if (SK_R32_SHIFT == 0) |
134 return PP_IMAGEDATAFORMAT_RGBA_PREMUL; | 139 return PP_IMAGEDATAFORMAT_RGBA_PREMUL; |
135 else | 140 else |
136 return PP_IMAGEDATAFORMAT_BGRA_PREMUL; // Default to something on failure. | 141 return PP_IMAGEDATAFORMAT_BGRA_PREMUL; // Default to something on failure. |
137 } | 142 } |
138 | 143 |
139 // static | 144 // static |
140 bool ImageData::IsImageDataFormatSupported(PP_ImageDataFormat format) { | 145 bool PPB_ImageData_Impl::IsImageDataFormatSupported( |
| 146 PP_ImageDataFormat format) { |
141 return format == PP_IMAGEDATAFORMAT_BGRA_PREMUL || | 147 return format == PP_IMAGEDATAFORMAT_BGRA_PREMUL || |
142 format == PP_IMAGEDATAFORMAT_RGBA_PREMUL; | 148 format == PP_IMAGEDATAFORMAT_RGBA_PREMUL; |
143 } | 149 } |
144 | 150 |
145 bool ImageData::Init(PP_ImageDataFormat format, | 151 PPB_ImageData_Impl* PPB_ImageData_Impl::AsPPB_ImageData_Impl() { |
146 int width, int height, | 152 return this; |
147 bool init_to_zero) { | 153 } |
| 154 |
| 155 bool PPB_ImageData_Impl::Init(PP_ImageDataFormat format, |
| 156 int width, int height, |
| 157 bool init_to_zero) { |
148 // TODO(brettw) this should be called only on the main thread! | 158 // TODO(brettw) this should be called only on the main thread! |
149 // TODO(brettw) use init_to_zero when we implement caching. | 159 // TODO(brettw) use init_to_zero when we implement caching. |
150 if (!IsImageDataFormatSupported(format)) | 160 if (!IsImageDataFormatSupported(format)) |
151 return false; // Only support this one format for now. | 161 return false; // Only support this one format for now. |
152 if (width <= 0 || height <= 0) | 162 if (width <= 0 || height <= 0) |
153 return false; | 163 return false; |
154 if (static_cast<int64>(width) * static_cast<int64>(height) >= | 164 if (static_cast<int64>(width) * static_cast<int64>(height) >= |
155 std::numeric_limits<int32>::max()) | 165 std::numeric_limits<int32>::max()) |
156 return false; // Prevent overflow of signed 32-bit ints. | 166 return false; // Prevent overflow of signed 32-bit ints. |
157 | 167 |
158 platform_image_.reset( | 168 platform_image_.reset( |
159 module()->GetSomeInstance()->delegate()->CreateImage2D(width, height)); | 169 module()->GetSomeInstance()->delegate()->CreateImage2D(width, height)); |
160 format_ = format; | 170 format_ = format; |
161 width_ = width; | 171 width_ = width; |
162 height_ = height; | 172 height_ = height; |
163 return !!platform_image_.get(); | 173 return !!platform_image_.get(); |
164 } | 174 } |
165 | 175 |
166 void ImageData::Describe(PP_ImageDataDesc* desc) const { | 176 void PPB_ImageData_Impl::Describe(PP_ImageDataDesc* desc) const { |
167 desc->format = format_; | 177 desc->format = format_; |
168 desc->size.width = width_; | 178 desc->size.width = width_; |
169 desc->size.height = height_; | 179 desc->size.height = height_; |
170 desc->stride = width_ * 4; | 180 desc->stride = width_ * 4; |
171 } | 181 } |
172 | 182 |
173 void* ImageData::Map() { | 183 void* PPB_ImageData_Impl::Map() { |
174 if (!mapped_canvas_.get()) { | 184 if (!mapped_canvas_.get()) { |
175 mapped_canvas_.reset(platform_image_->Map()); | 185 mapped_canvas_.reset(platform_image_->Map()); |
176 if (!mapped_canvas_.get()) | 186 if (!mapped_canvas_.get()) |
177 return NULL; | 187 return NULL; |
178 } | 188 } |
179 const SkBitmap& bitmap = | 189 const SkBitmap& bitmap = |
180 mapped_canvas_->getTopPlatformDevice().accessBitmap(true); | 190 mapped_canvas_->getTopPlatformDevice().accessBitmap(true); |
181 | 191 |
182 // Our platform bitmaps are set to opaque by default, which we don't want. | 192 // Our platform bitmaps are set to opaque by default, which we don't want. |
183 const_cast<SkBitmap&>(bitmap).setIsOpaque(false); | 193 const_cast<SkBitmap&>(bitmap).setIsOpaque(false); |
184 | 194 |
185 bitmap.lockPixels(); | 195 bitmap.lockPixels(); |
186 return bitmap.getAddr32(0, 0); | 196 return bitmap.getAddr32(0, 0); |
187 } | 197 } |
188 | 198 |
189 void ImageData::Unmap() { | 199 void PPB_ImageData_Impl::Unmap() { |
190 // This is currently unimplemented, which is OK. The data will just always | 200 // This is currently unimplemented, which is OK. The data will just always |
191 // be around once it's mapped. Chrome's TransportDIB isn't currently | 201 // be around once it's mapped. Chrome's TransportDIB isn't currently |
192 // unmappable without freeing it, but this may be something we want to support | 202 // unmappable without freeing it, but this may be something we want to support |
193 // in the future to save some memory. | 203 // in the future to save some memory. |
194 } | 204 } |
195 | 205 |
196 int ImageData::GetSharedMemoryHandle(uint32* byte_count) const { | 206 int PPB_ImageData_Impl::GetSharedMemoryHandle(uint32* byte_count) const { |
197 return platform_image_->GetSharedMemoryHandle(byte_count); | 207 return platform_image_->GetSharedMemoryHandle(byte_count); |
198 } | 208 } |
199 | 209 |
200 const SkBitmap* ImageData::GetMappedBitmap() const { | 210 const SkBitmap* PPB_ImageData_Impl::GetMappedBitmap() const { |
201 if (!mapped_canvas_.get()) | 211 if (!mapped_canvas_.get()) |
202 return NULL; | 212 return NULL; |
203 return &mapped_canvas_->getTopPlatformDevice().accessBitmap(false); | 213 return &mapped_canvas_->getTopPlatformDevice().accessBitmap(false); |
204 } | 214 } |
205 | 215 |
206 void ImageData::Swap(ImageData* other) { | 216 void PPB_ImageData_Impl::Swap(PPB_ImageData_Impl* other) { |
207 swap(other->platform_image_, platform_image_); | 217 swap(other->platform_image_, platform_image_); |
208 swap(other->mapped_canvas_, mapped_canvas_); | 218 swap(other->mapped_canvas_, mapped_canvas_); |
209 std::swap(other->format_, format_); | 219 std::swap(other->format_, format_); |
210 std::swap(other->width_, width_); | 220 std::swap(other->width_, width_); |
211 std::swap(other->height_, height_); | 221 std::swap(other->height_, height_); |
212 } | 222 } |
213 | 223 |
214 } // namespace pepper | 224 } // namespace ppapi |
| 225 } // namespace webkit |
| 226 |
OLD | NEW |