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 "ui/base/resource/resource_bundle.h"
7 #include "base/i18n/rtl.h"
8 #include "base/logging.h"
9 #include "base/memory/ref_counted_memory.h"
10 #include "base/path_service.h"
11 #include "base/synchronization/lock.h"
12 #include "third_party/skia/include/core/SkBitmap.h"
13 #include "ui/base/layout.h"
14 #include "ui/base/resource/resource_handle.h"
15 #include "ui/base/ui_base_paths.h"
16 #include "ui/gfx/image/image.h"
17 #include "ui/gfx/scoped_gobject.h"
25 // Convert the raw image data into a GdkPixbuf. The GdkPixbuf that is returned
26 // has a ref count of 1 so the caller must call g_object_unref to free the
28 GdkPixbuf* LoadPixbuf(base::RefCountedStaticMemory* data, bool rtl_enabled) {
29 ScopedGObject<GdkPixbufLoader>::Type loader(gdk_pixbuf_loader_new());
30 bool ok = data && gdk_pixbuf_loader_write(loader.get(),
31 reinterpret_cast<const guint8*>(data->front()), data->size(), NULL);
34 // Calling gdk_pixbuf_loader_close forces the data to be parsed by the
35 // loader. We must do this before calling gdk_pixbuf_loader_get_pixbuf.
36 ok = gdk_pixbuf_loader_close(loader.get(), NULL);
39 GdkPixbuf* pixbuf = gdk_pixbuf_loader_get_pixbuf(loader.get());
43 if (base::i18n::IsRTL() && rtl_enabled) {
44 // |pixbuf| will get unreffed and destroyed (see below). The returned value
46 return gdk_pixbuf_flip(pixbuf, TRUE);
48 // The pixbuf is owned by the loader, so add a ref so when we delete the
49 // loader (when the ScopedGObject goes out of scope), the pixbuf still
56 base::FilePath GetResourcesPakFilePath(const std::string& pak_name) {
58 if (PathService::Get(base::DIR_MODULE, &path))
59 return path.AppendASCII(pak_name.c_str());
61 // Return just the name of the pack file.
62 return base::FilePath(pak_name.c_str());
67 void ResourceBundle::LoadCommonResources() {
68 AddDataPackFromPath(GetResourcesPakFilePath(
69 "chrome_100_percent.pak"),
73 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) {
74 // Use the negative |resource_id| for the key for BIDI-aware images.
75 int key = rtl == RTL_ENABLED ? -resource_id : resource_id;
77 // Check to see if the image is already in the cache.
79 base::AutoLock lock_scope(*images_and_fonts_lock_);
80 if (images_.count(key))
86 image = delegate_->GetNativeImageNamed(resource_id, rtl);
88 if (image.IsEmpty()) {
89 scoped_refptr<base::RefCountedStaticMemory> data(
90 LoadDataResourceBytesForScale(resource_id, SCALE_FACTOR_100P));
91 GdkPixbuf* pixbuf = LoadPixbuf(data.get(), rtl == RTL_ENABLED);
94 LOG(WARNING) << "Unable to load pixbuf with id " << resource_id;
95 NOTREACHED(); // Want to assert in debug mode.
96 return GetEmptyImage();
99 image = gfx::Image(pixbuf); // Takes ownership.
102 base::AutoLock lock_scope(*images_and_fonts_lock_);
104 // Another thread raced the load and has already cached the image.
105 if (images_.count(key))
108 images_[key] = image;