Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: ui/android/resources/resource_manager_impl.cc

Issue 2293573002: Add tinted static UI resource cache (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/android/resources/resource_manager_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 11 matching lines...) Expand all
97 jint aperture_left, 172 jint aperture_left,
98 jint aperture_top, 173 jint aperture_top,
99 jint aperture_right, 174 jint aperture_right,
100 jint aperture_bottom) { 175 jint aperture_bottom) {
101 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); 176 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST);
102 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); 177 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST);
103 TRACE_EVENT2("ui", "ResourceManagerImpl::OnResourceReady", 178 TRACE_EVENT2("ui", "ResourceManagerImpl::OnResourceReady",
104 "resource_type", res_type, 179 "resource_type", res_type,
105 "resource_id", res_id); 180 "resource_id", res_id);
106 181
182
aelias_OOO_until_Jul13 2016/09/08 22:04:41 nit: unnecessary newline
mdjones 2016/09/09 01:19:15 Done.
107 Resource* resource = resources_[res_type].Lookup(res_id); 183 Resource* resource = resources_[res_type].Lookup(res_id);
108 if (!resource) { 184 if (!resource) {
109 resource = new Resource(); 185 resource = new Resource();
110 resources_[res_type].AddWithID(resource, res_id); 186 resources_[res_type].AddWithID(resource, res_id);
111 } 187 }
112 188
113 gfx::JavaBitmap jbitmap(bitmap.obj()); 189 gfx::JavaBitmap jbitmap(bitmap.obj());
114 resource->size = jbitmap.size(); 190 resource->size = jbitmap.size();
115 resource->padding.SetRect(padding_left, padding_top, 191 resource->padding.SetRect(padding_left, padding_top,
116 padding_right - padding_left, 192 padding_right - padding_left,
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 TRACE_EVENT2("ui", 322 TRACE_EVENT2("ui",
247 "ResourceManagerImpl::RequestCrushedSpriteResourceFromJava", 323 "ResourceManagerImpl::RequestCrushedSpriteResourceFromJava",
248 "bitmap_res_id", bitmap_res_id, 324 "bitmap_res_id", bitmap_res_id,
249 "metadata_res_id", metadata_res_id); 325 "metadata_res_id", metadata_res_id);
250 Java_ResourceManager_crushedSpriteResourceRequested( 326 Java_ResourceManager_crushedSpriteResourceRequested(
251 base::android::AttachCurrentThread(), java_obj_, bitmap_res_id, 327 base::android::AttachCurrentThread(), java_obj_, bitmap_res_id,
252 metadata_res_id, reloading); 328 metadata_res_id, reloading);
253 } 329 }
254 330
255 } // namespace ui 331 } // namespace ui
OLDNEW
« no previous file with comments | « ui/android/resources/resource_manager_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698