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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/memory/ref_counted.h" | |
13 #include "ui/base/ui_export.h" | |
12 #include "third_party/skia/include/core/SkBitmap.h" | 14 #include "third_party/skia/include/core/SkBitmap.h" |
13 #include "ui/gfx/canvas.h" | |
14 #include "ui/gfx/size.h" | |
15 | 15 |
16 namespace gfx { | 16 namespace gfx { |
17 | 17 |
18 namespace internal { | |
19 class ImageSkiaStorage; | |
20 } // namespace internal | |
21 | |
18 // Container for the same image at different densities, similar to NSImage. | 22 // Container for the same image at different densities, similar to NSImage. |
19 // Smallest image is assumed to represent 1x density. | 23 // Smallest image is assumed to represent 1x density. |
20 | 24 // |
21 // ImageSkia should be used for caching and drawing images of different | 25 // ImageSkia should be used whenever possible instead of SkBitmap. |
22 // densities. It should not be used as an SkBitmap wrapper. | 26 // Functions which mutate the image should operate on the SkBitmap returned |
Robert Sesek
2012/05/05 02:08:34
nit: s/which/that (restrictive clause)
pkotwicz
2012/05/08 22:10:14
Done.
| |
27 // from ImageSkia::GetBitmapForScale, not on ImageSkia. | |
28 // | |
29 // ImageSkia is cheap to copy and intentionally supports copy semantics. | |
23 class UI_EXPORT ImageSkia { | 30 class UI_EXPORT ImageSkia { |
24 public: | 31 public: |
32 // Creates instance with no bitmaps. | |
33 ImageSkia(); | |
34 | |
35 // Adds ref to passed in bitmap. | |
36 // DIP width and height are set based on scale factor of 1x. | |
Robert Sesek
2012/05/05 02:08:34
Maybe want to comment what "DIP" means at the clas
pkotwicz
2012/05/08 22:10:14
Done.
| |
37 // TODO(pkotwicz): This is temporary till conversion to gfx::ImageSkia is | |
38 // done. | |
39 ImageSkia(const SkBitmap& bitmap); | |
40 | |
41 // Adds ref to passed in bitmap. | |
42 // DIP width and height are set based on |dip_scale_factor|. | |
43 ImageSkia(const SkBitmap& bitmap, float dip_scale_factor); | |
44 | |
45 // Takes ownership of passed in bitmap. | |
46 // Caller should not assume that |bitmap| will be valid after constructor | |
47 // is called. | |
48 // DIP width and height are set based on scale factor of 1x. | |
49 // TODO(pkotwicz): This is temporary till conversion to gfx::ImageSkia is | |
50 // done. | |
25 explicit ImageSkia(const SkBitmap* bitmap); | 51 explicit ImageSkia(const SkBitmap* bitmap); |
26 explicit ImageSkia(const std::vector<const SkBitmap*>& bitmaps); | 52 |
53 // Copies a reference to |other|'s storage. | |
54 ImageSkia(const ImageSkia& other); | |
55 | |
56 // Copies a reference to |other|'s storage. | |
57 ImageSkia& operator=(const ImageSkia& other); | |
58 | |
59 // Converts from SkBitmap. | |
60 // Adds ref to passed in bitmap. | |
61 // DIP width and height are set based on scale factor of 1x. | |
62 // TODO(pkotwicz): This is temporary till conversion to gfx::ImageSkia is | |
63 // done. | |
64 ImageSkia& operator=(const SkBitmap& other); | |
65 | |
66 // Converts to SkBitmap. | |
67 // TODO(pkotwicz): This is temporary till conversion to gfx::ImageSkia is | |
68 // done. | |
69 operator SkBitmap&() const; | |
70 | |
27 ~ImageSkia(); | 71 ~ImageSkia(); |
28 | 72 |
29 // Build mipmap at time of next call to |DrawToCanvasInt|. | 73 // Adds |bitmap| for |dip_scale_factor| to bitmaps contained by this object. |
30 void BuildMipMap(); | 74 // Adds ref to passed in bitmap. |
75 // DIP width and height are set based on |dip_scale_factor|. | |
76 void AddBitmapForScale(const SkBitmap& bitmap, float dip_scale_factor); | |
31 | 77 |
32 // Draws the image with the origin at the specified location. The upper left | 78 // Returns the bitmap whose density best matches |x_scale_factor| and |
33 // corner of the image is rendered at the specified location. | 79 // |y_scale_factor|. |
34 void DrawToCanvasInt(Canvas* canvas, int x, int y); | 80 // Returns a null bitmap if object contains no bitmaps. |
81 // |bitmap_scale_factor| is set to the scale factor of the returned bitmap. | |
82 const SkBitmap& GetBitmapForScale(float x_scale_factor, | |
83 float y_scale_factor, | |
84 float* bitmap_scale_factor) const; | |
35 | 85 |
36 // Draws the image with the origin at the specified location, using the | 86 // Returns true if object is null or |size_| is empty. |
37 // specified paint. The upper left corner of the image is rendered at the | 87 bool empty() const; |
38 // specified location. | |
39 void DrawToCanvasInt(Canvas* canvas, | |
40 int x, int y, | |
41 const SkPaint& paint); | |
42 | 88 |
43 // Draws a portion of the image in the specified location. The src parameters | 89 // Returns true if this is a null object. |
44 // correspond to the region of the image to draw in the region defined | 90 // TODO(pkotwicz): Merge this function into empty(). |
45 // by the dest coordinates. | 91 bool isNull() const { return storage_ == NULL; } |
Robert Sesek
2012/05/05 02:08:34
Won't nit this since it's going away, but style is
| |
46 // | |
47 // If the width or height of the source differs from that of the destination, | |
48 // the image will be scaled. When scaling down, it is highly recommended | |
49 // that you call BuildMipMap() on your image to ensure that it has | |
50 // a mipmap, which will result in much higher-quality output. Set |filter| to | |
51 // use filtering for bitmaps, otherwise the nearest-neighbor algorithm is used | |
52 // for resampling. | |
53 // | |
54 // An optional custom SkPaint can be provided. | |
55 void DrawToCanvasInt(Canvas* canvas, | |
56 int src_x, int src_y, int src_w, int src_h, | |
57 int dest_x, int dest_y, int dest_w, int dest_h, | |
58 bool filter); | |
59 void DrawToCanvasInt(Canvas* canvas, | |
60 int src_x, int src_y, int src_w, int src_h, | |
61 int dest_x, int dest_y, int dest_w, int dest_h, | |
62 bool filter, | |
63 const SkPaint& paint); | |
64 | |
65 // Returns true if |size_| is empty. | |
66 bool IsZeroSized() const { return size_.IsEmpty(); } | |
67 | 92 |
68 // Width and height of image in DIP coordinate system. | 93 // Width and height of image in DIP coordinate system. |
69 int width() const { return size_.width(); } | 94 int width() const; |
70 int height() const { return size_.height(); } | 95 int height() const; |
96 | |
97 // Wrapper function for SkBitmap::extractBitmap. | |
98 // Operates on bitmap at index 0 if available. | |
99 // TODO(pkotwicz): This is temporary till conversion to gfx::ImageSkia is | |
100 // done. | |
101 bool extractSubset(ImageSkia* dst, SkIRect& subset) const; | |
Robert Sesek
2012/05/05 02:08:34
Same naming comment
| |
71 | 102 |
72 // Returns a vector with the SkBitmaps contained in this object. | 103 // Returns a vector with the SkBitmaps contained in this object. |
73 const std::vector<const SkBitmap*>& bitmaps() const { return bitmaps_; } | 104 const std::vector<SkBitmap>& bitmaps() const; |
74 | 105 |
75 private: | 106 private: |
76 // Returns the bitmap whose density best matches |x_scale_factor| and | 107 // Initialize ImageStorage with passed in parameters. |
77 // |y_scale_factor|. | 108 // If |bitmap.isNull()|, ImageStorage is set to NULL. |
78 const SkBitmap* GetBitmapForScale(float x_scale_factor, | 109 // Scale factor is set based on default scale factor of 1x. |
79 float y_scale_factor) const; | 110 // TODO(pkotwicz): This is temporary till conversion to gfx::ImageSkia is |
111 // done. | |
112 void Init(const SkBitmap& bitmap); | |
80 | 113 |
81 std::vector<const SkBitmap*> bitmaps_; | 114 // Initialize ImageStorage with passed in parameters. |
82 gfx::Size size_; | 115 // If |bitmap.isNull()|, ImageStorage is set to NULL. |
83 bool mip_map_build_pending_; | 116 void Init(const SkBitmap& bitmap, float scale_factor); |
84 | 117 |
85 DISALLOW_COPY_AND_ASSIGN(ImageSkia); | 118 // A refptr so that ImageRepSkia can be copied cheaply. |
119 scoped_refptr<internal::ImageSkiaStorage> storage_; | |
86 }; | 120 }; |
87 | 121 |
88 } // namespace gfx | 122 } // namespace gfx |
89 | 123 |
90 #endif // UI_GFX_IMAGE_IMAGE_SKIA_H_ | 124 #endif // UI_GFX_IMAGE_IMAGE_SKIA_H_ |
OLD | NEW |