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..4dd8d25cac6c1ffff53f5c8f7170726e46b8500e 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" |
@@ -24,6 +27,13 @@ using base::android::JavaRef; |
namespace ui { |
+namespace { |
+// The maximum number of tinted variations of a single resource. |
+// TODO(mdjones): Implement real caching logic; something similar to priority |
+// that is used by the thumbnails. |
+const int kMaxTintResourceCount = 4; |
+} // namespace |
+ |
// static |
ResourceManagerImpl* ResourceManagerImpl::FromJavaObject(jobject jobj) { |
return reinterpret_cast<ResourceManagerImpl*>( |
@@ -73,6 +83,79 @@ ResourceManager::Resource* ResourceManagerImpl::GetResource( |
return resource; |
} |
+ResourceManager::Resource* ResourceManagerImpl::GetStaticResourceWithTint( |
+ int res_id, |
+ int tint_color, |
+ int default_color) { |
+ int color = tint_color; |
+ ResourceMap* resource_map = tinted_resources_.Lookup(color); |
+ |
+ // If the cache is too large, use the default color for the resource. |
+ if (!resource_map && tinted_resources_.size() >= kMaxTintResourceCount) { |
+ color = default_color; |
+ resource_map = tinted_resources_.Lookup(color); |
+ } |
+ |
+ if (!resource_map) { |
+ resource_map = new ResourceMap(); |
+ tinted_resources_.AddWithID(resource_map, color); |
+ } |
+ |
+ 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) |
+ return nullptr; |
+ |
+ SkBitmap tinted_bitmap; |
+ 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(color) / 255.0f, 0, 0, 0, 0, |
+ 0, SkColorGetG(color) / 255.0f, 0, 0, 0, |
+ 0, 0, SkColorGetB(color) / 255.0f, 0, 0, |
+ 0, 0, 0, 1.0f, 0 |
+ }; |
+ color_filter.setColorFilter( |
+ SkColorFilter::MakeMatrixFilterRowMajor255(color_matrix)); |
+ |
+ // 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->tint = color; |
+ 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); |