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