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

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

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 #include "ui/gfx/image/image_skia.h" 5 #include "ui/gfx/image/image_skia.h"
6 6
7 #include <limits> 7 #include <limits>
8 #include <cmath> 8 #include <cmath>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/stl_util.h" 11 #include "ui/gfx/size.h"
12 #include "base/message_loop.h"
13 #include "third_party/skia/include/core/SkPixelRef.h"
12 14
13 namespace gfx { 15 namespace gfx {
14 16
15 ImageSkia::ImageSkia(const SkBitmap* bitmap) 17 namespace internal {
16 : size_(bitmap->width(), bitmap->height()), 18
17 mip_map_build_pending_(false) { 19 // A helper class such that ImageSkia can be cheaply copied. ImageSkia holds a
18 CHECK(bitmap); 20 // refptr instance of ImageSkiaStorage, which in turn holds all of ImageSkia's
19 // TODO(pkotwicz): Add a CHECK to ensure that !bitmap->isNull() 21 // information.
20 bitmaps_.push_back(bitmap); 22 class ImageSkiaStorage : public base::RefCounted<ImageSkiaStorage> {
23 public:
24 ImageSkiaStorage() {
25 }
26
27 void add_bitmap(const SkBitmap& bitmap) {
28 bitmaps_.push_back(bitmap);
29 }
30
31 const std::vector<SkBitmap>& bitmaps() const { return bitmaps_; }
32
33 void set_size(gfx::Size size) { size_ = size; }
sky 2012/05/04 20:12:25 const gfx::Size& (same for next line).
34 gfx::Size size() const { return size_; }
35
36 private:
37 ~ImageSkiaStorage() {
38 }
39
40 // Bitmaps at different densities.
41 std::vector<SkBitmap> bitmaps_;
42
43 // Size of the image in DIP.
44 gfx::Size size_;
45
46 friend class base::RefCounted<ImageSkiaStorage>;
47 };
48
49 } // internal
50
51 ImageSkia::ImageSkia() : storage_(NULL) {
21 } 52 }
22 53
23 ImageSkia::ImageSkia(const std::vector<const SkBitmap*>& bitmaps) 54 ImageSkia::ImageSkia(const SkBitmap& bitmap) {
24 : bitmaps_(bitmaps), 55 Init(bitmap);
25 mip_map_build_pending_(false) { 56 }
26 CHECK(!bitmaps_.empty()); 57
27 // TODO(pkotwicz): Add a CHECK to ensure that !bitmap->isNull() for each 58 ImageSkia::ImageSkia(const SkBitmap& bitmap, float dip_scale_factor) {
28 // vector element. 59 Init(bitmap, dip_scale_factor);
29 // Assume that the smallest bitmap represents 1x scale factor. 60 }
30 for (size_t i = 0; i < bitmaps_.size(); ++i) { 61
31 gfx::Size bitmap_size(bitmaps_[i]->width(), bitmaps_[i]->height()); 62 ImageSkia::ImageSkia(const SkBitmap* bitmap) {
32 if (size_.IsEmpty() || bitmap_size.GetArea() < size_.GetArea()) 63 Init(*bitmap);
33 size_ = bitmap_size; 64
65 if (MessageLoop::current()) {
sky 2012/05/04 20:12:25 Document why you use DeleteSoon.
66 MessageLoop::current()->DeleteSoon(FROM_HERE, bitmap);
67 } else {
68 // Hit in unittests.
69 delete bitmap;
34 } 70 }
35 } 71 }
36 72
37 ImageSkia::~ImageSkia() { 73 ImageSkia::ImageSkia(const ImageSkia& other) : storage_(other.storage_) {
38 STLDeleteElements(&bitmaps_);
39 } 74 }
40 75
41 void ImageSkia::BuildMipMap() { 76 ImageSkia& ImageSkia::operator=(const ImageSkia& other) {
42 mip_map_build_pending_ = true; 77 storage_ = other.storage_;
78 return *this;
43 } 79 }
44 80
45 void ImageSkia::DrawToCanvasInt(gfx::Canvas* canvas, int x, int y) { 81 ImageSkia& ImageSkia::operator=(const SkBitmap& other) {
46 SkPaint p; 82 Init(other);
47 DrawToCanvasInt(canvas, x, y, p); 83 return *this;
48 } 84 }
49 85
50 void ImageSkia::DrawToCanvasInt(gfx::Canvas* canvas, 86 ImageSkia::operator SkBitmap&() const {
51 int x, int y, 87 if (isNull()) {
52 const SkPaint& paint) { 88 // TODO(pkotwicz): This is temporary till conversion to gfx::ImageSkia is
53 89 // done.
54 if (IsZeroSized()) 90 // Static null bitmap such that we are not returning a temporary.
55 return; 91 static SkBitmap* null_bitmap = new SkBitmap();
56 92 return *null_bitmap;
57 SkMatrix m = canvas->sk_canvas()->getTotalMatrix();
58 float scale_x = std::abs(SkScalarToFloat(m.getScaleX()));
59 float scale_y = std::abs(SkScalarToFloat(m.getScaleY()));
60
61 const SkBitmap* bitmap = GetBitmapForScale(scale_x, scale_y);
62
63 if (mip_map_build_pending_) {
64 const_cast<SkBitmap*>(bitmap)->buildMipMap();
65 mip_map_build_pending_ = false;
66 } 93 }
67 94
68 float bitmap_scale_x = static_cast<float>(bitmap->width()) / width(); 95 std::vector<SkBitmap>& v = const_cast<std::vector<SkBitmap>&>(bitmaps());
69 float bitmap_scale_y = static_cast<float>(bitmap->height()) / height(); 96 return v[0];
70
71 canvas->Save();
72 canvas->sk_canvas()->scale(1.0f / bitmap_scale_x,
73 1.0f / bitmap_scale_y);
74 canvas->sk_canvas()->drawBitmap(*bitmap, SkFloatToScalar(x * bitmap_scale_x),
75 SkFloatToScalar(y * bitmap_scale_y));
76 canvas->Restore();
77 } 97 }
78 98
79 void ImageSkia::DrawToCanvasInt(gfx::Canvas* canvas, 99 ImageSkia::~ImageSkia() {
80 int src_x, int src_y, int src_w, int src_h,
81 int dest_x, int dest_y, int dest_w, int dest_h,
82 bool filter) {
83 SkPaint p;
84 DrawToCanvasInt(canvas, src_x, src_y, src_w, src_h, dest_x, dest_y,
85 dest_w, dest_h, filter, p);
86 } 100 }
87 101
88 void ImageSkia::DrawToCanvasInt(gfx::Canvas* canvas, 102 void ImageSkia::AddBitmapForScale(const SkBitmap& bitmap,
89 int src_x, int src_y, int src_w, int src_h, 103 float dip_scale_factor) {
90 int dest_x, int dest_y, int dest_w, int dest_h, 104 DCHECK(!bitmap.isNull());
91 bool filter,
92 const SkPaint& paint) {
93 if (IsZeroSized())
94 return;
95 105
96 SkMatrix m = canvas->sk_canvas()->getTotalMatrix(); 106 if (isNull())
sky 2012/05/04 20:12:25 Can you add some error checking here (just for deb
pkotwicz 2012/05/04 22:35:36 Right now we assume that bitmap sizes are relative
sky 2012/05/04 23:41:42 Can you make what you just described a DCHECK? At
97 float scale_x = std::abs(SkScalarToFloat(m.getScaleX())); 107 Init(bitmap, dip_scale_factor);
98 float scale_y = std::abs(SkScalarToFloat(m.getScaleY())); 108 else
99 109 storage_->add_bitmap(bitmap);
100 const SkBitmap* bitmap = GetBitmapForScale(scale_x, scale_y);
101
102 if (mip_map_build_pending_) {
103 const_cast<SkBitmap*>(bitmap)->buildMipMap();
104 mip_map_build_pending_ = false;
105 }
106
107 float bitmap_scale_x = static_cast<float>(bitmap->width()) / width();
108 float bitmap_scale_y = static_cast<float>(bitmap->height()) / height();
109
110 canvas->Save();
111 canvas->sk_canvas()->scale(1.0f / bitmap_scale_x,
112 1.0f / bitmap_scale_y);
113 canvas->DrawBitmapFloat(*bitmap,
114 src_x * bitmap_scale_x, src_y * bitmap_scale_x,
115 src_w * bitmap_scale_x, src_h * bitmap_scale_y,
116 dest_x * bitmap_scale_x, dest_y * bitmap_scale_y,
117 dest_w * bitmap_scale_x, dest_h * bitmap_scale_y,
118 filter, paint);
119
120 canvas->Restore();
121 } 110 }
122 111
123 const SkBitmap* ImageSkia::GetBitmapForScale(float x_scale_factor, 112 bool ImageSkia::GetBitmapForScale(float x_scale_factor,
sky 2012/05/04 20:12:25 Can this return a const SkBitmap& ?
pkotwicz 2012/05/04 22:35:36 I am not sure. What would we return in the case wh
sky 2012/05/04 23:41:42 Yes.
124 float y_scale_factor) const { 113 float y_scale_factor,
114 const SkBitmap** bitmap,
115 float* bitmap_scale_factor) const {
116 if (empty())
117 return false;
118
125 // Get the desired bitmap width and height given |x_scale_factor|, 119 // Get the desired bitmap width and height given |x_scale_factor|,
126 // |y_scale_factor| and |size_| at 1x density. 120 // |y_scale_factor| and size at 1x density.
127 float desired_width = size_.width() * x_scale_factor; 121 float desired_width = width() * x_scale_factor;
128 float desired_height = size_.height() * y_scale_factor; 122 float desired_height = height() * y_scale_factor;
129 123
124 const std::vector<SkBitmap>& bitmaps = storage_->bitmaps();
130 size_t closest_index = 0; 125 size_t closest_index = 0;
131 float smallest_diff = std::numeric_limits<float>::max(); 126 float smallest_diff = std::numeric_limits<float>::max();
132 for (size_t i = 0; i < bitmaps_.size(); ++i) { 127 for (size_t i = 0; i < bitmaps.size(); ++i) {
133 if (bitmaps_[i]->isNull()) 128 float diff = std::abs(bitmaps[i].width() - desired_width) +
134 continue; 129 std::abs(bitmaps[i].height() - desired_height);
135
136 float diff = std::abs(bitmaps_[i]->width() - desired_width) +
137 std::abs(bitmaps_[i]->height() - desired_height);
138 if (diff < smallest_diff) { 130 if (diff < smallest_diff) {
139 closest_index = i; 131 closest_index = i;
140 smallest_diff = diff; 132 smallest_diff = diff;
141 } 133 }
142 } 134 }
143 return bitmaps_[closest_index]; 135 if (smallest_diff < std::numeric_limits<float>::max()) {
136 *bitmap = &bitmaps[closest_index];
137 *bitmap_scale_factor = bitmaps[closest_index].width() / width();
138 return true;
139 }
140
141 return false;
142 }
143
144 bool ImageSkia::empty() const {
145 return isNull() || storage_->size().IsEmpty();
146 }
147
148 int ImageSkia::width() const {
149 return isNull() ? 0 : storage_->size().width();
150 }
151
152 int ImageSkia::height() const {
153 return isNull() ? 0 : storage_->size().height();
154 }
155
156 bool ImageSkia::extractSubset(ImageSkia* dst, SkIRect& subset) const {
157 if (isNull())
158 return false;
159 SkBitmap dst_bitmap;
160 bool return_value = bitmaps()[0].extractSubset(&dst_bitmap, subset);
161 *dst = ImageSkia(dst_bitmap);
162 return return_value;
163 }
164
165 const std::vector<SkBitmap>& ImageSkia::bitmaps() const {
166 return storage_->bitmaps();
167 }
168
169 void ImageSkia::Init(const SkBitmap& bitmap) {
170 Init(bitmap, 1.0f);
171 }
172
173 void ImageSkia::Init(const SkBitmap& bitmap, float scale_factor) {
174 DCHECK(scale_factor >= 1.0f);
175 if (bitmap.isNull()) {
176 storage_ = NULL;
177 return;
178 }
179 storage_ = new internal::ImageSkiaStorage();
180 storage_->set_size(gfx::Size(static_cast<int>(bitmap.width() / scale_factor),
181 static_cast<int>(bitmap.height() / scale_factor)));
182 storage_->add_bitmap(bitmap);
144 } 183 }
145 184
146 } // namespace gfx 185 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698