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. 4 5 #include "chrome/browser/themes/theme_service.h" 6 7 #include <gdk-pixbuf/gdk-pixbuf.h> 8 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" 15 16 GdkPixbuf* ThemeService::GetRTLEnabledPixbufNamed(int id) const { 17 return GetPixbufImpl(id, true); 18 } 19 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; 24 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; 29 30 SkBitmap bitmap = GetImageNamed(id).AsBitmap(); 31 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(bitmap); 32 33 // We loaded successfully. Cache the pixbuf. 34 if (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); 39 } 40 41 gdk_pixbufs_[key] = pixbuf; 42 return pixbuf; 43 } 44 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. 48 49 static GdkPixbuf* empty_bitmap = NULL; 50 if (!empty_bitmap) { 51 // The placeholder bitmap is bright red so people notice the problem. 52 SkBitmap skia_bitmap; 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); 57 } 58 return empty_bitmap; 59 } 60 61 void ThemeService::FreePlatformCaches() { 62 DCHECK(CalledOnValidThread()); 63 64 // Free GdkPixbufs. 65 for (GdkPixbufMap::iterator i = gdk_pixbufs_.begin(); 66 i != gdk_pixbufs_.end(); i++) { 67 g_object_unref(i->second); 68 } 69 gdk_pixbufs_.clear(); 70 } 71