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 |
| 30 namespace { | |
| 31 // The maximum number of tinted variations of a single resource. | |
| 32 // TODO(mdjones): Implement real caching logic; something similar to priority | |
| 33 // that is used by the thumbnails. | |
| 34 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.
| |
| 35 } // namespace | |
| 36 | |
| 27 // static | 37 // static |
| 28 ResourceManagerImpl* ResourceManagerImpl::FromJavaObject(jobject jobj) { | 38 ResourceManagerImpl* ResourceManagerImpl::FromJavaObject(jobject jobj) { |
| 29 return reinterpret_cast<ResourceManagerImpl*>( | 39 return reinterpret_cast<ResourceManagerImpl*>( |
| 30 Java_ResourceManager_getNativePtr(base::android::AttachCurrentThread(), | 40 Java_ResourceManager_getNativePtr(base::android::AttachCurrentThread(), |
| 31 jobj)); | 41 jobj)); |
| 32 } | 42 } |
| 33 | 43 |
| 34 ResourceManagerImpl::ResourceManagerImpl(gfx::NativeWindow native_window) | 44 ResourceManagerImpl::ResourceManagerImpl(gfx::NativeWindow native_window) |
| 35 : host_(nullptr) { | 45 : host_(nullptr) { |
| 36 JNIEnv* env = base::android::AttachCurrentThread(); | 46 JNIEnv* env = base::android::AttachCurrentThread(); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 66 | 76 |
| 67 if (!resource || res_type == ANDROID_RESOURCE_TYPE_DYNAMIC || | 77 if (!resource || res_type == ANDROID_RESOURCE_TYPE_DYNAMIC || |
| 68 res_type == ANDROID_RESOURCE_TYPE_DYNAMIC_BITMAP) { | 78 res_type == ANDROID_RESOURCE_TYPE_DYNAMIC_BITMAP) { |
| 69 RequestResourceFromJava(res_type, res_id); | 79 RequestResourceFromJava(res_type, res_id); |
| 70 resource = resources_[res_type].Lookup(res_id); | 80 resource = resources_[res_type].Lookup(res_id); |
| 71 } | 81 } |
| 72 | 82 |
| 73 return resource; | 83 return resource; |
| 74 } | 84 } |
| 75 | 85 |
| 86 ResourceManager::Resource* ResourceManagerImpl::GetStaticResourceWithTint( | |
| 87 int res_id, | |
| 88 int tint_color, | |
| 89 int default_color) { | |
| 90 ResourceMap* resource_map = tinted_resources_.Lookup(tint_color); | |
| 91 if (!resource_map) { | |
| 92 resource_map = new ResourceMap(); | |
| 93 tinted_resources_.AddWithID(resource_map, tint_color); | |
| 94 } else if (resource_map->size() >= max_tint_resource_count) { | |
| 95 // If the cache is too large, use the default color for the resource. | |
| 96 resource_map = tinted_resources_.Lookup(default_color); | |
| 97 } | |
| 98 | |
| 99 Resource* tinted_resource = resource_map->Lookup(res_id); | |
| 100 | |
| 101 // If the resource is already cached, use it. | |
| 102 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.
| |
| 103 | |
| 104 ResourceManager::Resource* base_image = | |
| 105 GetResource(ANDROID_RESOURCE_TYPE_STATIC, res_id); | |
| 106 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.
| |
| 107 | |
| 108 SkBitmap tinted_bitmap; | |
| 109 tinted_bitmap.allocPixels(SkImageInfo::MakeN32Premul(base_image->size.width(), | |
| 110 base_image->size.height())); | |
| 111 | |
| 112 SkCanvas canvas(tinted_bitmap); | |
| 113 canvas.clear(SK_ColorTRANSPARENT); | |
| 114 | |
| 115 // Build a color filter to use on the base resource. This filter multiplies | |
| 116 // the RGB components by the components of the new color but retains the | |
| 117 // alpha of the original image. | |
| 118 SkPaint color_filter; | |
| 119 SkScalar color_matrix[] = { | |
| 120 SkColorGetR(tint_color) / 255.0f, 0, 0, 0, 0, | |
| 121 0, SkColorGetG(tint_color) / 255.0f, 0, 0, 0, | |
| 122 0, 0, SkColorGetB(tint_color) / 255.0f, 0, 0, | |
| 123 0, 0, 0, 1.0f, 0 | |
| 124 }; | |
| 125 color_filter.setColorFilter( | |
| 126 SkColorFilter::MakeMatrixFilterRowMajor255(color_matrix)); | |
| 127 | |
| 128 // Draw the resource and make it immutable. | |
| 129 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
| |
| 130 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 |