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

Unified Diff: ui/android/resources/resource_manager_impl.cc

Issue 2293573002: Add tinted static UI resource cache (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: use custom caching mechanism Created 4 years, 3 months 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 side-by-side diff with in-line comments
Download patch
Index: ui/android/resources/resource_manager_impl.cc
diff --git a/ui/android/resources/resource_manager_impl.cc b/ui/android/resources/resource_manager_impl.cc
index 61c7bdad554d3f7999494da3fad752f8e48e2726..d8aec1dfa10ba908446cd00803713b490265bd8f 100644
--- a/ui/android/resources/resource_manager_impl.cc
+++ b/ui/android/resources/resource_manager_impl.cc
@@ -14,6 +14,9 @@
#include "base/trace_event/trace_event.h"
#include "cc/resources/scoped_ui_resource.h"
#include "jni/ResourceManager_jni.h"
+#include "third_party/skia/include/core/SkBitmap.h"
+#include "third_party/skia/include/core/SkCanvas.h"
+#include "third_party/skia/include/core/SkColorFilter.h"
#include "ui/android/resources/ui_resource_provider.h"
#include "ui/android/window_android.h"
#include "ui/gfx/android/java_bitmap.h"
@@ -73,6 +76,83 @@ ResourceManager::Resource* ResourceManagerImpl::GetResource(
return resource;
}
+void ResourceManagerImpl::RemoveUnusedTints(
+ const std::unordered_set<int>& used_tints) {
+ // Iterate over the currently cached tints and remove ones that were not
+ // used as defined in |used_tints|.
+ for (const auto& a : tinted_resources_) {
+ if (used_tints.find(a.first) == used_tints.end()) {
+ tinted_resources_.erase(a.first);
aelias_OOO_until_Jul13 2016/09/07 19:34:44 I think the for loop implicit iterator would still
mdjones 2016/09/08 16:34:44 Done.
+ }
+ }
+}
+
+ResourceManager::Resource* ResourceManagerImpl::GetStaticResourceWithTint(
+ int res_id,
+ int tint_color) {
+ auto item = tinted_resources_.find(tint_color);
aelias_OOO_until_Jul13 2016/09/07 19:34:44 I think the iterator name wouldn't be ridiculously
mdjones 2016/09/08 16:34:44 Done.
+ ResourceMap* resource_map;
+ if (item == tinted_resources_.end()) {
+ resource_map = new ResourceMap();
+ tinted_resources_.insert({tint_color, resource_map});
+ } else {
+ resource_map = item->second;
+ }
+
+ Resource* tinted_resource = resource_map->Lookup(res_id);
+
+ // If the resource is already cached, use it.
+ if (tinted_resource)
+ return tinted_resource;
+
+ ResourceManager::Resource* base_image =
+ GetResource(ANDROID_RESOURCE_TYPE_STATIC, res_id);
+ if (!base_image)
aelias_OOO_until_Jul13 2016/09/07 19:34:44 What is this scenario, exactly? Can we DCHECK?
mdjones 2016/09/08 16:34:44 Resource might not be ready. DCHECK should be fine
+ return nullptr;
+
+ SkBitmap tinted_bitmap;
aelias_OOO_until_Jul13 2016/09/07 19:34:44 Could you add a TRACE_EVENT0(browser, "ResourceMan
mdjones 2016/09/08 16:34:44 Done.
+ tinted_bitmap.allocPixels(SkImageInfo::MakeN32Premul(base_image->size.width(),
+ base_image->size.height()));
+
+ SkCanvas canvas(tinted_bitmap);
+ canvas.clear(SK_ColorTRANSPARENT);
+
+ // Build a color filter to use on the base resource. This filter multiplies
+ // the RGB components by the components of the new color but retains the
+ // alpha of the original image.
+ SkPaint color_filter;
+ SkScalar color_matrix[] = {
+ SkColorGetR(tint_color) / 255.0f, 0, 0, 0, 0,
+ 0, SkColorGetG(tint_color) / 255.0f, 0, 0, 0,
+ 0, 0, SkColorGetB(tint_color) / 255.0f, 0, 0,
+ 0, 0, 0, 1.0f, 0
+ };
+ color_filter.setColorFilter(
+ SkColorFilter::MakeMatrixFilterRowMajor255(color_matrix));
aelias_OOO_until_Jul13 2016/09/07 19:34:44 Could you switch to SkFilter::MakeModeFilter(color
mdjones 2016/09/08 16:34:44 Done.
+
+ // Draw the resource and make it immutable.
+ base_image->ui_resource->GetBitmap(base_image->ui_resource->id(), false)
+ .DrawToCanvas(&canvas, &color_filter);
+ tinted_bitmap.setImmutable();
+
+ // Create a UI resource from the new bitmap.
+ tinted_resource = new Resource();
+ tinted_resource->size = gfx::Size(base_image->size);
+ tinted_resource->padding = gfx::Rect(base_image->padding);
+ tinted_resource->aperture = gfx::Rect(base_image->aperture);
+ tinted_resource->ui_resource = cc::ScopedUIResource::Create(host_,
+ cc::UIResourceBitmap(tinted_bitmap));
+
+ resource_map->AddWithID(tinted_resource, res_id);
+
+ return tinted_resource;
+}
+
+void ResourceManagerImpl::ClearTintedResourceCache(JNIEnv* env,
+ const JavaRef<jobject>& jobj) {
+ tinted_resources_.clear();
+}
+
void ResourceManagerImpl::PreloadResource(AndroidResourceType res_type,
int res_id) {
DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST);
« ui/android/resources/resource_manager_impl.h ('K') | « ui/android/resources/resource_manager_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698