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

Side by Side Diff: ui/gfx/icon_util.cc

Issue 1406403007: Eliminate HICON leaks caused by creating icons from bitmap image. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use ScopedGeneric to define ScopedGDIObject. Created 5 years, 1 month 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 | « printing/image_win.cc ('k') | ui/gfx/path_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/icon_util.h" 5 #include "ui/gfx/icon_util.h"
6 6
7 #include "base/files/file_util.h" 7 #include "base/files/file_util.h"
8 #include "base/files/important_file_writer.h" 8 #include "base/files/important_file_writer.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 48, // Alt+Tab icon size. 156 48, // Alt+Tab icon size.
157 64, // Recommended by the MSDN as a nice to have icon size. 157 64, // Recommended by the MSDN as a nice to have icon size.
158 96, // Recommended by the MSDN as a nice to have icon size. 158 96, // Recommended by the MSDN as a nice to have icon size.
159 128, // Used by the Shell (e.g. for shortcuts). 159 128, // Used by the Shell (e.g. for shortcuts).
160 256 // Used by Vista onwards for large icons. 160 256 // Used by Vista onwards for large icons.
161 }; 161 };
162 162
163 const size_t IconUtil::kNumIconDimensions = arraysize(kIconDimensions); 163 const size_t IconUtil::kNumIconDimensions = arraysize(kIconDimensions);
164 const size_t IconUtil::kNumIconDimensionsUpToMediumSize = 9; 164 const size_t IconUtil::kNumIconDimensionsUpToMediumSize = 9;
165 165
166 HICON IconUtil::CreateHICONFromSkBitmap(const SkBitmap& bitmap) { 166 HICON IconUtil::CreateHICONFromSkBitmap(const SkBitmap& bitmap) {
grt (UTC plus 2) 2015/11/09 16:55:39 It seems safest for this to return base::win::Scop
anpol 2015/11/10 15:40:23 Done.
167 // Only 32 bit ARGB bitmaps are supported. We also try to perform as many 167 // Only 32 bit ARGB bitmaps are supported. We also try to perform as many
168 // validations as we can on the bitmap. 168 // validations as we can on the bitmap.
169 SkAutoLockPixels bitmap_lock(bitmap); 169 SkAutoLockPixels bitmap_lock(bitmap);
170 if ((bitmap.colorType() != kN32_SkColorType) || 170 if ((bitmap.colorType() != kN32_SkColorType) ||
171 (bitmap.width() <= 0) || (bitmap.height() <= 0) || 171 (bitmap.width() <= 0) || (bitmap.height() <= 0) ||
172 (bitmap.getPixels() == NULL)) 172 (bitmap.getPixels() == NULL))
173 return NULL; 173 return NULL;
174 174
175 // We start by creating a DIB which we'll use later on in order to create 175 // We start by creating a DIB which we'll use later on in order to create
176 // the HICON. We use BITMAPV5HEADER since the bitmap we are about to convert 176 // the HICON. We use BITMAPV5HEADER since the bitmap we are about to convert
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 if (!::GetIconInfo(icon, &icon_info)) 312 if (!::GetIconInfo(icon, &icon_info))
313 return NULL; 313 return NULL;
314 314
315 if (!::GetObject(icon_info.hbmMask, sizeof(bitmap_info), &bitmap_info)) 315 if (!::GetObject(icon_info.hbmMask, sizeof(bitmap_info), &bitmap_info))
316 return NULL; 316 return NULL;
317 317
318 gfx::Size icon_size(bitmap_info.bmWidth, bitmap_info.bmHeight); 318 gfx::Size icon_size(bitmap_info.bmWidth, bitmap_info.bmHeight);
319 return new SkBitmap(CreateSkBitmapFromHICONHelper(icon, icon_size)); 319 return new SkBitmap(CreateSkBitmapFromHICONHelper(icon, icon_size));
320 } 320 }
321 321
322 HICON IconUtil::CreateCursorFromDIB(const gfx::Size& icon_size, 322 HICON IconUtil::CreateCursorFromDIB(const gfx::Size& icon_size,
grt (UTC plus 2) 2015/11/09 16:55:39 base::win::ScopedHICON?
323 const gfx::Point& hotspot, 323 const gfx::Point& hotspot,
324 const void* dib_bits, 324 const void* dib_bits,
325 size_t dib_size) { 325 size_t dib_size) {
326 BITMAPINFO icon_bitmap_info = {}; 326 BITMAPINFO icon_bitmap_info = {};
327 gfx::CreateBitmapHeader( 327 gfx::CreateBitmapHeader(
328 icon_size.width(), 328 icon_size.width(),
329 icon_size.height(), 329 icon_size.height(),
330 reinterpret_cast<BITMAPINFOHEADER*>(&icon_bitmap_info)); 330 reinterpret_cast<BITMAPINFOHEADER*>(&icon_bitmap_info));
331 331
332 base::win::ScopedGetDC dc(NULL); 332 base::win::ScopedGetDC dc(NULL);
333 base::win::ScopedCreateDC working_dc(CreateCompatibleDC(dc)); 333 base::win::ScopedCreateDC working_dc(CreateCompatibleDC(dc));
334 base::win::ScopedGDIObject<HBITMAP> bitmap_handle( 334 base::win::ScopedGDIObject<HBITMAP> bitmap_handle(
335 CreateDIBSection(dc, 335 CreateDIBSection(dc,
336 &icon_bitmap_info, 336 &icon_bitmap_info,
337 DIB_RGB_COLORS, 337 DIB_RGB_COLORS,
338 0, 338 0,
339 0, 339 0,
340 0)); 340 0));
341 if (dib_size > 0) { 341 if (dib_size > 0) {
342 SetDIBits(0, 342 SetDIBits(0,
343 bitmap_handle, 343 bitmap_handle.get(),
344 0, 344 0,
345 icon_size.height(), 345 icon_size.height(),
346 dib_bits, 346 dib_bits,
347 &icon_bitmap_info, 347 &icon_bitmap_info,
348 DIB_RGB_COLORS); 348 DIB_RGB_COLORS);
349 } 349 }
350 350
351 HBITMAP old_bitmap = reinterpret_cast<HBITMAP>( 351 HBITMAP old_bitmap = reinterpret_cast<HBITMAP>(
352 SelectObject(working_dc.Get(), bitmap_handle)); 352 SelectObject(working_dc.Get(), bitmap_handle.get()));
353 SetBkMode(working_dc.Get(), TRANSPARENT); 353 SetBkMode(working_dc.Get(), TRANSPARENT);
354 SelectObject(working_dc.Get(), old_bitmap); 354 SelectObject(working_dc.Get(), old_bitmap);
355 355
356 base::win::ScopedGDIObject<HBITMAP> mask( 356 base::win::ScopedGDIObject<HBITMAP> mask(
357 CreateBitmap(icon_size.width(), 357 CreateBitmap(icon_size.width(),
358 icon_size.height(), 358 icon_size.height(),
359 1, 359 1,
360 1, 360 1,
361 NULL)); 361 NULL));
362 ICONINFO ii = {0}; 362 ICONINFO ii = {0};
363 ii.fIcon = FALSE; 363 ii.fIcon = FALSE;
364 ii.xHotspot = hotspot.x(); 364 ii.xHotspot = hotspot.x();
365 ii.yHotspot = hotspot.y(); 365 ii.yHotspot = hotspot.y();
366 ii.hbmMask = mask; 366 ii.hbmMask = mask.get();
367 ii.hbmColor = bitmap_handle; 367 ii.hbmColor = bitmap_handle.get();
368 368
369 return CreateIconIndirect(&ii); 369 return CreateIconIndirect(&ii);
370 } 370 }
371 371
372 SkBitmap IconUtil::CreateSkBitmapFromHICONHelper(HICON icon, 372 SkBitmap IconUtil::CreateSkBitmapFromHICONHelper(HICON icon,
373 const gfx::Size& s) { 373 const gfx::Size& s) {
374 DCHECK(icon); 374 DCHECK(icon);
375 DCHECK(!s.IsEmpty()); 375 DCHECK(!s.IsEmpty());
376 376
377 // Allocating memory for the SkBitmap object. We are going to create an ARGB 377 // Allocating memory for the SkBitmap object. We are going to create an ARGB
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after
694 // Once we compute the size for a singe AND mask scan line, we multiply that 694 // Once we compute the size for a singe AND mask scan line, we multiply that
695 // number by the image height in order to get the total number of bytes for 695 // number by the image height in order to get the total number of bytes for
696 // the AND mask. Thus, for a 15X15 image, we need 15 * 4 which is 60 bytes 696 // the AND mask. Thus, for a 15X15 image, we need 15 * 4 which is 60 bytes
697 // for the monochrome bitmap representing the AND mask. 697 // for the monochrome bitmap representing the AND mask.
698 size_t and_line_length = (bitmap.width() + 7) >> 3; 698 size_t and_line_length = (bitmap.width() + 7) >> 3;
699 and_line_length = (and_line_length + 3) & ~3; 699 and_line_length = (and_line_length + 3) & ~3;
700 size_t and_mask_size = and_line_length * bitmap.height(); 700 size_t and_mask_size = and_line_length * bitmap.height();
701 size_t masks_size = *xor_mask_size + and_mask_size; 701 size_t masks_size = *xor_mask_size + and_mask_size;
702 *bytes_in_resource = masks_size + sizeof(BITMAPINFOHEADER); 702 *bytes_in_resource = masks_size + sizeof(BITMAPINFOHEADER);
703 } 703 }
OLDNEW
« no previous file with comments | « printing/image_win.cc ('k') | ui/gfx/path_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698