Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(340)

Side by Side Diff: webkit/plugins/ppapi/ppb_image_data_impl.cc

Issue 5828003: Move the Pepper implementation from webkit/glue/plugins/pepper_* to... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 plugins {
25 namespace ppapi {
24 26
25 namespace { 27 namespace {
26 28
27 PP_ImageDataFormat GetNativeImageDataFormat() { 29 PP_ImageDataFormat GetNativeImageDataFormat() {
28 return ImageData::GetNativeImageDataFormat(); 30 return PPB_ImageData_Impl::GetNativeImageDataFormat();
29 } 31 }
30 32
31 PP_Bool IsImageDataFormatSupported(PP_ImageDataFormat format) { 33 PP_Bool IsImageDataFormatSupported(PP_ImageDataFormat format) {
32 return BoolToPPBool(ImageData::IsImageDataFormatSupported(format)); 34 return BoolToPPBool(PPB_ImageData_Impl::IsImageDataFormatSupported(format));
33 } 35 }
34 36
35 PP_Resource Create(PP_Module module_id, 37 PP_Resource Create(PP_Module module_id,
36 PP_ImageDataFormat format, 38 PP_ImageDataFormat format,
37 const PP_Size* size, 39 const PP_Size* size,
38 PP_Bool init_to_zero) { 40 PP_Bool init_to_zero) {
39 PluginModule* module = ResourceTracker::Get()->GetModule(module_id); 41 PluginModule* module = ResourceTracker::Get()->GetModule(module_id);
40 if (!module) 42 if (!module)
41 return 0; 43 return 0;
42 44
43 scoped_refptr<ImageData> data(new ImageData(module)); 45 scoped_refptr<PPB_ImageData_Impl> data(new PPB_ImageData_Impl(module));
44 if (!data->Init(format, 46 if (!data->Init(format,
45 size->width, 47 size->width,
46 size->height, 48 size->height,
47 PPBoolToBool(init_to_zero))) { 49 PPBoolToBool(init_to_zero))) {
48 return 0; 50 return 0;
49 } 51 }
50 52
51 return data->GetReference(); 53 return data->GetReference();
52 } 54 }
53 55
54 PP_Bool IsImageData(PP_Resource resource) { 56 PP_Bool IsImageData(PP_Resource resource) {
55 return BoolToPPBool(!!Resource::GetAs<ImageData>(resource)); 57 return BoolToPPBool(!!Resource::GetAs<PPB_ImageData_Impl>(resource));
56 } 58 }
57 59
58 PP_Bool Describe(PP_Resource resource, PP_ImageDataDesc* desc) { 60 PP_Bool Describe(PP_Resource resource, PP_ImageDataDesc* desc) {
59 // Give predictable values on failure. 61 // Give predictable values on failure.
60 memset(desc, 0, sizeof(PP_ImageDataDesc)); 62 memset(desc, 0, sizeof(PP_ImageDataDesc));
61 63
62 scoped_refptr<ImageData> image_data(Resource::GetAs<ImageData>(resource)); 64 scoped_refptr<PPB_ImageData_Impl> image_data(
65 Resource::GetAs<PPB_ImageData_Impl>(resource));
63 if (!image_data) 66 if (!image_data)
64 return PP_FALSE; 67 return PP_FALSE;
65 image_data->Describe(desc); 68 image_data->Describe(desc);
66 return PP_TRUE; 69 return PP_TRUE;
67 } 70 }
68 71
69 void* Map(PP_Resource resource) { 72 void* Map(PP_Resource resource) {
70 scoped_refptr<ImageData> image_data(Resource::GetAs<ImageData>(resource)); 73 scoped_refptr<PPB_ImageData_Impl> image_data(
74 Resource::GetAs<PPB_ImageData_Impl>(resource));
71 if (!image_data) 75 if (!image_data)
72 return NULL; 76 return NULL;
73 return image_data->Map(); 77 return image_data->Map();
74 } 78 }
75 79
76 void Unmap(PP_Resource resource) { 80 void Unmap(PP_Resource resource) {
77 scoped_refptr<ImageData> image_data(Resource::GetAs<ImageData>(resource)); 81 scoped_refptr<PPB_ImageData_Impl> image_data(
82 Resource::GetAs<PPB_ImageData_Impl>(resource));
78 if (image_data) 83 if (image_data)
79 image_data->Unmap(); 84 image_data->Unmap();
80 } 85 }
81 86
82 int32_t GetSharedMemory(PP_Resource resource, 87 int32_t GetSharedMemory(PP_Resource resource,
83 int* handle, 88 int* handle,
84 uint32_t* byte_count) { 89 uint32_t* byte_count) {
85 scoped_refptr<ImageData> image_data(Resource::GetAs<ImageData>(resource)); 90 scoped_refptr<PPB_ImageData_Impl> image_data(
91 Resource::GetAs<PPB_ImageData_Impl>(resource));
86 if (image_data) { 92 if (image_data) {
87 *handle = image_data->GetSharedMemoryHandle(byte_count); 93 *handle = image_data->GetSharedMemoryHandle(byte_count);
88 return PP_OK; 94 return PP_OK;
89 } 95 }
90 return PP_ERROR_BADRESOURCE; 96 return PP_ERROR_BADRESOURCE;
91 } 97 }
92 98
93 const PPB_ImageData ppb_imagedata = { 99 const PPB_ImageData ppb_imagedata = {
94 &GetNativeImageDataFormat, 100 &GetNativeImageDataFormat,
95 &IsImageDataFormatSupported, 101 &IsImageDataFormatSupported,
96 &Create, 102 &Create,
97 &IsImageData, 103 &IsImageData,
98 &Describe, 104 &Describe,
99 &Map, 105 &Map,
100 &Unmap, 106 &Unmap,
101 }; 107 };
102 108
103 const PPB_ImageDataTrusted ppb_imagedata_trusted = { 109 const PPB_ImageDataTrusted ppb_imagedata_trusted = {
104 &GetSharedMemory, 110 &GetSharedMemory,
105 }; 111 };
106 112
107 } // namespace 113 } // namespace
108 114
109 ImageData::ImageData(PluginModule* module) 115 PPB_ImageData_Impl::PPB_ImageData_Impl(PluginModule* module)
110 : Resource(module), 116 : Resource(module),
111 format_(PP_IMAGEDATAFORMAT_BGRA_PREMUL), 117 format_(PP_IMAGEDATAFORMAT_BGRA_PREMUL),
112 width_(0), 118 width_(0),
113 height_(0) { 119 height_(0) {
114 } 120 }
115 121
116 ImageData::~ImageData() { 122 PPB_ImageData_Impl::~PPB_ImageData_Impl() {
117 } 123 }
118 124
119 // static 125 // static
120 const PPB_ImageData* ImageData::GetInterface() { 126 const PPB_ImageData* PPB_ImageData_Impl::GetInterface() {
121 return &ppb_imagedata; 127 return &ppb_imagedata;
122 } 128 }
123 129
124 // static 130 // static
125 const PPB_ImageDataTrusted* ImageData::GetTrustedInterface() { 131 const PPB_ImageDataTrusted* PPB_ImageData_Impl::GetTrustedInterface() {
126 return &ppb_imagedata_trusted; 132 return &ppb_imagedata_trusted;
127 } 133 }
128 134
129 // static 135 // static
130 PP_ImageDataFormat ImageData::GetNativeImageDataFormat() { 136 PP_ImageDataFormat PPB_ImageData_Impl::GetNativeImageDataFormat() {
131 if (SK_B32_SHIFT == 0) 137 if (SK_B32_SHIFT == 0)
132 return PP_IMAGEDATAFORMAT_BGRA_PREMUL; 138 return PP_IMAGEDATAFORMAT_BGRA_PREMUL;
133 else if (SK_R32_SHIFT == 0) 139 else if (SK_R32_SHIFT == 0)
134 return PP_IMAGEDATAFORMAT_RGBA_PREMUL; 140 return PP_IMAGEDATAFORMAT_RGBA_PREMUL;
135 else 141 else
136 return PP_IMAGEDATAFORMAT_BGRA_PREMUL; // Default to something on failure. 142 return PP_IMAGEDATAFORMAT_BGRA_PREMUL; // Default to something on failure.
137 } 143 }
138 144
139 // static 145 // static
140 bool ImageData::IsImageDataFormatSupported(PP_ImageDataFormat format) { 146 bool PPB_ImageData_Impl::IsImageDataFormatSupported(
147 PP_ImageDataFormat format) {
141 return format == PP_IMAGEDATAFORMAT_BGRA_PREMUL || 148 return format == PP_IMAGEDATAFORMAT_BGRA_PREMUL ||
142 format == PP_IMAGEDATAFORMAT_RGBA_PREMUL; 149 format == PP_IMAGEDATAFORMAT_RGBA_PREMUL;
143 } 150 }
144 151
145 bool ImageData::Init(PP_ImageDataFormat format, 152 PPB_ImageData_Impl* PPB_ImageData_Impl::AsImageData() {
146 int width, int height, 153 return this;
147 bool init_to_zero) { 154 }
155
156 bool PPB_ImageData_Impl::Init(PP_ImageDataFormat format,
157 int width, int height,
158 bool init_to_zero) {
148 // TODO(brettw) this should be called only on the main thread! 159 // TODO(brettw) this should be called only on the main thread!
149 // TODO(brettw) use init_to_zero when we implement caching. 160 // TODO(brettw) use init_to_zero when we implement caching.
150 if (!IsImageDataFormatSupported(format)) 161 if (!IsImageDataFormatSupported(format))
151 return false; // Only support this one format for now. 162 return false; // Only support this one format for now.
152 if (width <= 0 || height <= 0) 163 if (width <= 0 || height <= 0)
153 return false; 164 return false;
154 if (static_cast<int64>(width) * static_cast<int64>(height) >= 165 if (static_cast<int64>(width) * static_cast<int64>(height) >=
155 std::numeric_limits<int32>::max()) 166 std::numeric_limits<int32>::max())
156 return false; // Prevent overflow of signed 32-bit ints. 167 return false; // Prevent overflow of signed 32-bit ints.
157 168
158 platform_image_.reset( 169 platform_image_.reset(
159 module()->GetSomeInstance()->delegate()->CreateImage2D(width, height)); 170 module()->GetSomeInstance()->delegate()->CreateImage2D(width, height));
160 format_ = format; 171 format_ = format;
161 width_ = width; 172 width_ = width;
162 height_ = height; 173 height_ = height;
163 return !!platform_image_.get(); 174 return !!platform_image_.get();
164 } 175 }
165 176
166 void ImageData::Describe(PP_ImageDataDesc* desc) const { 177 void PPB_ImageData_Impl::Describe(PP_ImageDataDesc* desc) const {
167 desc->format = format_; 178 desc->format = format_;
168 desc->size.width = width_; 179 desc->size.width = width_;
169 desc->size.height = height_; 180 desc->size.height = height_;
170 desc->stride = width_ * 4; 181 desc->stride = width_ * 4;
171 } 182 }
172 183
173 void* ImageData::Map() { 184 void* PPB_ImageData_Impl::Map() {
174 if (!mapped_canvas_.get()) { 185 if (!mapped_canvas_.get()) {
175 mapped_canvas_.reset(platform_image_->Map()); 186 mapped_canvas_.reset(platform_image_->Map());
176 if (!mapped_canvas_.get()) 187 if (!mapped_canvas_.get())
177 return NULL; 188 return NULL;
178 } 189 }
179 const SkBitmap& bitmap = 190 const SkBitmap& bitmap =
180 mapped_canvas_->getTopPlatformDevice().accessBitmap(true); 191 mapped_canvas_->getTopPlatformDevice().accessBitmap(true);
181 192
182 // Our platform bitmaps are set to opaque by default, which we don't want. 193 // Our platform bitmaps are set to opaque by default, which we don't want.
183 const_cast<SkBitmap&>(bitmap).setIsOpaque(false); 194 const_cast<SkBitmap&>(bitmap).setIsOpaque(false);
184 195
185 bitmap.lockPixels(); 196 bitmap.lockPixels();
186 return bitmap.getAddr32(0, 0); 197 return bitmap.getAddr32(0, 0);
187 } 198 }
188 199
189 void ImageData::Unmap() { 200 void PPB_ImageData_Impl::Unmap() {
190 // This is currently unimplemented, which is OK. The data will just always 201 // 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 202 // 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 203 // unmappable without freeing it, but this may be something we want to support
193 // in the future to save some memory. 204 // in the future to save some memory.
194 } 205 }
195 206
196 int ImageData::GetSharedMemoryHandle(uint32* byte_count) const { 207 int PPB_ImageData_Impl::GetSharedMemoryHandle(uint32* byte_count) const {
197 return platform_image_->GetSharedMemoryHandle(byte_count); 208 return platform_image_->GetSharedMemoryHandle(byte_count);
198 } 209 }
199 210
200 const SkBitmap* ImageData::GetMappedBitmap() const { 211 const SkBitmap* PPB_ImageData_Impl::GetMappedBitmap() const {
201 if (!mapped_canvas_.get()) 212 if (!mapped_canvas_.get())
202 return NULL; 213 return NULL;
203 return &mapped_canvas_->getTopPlatformDevice().accessBitmap(false); 214 return &mapped_canvas_->getTopPlatformDevice().accessBitmap(false);
204 } 215 }
205 216
206 void ImageData::Swap(ImageData* other) { 217 void PPB_ImageData_Impl::Swap(PPB_ImageData_Impl* other) {
207 swap(other->platform_image_, platform_image_); 218 swap(other->platform_image_, platform_image_);
208 swap(other->mapped_canvas_, mapped_canvas_); 219 swap(other->mapped_canvas_, mapped_canvas_);
209 std::swap(other->format_, format_); 220 std::swap(other->format_, format_);
210 std::swap(other->width_, width_); 221 std::swap(other->width_, width_);
211 std::swap(other->height_, height_); 222 std::swap(other->height_, height_);
212 } 223 }
213 224
214 } // namespace pepper 225 } // namespace ppapi
226 } // namespace plugins
227 } // namespace webkit
228
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698