Chromium Code Reviews| 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/trace_event/trace_event.h" | 14 #include "base/trace_event/trace_event.h" |
| 15 #include "cc/resources/scoped_ui_resource.h" | 15 #include "cc/resources/scoped_ui_resource.h" |
| 16 #include "jni/ResourceManager_jni.h" | 16 #include "jni/ResourceManager_jni.h" |
| 17 #include "third_party/skia/include/core/SkBitmap.h" | |
| 18 #include "third_party/skia/include/core/SkCanvas.h" | |
| 19 #include "third_party/skia/include/core/SkColorFilter.h" | |
| 17 #include "ui/android/resources/ui_resource_provider.h" | 20 #include "ui/android/resources/ui_resource_provider.h" |
| 18 #include "ui/android/window_android.h" | 21 #include "ui/android/window_android.h" |
| 19 #include "ui/gfx/android/java_bitmap.h" | 22 #include "ui/gfx/android/java_bitmap.h" |
| 20 #include "ui/gfx/geometry/rect.h" | 23 #include "ui/gfx/geometry/rect.h" |
| 21 | 24 |
| 22 using base::android::JavaArrayOfIntArrayToIntVector; | 25 using base::android::JavaArrayOfIntArrayToIntVector; |
| 23 using base::android::JavaRef; | 26 using base::android::JavaRef; |
| 24 | 27 |
| 25 namespace ui { | 28 namespace ui { |
| 26 | 29 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 66 | 69 |
| 67 if (!resource || res_type == ANDROID_RESOURCE_TYPE_DYNAMIC || | 70 if (!resource || res_type == ANDROID_RESOURCE_TYPE_DYNAMIC || |
| 68 res_type == ANDROID_RESOURCE_TYPE_DYNAMIC_BITMAP) { | 71 res_type == ANDROID_RESOURCE_TYPE_DYNAMIC_BITMAP) { |
| 69 RequestResourceFromJava(res_type, res_id); | 72 RequestResourceFromJava(res_type, res_id); |
| 70 resource = resources_[res_type].Lookup(res_id); | 73 resource = resources_[res_type].Lookup(res_id); |
| 71 } | 74 } |
| 72 | 75 |
| 73 return resource; | 76 return resource; |
| 74 } | 77 } |
| 75 | 78 |
| 79 void ResourceManagerImpl::RemoveUnusedTints( | |
| 80 const std::unordered_set<int>& used_tints) { | |
| 81 // Iterate over the currently cached tints and remove ones that were not | |
| 82 // used as defined in |used_tints|. | |
| 83 for (const auto& a : tinted_resources_) { | |
| 84 if (used_tints.find(a.first) == used_tints.end()) { | |
| 85 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.
| |
| 86 } | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 ResourceManager::Resource* ResourceManagerImpl::GetStaticResourceWithTint( | |
| 91 int res_id, | |
| 92 int tint_color) { | |
| 93 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.
| |
| 94 ResourceMap* resource_map; | |
| 95 if (item == tinted_resources_.end()) { | |
| 96 resource_map = new ResourceMap(); | |
| 97 tinted_resources_.insert({tint_color, resource_map}); | |
| 98 } else { | |
| 99 resource_map = item->second; | |
| 100 } | |
| 101 | |
| 102 Resource* tinted_resource = resource_map->Lookup(res_id); | |
| 103 | |
| 104 // If the resource is already cached, use it. | |
| 105 if (tinted_resource) | |
| 106 return tinted_resource; | |
| 107 | |
| 108 ResourceManager::Resource* base_image = | |
| 109 GetResource(ANDROID_RESOURCE_TYPE_STATIC, res_id); | |
| 110 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
| |
| 111 return nullptr; | |
| 112 | |
| 113 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.
| |
| 114 tinted_bitmap.allocPixels(SkImageInfo::MakeN32Premul(base_image->size.width(), | |
| 115 base_image->size.height())); | |
| 116 | |
| 117 SkCanvas canvas(tinted_bitmap); | |
| 118 canvas.clear(SK_ColorTRANSPARENT); | |
| 119 | |
| 120 // Build a color filter to use on the base resource. This filter multiplies | |
| 121 // the RGB components by the components of the new color but retains the | |
| 122 // alpha of the original image. | |
| 123 SkPaint color_filter; | |
| 124 SkScalar color_matrix[] = { | |
| 125 SkColorGetR(tint_color) / 255.0f, 0, 0, 0, 0, | |
| 126 0, SkColorGetG(tint_color) / 255.0f, 0, 0, 0, | |
| 127 0, 0, SkColorGetB(tint_color) / 255.0f, 0, 0, | |
| 128 0, 0, 0, 1.0f, 0 | |
| 129 }; | |
| 130 color_filter.setColorFilter( | |
| 131 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.
| |
| 132 | |
| 133 // Draw the resource and make it immutable. | |
| 134 base_image->ui_resource->GetBitmap(base_image->ui_resource->id(), false) | |
| 135 .DrawToCanvas(&canvas, &color_filter); | |
| 136 tinted_bitmap.setImmutable(); | |
| 137 | |
| 138 // Create a UI resource from the new bitmap. | |
| 139 tinted_resource = new Resource(); | |
| 140 tinted_resource->size = gfx::Size(base_image->size); | |
| 141 tinted_resource->padding = gfx::Rect(base_image->padding); | |
| 142 tinted_resource->aperture = gfx::Rect(base_image->aperture); | |
| 143 tinted_resource->ui_resource = cc::ScopedUIResource::Create(host_, | |
| 144 cc::UIResourceBitmap(tinted_bitmap)); | |
| 145 | |
| 146 resource_map->AddWithID(tinted_resource, res_id); | |
| 147 | |
| 148 return tinted_resource; | |
| 149 } | |
| 150 | |
| 151 void ResourceManagerImpl::ClearTintedResourceCache(JNIEnv* env, | |
| 152 const JavaRef<jobject>& jobj) { | |
| 153 tinted_resources_.clear(); | |
| 154 } | |
| 155 | |
| 76 void ResourceManagerImpl::PreloadResource(AndroidResourceType res_type, | 156 void ResourceManagerImpl::PreloadResource(AndroidResourceType res_type, |
| 77 int res_id) { | 157 int res_id) { |
| 78 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); | 158 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); |
| 79 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); | 159 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); |
| 80 | 160 |
| 81 // Don't send out a query if the resource is already loaded. | 161 // Don't send out a query if the resource is already loaded. |
| 82 if (resources_[res_type].Lookup(res_id)) | 162 if (resources_[res_type].Lookup(res_id)) |
| 83 return; | 163 return; |
| 84 | 164 |
| 85 PreloadResourceFromJava(res_type, res_id); | 165 PreloadResourceFromJava(res_type, res_id); |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 246 TRACE_EVENT2("ui", | 326 TRACE_EVENT2("ui", |
| 247 "ResourceManagerImpl::RequestCrushedSpriteResourceFromJava", | 327 "ResourceManagerImpl::RequestCrushedSpriteResourceFromJava", |
| 248 "bitmap_res_id", bitmap_res_id, | 328 "bitmap_res_id", bitmap_res_id, |
| 249 "metadata_res_id", metadata_res_id); | 329 "metadata_res_id", metadata_res_id); |
| 250 Java_ResourceManager_crushedSpriteResourceRequested( | 330 Java_ResourceManager_crushedSpriteResourceRequested( |
| 251 base::android::AttachCurrentThread(), java_obj_, bitmap_res_id, | 331 base::android::AttachCurrentThread(), java_obj_, bitmap_res_id, |
| 252 metadata_res_id, reloading); | 332 metadata_res_id, reloading); |
| 253 } | 333 } |
| 254 | 334 |
| 255 } // namespace ui | 335 } // namespace ui |
| OLD | NEW |