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

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

Issue 2707993005: Avoid returning a shallow copy of gfx::Image from gfx::ImageFamily::CreateExact (Closed)
Patch Set: +comment Created 3 years, 10 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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_family.h" 5 #include "ui/gfx/image/image_family.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 8
9 #include "skia/ext/image_operations.h" 9 #include "skia/ext/image_operations.h"
10 #include "ui/gfx/geometry/size.h" 10 #include "ui/gfx/geometry/size.h"
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 110
111 gfx::Image ImageFamily::CreateExact(int width, int height) const { 111 gfx::Image ImageFamily::CreateExact(int width, int height) const {
112 // Resize crashes if width or height is 0, so just return an empty image. 112 // Resize crashes if width or height is 0, so just return an empty image.
113 if (width == 0 || height == 0) 113 if (width == 0 || height == 0)
114 return gfx::Image(); 114 return gfx::Image();
115 115
116 const gfx::Image* image = GetBest(width, height); 116 const gfx::Image* image = GetBest(width, height);
117 if (!image) 117 if (!image)
118 return gfx::Image(); 118 return gfx::Image();
119 119
120 if (image->Width() == width && image->Height() == height) 120 if (image->Width() == width && image->Height() == height) {
121 return gfx::Image(*image); 121 // Make a copy at gfx::ImageSkia level, so that resulting image's ref count
122 // is not racy to |image|. Since this function can run on a different thread
123 // than the thread |image| created on, we should not touch the
124 // non-thread-safe ref count in gfx::Image here.
125 std::unique_ptr<gfx::ImageSkia> image_skia(image->CopyImageSkia());
126 return gfx::Image(*image_skia);
127 }
122 128
123 SkBitmap bitmap = image->AsBitmap(); 129 SkBitmap bitmap = image->AsBitmap();
124 SkBitmap resized_bitmap = skia::ImageOperations::Resize( 130 SkBitmap resized_bitmap = skia::ImageOperations::Resize(
125 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, width, height); 131 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, width, height);
126 return gfx::Image::CreateFrom1xBitmap(resized_bitmap); 132 return gfx::Image::CreateFrom1xBitmap(resized_bitmap);
127 } 133 }
128 134
129 gfx::Image ImageFamily::CreateExact(const gfx::Size& size) const { 135 gfx::Image ImageFamily::CreateExact(const gfx::Size& size) const {
130 return CreateExact(size.width(), size.height()); 136 return CreateExact(size.width(), size.height());
131 } 137 }
(...skipping 12 matching lines...) Expand all
144 DCHECK(greater_or_equal != map_.begin()); 150 DCHECK(greater_or_equal != map_.begin());
145 std::map<MapKey, gfx::Image>::const_iterator less_than = greater_or_equal; 151 std::map<MapKey, gfx::Image>::const_iterator less_than = greater_or_equal;
146 --less_than; 152 --less_than;
147 // This must be true because there must be at least one image with |aspect|. 153 // This must be true because there must be at least one image with |aspect|.
148 DCHECK_EQ(less_than->first.aspect(), aspect); 154 DCHECK_EQ(less_than->first.aspect(), aspect);
149 // We have found the largest image smaller than desired. 155 // We have found the largest image smaller than desired.
150 return &less_than->second; 156 return &less_than->second;
151 } 157 }
152 158
153 } // namespace gfx 159 } // namespace gfx
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698