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
3 // found in the LICENSE file.
5 #include "chrome/browser/themes/theme_service.h"
7 #include <gdk-pixbuf/gdk-pixbuf.h>
9 #include "base/i18n/rtl.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "third_party/skia/include/core/SkBitmap.h"
13 #include "ui/gfx/gtk_util.h"
14 #include "ui/gfx/image/image.h"
16 GdkPixbuf
* ThemeService::GetRTLEnabledPixbufNamed(int id
) const {
17 return GetPixbufImpl(id
, true);
20 GdkPixbuf
* ThemeService::GetPixbufImpl(int id
, bool rtl_enabled
) const {
21 DCHECK(CalledOnValidThread());
22 // Use the negative |resource_id| for the key for BIDI-aware images.
23 int key
= rtl_enabled
? -id
: id
;
25 // Check to see if we already have the pixbuf in the cache.
26 GdkPixbufMap::const_iterator pixbufs_iter
= gdk_pixbufs_
.find(key
);
27 if (pixbufs_iter
!= gdk_pixbufs_
.end())
28 return pixbufs_iter
->second
;
30 SkBitmap bitmap
= GetImageNamed(id
).AsBitmap();
31 GdkPixbuf
* pixbuf
= gfx::GdkPixbufFromSkBitmap(bitmap
);
33 // We loaded successfully. Cache the pixbuf.
35 if (base::i18n::IsRTL() && rtl_enabled
) {
36 GdkPixbuf
* original_pixbuf
= pixbuf
;
37 pixbuf
= gdk_pixbuf_flip(pixbuf
, TRUE
);
38 g_object_unref(original_pixbuf
);
41 gdk_pixbufs_
[key
] = pixbuf
;
45 // We failed to retrieve the bitmap, show a debugging red square.
46 LOG(WARNING
) << "Unable to load GdkPixbuf with id " << id
;
47 NOTREACHED(); // Want to assert in debug mode.
49 static GdkPixbuf
* empty_bitmap
= NULL
;
51 // The placeholder bitmap is bright red so people notice the problem.
53 skia_bitmap
.setConfig(SkBitmap::kARGB_8888_Config
, 32, 32);
54 skia_bitmap
.allocPixels();
55 skia_bitmap
.eraseARGB(255, 255, 0, 0);
56 empty_bitmap
= gfx::GdkPixbufFromSkBitmap(skia_bitmap
);
61 void ThemeService::FreePlatformCaches() {
62 DCHECK(CalledOnValidThread());
65 for (GdkPixbufMap::iterator i
= gdk_pixbufs_
.begin();
66 i
!= gdk_pixbufs_
.end(); i
++) {
67 g_object_unref(i
->second
);