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

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: fix text color when cache is full 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
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/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 kMaxTintResourceCount = 4;
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
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 int color = tint_color;
91 ResourceMap* resource_map = tinted_resources_.Lookup(color);
92
93 // If the cache is too large, use the default color for the resource.
94 if (!resource_map && tinted_resources_.size() >= kMaxTintResourceCount) {
95 color = default_color;
96 resource_map = tinted_resources_.Lookup(color);
97 }
98
99 if (!resource_map) {
100 resource_map = new ResourceMap();
101 tinted_resources_.AddWithID(resource_map, color);
102 }
103
104 Resource* tinted_resource = resource_map->Lookup(res_id);
105
106 // If the resource is already cached, use it.
107 if (tinted_resource)
108 return tinted_resource;
109
110 ResourceManager::Resource* base_image =
111 GetResource(ANDROID_RESOURCE_TYPE_STATIC, res_id);
112 if (!base_image)
113 return nullptr;
114
115 SkBitmap tinted_bitmap;
116 tinted_bitmap.allocPixels(SkImageInfo::MakeN32Premul(base_image->size.width(),
117 base_image->size.height()));
118
119 SkCanvas canvas(tinted_bitmap);
120 canvas.clear(SK_ColorTRANSPARENT);
121
122 // Build a color filter to use on the base resource. This filter multiplies
123 // the RGB components by the components of the new color but retains the
124 // alpha of the original image.
125 SkPaint color_filter;
126 SkScalar color_matrix[] = {
127 SkColorGetR(color) / 255.0f, 0, 0, 0, 0,
128 0, SkColorGetG(color) / 255.0f, 0, 0, 0,
129 0, 0, SkColorGetB(color) / 255.0f, 0, 0,
130 0, 0, 0, 1.0f, 0
131 };
132 color_filter.setColorFilter(
133 SkColorFilter::MakeMatrixFilterRowMajor255(color_matrix));
134
135 // Draw the resource and make it immutable.
136 base_image->ui_resource->GetBitmap(base_image->ui_resource->id(), false)
137 .DrawToCanvas(&canvas, &color_filter);
138 tinted_bitmap.setImmutable();
139
140 // Create a UI resource from the new bitmap.
141 tinted_resource = new Resource();
142 tinted_resource->size = gfx::Size(base_image->size);
143 tinted_resource->padding = gfx::Rect(base_image->padding);
144 tinted_resource->aperture = gfx::Rect(base_image->aperture);
145 tinted_resource->tint = color;
146 tinted_resource->ui_resource = cc::ScopedUIResource::Create(host_,
147 cc::UIResourceBitmap(tinted_bitmap));
148
149 resource_map->AddWithID(tinted_resource, res_id);
150
151 return tinted_resource;
152 }
153
154 void ResourceManagerImpl::ClearTintedResourceCache(JNIEnv* env,
155 const JavaRef<jobject>& jobj) {
156 tinted_resources_.Clear();
157 }
158
76 void ResourceManagerImpl::PreloadResource(AndroidResourceType res_type, 159 void ResourceManagerImpl::PreloadResource(AndroidResourceType res_type,
77 int res_id) { 160 int res_id) {
78 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); 161 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST);
79 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); 162 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST);
80 163
81 // Don't send out a query if the resource is already loaded. 164 // Don't send out a query if the resource is already loaded.
82 if (resources_[res_type].Lookup(res_id)) 165 if (resources_[res_type].Lookup(res_id))
83 return; 166 return;
84 167
85 PreloadResourceFromJava(res_type, res_id); 168 PreloadResourceFromJava(res_type, res_id);
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 TRACE_EVENT2("ui", 329 TRACE_EVENT2("ui",
247 "ResourceManagerImpl::RequestCrushedSpriteResourceFromJava", 330 "ResourceManagerImpl::RequestCrushedSpriteResourceFromJava",
248 "bitmap_res_id", bitmap_res_id, 331 "bitmap_res_id", bitmap_res_id,
249 "metadata_res_id", metadata_res_id); 332 "metadata_res_id", metadata_res_id);
250 Java_ResourceManager_crushedSpriteResourceRequested( 333 Java_ResourceManager_crushedSpriteResourceRequested(
251 base::android::AttachCurrentThread(), java_obj_, bitmap_res_id, 334 base::android::AttachCurrentThread(), java_obj_, bitmap_res_id,
252 metadata_res_id, reloading); 335 metadata_res_id, reloading);
253 } 336 }
254 337
255 } // namespace ui 338 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698