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