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"
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
) {
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.
39 virtual ~Backend() {};
40 virtual bool Init(PPB_ImageData_Impl
* impl
,
41 PP_ImageDataFormat format
,
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.
64 PPB_ImageData_Impl(PP_Instance instance
, ForTest
);
66 bool Init(PP_ImageDataFormat format
,
71 static PP_Resource
Create(PP_Instance pp_instance
,
72 PPB_ImageData_Shared::ImageDataType type
,
73 PP_ImageDataFormat format
,
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;
89 ppapi::thunk::PPB_ImageData_API
* AsPPB_ImageData_API() override
;
91 // PPB_ImageData_API implementation.
92 PP_Bool
Describe(PP_ImageDataDesc
* desc
) 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;
104 ~PPB_ImageData_Impl() override
;
106 PP_ImageDataFormat format_
;
109 scoped_ptr
<Backend
> backend_
;
111 DISALLOW_COPY_AND_ASSIGN(PPB_ImageData_Impl
);
114 class ImageDataPlatformBackend
: public PPB_ImageData_Impl::Backend
{
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
,
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
;
138 // This will be NULL before initialization, and if this PPB_ImageData_Impl is
139 // swapped with another.
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
{
152 ImageDataSimpleBackend();
153 ~ImageDataSimpleBackend() override
;
155 // PPB_ImageData_Impl::Backend implementation.
156 bool Init(PPB_ImageData_Impl
* impl
,
157 PP_ImageDataFormat format
,
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
;
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_
;
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.
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
{
193 explicit ImageDataAutoMapper(PPB_ImageData_Impl
* image_data
)
194 : image_data_(image_data
) {
195 if (image_data_
->IsMapped()) {
197 needs_unmap_
= false;
199 is_valid_
= needs_unmap_
= !!image_data_
->Map();
203 ~ImageDataAutoMapper() {
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_
; }
213 PPB_ImageData_Impl
* image_data_
;
217 DISALLOW_COPY_AND_ASSIGN(ImageDataAutoMapper
);
220 } // namespace content
222 #endif // CONTENT_RENDERER_PEPPER_PPB_IMAGE_DATA_IMPL_H_