| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 CONTENT_RENDERER_PEPPER_PPB_IMAGE_DATA_IMPL_H_ | 5 #ifndef CONTENT_RENDERER_PEPPER_PPB_IMAGE_DATA_IMPL_H_ |
| 6 #define CONTENT_RENDERER_PEPPER_PPB_IMAGE_DATA_IMPL_H_ | 6 #define CONTENT_RENDERER_PEPPER_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 "content/common/content_export.h" | 10 #include "content/common/content_export.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 public ppapi::PPB_ImageData_Shared, | 29 public ppapi::PPB_ImageData_Shared, |
| 30 public NON_EXPORTED_BASE(ppapi::thunk::PPB_ImageData_API) { | 30 public NON_EXPORTED_BASE(ppapi::thunk::PPB_ImageData_API) { |
| 31 public: | 31 public: |
| 32 // We delegate most of our implementation to a back-end class that either uses | 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 | 33 // a PlatformCanvas (for most trusted stuff) or bare shared memory (for use by |
| 34 // NaCl, or trusted plugins when the PlatformCanvas isn't needed). This makes | 34 // NaCl, or trusted plugins when the PlatformCanvas isn't needed). This makes |
| 35 // it cheap & easy to implement Swap. | 35 // it cheap & easy to implement Swap. |
| 36 class Backend { | 36 class Backend { |
| 37 public: | 37 public: |
| 38 virtual ~Backend() {}; | 38 virtual ~Backend() {}; |
| 39 virtual bool Init(PPB_ImageData_Impl* impl, PP_ImageDataFormat format, | 39 virtual bool Init(PPB_ImageData_Impl* impl, |
| 40 int width, int height, bool init_to_zero) = 0; | 40 PP_ImageDataFormat format, |
| 41 int width, |
| 42 int height, |
| 43 bool init_to_zero) = 0; |
| 41 virtual bool IsMapped() const = 0; | 44 virtual bool IsMapped() const = 0; |
| 42 virtual TransportDIB* GetTransportDIB() const = 0; | 45 virtual TransportDIB* GetTransportDIB() const = 0; |
| 43 virtual void* Map() = 0; | 46 virtual void* Map() = 0; |
| 44 virtual void Unmap() = 0; | 47 virtual void Unmap() = 0; |
| 45 virtual int32_t GetSharedMemory(int* handle, uint32_t* byte_count) = 0; | 48 virtual int32_t GetSharedMemory(int* handle, uint32_t* byte_count) = 0; |
| 46 virtual SkCanvas* GetPlatformCanvas() = 0; | 49 virtual SkCanvas* GetPlatformCanvas() = 0; |
| 47 virtual SkCanvas* GetCanvas() = 0; | 50 virtual SkCanvas* GetCanvas() = 0; |
| 48 virtual const SkBitmap* GetMappedBitmap() const = 0; | 51 virtual const SkBitmap* GetMappedBitmap() const = 0; |
| 49 }; | 52 }; |
| 50 | 53 |
| 51 // If you call this constructor, you must also call Init before use. Normally | 54 // If you call this constructor, you must also call Init before use. Normally |
| 52 // you should use the static Create function, but this constructor is needed | 55 // you should use the static Create function, but this constructor is needed |
| 53 // for some internal uses of ImageData (like Graphics2D). | 56 // for some internal uses of ImageData (like Graphics2D). |
| 54 PPB_ImageData_Impl(PP_Instance instance, | 57 PPB_ImageData_Impl(PP_Instance instance, |
| 55 PPB_ImageData_Shared::ImageDataType type); | 58 PPB_ImageData_Shared::ImageDataType type); |
| 56 | 59 |
| 57 // Constructor used for unittests. The ImageData is always allocated locally. | 60 // Constructor used for unittests. The ImageData is always allocated locally. |
| 58 struct ForTest {}; | 61 struct ForTest {}; |
| 59 PPB_ImageData_Impl(PP_Instance instance, | 62 PPB_ImageData_Impl(PP_Instance instance, ForTest); |
| 60 ForTest); | |
| 61 | 63 |
| 62 bool Init(PP_ImageDataFormat format, | 64 bool Init(PP_ImageDataFormat format, |
| 63 int width, int height, | 65 int width, |
| 66 int height, |
| 64 bool init_to_zero); | 67 bool init_to_zero); |
| 65 | 68 |
| 66 static PP_Resource Create(PP_Instance pp_instance, | 69 static PP_Resource Create(PP_Instance pp_instance, |
| 67 PPB_ImageData_Shared::ImageDataType type, | 70 PPB_ImageData_Shared::ImageDataType type, |
| 68 PP_ImageDataFormat format, | 71 PP_ImageDataFormat format, |
| 69 const PP_Size& size, | 72 const PP_Size& size, |
| 70 PP_Bool init_to_zero); | 73 PP_Bool init_to_zero); |
| 71 | 74 |
| 72 int width() const { return width_; } | 75 int width() const { return width_; } |
| 73 int height() const { return height_; } | 76 int height() const { return height_; } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 }; | 109 }; |
| 107 | 110 |
| 108 class ImageDataPlatformBackend : public PPB_ImageData_Impl::Backend { | 111 class ImageDataPlatformBackend : public PPB_ImageData_Impl::Backend { |
| 109 public: | 112 public: |
| 110 // |is_browser_allocated| indicates whether the backing shared memory should | 113 // |is_browser_allocated| indicates whether the backing shared memory should |
| 111 // be allocated by the browser process. | 114 // be allocated by the browser process. |
| 112 ImageDataPlatformBackend(bool is_browser_allocated); | 115 ImageDataPlatformBackend(bool is_browser_allocated); |
| 113 virtual ~ImageDataPlatformBackend(); | 116 virtual ~ImageDataPlatformBackend(); |
| 114 | 117 |
| 115 // PPB_ImageData_Impl::Backend implementation. | 118 // PPB_ImageData_Impl::Backend implementation. |
| 116 virtual bool Init(PPB_ImageData_Impl* impl, PP_ImageDataFormat format, | 119 virtual bool Init(PPB_ImageData_Impl* impl, |
| 117 int width, int height, bool init_to_zero) OVERRIDE; | 120 PP_ImageDataFormat format, |
| 121 int width, |
| 122 int height, |
| 123 bool init_to_zero) OVERRIDE; |
| 118 virtual bool IsMapped() const OVERRIDE; | 124 virtual bool IsMapped() const OVERRIDE; |
| 119 virtual TransportDIB* GetTransportDIB() const OVERRIDE; | 125 virtual TransportDIB* GetTransportDIB() const OVERRIDE; |
| 120 virtual void* Map() OVERRIDE; | 126 virtual void* Map() OVERRIDE; |
| 121 virtual void Unmap() OVERRIDE; | 127 virtual void Unmap() OVERRIDE; |
| 122 virtual int32_t GetSharedMemory(int* handle, uint32_t* byte_count) OVERRIDE; | 128 virtual int32_t GetSharedMemory(int* handle, uint32_t* byte_count) OVERRIDE; |
| 123 virtual SkCanvas* GetPlatformCanvas() OVERRIDE; | 129 virtual SkCanvas* GetPlatformCanvas() OVERRIDE; |
| 124 virtual SkCanvas* GetCanvas() OVERRIDE; | 130 virtual SkCanvas* GetCanvas() OVERRIDE; |
| 125 virtual const SkBitmap* GetMappedBitmap() const OVERRIDE; | 131 virtual const SkBitmap* GetMappedBitmap() const OVERRIDE; |
| 126 | 132 |
| 127 private: | 133 private: |
| (...skipping 10 matching lines...) Expand all Loading... |
| 138 | 144 |
| 139 DISALLOW_COPY_AND_ASSIGN(ImageDataPlatformBackend); | 145 DISALLOW_COPY_AND_ASSIGN(ImageDataPlatformBackend); |
| 140 }; | 146 }; |
| 141 | 147 |
| 142 class ImageDataSimpleBackend : public PPB_ImageData_Impl::Backend { | 148 class ImageDataSimpleBackend : public PPB_ImageData_Impl::Backend { |
| 143 public: | 149 public: |
| 144 ImageDataSimpleBackend(); | 150 ImageDataSimpleBackend(); |
| 145 virtual ~ImageDataSimpleBackend(); | 151 virtual ~ImageDataSimpleBackend(); |
| 146 | 152 |
| 147 // PPB_ImageData_Impl::Backend implementation. | 153 // PPB_ImageData_Impl::Backend implementation. |
| 148 virtual bool Init(PPB_ImageData_Impl* impl, PP_ImageDataFormat format, | 154 virtual bool Init(PPB_ImageData_Impl* impl, |
| 149 int width, int height, bool init_to_zero) OVERRIDE; | 155 PP_ImageDataFormat format, |
| 156 int width, |
| 157 int height, |
| 158 bool init_to_zero) OVERRIDE; |
| 150 virtual bool IsMapped() const OVERRIDE; | 159 virtual bool IsMapped() const OVERRIDE; |
| 151 virtual TransportDIB* GetTransportDIB() const OVERRIDE; | 160 virtual TransportDIB* GetTransportDIB() const OVERRIDE; |
| 152 virtual void* Map() OVERRIDE; | 161 virtual void* Map() OVERRIDE; |
| 153 virtual void Unmap() OVERRIDE; | 162 virtual void Unmap() OVERRIDE; |
| 154 virtual int32_t GetSharedMemory(int* handle, uint32_t* byte_count) OVERRIDE; | 163 virtual int32_t GetSharedMemory(int* handle, uint32_t* byte_count) OVERRIDE; |
| 155 virtual SkCanvas* GetPlatformCanvas() OVERRIDE; | 164 virtual SkCanvas* GetPlatformCanvas() OVERRIDE; |
| 156 virtual SkCanvas* GetCanvas() OVERRIDE; | 165 virtual SkCanvas* GetCanvas() OVERRIDE; |
| 157 virtual const SkBitmap* GetMappedBitmap() const OVERRIDE; | 166 virtual const SkBitmap* GetMappedBitmap() const OVERRIDE; |
| 158 | 167 |
| 159 private: | 168 private: |
| (...skipping 12 matching lines...) Expand all Loading... |
| 172 // mapped before using it. | 181 // mapped before using it. |
| 173 // | 182 // |
| 174 // Example: | 183 // Example: |
| 175 // ImageDataAutoMapper mapper(image_data); | 184 // ImageDataAutoMapper mapper(image_data); |
| 176 // if (!mapper.is_valid()) | 185 // if (!mapper.is_valid()) |
| 177 // return utter_failure; | 186 // return utter_failure; |
| 178 // image_data->mapped_canvas()->blah(); // Guaranteed valid. | 187 // image_data->mapped_canvas()->blah(); // Guaranteed valid. |
| 179 class ImageDataAutoMapper { | 188 class ImageDataAutoMapper { |
| 180 public: | 189 public: |
| 181 explicit ImageDataAutoMapper(PPB_ImageData_Impl* image_data) | 190 explicit ImageDataAutoMapper(PPB_ImageData_Impl* image_data) |
| 182 : image_data_(image_data) { | 191 : image_data_(image_data) { |
| 183 if (image_data_->IsMapped()) { | 192 if (image_data_->IsMapped()) { |
| 184 is_valid_ = true; | 193 is_valid_ = true; |
| 185 needs_unmap_ = false; | 194 needs_unmap_ = false; |
| 186 } else { | 195 } else { |
| 187 is_valid_ = needs_unmap_ = !!image_data_->Map(); | 196 is_valid_ = needs_unmap_ = !!image_data_->Map(); |
| 188 } | 197 } |
| 189 } | 198 } |
| 190 | 199 |
| 191 ~ImageDataAutoMapper() { | 200 ~ImageDataAutoMapper() { |
| 192 if (needs_unmap_) | 201 if (needs_unmap_) |
| 193 image_data_->Unmap(); | 202 image_data_->Unmap(); |
| 194 } | 203 } |
| 195 | 204 |
| 196 // Check this to see if the image was successfully mapped. If this is false, | 205 // Check this to see if the image was successfully mapped. If this is false, |
| 197 // the image could not be mapped and is unusable. | 206 // the image could not be mapped and is unusable. |
| 198 bool is_valid() const { return is_valid_; } | 207 bool is_valid() const { return is_valid_; } |
| 199 | 208 |
| 200 private: | 209 private: |
| 201 PPB_ImageData_Impl* image_data_; | 210 PPB_ImageData_Impl* image_data_; |
| 202 bool is_valid_; | 211 bool is_valid_; |
| 203 bool needs_unmap_; | 212 bool needs_unmap_; |
| 204 | 213 |
| 205 DISALLOW_COPY_AND_ASSIGN(ImageDataAutoMapper); | 214 DISALLOW_COPY_AND_ASSIGN(ImageDataAutoMapper); |
| 206 }; | 215 }; |
| 207 | 216 |
| 208 } // namespace content | 217 } // namespace content |
| 209 | 218 |
| 210 #endif // CONTENT_RENDERER_PEPPER_PPB_IMAGE_DATA_IMPL_H_ | 219 #endif // CONTENT_RENDERER_PEPPER_PPB_IMAGE_DATA_IMPL_H_ |
| OLD | NEW |