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..641095301a283082431e7b566f5cf32e59879923 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 max_tint_resource_count = 30; |
|
David Trainor- moved to gerrit
2016/08/30 19:27:32
needs to be of form kMaxTintResourceCount
mdjones
2016/08/30 21:52:16
Done.
|
| +} // namespace |
| + |
| // static |
| ResourceManagerImpl* ResourceManagerImpl::FromJavaObject(jobject jobj) { |
| return reinterpret_cast<ResourceManagerImpl*>( |
| @@ -73,6 +83,71 @@ 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() >= max_tint_resource_count) { |
| + // 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; |
|
David Trainor- moved to gerrit
2016/08/30 19:27:32
new line here:
if (tinted_resource)
return tint
mdjones
2016/08/30 21:52:16
Done.
|
| + |
| + ResourceManager::Resource* base_image = |
| + GetResource(ANDROID_RESOURCE_TYPE_STATIC, res_id); |
| + if (!base_image) return nullptr; |
|
David Trainor- moved to gerrit
2016/08/30 19:27:32
new line here too
mdjones
2016/08/30 21:52:16
Done.
|
| + |
| + 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(res_id, false).Draw(canvas, |
|
David Trainor- moved to gerrit
2016/08/30 19:27:32
Is res_id the right id? It just happens to work b
mdjones
2016/08/30 21:52:16
Ah yes, I read through that code and originally pu
|
| + 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); |