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