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 | |
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
| |
33 // that is used by the thumbnails. | |
34 const int kMaxTintResourceCount = 30; | |
mdjones
2016/08/31 17:49:12
I can also reduce the memory footprint to max of 2
| |
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() >= kMaxTintResourceCount) { | |
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) | |
103 return tinted_resource; | |
104 | |
105 ResourceManager::Resource* base_image = | |
106 GetResource(ANDROID_RESOURCE_TYPE_STATIC, res_id); | |
107 if (!base_image) | |
108 return nullptr; | |
109 | |
110 SkBitmap tinted_bitmap; | |
111 tinted_bitmap.allocPixels(SkImageInfo::MakeN32Premul(base_image->size.width(), | |
112 base_image->size.height())); | |
113 | |
114 SkCanvas canvas(tinted_bitmap); | |
115 canvas.clear(SK_ColorTRANSPARENT); | |
116 | |
117 // Build a color filter to use on the base resource. This filter multiplies | |
118 // the RGB components by the components of the new color but retains the | |
119 // alpha of the original image. | |
120 SkPaint color_filter; | |
121 SkScalar color_matrix[] = { | |
122 SkColorGetR(tint_color) / 255.0f, 0, 0, 0, 0, | |
123 0, SkColorGetG(tint_color) / 255.0f, 0, 0, 0, | |
124 0, 0, SkColorGetB(tint_color) / 255.0f, 0, 0, | |
125 0, 0, 0, 1.0f, 0 | |
126 }; | |
127 color_filter.setColorFilter( | |
128 SkColorFilter::MakeMatrixFilterRowMajor255(color_matrix)); | |
129 | |
130 // Draw the resource and make it immutable. | |
131 base_image->ui_resource->GetBitmap(base_image->ui_resource->id(), false).Draw( | |
132 canvas, color_filter); | |
133 tinted_bitmap.setImmutable(); | |
134 | |
135 // Create a UI resource from the new bitmap. | |
136 tinted_resource = new Resource(); | |
137 tinted_resource->size = gfx::Size(base_image->size); | |
138 tinted_resource->padding = gfx::Rect(base_image->padding); | |
139 tinted_resource->aperture = gfx::Rect(base_image->aperture); | |
140 tinted_resource->ui_resource = cc::ScopedUIResource::Create(host_, | |
141 cc::UIResourceBitmap(tinted_bitmap)); | |
142 | |
143 resource_map->AddWithID(tinted_resource, res_id); | |
144 | |
145 return tinted_resource; | |
146 } | |
147 | |
148 void ResourceManagerImpl::ClearTintedResourceCache(JNIEnv* env, | |
149 const JavaRef<jobject>& jobj) { | |
150 tinted_resources_.Clear(); | |
151 } | |
152 | |
76 void ResourceManagerImpl::PreloadResource(AndroidResourceType res_type, | 153 void ResourceManagerImpl::PreloadResource(AndroidResourceType res_type, |
77 int res_id) { | 154 int res_id) { |
78 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); | 155 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); |
79 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); | 156 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); |
80 | 157 |
81 // Don't send out a query if the resource is already loaded. | 158 // Don't send out a query if the resource is already loaded. |
82 if (resources_[res_type].Lookup(res_id)) | 159 if (resources_[res_type].Lookup(res_id)) |
83 return; | 160 return; |
84 | 161 |
85 PreloadResourceFromJava(res_type, res_id); | 162 PreloadResourceFromJava(res_type, res_id); |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
246 TRACE_EVENT2("ui", | 323 TRACE_EVENT2("ui", |
247 "ResourceManagerImpl::RequestCrushedSpriteResourceFromJava", | 324 "ResourceManagerImpl::RequestCrushedSpriteResourceFromJava", |
248 "bitmap_res_id", bitmap_res_id, | 325 "bitmap_res_id", bitmap_res_id, |
249 "metadata_res_id", metadata_res_id); | 326 "metadata_res_id", metadata_res_id); |
250 Java_ResourceManager_crushedSpriteResourceRequested( | 327 Java_ResourceManager_crushedSpriteResourceRequested( |
251 base::android::AttachCurrentThread(), java_obj_, bitmap_res_id, | 328 base::android::AttachCurrentThread(), java_obj_, bitmap_res_id, |
252 metadata_res_id, reloading); | 329 metadata_res_id, reloading); |
253 } | 330 } |
254 | 331 |
255 } // namespace ui | 332 } // namespace ui |
OLD | NEW |