| OLD | NEW | 
|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // An Image wraps an image any flavor, be it platform-native GdkBitmap/NSImage, | 5 // An Image wraps an image any flavor, be it platform-native GdkBitmap/NSImage, | 
| 6 // or a SkBitmap. This also provides easy conversion to other image types | 6 // or a SkBitmap. This also provides easy conversion to other image types | 
| 7 // through operator overloading. It will cache the converted representations | 7 // through operator overloading. It will cache the converted representations | 
| 8 // internally to prevent double-conversion. | 8 // internally to prevent double-conversion. | 
| 9 // | 9 // | 
| 10 // The lifetime of both the initial representation and any converted ones are | 10 // The lifetime of both the initial representation and any converted ones are | 
| 11 // tied to the lifetime of the Image object itself. | 11 // tied to the lifetime of the Image object itself. | 
| 12 | 12 | 
| 13 #ifndef UI_GFX_IMAGE_H_ | 13 #ifndef UI_GFX_IMAGE_H_ | 
| 14 #define UI_GFX_IMAGE_H_ | 14 #define UI_GFX_IMAGE_H_ | 
| 15 #pragma once | 15 #pragma once | 
| 16 | 16 | 
| 17 #include <map> | 17 #include <map> | 
|  | 18 #include <vector> | 
| 18 | 19 | 
| 19 #include "base/basictypes.h" | 20 #include "base/basictypes.h" | 
| 20 #include "base/gtest_prod_util.h" | 21 #include "base/gtest_prod_util.h" | 
| 21 #include "build/build_config.h" | 22 #include "build/build_config.h" | 
| 22 #include "ui/gfx/native_widget_types.h"  // Forward-declares GdkPixbuf and NSIma
     ge. | 23 #include "ui/gfx/native_widget_types.h"  // Forward-declares GdkPixbuf and NSIma
     ge. | 
| 23 | 24 | 
| 24 class SkBitmap; | 25 class SkBitmap; | 
| 25 | 26 | 
| 26 namespace { | 27 namespace { | 
| 27 class ImageTest; | 28 class ImageTest; | 
|  | 29 class ImageMacTest; | 
| 28 } | 30 } | 
| 29 | 31 | 
| 30 namespace gfx { | 32 namespace gfx { | 
| 31 | 33 | 
| 32 namespace internal { | 34 namespace internal { | 
| 33 class ImageRep; | 35 class ImageRep; | 
| 34 } | 36 } | 
| 35 | 37 | 
| 36 class Image { | 38 class Image { | 
| 37  public: | 39  public: | 
| 38   enum RepresentationType { | 40   enum RepresentationType { | 
| 39     kGdkPixbufRep, | 41     kGdkPixbufRep, | 
| 40     kNSImageRep, | 42     kNSImageRep, | 
| 41     kSkBitmapRep, | 43     kSkBitmapRep, | 
| 42   }; | 44   }; | 
| 43 | 45 | 
| 44   // Creates a new image with the default representation. The object will take | 46   // Creates a new image with the default representation. The object will take | 
| 45   // ownership of the image. | 47   // ownership of the image. | 
| 46   explicit Image(const SkBitmap* bitmap); | 48   explicit Image(const SkBitmap* bitmap); | 
|  | 49   // To create an Image that supports multiple resolutions pass a vector | 
|  | 50   // of bitmaps, one for each resolution. | 
|  | 51   explicit Image(const std::vector<const SkBitmap*>& bitmaps); | 
|  | 52 | 
| 47 #if defined(OS_LINUX) | 53 #if defined(OS_LINUX) | 
| 48   // Does not increase |pixbuf|'s reference count; expects to take ownership. | 54   // Does not increase |pixbuf|'s reference count; expects to take ownership. | 
| 49   explicit Image(GdkPixbuf* pixbuf); | 55   explicit Image(GdkPixbuf* pixbuf); | 
| 50 #elif defined(OS_MACOSX) | 56 #elif defined(OS_MACOSX) | 
| 51   // Does not retain |image|; expects to take ownership. | 57   // Does not retain |image|; expects to take ownership. | 
|  | 58   // A single NSImage object can contain multiple bitmaps so there's no reason | 
|  | 59   // to pass a vector of these. | 
| 52   explicit Image(NSImage* image); | 60   explicit Image(NSImage* image); | 
| 53 #endif | 61 #endif | 
| 54 | 62 | 
| 55   // Deletes the image and all of its cached representations. | 63   // Deletes the image and all of its cached representations. | 
| 56   ~Image(); | 64   ~Image(); | 
| 57 | 65 | 
| 58   // Conversion handlers. | 66   // Conversion handlers. | 
| 59   operator const SkBitmap*(); | 67   operator const SkBitmap*(); | 
| 60   operator const SkBitmap&(); | 68   operator const SkBitmap&(); | 
| 61 #if defined(OS_LINUX) | 69 #if defined(OS_LINUX) | 
| 62   operator GdkPixbuf*(); | 70   operator GdkPixbuf*(); | 
| 63 #elif defined(OS_MACOSX) | 71 #elif defined(OS_MACOSX) | 
| 64   operator NSImage*(); | 72   operator NSImage*(); | 
| 65 #endif | 73 #endif | 
| 66 | 74 | 
|  | 75   // Gets the number of bitmaps in this image. This may cause a conversion | 
|  | 76   // to a bitmap representation. Note, this function and GetSkBitmapAtIndex() | 
|  | 77   // are primarily meant to be used by the theme provider. | 
|  | 78   size_t GetNumberOfSkBitmaps(); | 
|  | 79 | 
|  | 80   // Gets the bitmap at the given index. This may cause a conversion | 
|  | 81   // to a bitmap representation. Note, the internal ordering of bitmaps is not | 
|  | 82   // guaranteed. | 
|  | 83   const SkBitmap* GetSkBitmapAtIndex(size_t index); | 
|  | 84 | 
| 67   // Inspects the representations map to see if the given type exists. | 85   // Inspects the representations map to see if the given type exists. | 
| 68   bool HasRepresentation(RepresentationType type); | 86   bool HasRepresentation(RepresentationType type); | 
| 69 | 87 | 
| 70   // Swaps this image's internal representations with |other|. | 88   // Swaps this image's internal representations with |other|. | 
| 71   void SwapRepresentations(gfx::Image* other); | 89   void SwapRepresentations(gfx::Image* other); | 
| 72 | 90 | 
| 73  private: | 91  private: | 
| 74   typedef std::map<RepresentationType, internal::ImageRep*> RepresentationMap; | 92   typedef std::map<RepresentationType, internal::ImageRep*> RepresentationMap; | 
| 75 | 93 | 
| 76   // Returns the ImageRep for the default representation. | 94   // Returns the ImageRep for the default representation. | 
| 77   internal::ImageRep* DefaultRepresentation(); | 95   internal::ImageRep* DefaultRepresentation(); | 
| 78 | 96 | 
| 79   // Returns a ImageRep for the given representation type, converting and | 97   // Returns a ImageRep for the given representation type, converting and | 
| 80   // caching if necessary. | 98   // caching if necessary. | 
| 81   internal::ImageRep* GetRepresentation(RepresentationType rep); | 99   internal::ImageRep* GetRepresentation(RepresentationType rep); | 
| 82 | 100 | 
| 83   // Stores a representation into the map. | 101   // Stores a representation into the map. | 
| 84   void AddRepresentation(internal::ImageRep* rep); | 102   void AddRepresentation(internal::ImageRep* rep); | 
| 85 | 103 | 
| 86   // The type of image that was passed to the constructor. This key will always | 104   // The type of image that was passed to the constructor. This key will always | 
| 87   // exist in the |representations_| map. | 105   // exist in the |representations_| map. | 
| 88   RepresentationType default_representation_; | 106   RepresentationType default_representation_; | 
| 89 | 107 | 
| 90   // All the representations of an Image. Size will always be at least one, with | 108   // All the representations of an Image. Size will always be at least one, with | 
| 91   // more for any converted representations. | 109   // more for any converted representations. | 
| 92   RepresentationMap representations_; | 110   RepresentationMap representations_; | 
| 93 | 111 | 
| 94   friend class ::ImageTest; | 112   friend class ::ImageTest; | 
|  | 113   friend class ::ImageMacTest; | 
| 95   DISALLOW_COPY_AND_ASSIGN(Image); | 114   DISALLOW_COPY_AND_ASSIGN(Image); | 
| 96 }; | 115 }; | 
| 97 | 116 | 
| 98 }  // namespace gfx | 117 }  // namespace gfx | 
| 99 | 118 | 
| 100 #endif  // UI_GFX_IMAGE_H_ | 119 #endif  // UI_GFX_IMAGE_H_ | 
| OLD | NEW | 
|---|