| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 UI_GFX_IMAGE_IMAGE_SKIA_H_ | 5 #ifndef UI_GFX_IMAGE_IMAGE_SKIA_H_ |
| 6 #define UI_GFX_IMAGE_IMAGE_SKIA_H_ | 6 #define UI_GFX_IMAGE_IMAGE_SKIA_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/gtest_prod_util.h" |
| 11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 12 #include "ui/base/ui_export.h" | 13 #include "ui/base/ui_export.h" |
| 13 #include "ui/gfx/image/image_skia_rep.h" | 14 #include "ui/gfx/image/image_skia_rep.h" |
| 14 | 15 |
| 15 namespace gfx { | 16 namespace gfx { |
| 16 class ImageSkiaSource; | 17 class ImageSkiaSource; |
| 17 class Size; | 18 class Size; |
| 18 | 19 |
| 19 namespace internal { | 20 namespace internal { |
| 20 class ImageSkiaStorage; | 21 class ImageSkiaStorage; |
| 21 } // namespace internal | 22 } // namespace internal |
| 22 | 23 |
| 24 namespace test { |
| 25 class TestOnThread; |
| 26 } |
| 27 |
| 23 // Container for the same image at different densities, similar to NSImage. | 28 // Container for the same image at different densities, similar to NSImage. |
| 24 // Image height and width are in DIP (Density Indepent Pixel) coordinates. | 29 // Image height and width are in DIP (Density Indepent Pixel) coordinates. |
| 25 // | 30 // |
| 26 // ImageSkia should be used whenever possible instead of SkBitmap. | 31 // ImageSkia should be used whenever possible instead of SkBitmap. |
| 27 // Functions that mutate the image should operate on the gfx::ImageSkiaRep | 32 // Functions that mutate the image should operate on the gfx::ImageSkiaRep |
| 28 // returned from ImageSkia::GetRepresentation, not on ImageSkia. | 33 // returned from ImageSkia::GetRepresentation, not on ImageSkia. |
| 29 // | 34 // |
| 30 // ImageSkia is cheap to copy and intentionally supports copy semantics. | 35 // ImageSkia is cheap to copy and intentionally supports copy semantics. |
| 31 class UI_EXPORT ImageSkia { | 36 class UI_EXPORT ImageSkia { |
| 32 public: | 37 public: |
| 33 typedef std::vector<ImageSkiaRep> ImageSkiaReps; | 38 typedef std::vector<ImageSkiaRep> ImageSkiaReps; |
| 34 | 39 |
| 35 // Creates an instance with no bitmaps. | 40 // Creates an instance with no bitmaps. |
| 36 ImageSkia(); | 41 ImageSkia(); |
| 37 | 42 |
| 38 // Creates an instance that will use the |source| to get the image | 43 // Creates an instance that will use the |source| to get the image |
| 39 // for scale factors. |size| specifes the size of the image in DIP. | 44 // for scale factors. |size| specifes the size of the image in DIP. |
| 45 // ImageSkia owns |source|. |
| 40 ImageSkia(ImageSkiaSource* source, const gfx::Size& size); | 46 ImageSkia(ImageSkiaSource* source, const gfx::Size& size); |
| 41 | 47 |
| 42 // Adds ref to passed in bitmap. | 48 // Adds ref to passed in bitmap. |
| 43 // DIP width and height are set based on scale factor of 1x. | 49 // DIP width and height are set based on scale factor of 1x. |
| 44 // TODO(pkotwicz): This is temporary till conversion to gfx::ImageSkia is | 50 // TODO(pkotwicz): This is temporary till conversion to gfx::ImageSkia is |
| 45 // done. | 51 // done. |
| 46 ImageSkia(const SkBitmap& bitmap); | 52 ImageSkia(const SkBitmap& bitmap); |
| 47 | 53 |
| 48 ImageSkia(const gfx::ImageSkiaRep& image_rep); | 54 ImageSkia(const gfx::ImageSkiaRep& image_rep); |
| 49 | 55 |
| 50 // Copies a reference to |other|'s storage. | 56 // Copies a reference to |other|'s storage. |
| 51 ImageSkia(const ImageSkia& other); | 57 ImageSkia(const ImageSkia& other); |
| 52 | 58 |
| 53 // Copies a reference to |other|'s storage. | 59 // Copies a reference to |other|'s storage. |
| 54 ImageSkia& operator=(const ImageSkia& other); | 60 ImageSkia& operator=(const ImageSkia& other); |
| 55 | 61 |
| 56 #if defined(OS_WIN) | 62 #if defined(OS_WIN) |
| 57 // Converts to gfx::ImageSkiaRep and SkBitmap. | 63 // Converts to gfx::ImageSkiaRep and SkBitmap. |
| 58 // TODO(pkotwicz): This is temporary till conversion to gfx::ImageSkia is | 64 // TODO(pkotwicz): This is temporary till conversion to gfx::ImageSkia is |
| 59 // done. | 65 // done. |
| 60 operator SkBitmap&() const { return GetBitmap(); } | 66 operator SkBitmap&() const { return GetBitmap(); } |
| 61 #endif | 67 #endif |
| 62 | 68 |
| 63 ~ImageSkia(); | 69 ~ImageSkia(); |
| 64 | 70 |
| 71 // Returns a deep copy of this ImageSkia which has its own storage with |
| 72 // the ImageSkiaRep instances that this ImageSkia currently has. |
| 73 // This can be safely passed to and manipulated by another thread. |
| 74 // Note that this does NOT generate ImageSkiaReps from its source. |
| 75 // If you want to create a deep copy with ImageSkiaReps for supported |
| 76 // scale factors, you need to explicitly call |
| 77 // |EnsureRepsForSupportedScaleFactors()| first. |
| 78 ImageSkia DeepCopy() const; |
| 79 |
| 65 // Returns true if this object is backed by the same ImageSkiaStorage as | 80 // Returns true if this object is backed by the same ImageSkiaStorage as |
| 66 // |other|. Will also return true if both images are isNull(). | 81 // |other|. Will also return true if both images are isNull(). |
| 67 bool BackedBySameObjectAs(const gfx::ImageSkia& other) const; | 82 bool BackedBySameObjectAs(const gfx::ImageSkia& other) const; |
| 68 | 83 |
| 69 // Adds |image_rep| to the image reps contained by this object. | 84 // Adds |image_rep| to the image reps contained by this object. |
| 70 void AddRepresentation(const gfx::ImageSkiaRep& image_rep); | 85 void AddRepresentation(const gfx::ImageSkiaRep& image_rep); |
| 71 | 86 |
| 72 // Removes the image rep of |scale_factor| if present. | 87 // Removes the image rep of |scale_factor| if present. |
| 73 void RemoveRepresentation(ui::ScaleFactor scale_factor); | 88 void RemoveRepresentation(ui::ScaleFactor scale_factor); |
| 74 | 89 |
| 75 // Returns true if the object owns an image rep whose density matches | 90 // Returns true if the object owns an image rep whose density matches |
| 76 // |scale_factor| exactly. | 91 // |scale_factor| exactly. |
| 77 bool HasRepresentation(ui::ScaleFactor scale_factor) const; | 92 bool HasRepresentation(ui::ScaleFactor scale_factor) const; |
| 78 | 93 |
| 79 // Returns the image rep whose density best matches | 94 // Returns the image rep whose density best matches |
| 80 // |scale_factor|. | 95 // |scale_factor|. |
| 81 // Returns a null image rep if the object contains no image reps. | 96 // Returns a null image rep if the object contains no image reps. |
| 82 const gfx::ImageSkiaRep& GetRepresentation( | 97 const gfx::ImageSkiaRep& GetRepresentation( |
| 83 ui::ScaleFactor scale_factor) const; | 98 ui::ScaleFactor scale_factor) const; |
| 84 | 99 |
| 85 #if defined(OS_MACOSX) | 100 // Make the ImageSkia instance read-only. Note that this only prevent |
| 86 // Returns the image reps contained by this object. | 101 // modification from client code, and the storage may still be |
| 87 // If the image has a source, this method will attempt to generate | 102 // modified by the source if any (thus, it's not thread safe). This |
| 88 // representations from the source for all supported scale factors. | 103 // detaches the storage from currently accessing thread, so its safe |
| 89 // Mac only for now. | 104 // to pass it to other thread as long as it is accessed only by that |
| 90 std::vector<ImageSkiaRep> GetRepresentations() const; | 105 // thread. If this ImageSkia's storage will be accessed by multiple |
| 91 #endif // OS_MACOSX | 106 // threads, use |MakeThreadSafe()| method. |
| 107 void SetReadOnly(); |
| 108 |
| 109 // Make the image thread safe by making the storage read only and remove |
| 110 // its source if any. All ImageSkia that shares the same storage will also |
| 111 // become thread safe. Note that in order to make it 100% thread safe, |
| 112 // this must be called before it's been passed to anther thread. |
| 113 void MakeThreadSafe(); |
| 114 bool IsThreadSafe() const; |
| 92 | 115 |
| 93 // Returns true if this is a null object. | 116 // Returns true if this is a null object. |
| 94 // TODO(pkotwicz): Merge this function into empty(). | |
| 95 bool isNull() const { return storage_ == NULL; } | 117 bool isNull() const { return storage_ == NULL; } |
| 96 | 118 |
| 97 // Width and height of image in DIP coordinate system. | 119 // Width and height of image in DIP coordinate system. |
| 98 int width() const; | 120 int width() const; |
| 99 int height() const; | 121 int height() const; |
| 100 gfx::Size size() const; | 122 gfx::Size size() const; |
| 101 | 123 |
| 102 // Returns pointer to 1x bitmap contained by this object. If there is no 1x | 124 // Returns pointer to 1x bitmap contained by this object. If there is no 1x |
| 103 // bitmap, the bitmap whose scale factor is closest to 1x is returned. | 125 // bitmap, the bitmap whose scale factor is closest to 1x is returned. |
| 104 // This function should only be used in unittests and on platforms which do | 126 // This function should only be used in unittests and on platforms which do |
| 105 // not support scale factors other than 1x. | 127 // not support scale factors other than 1x. |
| 106 // TODO(pkotwicz): Return null SkBitmap when the object has no 1x bitmap. | 128 // TODO(pkotwicz): Return null SkBitmap when the object has no 1x bitmap. |
| 107 const SkBitmap* bitmap() const { return &GetBitmap(); } | 129 const SkBitmap* bitmap() const { return &GetBitmap(); } |
| 108 | 130 |
| 109 // Returns a vector with the image reps contained in this object. | 131 // Returns a vector with the image reps contained in this object. |
| 110 // There is no guarantee that this will return all images rep for | 132 // There is no guarantee that this will return all images rep for |
| 111 // supported scale factors. | 133 // supported scale factors. |
| 112 // TODO(oshima): Update all use of this API and make this to fail | |
| 113 // when source is used. | |
| 114 std::vector<gfx::ImageSkiaRep> image_reps() const; | 134 std::vector<gfx::ImageSkiaRep> image_reps() const; |
| 115 | 135 |
| 136 // When the source is available, generates all ImageReps for |
| 137 // supported scale factors. This method is defined as const as |
| 138 // the state change in the storage is agnostic to the caller. |
| 139 void EnsureRepsForSupportedScaleFactors() const; |
| 140 |
| 116 private: | 141 private: |
| 142 friend class test::TestOnThread; |
| 143 FRIEND_TEST_ALL_PREFIXES(ImageSkiaTest, EmptyOnThreadTest); |
| 144 FRIEND_TEST_ALL_PREFIXES(ImageSkiaTest, StaticOnThreadTest); |
| 145 FRIEND_TEST_ALL_PREFIXES(ImageSkiaTest, SourceOnThreadTest); |
| 146 |
| 117 // Initialize ImageSkiaStorage with passed in parameters. | 147 // Initialize ImageSkiaStorage with passed in parameters. |
| 118 // If the image rep's bitmap is empty, ImageStorage is set to NULL. | 148 // If the image rep's bitmap is empty, ImageStorage is set to NULL. |
| 119 void Init(const gfx::ImageSkiaRep& image_rep); | 149 void Init(const gfx::ImageSkiaRep& image_rep); |
| 120 | 150 |
| 121 SkBitmap& GetBitmap() const; | 151 SkBitmap& GetBitmap() const; |
| 122 | 152 |
| 153 // Checks if the current thread can read/modify the ImageSkia. |
| 154 bool CanRead() const; |
| 155 bool CanModify() const; |
| 156 |
| 157 // Detach the storage from the currently assinged thread |
| 158 // so that other thread can access the storage. |
| 159 void DetachStorageFromThread(); |
| 160 |
| 123 // A refptr so that ImageRepSkia can be copied cheaply. | 161 // A refptr so that ImageRepSkia can be copied cheaply. |
| 124 scoped_refptr<internal::ImageSkiaStorage> storage_; | 162 scoped_refptr<internal::ImageSkiaStorage> storage_; |
| 125 }; | 163 }; |
| 126 | 164 |
| 127 } // namespace gfx | 165 } // namespace gfx |
| 128 | 166 |
| 129 #endif // UI_GFX_IMAGE_IMAGE_SKIA_H_ | 167 #endif // UI_GFX_IMAGE_IMAGE_SKIA_H_ |
| OLD | NEW |