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 #include "chrome/browser/themes/theme_service.h" | 5 #include "chrome/browser/themes/theme_service.h" |
6 | 6 |
7 #include <gdk-pixbuf/gdk-pixbuf.h> | 7 #include <gdk-pixbuf/gdk-pixbuf.h> |
8 | 8 |
9 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "third_party/skia/include/core/SkBitmap.h" | 12 #include "third_party/skia/include/core/SkBitmap.h" |
13 #include "ui/gfx/gtk_util.h" | 13 #include "ui/gfx/gtk_util.h" |
14 | 14 |
15 GdkPixbuf* ThemeService::GetRTLEnabledPixbufNamed(int id) const { | 15 GdkPixbuf* ThemeService::GetRTLEnabledPixbufNamed(int id) const { |
16 return GetPixbufImpl(id, true); | 16 return GetPixbufImpl(id, true); |
17 } | 17 } |
18 | 18 |
19 GdkPixbuf* ThemeService::GetPixbufImpl(int id, bool rtl_enabled) const { | 19 GdkPixbuf* ThemeService::GetPixbufImpl(int id, bool rtl_enabled) const { |
20 DCHECK(CalledOnValidThread()); | 20 DCHECK(CalledOnValidThread()); |
21 // Use the negative |resource_id| for the key for BIDI-aware images. | 21 // Use the negative |resource_id| for the key for BIDI-aware images. |
22 int key = rtl_enabled ? -id : id; | 22 int key = rtl_enabled ? -id : id; |
23 | 23 |
24 // Check to see if we already have the pixbuf in the cache. | 24 // Check to see if we already have the pixbuf in the cache. |
25 GdkPixbufMap::const_iterator pixbufs_iter = gdk_pixbufs_.find(key); | 25 GdkPixbufMap::const_iterator pixbufs_iter = gdk_pixbufs_.find(key); |
26 if (pixbufs_iter != gdk_pixbufs_.end()) | 26 if (pixbufs_iter != gdk_pixbufs_.end()) |
27 return pixbufs_iter->second; | 27 return pixbufs_iter->second; |
28 | 28 |
29 SkBitmap* bitmap = GetBitmapNamed(id); | 29 SkBitmap* bitmap = GetBitmapNamed(id); |
30 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(bitmap); | 30 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(*bitmap); |
31 | 31 |
32 // We loaded successfully. Cache the pixbuf. | 32 // We loaded successfully. Cache the pixbuf. |
33 if (pixbuf) { | 33 if (pixbuf) { |
34 if (base::i18n::IsRTL() && rtl_enabled) { | 34 if (base::i18n::IsRTL() && rtl_enabled) { |
35 GdkPixbuf* original_pixbuf = pixbuf; | 35 GdkPixbuf* original_pixbuf = pixbuf; |
36 pixbuf = gdk_pixbuf_flip(pixbuf, TRUE); | 36 pixbuf = gdk_pixbuf_flip(pixbuf, TRUE); |
37 g_object_unref(original_pixbuf); | 37 g_object_unref(original_pixbuf); |
38 } | 38 } |
39 | 39 |
40 gdk_pixbufs_[key] = pixbuf; | 40 gdk_pixbufs_[key] = pixbuf; |
41 return pixbuf; | 41 return pixbuf; |
42 } | 42 } |
43 | 43 |
44 // We failed to retrieve the bitmap, show a debugging red square. | 44 // We failed to retrieve the bitmap, show a debugging red square. |
45 LOG(WARNING) << "Unable to load GdkPixbuf with id " << id; | 45 LOG(WARNING) << "Unable to load GdkPixbuf with id " << id; |
46 NOTREACHED(); // Want to assert in debug mode. | 46 NOTREACHED(); // Want to assert in debug mode. |
47 | 47 |
48 static GdkPixbuf* empty_bitmap = NULL; | 48 static GdkPixbuf* empty_bitmap = NULL; |
49 if (!empty_bitmap) { | 49 if (!empty_bitmap) { |
50 // The placeholder bitmap is bright red so people notice the problem. | 50 // The placeholder bitmap is bright red so people notice the problem. |
51 // This bitmap will be leaked, but this code should never be hit. | 51 SkBitmap skia_bitmap; |
52 scoped_ptr<SkBitmap> skia_bitmap(new SkBitmap()); | 52 skia_bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); |
53 skia_bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32); | 53 skia_bitmap.allocPixels(); |
54 skia_bitmap->allocPixels(); | 54 skia_bitmap.eraseARGB(255, 255, 0, 0); |
55 skia_bitmap->eraseARGB(255, 255, 0, 0); | 55 empty_bitmap = gfx::GdkPixbufFromSkBitmap(skia_bitmap); |
56 empty_bitmap = gfx::GdkPixbufFromSkBitmap(skia_bitmap.get()); | |
57 } | 56 } |
58 return empty_bitmap; | 57 return empty_bitmap; |
59 } | 58 } |
60 | 59 |
61 void ThemeService::FreePlatformCaches() { | 60 void ThemeService::FreePlatformCaches() { |
62 DCHECK(CalledOnValidThread()); | 61 DCHECK(CalledOnValidThread()); |
63 | 62 |
64 // Free GdkPixbufs. | 63 // Free GdkPixbufs. |
65 for (GdkPixbufMap::iterator i = gdk_pixbufs_.begin(); | 64 for (GdkPixbufMap::iterator i = gdk_pixbufs_.begin(); |
66 i != gdk_pixbufs_.end(); i++) { | 65 i != gdk_pixbufs_.end(); i++) { |
67 g_object_unref(i->second); | 66 g_object_unref(i->second); |
68 } | 67 } |
69 gdk_pixbufs_.clear(); | 68 gdk_pixbufs_.clear(); |
70 } | 69 } |
OLD | NEW |