OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef WEBKIT_PLUGINS_PPAPI_PPB_IMAGE_DATA_IMPL_H_ | |
6 #define WEBKIT_PLUGINS_PPAPI_PPB_IMAGE_DATA_IMPL_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "ppapi/c/ppb_image_data.h" | |
11 #include "ppapi/shared_impl/ppb_image_data_shared.h" | |
12 #include "ppapi/shared_impl/resource.h" | |
13 #include "ppapi/thunk/ppb_image_data_api.h" | |
14 #include "third_party/skia/include/core/SkCanvas.h" | |
15 #include "webkit/plugins/ppapi/plugin_delegate.h" | |
16 #include "webkit/plugins/webkit_plugins_export.h" | |
17 | |
18 class SkBitmap; | |
19 class SkCanvas; | |
20 | |
21 namespace webkit { | |
22 namespace ppapi { | |
23 | |
24 class WEBKIT_PLUGINS_EXPORT PPB_ImageData_Impl | |
25 : public ::ppapi::Resource, | |
26 public ::ppapi::PPB_ImageData_Shared, | |
27 public NON_EXPORTED_BASE(::ppapi::thunk::PPB_ImageData_API) { | |
28 public: | |
29 // We delegate most of our implementation to a back-end class that either uses | |
30 // a PlatformCanvas (for most trusted stuff) or bare shared memory (for use by | |
31 // NaCl, or trusted plugins when the PlatformCanvas isn't needed). This makes | |
32 // it cheap & easy to implement Swap. | |
33 class Backend { | |
34 public: | |
35 virtual ~Backend() {}; | |
36 virtual bool Init(PPB_ImageData_Impl* impl, PP_ImageDataFormat format, | |
37 int width, int height, bool init_to_zero) = 0; | |
38 virtual bool IsMapped() const = 0; | |
39 virtual PluginDelegate::PlatformImage2D* PlatformImage() const = 0; | |
40 virtual void* Map() = 0; | |
41 virtual void Unmap() = 0; | |
42 virtual int32_t GetSharedMemory(int* handle, uint32_t* byte_count) = 0; | |
43 virtual SkCanvas* GetPlatformCanvas() = 0; | |
44 virtual SkCanvas* GetCanvas() = 0; | |
45 virtual const SkBitmap* GetMappedBitmap() const = 0; | |
46 }; | |
47 | |
48 // If you call this constructor, you must also call Init before use. Normally | |
49 // you should use the static Create function, but this constructor is needed | |
50 // for some internal uses of ImageData (like Graphics2D). | |
51 PPB_ImageData_Impl(PP_Instance instance, | |
52 PPB_ImageData_Shared::ImageDataType type); | |
53 virtual ~PPB_ImageData_Impl(); | |
54 | |
55 bool Init(PP_ImageDataFormat format, | |
56 int width, int height, | |
57 bool init_to_zero); | |
58 | |
59 static PP_Resource Create(PP_Instance pp_instance, | |
60 PPB_ImageData_Shared::ImageDataType type, | |
61 PP_ImageDataFormat format, | |
62 const PP_Size& size, | |
63 PP_Bool init_to_zero); | |
64 | |
65 int width() const { return width_; } | |
66 int height() const { return height_; } | |
67 | |
68 // Returns the image format. | |
69 PP_ImageDataFormat format() const { return format_; } | |
70 | |
71 // Returns true if this image is mapped. False means that the image is either | |
72 // invalid or not mapped. See ImageDataAutoMapper below. | |
73 bool IsMapped() const; | |
74 PluginDelegate::PlatformImage2D* PlatformImage() const; | |
75 | |
76 // Resource override. | |
77 virtual ::ppapi::thunk::PPB_ImageData_API* AsPPB_ImageData_API() OVERRIDE; | |
78 | |
79 // PPB_ImageData_API implementation. | |
80 virtual PP_Bool Describe(PP_ImageDataDesc* desc) OVERRIDE; | |
81 virtual void* Map() OVERRIDE; | |
82 virtual void Unmap() OVERRIDE; | |
83 virtual int32_t GetSharedMemory(int* handle, uint32_t* byte_count) OVERRIDE; | |
84 virtual SkCanvas* GetPlatformCanvas() OVERRIDE; | |
85 virtual SkCanvas* GetCanvas() OVERRIDE; | |
86 virtual void SetIsCandidateForReuse() OVERRIDE; | |
87 | |
88 const SkBitmap* GetMappedBitmap() const; | |
89 | |
90 private: | |
91 PP_ImageDataFormat format_; | |
92 int width_; | |
93 int height_; | |
94 scoped_ptr<Backend> backend_; | |
95 | |
96 DISALLOW_COPY_AND_ASSIGN(PPB_ImageData_Impl); | |
97 }; | |
98 | |
99 class ImageDataPlatformBackend : public PPB_ImageData_Impl::Backend { | |
100 public: | |
101 ImageDataPlatformBackend(); | |
102 virtual ~ImageDataPlatformBackend(); | |
103 | |
104 // PPB_ImageData_Impl::Backend implementation. | |
105 virtual bool Init(PPB_ImageData_Impl* impl, PP_ImageDataFormat format, | |
106 int width, int height, bool init_to_zero) OVERRIDE; | |
107 virtual bool IsMapped() const OVERRIDE; | |
108 virtual PluginDelegate::PlatformImage2D* PlatformImage() const OVERRIDE; | |
109 virtual void* Map() OVERRIDE; | |
110 virtual void Unmap() OVERRIDE; | |
111 virtual int32_t GetSharedMemory(int* handle, uint32_t* byte_count) OVERRIDE; | |
112 virtual SkCanvas* GetPlatformCanvas() OVERRIDE; | |
113 virtual SkCanvas* GetCanvas() OVERRIDE; | |
114 virtual const SkBitmap* GetMappedBitmap() const OVERRIDE; | |
115 | |
116 private: | |
117 // This will be NULL before initialization, and if this PPB_ImageData_Impl is | |
118 // swapped with another. | |
119 scoped_ptr<PluginDelegate::PlatformImage2D> platform_image_; | |
120 | |
121 // When the device is mapped, this is the image. Null when umapped. | |
122 scoped_ptr<SkCanvas> mapped_canvas_; | |
123 | |
124 DISALLOW_COPY_AND_ASSIGN(ImageDataPlatformBackend); | |
125 }; | |
126 | |
127 class ImageDataSimpleBackend : public PPB_ImageData_Impl::Backend { | |
128 public: | |
129 ImageDataSimpleBackend(); | |
130 virtual ~ImageDataSimpleBackend(); | |
131 | |
132 // PPB_ImageData_Impl::Backend implementation. | |
133 bool Init(PPB_ImageData_Impl* impl, PP_ImageDataFormat format, | |
134 int width, int height, bool init_to_zero) OVERRIDE; | |
135 virtual bool IsMapped() const OVERRIDE; | |
136 PluginDelegate::PlatformImage2D* PlatformImage() const OVERRIDE; | |
137 virtual void* Map() OVERRIDE; | |
138 virtual void Unmap() OVERRIDE; | |
139 virtual int32_t GetSharedMemory(int* handle, uint32_t* byte_count) OVERRIDE; | |
140 virtual SkCanvas* GetPlatformCanvas() OVERRIDE; | |
141 virtual SkCanvas* GetCanvas() OVERRIDE; | |
142 virtual const SkBitmap* GetMappedBitmap() const OVERRIDE; | |
143 | |
144 private: | |
145 scoped_ptr<base::SharedMemory> shared_memory_; | |
146 // skia_bitmap_ is backed by shared_memory_. | |
147 SkBitmap skia_bitmap_; | |
148 scoped_ptr<SkCanvas> skia_canvas_; | |
149 uint32 map_count_; | |
150 | |
151 DISALLOW_COPY_AND_ASSIGN(ImageDataSimpleBackend); | |
152 }; | |
153 | |
154 // Manages mapping an image resource if necessary. Use this to ensure the | |
155 // image is mapped. The destructor will put the image back into the previous | |
156 // state. You must check is_valid() to make sure the image was successfully | |
157 // mapped before using it. | |
158 // | |
159 // Example: | |
160 // ImageDataAutoMapper mapper(image_data); | |
161 // if (!mapper.is_valid()) | |
162 // return utter_failure; | |
163 // image_data->mapped_canvas()->blah(); // Guaranteed valid. | |
164 class ImageDataAutoMapper { | |
165 public: | |
166 explicit ImageDataAutoMapper(PPB_ImageData_Impl* image_data) | |
167 : image_data_(image_data) { | |
168 if (image_data_->IsMapped()) { | |
169 is_valid_ = true; | |
170 needs_unmap_ = false; | |
171 } else { | |
172 is_valid_ = needs_unmap_ = !!image_data_->Map(); | |
173 } | |
174 } | |
175 | |
176 ~ImageDataAutoMapper() { | |
177 if (needs_unmap_) | |
178 image_data_->Unmap(); | |
179 } | |
180 | |
181 // Check this to see if the image was successfully mapped. If this is false, | |
182 // the image could not be mapped and is unusable. | |
183 bool is_valid() const { return is_valid_; } | |
184 | |
185 private: | |
186 PPB_ImageData_Impl* image_data_; | |
187 bool is_valid_; | |
188 bool needs_unmap_; | |
189 | |
190 DISALLOW_COPY_AND_ASSIGN(ImageDataAutoMapper); | |
191 }; | |
192 | |
193 } // namespace ppapi | |
194 } // namespace webkit | |
195 | |
196 #endif // WEBKIT_PLUGINS_PPAPI_PPB_IMAGE_DATA_IMPL_H_ | |
OLD | NEW |