Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / pepper / ppb_image_data_impl.h
blobd31e7b1d12cd08058fd97936341c331d8d48ae4f
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 "content/common/content_export.h"
11 #include "ppapi/c/ppb_image_data.h"
12 #include "ppapi/shared_impl/ppb_image_data_shared.h"
13 #include "ppapi/shared_impl/resource.h"
14 #include "ppapi/thunk/ppb_image_data_api.h"
15 #include "third_party/skia/include/core/SkCanvas.h"
17 class SkBitmap;
18 class SkCanvas;
19 class TransportDIB;
21 namespace base {
22 class SharedMemory;
25 namespace content {
27 class CONTENT_EXPORT PPB_ImageData_Impl
28 : public ppapi::Resource,
29 public ppapi::PPB_ImageData_Shared,
30 public NON_EXPORTED_BASE(ppapi::thunk::PPB_ImageData_API) {
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, or trusted plugins when the PlatformCanvas isn't needed). This makes
35 // it cheap & easy to implement Swap.
36 class Backend {
37 public:
38 virtual ~Backend() {};
39 virtual bool Init(PPB_ImageData_Impl* impl,
40 PP_ImageDataFormat format,
41 int width,
42 int height,
43 bool init_to_zero) = 0;
44 virtual bool IsMapped() const = 0;
45 virtual TransportDIB* GetTransportDIB() const = 0;
46 virtual void* Map() = 0;
47 virtual void Unmap() = 0;
48 virtual int32_t GetSharedMemory(int* handle, uint32_t* byte_count) = 0;
49 virtual SkCanvas* GetPlatformCanvas() = 0;
50 virtual SkCanvas* GetCanvas() = 0;
51 virtual const SkBitmap* GetMappedBitmap() const = 0;
54 // If you call this constructor, you must also call Init before use. Normally
55 // you should use the static Create function, but this constructor is needed
56 // for some internal uses of ImageData (like Graphics2D).
57 PPB_ImageData_Impl(PP_Instance instance,
58 PPB_ImageData_Shared::ImageDataType type);
60 // Constructor used for unittests. The ImageData is always allocated locally.
61 struct ForTest {};
62 PPB_ImageData_Impl(PP_Instance instance, ForTest);
64 bool Init(PP_ImageDataFormat format,
65 int width,
66 int height,
67 bool init_to_zero);
69 static PP_Resource Create(PP_Instance pp_instance,
70 PPB_ImageData_Shared::ImageDataType type,
71 PP_ImageDataFormat format,
72 const PP_Size& size,
73 PP_Bool init_to_zero);
75 int width() const { return width_; }
76 int height() const { return height_; }
78 // Returns the image format.
79 PP_ImageDataFormat format() const { return format_; }
81 // Returns true if this image is mapped. False means that the image is either
82 // invalid or not mapped. See ImageDataAutoMapper below.
83 bool IsMapped() const;
84 TransportDIB* GetTransportDIB() const;
86 // Resource override.
87 ppapi::thunk::PPB_ImageData_API* AsPPB_ImageData_API() override;
89 // PPB_ImageData_API implementation.
90 PP_Bool Describe(PP_ImageDataDesc* desc) override;
91 void* Map() override;
92 void Unmap() override;
93 int32_t GetSharedMemory(int* handle, uint32_t* byte_count) override;
94 SkCanvas* GetPlatformCanvas() override;
95 SkCanvas* GetCanvas() override;
96 void SetIsCandidateForReuse() override;
98 const SkBitmap* GetMappedBitmap() const;
100 private:
101 ~PPB_ImageData_Impl() override;
103 PP_ImageDataFormat format_;
104 int width_;
105 int height_;
106 scoped_ptr<Backend> backend_;
108 DISALLOW_COPY_AND_ASSIGN(PPB_ImageData_Impl);
111 class ImageDataPlatformBackend : public PPB_ImageData_Impl::Backend {
112 public:
113 // |is_browser_allocated| indicates whether the backing shared memory should
114 // be allocated by the browser process.
115 ImageDataPlatformBackend();
116 ~ImageDataPlatformBackend() override;
118 // PPB_ImageData_Impl::Backend implementation.
119 bool Init(PPB_ImageData_Impl* impl,
120 PP_ImageDataFormat format,
121 int width,
122 int height,
123 bool init_to_zero) override;
124 bool IsMapped() const override;
125 TransportDIB* GetTransportDIB() const override;
126 void* Map() override;
127 void Unmap() override;
128 int32_t GetSharedMemory(int* handle, uint32_t* byte_count) override;
129 SkCanvas* GetPlatformCanvas() override;
130 SkCanvas* GetCanvas() override;
131 const SkBitmap* GetMappedBitmap() const override;
133 private:
134 // This will be NULL before initialization, and if this PPB_ImageData_Impl is
135 // swapped with another.
136 int width_;
137 int height_;
138 scoped_ptr<TransportDIB> dib_;
140 // When the device is mapped, this is the image. Null when umapped.
141 scoped_ptr<SkCanvas> mapped_canvas_;
143 DISALLOW_COPY_AND_ASSIGN(ImageDataPlatformBackend);
146 class ImageDataSimpleBackend : public PPB_ImageData_Impl::Backend {
147 public:
148 ImageDataSimpleBackend();
149 ~ImageDataSimpleBackend() override;
151 // PPB_ImageData_Impl::Backend implementation.
152 bool Init(PPB_ImageData_Impl* impl,
153 PP_ImageDataFormat format,
154 int width,
155 int height,
156 bool init_to_zero) override;
157 bool IsMapped() const override;
158 TransportDIB* GetTransportDIB() const override;
159 void* Map() override;
160 void Unmap() override;
161 int32_t GetSharedMemory(int* handle, uint32_t* byte_count) override;
162 SkCanvas* GetPlatformCanvas() override;
163 SkCanvas* GetCanvas() override;
164 const SkBitmap* GetMappedBitmap() const override;
166 private:
167 scoped_ptr<base::SharedMemory> shared_memory_;
168 // skia_bitmap_ is backed by shared_memory_.
169 SkBitmap skia_bitmap_;
170 scoped_ptr<SkCanvas> skia_canvas_;
171 uint32 map_count_;
173 DISALLOW_COPY_AND_ASSIGN(ImageDataSimpleBackend);
176 // Manages mapping an image resource if necessary. Use this to ensure the
177 // image is mapped. The destructor will put the image back into the previous
178 // state. You must check is_valid() to make sure the image was successfully
179 // mapped before using it.
181 // Example:
182 // ImageDataAutoMapper mapper(image_data);
183 // if (!mapper.is_valid())
184 // return utter_failure;
185 // image_data->mapped_canvas()->blah(); // Guaranteed valid.
186 class ImageDataAutoMapper {
187 public:
188 explicit ImageDataAutoMapper(PPB_ImageData_Impl* image_data)
189 : image_data_(image_data) {
190 if (image_data_->IsMapped()) {
191 is_valid_ = true;
192 needs_unmap_ = false;
193 } else {
194 is_valid_ = needs_unmap_ = !!image_data_->Map();
198 ~ImageDataAutoMapper() {
199 if (needs_unmap_)
200 image_data_->Unmap();
203 // Check this to see if the image was successfully mapped. If this is false,
204 // the image could not be mapped and is unusable.
205 bool is_valid() const { return is_valid_; }
207 private:
208 PPB_ImageData_Impl* image_data_;
209 bool is_valid_;
210 bool needs_unmap_;
212 DISALLOW_COPY_AND_ASSIGN(ImageDataAutoMapper);
215 } // namespace content
217 #endif // CONTENT_RENDERER_PEPPER_PPB_IMAGE_DATA_IMPL_H_