OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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. | |
4 | |
5 #ifndef WEBKIT_GLUE_PLUGINS_PEPPER_IMAGE_DATA_H_ | |
6 #define WEBKIT_GLUE_PLUGINS_PEPPER_IMAGE_DATA_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/scoped_ptr.h" | |
10 #include "ppapi/c/ppb_image_data.h" | |
11 #include "webkit/glue/plugins/pepper_plugin_delegate.h" | |
12 #include "webkit/glue/plugins/pepper_resource.h" | |
13 | |
14 namespace skia { | |
15 class PlatformCanvas; | |
16 } | |
17 | |
18 struct PPB_ImageDataTrusted; | |
19 class SkBitmap; | |
20 | |
21 namespace pepper { | |
22 | |
23 class ImageData : public Resource { | |
24 public: | |
25 explicit ImageData(PluginModule* module); | |
26 virtual ~ImageData(); | |
27 | |
28 int width() const { return width_; } | |
29 int height() const { return height_; } | |
30 | |
31 // Returns the image format. | |
32 PP_ImageDataFormat format() const { return format_; } | |
33 | |
34 // Returns true if this image is mapped. False means that the image is either | |
35 // invalid or not mapped. See ImageDataAutoMapper below. | |
36 bool is_mapped() const { return !!mapped_canvas_.get(); } | |
37 | |
38 PluginDelegate::PlatformImage2D* platform_image() const { | |
39 return platform_image_.get(); | |
40 } | |
41 | |
42 // Returns a pointer to the interface implementing PPB_ImageData that is | |
43 // exposed to the plugin. | |
44 static const PPB_ImageData* GetInterface(); | |
45 static const PPB_ImageDataTrusted* GetTrustedInterface(); | |
46 | |
47 // Returns the image data format used by the browser. If the plugin uses the | |
48 // same format, there is no conversion. Otherwise the browser will be in | |
49 // charge of converting from a supported format to its native format. | |
50 static PP_ImageDataFormat GetNativeImageDataFormat(); | |
51 | |
52 // Returns true if the format is supported by the browser. | |
53 static bool IsImageDataFormatSupported(PP_ImageDataFormat format); | |
54 | |
55 // Resource overrides. | |
56 virtual ImageData* AsImageData(); | |
57 | |
58 // PPB_ImageData implementation. | |
59 bool Init(PP_ImageDataFormat format, | |
60 int width, int height, | |
61 bool init_to_zero); | |
62 void Describe(PP_ImageDataDesc* desc) const; | |
63 void* Map(); | |
64 void Unmap(); | |
65 | |
66 // PPB_ImageDataTrusted implementation. | |
67 int GetSharedMemoryHandle(uint32* byte_count) const; | |
68 | |
69 // The mapped bitmap and canvas will be NULL if the image is not mapped. | |
70 skia::PlatformCanvas* mapped_canvas() const { return mapped_canvas_.get(); } | |
71 const SkBitmap* GetMappedBitmap() const; | |
72 | |
73 // Swaps the guts of this image data with another. | |
74 void Swap(ImageData* other); | |
75 | |
76 private: | |
77 // This will be NULL before initialization, and if this ImageData is | |
78 // swapped with another. | |
79 scoped_ptr<PluginDelegate::PlatformImage2D> platform_image_; | |
80 | |
81 // When the device is mapped, this is the image. Null when umapped. | |
82 scoped_ptr<skia::PlatformCanvas> mapped_canvas_; | |
83 | |
84 PP_ImageDataFormat format_; | |
85 int width_; | |
86 int height_; | |
87 | |
88 DISALLOW_COPY_AND_ASSIGN(ImageData); | |
89 }; | |
90 | |
91 // Manages mapping an image resource if necessary. Use this to ensure the | |
92 // image is mapped. The destructor will put the image back into the previous | |
93 // state. You must check is_valid() to make sure the image was successfully | |
94 // mapped before using it. | |
95 // | |
96 // Example: | |
97 // ImageDataAutoMapper mapper(image_data); | |
98 // if (!mapper.is_valid()) | |
99 // return utter_failure; | |
100 // image_data->mapped_canvas()->blah(); // Guaranteed valid. | |
101 class ImageDataAutoMapper { | |
102 public: | |
103 ImageDataAutoMapper(ImageData* image_data) : image_data_(image_data) { | |
104 if (image_data_->is_mapped()) { | |
105 is_valid_ = true; | |
106 needs_unmap_ = false; | |
107 } else { | |
108 is_valid_ = needs_unmap_ = !!image_data_->Map(); | |
109 } | |
110 } | |
111 | |
112 ~ImageDataAutoMapper() { | |
113 if (needs_unmap_) | |
114 image_data_->Unmap(); | |
115 } | |
116 | |
117 // Check this to see if the image was successfully mapped. If this is false, | |
118 // the image could not be mapped and is unusable. | |
119 bool is_valid() const { return is_valid_; } | |
120 | |
121 private: | |
122 ImageData* image_data_; | |
123 bool is_valid_; | |
124 bool needs_unmap_; | |
125 | |
126 DISALLOW_COPY_AND_ASSIGN(ImageDataAutoMapper); | |
127 }; | |
128 | |
129 } // namespace pepper | |
130 | |
131 #endif // WEBKIT_GLUE_PLUGINS_PEPPER_IMAGE_DATA_H_ | |
OLD | NEW |