Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(365)

Side by Side Diff: ui/gfx/image/image_skia.h

Issue 10245003: Makes ImageSkia more like SkBitmap (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Patch Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
27 // from ImageSkia::GetBitmapForScale, not on ImageSkia.
28 //
29 // ImageSkia supports copy semantics as legacy from SkBitmap. Ideally, this
sky 2012/05/04 20:12:25 This comment isn't right, we want copy semantics.
pkotwicz 2012/05/04 22:35:36 Do you have a suggestion for a good comment? Copy
sky 2012/05/04 23:41:42 I agree. My point is your comment makes it sounds
30 // would not be necessary and all classes which add a ref to ImageSkia would
31 // hold a ref_ptr.
23 class UI_EXPORT ImageSkia { 32 class UI_EXPORT ImageSkia {
24 public: 33 public:
34 // Creates null instance.
sky 2012/05/04 20:12:25 null instance -> instance with no images.
35 ImageSkia();
36
37 // Adds ref to passed in bitmap.
38 // DIP width and height are set based on scale factor of 1x.
39 // TODO(pkotwicz): This is temporary till conversion to gfx::ImageSkia is
40 // done.
41 ImageSkia(const SkBitmap& bitmap);
42
43 // Adds ref to passed in bitmap.
44 // DIP width and height are set based on |dip_scale_factor|.
45 ImageSkia(const SkBitmap& bitmap, float dip_scale_factor);
46
47 // Takes ownership of passed in bitmap.
48 // Caller should not assume that |bitmap| will be valid after constructor
49 // is called.
50 // DIP width and height are set based on scale factor of 1x.
51 // TODO(pkotwicz): This is temporary till conversion to gfx::ImageSkia is
52 // done.
25 explicit ImageSkia(const SkBitmap* bitmap); 53 explicit ImageSkia(const SkBitmap* bitmap);
26 explicit ImageSkia(const std::vector<const SkBitmap*>& bitmaps); 54
55 // Copies a reference to |other|'s storage.
56 ImageSkia(const ImageSkia& other);
57
58 // Copies a reference to |other|'s storage.
59 ImageSkia& operator=(const ImageSkia& other);
60
61 // Converts from SkBitmap.
62 // Adds ref to passed in bitmap.
63 // DIP width and height are set based on scale factor of 1x.
64 // TODO(pkotwicz): This is temporary till conversion to gfx::ImageSkia is
65 // done.
66 ImageSkia& operator=(const SkBitmap& other);
67
68 // Converts to SkBitmap.
69 // TODO(pkotwicz): This is temporary till conversion to gfx::ImageSkia is
70 // done.
71 operator SkBitmap&() const;
72
27 ~ImageSkia(); 73 ~ImageSkia();
28 74
29 // Build mipmap at time of next call to |DrawToCanvasInt|. 75 // Adds |bitmap| for |dip_scale_factor| to bitmaps contained by this object.
30 void BuildMipMap(); 76 // Adds ref to passed in bitmap.
77 // DIP width and height are set based on |dip_scale_factor|.
78 void AddBitmapForScale(const SkBitmap& bitmap, float dip_scale_factor);
31 79
32 // Draws the image with the origin at the specified location. The upper left 80 // Finds the |bitmap| whose density best matches |x_scale_factor| and
33 // corner of the image is rendered at the specified location. 81 // |y_scale_factor|.
34 void DrawToCanvasInt(Canvas* canvas, int x, int y); 82 // |bitmap_scale_factor| is set to the scale factor of the returned bitmap.
83 // Returns true if a bitmap was found.
84 bool GetBitmapForScale(float x_scale_factor,
85 float y_scale_factor,
86 const SkBitmap** bitmap,
87 float* bitmap_scale_factor) const;
35 88
36 // Draws the image with the origin at the specified location, using the 89 // Returns true if object is null or |size_| is empty.
37 // specified paint. The upper left corner of the image is rendered at the 90 bool empty() const;
38 // specified location.
39 void DrawToCanvasInt(Canvas* canvas,
40 int x, int y,
41 const SkPaint& paint);
42 91
43 // Draws a portion of the image in the specified location. The src parameters 92 // Returns true if this is a null object.
44 // correspond to the region of the image to draw in the region defined 93 // TODO(pkotwicz): Merge this function into empty().
45 // by the dest coordinates. 94 bool isNull() const { return storage_ == NULL; }
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 95
68 // Width and height of image in DIP coordinate system. 96 // Width and height of image in DIP coordinate system.
69 int width() const { return size_.width(); } 97 int width() const;
70 int height() const { return size_.height(); } 98 int height() const;
99
100 // Wrapper function for SkBitmap::extractBitmap.
101 // Operates on bitmap at index 0 if available.
102 // TODO(pkotwicz): This is temporary till conversion to gfx::ImageSkia is
103 // done.
104 bool extractSubset(ImageSkia* dst, SkIRect& subset) const;
71 105
72 // Returns a vector with the SkBitmaps contained in this object. 106 // Returns a vector with the SkBitmaps contained in this object.
73 const std::vector<const SkBitmap*>& bitmaps() const { return bitmaps_; } 107 const std::vector<SkBitmap>& bitmaps() const;
74 108
75 private: 109 private:
76 // Returns the bitmap whose density best matches |x_scale_factor| and 110 // Initialize ImageStorage with passed in parameters.
77 // |y_scale_factor|. 111 // If |bitmap.isNull()|, ImageStorage is set to NULL.
78 const SkBitmap* GetBitmapForScale(float x_scale_factor, 112 // Scale factor is set based on default scale factor of 1x.
79 float y_scale_factor) const; 113 // TODO(pkotwicz): This is temporary till conversion to gfx::ImageSkia is
114 // done.
115 void Init(const SkBitmap& bitmap);
80 116
81 std::vector<const SkBitmap*> bitmaps_; 117 // Initialize ImageStorage with passed in parameters.
82 gfx::Size size_; 118 // If |bitmap.isNull()|, ImageStorage is set to NULL.
83 bool mip_map_build_pending_; 119 void Init(const SkBitmap& bitmap, float scale_factor);
84 120
85 DISALLOW_COPY_AND_ASSIGN(ImageSkia); 121 // A refptr so that ImageRepSkia can be copied cheaply.
122 scoped_refptr<internal::ImageSkiaStorage> storage_;
86 }; 123 };
87 124
88 } // namespace gfx 125 } // namespace gfx
89 126
90 #endif // UI_GFX_IMAGE_IMAGE_SKIA_H_ 127 #endif // UI_GFX_IMAGE_IMAGE_SKIA_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698