Add ICU message format support
[chromium-blink-merge.git] / content / renderer / pepper / ppb_image_data_impl.h
blobb7a4540150cd5cbb0df42d6b5da883b7ca584562
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.
5 #ifndef CONTENT_RENDERER_PEPPER_PPB_IMAGE_DATA_IMPL_H_
6 #define CONTENT_RENDERER_PEPPER_PPB_IMAGE_DATA_IMPL_H_
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/shared_memory.h"
11 #include "content/common/content_export.h"
12 #include "ppapi/c/ppb_image_data.h"
13 #include "ppapi/shared_impl/ppb_image_data_shared.h"
14 #include "ppapi/shared_impl/resource.h"
15 #include "ppapi/thunk/ppb_image_data_api.h"
16 #include "third_party/skia/include/core/SkCanvas.h"
18 class SkBitmap;
19 class SkCanvas;
20 class TransportDIB;
22 namespace base {
23 class SharedMemory;
26 namespace content {
28 class CONTENT_EXPORT PPB_ImageData_Impl
29 : public ppapi::Resource,
30 public ppapi::PPB_ImageData_Shared,
31 public NON_EXPORTED_BASE(ppapi::thunk::PPB_ImageData_API) {
32 public:
33 // We delegate most of our implementation to a back-end class that either uses
34 // a PlatformCanvas (for most trusted stuff) or bare shared memory (for use by
35 // NaCl, or trusted plugins when the PlatformCanvas isn't needed). This makes
36 // it cheap & easy to implement Swap.
37 class Backend {
38 public:
39 virtual ~Backend() {};
40 virtual bool Init(PPB_ImageData_Impl* impl,
41 PP_ImageDataFormat format,
42 int width,
43 int height,
44 bool init_to_zero) = 0;
45 virtual bool IsMapped() const = 0;
46 virtual TransportDIB* GetTransportDIB() const = 0;
47 virtual void* Map() = 0;
48 virtual void Unmap() = 0;
49 virtual int32_t GetSharedMemory(base::SharedMemory** shm,
50 uint32_t* byte_count) = 0;
51 virtual SkCanvas* GetPlatformCanvas() = 0;
52 virtual SkCanvas* GetCanvas() = 0;
53 virtual const SkBitmap* GetMappedBitmap() const = 0;
56 // If you call this constructor, you must also call Init before use. Normally
57 // you should use the static Create function, but this constructor is needed
58 // for some internal uses of ImageData (like Graphics2D).
59 PPB_ImageData_Impl(PP_Instance instance,
60 PPB_ImageData_Shared::ImageDataType type);
62 // Constructor used for unittests. The ImageData is always allocated locally.
63 struct ForTest {};
64 PPB_ImageData_Impl(PP_Instance instance, ForTest);
66 bool Init(PP_ImageDataFormat format,
67 int width,
68 int height,
69 bool init_to_zero);
71 static PP_Resource Create(PP_Instance pp_instance,
72 PPB_ImageData_Shared::ImageDataType type,
73 PP_ImageDataFormat format,
74 const PP_Size& size,
75 PP_Bool init_to_zero);
77 int width() const { return width_; }
78 int height() const { return height_; }
80 // Returns the image format.
81 PP_ImageDataFormat format() const { return format_; }
83 // Returns true if this image is mapped. False means that the image is either
84 // invalid or not mapped. See ImageDataAutoMapper below.
85 bool IsMapped() const;
86 TransportDIB* GetTransportDIB() const;
88 // Resource override.
89 ppapi::thunk::PPB_ImageData_API* AsPPB_ImageData_API() override;
91 // PPB_ImageData_API implementation.
92 PP_Bool Describe(PP_ImageDataDesc* desc) override;
93 void* Map() override;
94 void Unmap() override;
95 int32_t GetSharedMemory(base::SharedMemory** shm,
96 uint32_t* byte_count) override;
97 SkCanvas* GetPlatformCanvas() override;
98 SkCanvas* GetCanvas() override;
99 void SetIsCandidateForReuse() override;
101 const SkBitmap* GetMappedBitmap() const;
103 private:
104 ~PPB_ImageData_Impl() override;
106 PP_ImageDataFormat format_;
107 int width_;
108 int height_;
109 scoped_ptr<Backend> backend_;
111 DISALLOW_COPY_AND_ASSIGN(PPB_ImageData_Impl);
114 class ImageDataPlatformBackend : public PPB_ImageData_Impl::Backend {
115 public:
116 // |is_browser_allocated| indicates whether the backing shared memory should
117 // be allocated by the browser process.
118 ImageDataPlatformBackend();
119 ~ImageDataPlatformBackend() override;
121 // PPB_ImageData_Impl::Backend implementation.
122 bool Init(PPB_ImageData_Impl* impl,
123 PP_ImageDataFormat format,
124 int width,
125 int height,
126 bool init_to_zero) override;
127 bool IsMapped() const override;
128 TransportDIB* GetTransportDIB() const override;
129 void* Map() override;
130 void Unmap() override;
131 int32_t GetSharedMemory(base::SharedMemory** shm,
132 uint32_t* byte_count) override;
133 SkCanvas* GetPlatformCanvas() override;
134 SkCanvas* GetCanvas() override;
135 const SkBitmap* GetMappedBitmap() const override;
137 private:
138 // This will be NULL before initialization, and if this PPB_ImageData_Impl is
139 // swapped with another.
140 int width_;
141 int height_;
142 scoped_ptr<TransportDIB> dib_;
144 // When the device is mapped, this is the image. Null when umapped.
145 scoped_ptr<SkCanvas> mapped_canvas_;
147 DISALLOW_COPY_AND_ASSIGN(ImageDataPlatformBackend);
150 class ImageDataSimpleBackend : public PPB_ImageData_Impl::Backend {
151 public:
152 ImageDataSimpleBackend();
153 ~ImageDataSimpleBackend() override;
155 // PPB_ImageData_Impl::Backend implementation.
156 bool Init(PPB_ImageData_Impl* impl,
157 PP_ImageDataFormat format,
158 int width,
159 int height,
160 bool init_to_zero) override;
161 bool IsMapped() const override;
162 TransportDIB* GetTransportDIB() const override;
163 void* Map() override;
164 void Unmap() override;
165 int32_t GetSharedMemory(base::SharedMemory** shm,
166 uint32_t* byte_count) override;
167 SkCanvas* GetPlatformCanvas() override;
168 SkCanvas* GetCanvas() override;
169 const SkBitmap* GetMappedBitmap() const override;
171 private:
172 scoped_ptr<base::SharedMemory> shared_memory_;
173 // skia_bitmap_ is backed by shared_memory_.
174 SkBitmap skia_bitmap_;
175 scoped_ptr<SkCanvas> skia_canvas_;
176 uint32 map_count_;
178 DISALLOW_COPY_AND_ASSIGN(ImageDataSimpleBackend);
181 // Manages mapping an image resource if necessary. Use this to ensure the
182 // image is mapped. The destructor will put the image back into the previous
183 // state. You must check is_valid() to make sure the image was successfully
184 // mapped before using it.
186 // Example:
187 // ImageDataAutoMapper mapper(image_data);
188 // if (!mapper.is_valid())
189 // return utter_failure;
190 // image_data->mapped_canvas()->blah(); // Guaranteed valid.
191 class ImageDataAutoMapper {
192 public:
193 explicit ImageDataAutoMapper(PPB_ImageData_Impl* image_data)
194 : image_data_(image_data) {
195 if (image_data_->IsMapped()) {
196 is_valid_ = true;
197 needs_unmap_ = false;
198 } else {
199 is_valid_ = needs_unmap_ = !!image_data_->Map();
203 ~ImageDataAutoMapper() {
204 if (needs_unmap_)
205 image_data_->Unmap();
208 // Check this to see if the image was successfully mapped. If this is false,
209 // the image could not be mapped and is unusable.
210 bool is_valid() const { return is_valid_; }
212 private:
213 PPB_ImageData_Impl* image_data_;
214 bool is_valid_;
215 bool needs_unmap_;
217 DISALLOW_COPY_AND_ASSIGN(ImageDataAutoMapper);
220 } // namespace content
222 #endif // CONTENT_RENDERER_PEPPER_PPB_IMAGE_DATA_IMPL_H_