Chromium Code Reviews| 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..96a4a2d9efaa855fffa7df1c4b615d3fee143fad 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 |
|
aelias_OOO_until_Jul13
2016/08/31 17:12:26
If this is worth doing, we should do it before lan
mdjones
2016/08/31 17:41:00
I'll look at this if we determine this is an appro
|
| +// that is used by the thumbnails. |
| +const int kMaxTintResourceCount = 30; |
|
mdjones
2016/08/31 17:49:12
I can also reduce the memory footprint to max of 2
|
| +} // namespace |
| + |
| // static |
| ResourceManagerImpl* ResourceManagerImpl::FromJavaObject(jobject jobj) { |
| return reinterpret_cast<ResourceManagerImpl*>( |
| @@ -73,6 +83,73 @@ ResourceManager::Resource* ResourceManagerImpl::GetResource( |
| return resource; |
| } |
| +ResourceManager::Resource* ResourceManagerImpl::GetStaticResourceWithTint( |
| + int res_id, |
| + int tint_color, |
| + int default_color) { |
| + ResourceMap* resource_map = tinted_resources_.Lookup(tint_color); |
| + if (!resource_map) { |
| + resource_map = new ResourceMap(); |
| + tinted_resources_.AddWithID(resource_map, tint_color); |
| + } else if (resource_map->size() >= kMaxTintResourceCount) { |
| + // If the cache is too large, use the default color for the resource. |
| + resource_map = tinted_resources_.Lookup(default_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(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)); |
| + |
| + // Draw the resource and make it immutable. |
| + base_image->ui_resource->GetBitmap(base_image->ui_resource->id(), false).Draw( |
| + 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); |