| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/android/resources/resource_manager_impl.h" | 5 #include "ui/android/resources/resource_manager_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/android/jni_array.h" | 12 #include "base/android/jni_array.h" |
| 13 #include "base/android/jni_string.h" | 13 #include "base/android/jni_string.h" |
| 14 #include "base/memory/ptr_util.h" |
| 14 #include "base/trace_event/trace_event.h" | 15 #include "base/trace_event/trace_event.h" |
| 15 #include "cc/resources/scoped_ui_resource.h" | 16 #include "cc/resources/scoped_ui_resource.h" |
| 16 #include "jni/ResourceManager_jni.h" | 17 #include "jni/ResourceManager_jni.h" |
| 18 #include "third_party/skia/include/core/SkBitmap.h" |
| 19 #include "third_party/skia/include/core/SkCanvas.h" |
| 20 #include "third_party/skia/include/core/SkColorFilter.h" |
| 17 #include "ui/android/resources/ui_resource_provider.h" | 21 #include "ui/android/resources/ui_resource_provider.h" |
| 18 #include "ui/android/window_android.h" | 22 #include "ui/android/window_android.h" |
| 19 #include "ui/gfx/android/java_bitmap.h" | 23 #include "ui/gfx/android/java_bitmap.h" |
| 20 #include "ui/gfx/geometry/rect.h" | 24 #include "ui/gfx/geometry/rect.h" |
| 21 | 25 |
| 22 using base::android::JavaArrayOfIntArrayToIntVector; | 26 using base::android::JavaArrayOfIntArrayToIntVector; |
| 23 using base::android::JavaRef; | 27 using base::android::JavaRef; |
| 24 | 28 |
| 25 namespace ui { | 29 namespace ui { |
| 26 | 30 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 | 70 |
| 67 if (!resource || res_type == ANDROID_RESOURCE_TYPE_DYNAMIC || | 71 if (!resource || res_type == ANDROID_RESOURCE_TYPE_DYNAMIC || |
| 68 res_type == ANDROID_RESOURCE_TYPE_DYNAMIC_BITMAP) { | 72 res_type == ANDROID_RESOURCE_TYPE_DYNAMIC_BITMAP) { |
| 69 RequestResourceFromJava(res_type, res_id); | 73 RequestResourceFromJava(res_type, res_id); |
| 70 resource = resources_[res_type].Lookup(res_id); | 74 resource = resources_[res_type].Lookup(res_id); |
| 71 } | 75 } |
| 72 | 76 |
| 73 return resource; | 77 return resource; |
| 74 } | 78 } |
| 75 | 79 |
| 80 void ResourceManagerImpl::RemoveUnusedTints( |
| 81 const std::unordered_set<int>& used_tints) { |
| 82 // Iterate over the currently cached tints and remove ones that were not |
| 83 // used as defined in |used_tints|. |
| 84 for (auto it = tinted_resources_.cbegin(); it != tinted_resources_.cend();) { |
| 85 if (used_tints.find(it->first) == used_tints.end()) { |
| 86 it = tinted_resources_.erase(it); |
| 87 } else { |
| 88 ++it; |
| 89 } |
| 90 } |
| 91 } |
| 92 |
| 93 ResourceManager::Resource* ResourceManagerImpl::GetStaticResourceWithTint( |
| 94 int res_id, |
| 95 int tint_color) { |
| 96 if (tinted_resources_.find(tint_color) == tinted_resources_.end()) { |
| 97 tinted_resources_[tint_color] = base::MakeUnique<ResourceMap>(); |
| 98 } |
| 99 ResourceMap* resource_map = tinted_resources_[tint_color].get(); |
| 100 |
| 101 Resource* tinted_resource = resource_map->Lookup(res_id); |
| 102 |
| 103 // If the resource is already cached, use it. |
| 104 if (tinted_resource) |
| 105 return tinted_resource; |
| 106 |
| 107 tinted_resource = new Resource(); |
| 108 |
| 109 ResourceManager::Resource* base_image = |
| 110 GetResource(ANDROID_RESOURCE_TYPE_STATIC, res_id); |
| 111 DCHECK(base_image); |
| 112 |
| 113 TRACE_EVENT0("browser", "ResourceManagerImpl::GetStaticResourceWithTint"); |
| 114 SkBitmap tinted_bitmap; |
| 115 tinted_bitmap.allocPixels(SkImageInfo::MakeN32Premul(base_image->size.width(), |
| 116 base_image->size.height())); |
| 117 |
| 118 SkCanvas canvas(tinted_bitmap); |
| 119 canvas.clear(SK_ColorTRANSPARENT); |
| 120 |
| 121 // Build a color filter to use on the base resource. This filter multiplies |
| 122 // the RGB components by the components of the new color but retains the |
| 123 // alpha of the original image. |
| 124 SkPaint color_filter; |
| 125 color_filter.setColorFilter( |
| 126 SkColorFilter::MakeModeFilter(tint_color, SkXfermode::kModulate_Mode)); |
| 127 |
| 128 // Draw the resource and make it immutable. |
| 129 base_image->ui_resource->GetBitmap(base_image->ui_resource->id(), false) |
| 130 .DrawToCanvas(&canvas, &color_filter); |
| 131 tinted_bitmap.setImmutable(); |
| 132 |
| 133 // Create a UI resource from the new bitmap. |
| 134 tinted_resource = new Resource(); |
| 135 tinted_resource->size = gfx::Size(base_image->size); |
| 136 tinted_resource->padding = gfx::Rect(base_image->padding); |
| 137 tinted_resource->aperture = gfx::Rect(base_image->aperture); |
| 138 tinted_resource->ui_resource = cc::ScopedUIResource::Create(host_, |
| 139 cc::UIResourceBitmap(tinted_bitmap)); |
| 140 |
| 141 resource_map->AddWithID(tinted_resource, res_id); |
| 142 |
| 143 return tinted_resource; |
| 144 } |
| 145 |
| 146 void ResourceManagerImpl::ClearTintedResourceCache(JNIEnv* env, |
| 147 const JavaRef<jobject>& jobj) { |
| 148 tinted_resources_.clear(); |
| 149 } |
| 150 |
| 76 void ResourceManagerImpl::PreloadResource(AndroidResourceType res_type, | 151 void ResourceManagerImpl::PreloadResource(AndroidResourceType res_type, |
| 77 int res_id) { | 152 int res_id) { |
| 78 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); | 153 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); |
| 79 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); | 154 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); |
| 80 | 155 |
| 81 // Don't send out a query if the resource is already loaded. | 156 // Don't send out a query if the resource is already loaded. |
| 82 if (resources_[res_type].Lookup(res_id)) | 157 if (resources_[res_type].Lookup(res_id)) |
| 83 return; | 158 return; |
| 84 | 159 |
| 85 PreloadResourceFromJava(res_type, res_id); | 160 PreloadResourceFromJava(res_type, res_id); |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 TRACE_EVENT2("ui", | 321 TRACE_EVENT2("ui", |
| 247 "ResourceManagerImpl::RequestCrushedSpriteResourceFromJava", | 322 "ResourceManagerImpl::RequestCrushedSpriteResourceFromJava", |
| 248 "bitmap_res_id", bitmap_res_id, | 323 "bitmap_res_id", bitmap_res_id, |
| 249 "metadata_res_id", metadata_res_id); | 324 "metadata_res_id", metadata_res_id); |
| 250 Java_ResourceManager_crushedSpriteResourceRequested( | 325 Java_ResourceManager_crushedSpriteResourceRequested( |
| 251 base::android::AttachCurrentThread(), java_obj_, bitmap_res_id, | 326 base::android::AttachCurrentThread(), java_obj_, bitmap_res_id, |
| 252 metadata_res_id, reloading); | 327 metadata_res_id, reloading); |
| 253 } | 328 } |
| 254 | 329 |
| 255 } // namespace ui | 330 } // namespace ui |
| OLD | NEW |