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> |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 59 ResourceManagerImpl::GetJavaObject() { | 59 ResourceManagerImpl::GetJavaObject() { |
| 60 return base::android::ScopedJavaLocalRef<jobject>(java_obj_); | 60 return base::android::ScopedJavaLocalRef<jobject>(java_obj_); |
| 61 } | 61 } |
| 62 | 62 |
| 63 ResourceManager::Resource* ResourceManagerImpl::GetResource( | 63 ResourceManager::Resource* ResourceManagerImpl::GetResource( |
| 64 AndroidResourceType res_type, | 64 AndroidResourceType res_type, |
| 65 int res_id) { | 65 int res_id) { |
| 66 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); | 66 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); |
| 67 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); | 67 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); |
| 68 | 68 |
| 69 Resource* resource = resources_[res_type].Lookup(res_id); | 69 Resource* resource = nullptr; |
| 70 if (resources_[res_type].find(res_id) != resources_[res_type].end()) | |
| 71 resource = resources_[res_type][res_id].get(); | |
| 70 | 72 |
| 71 if (!resource || res_type == ANDROID_RESOURCE_TYPE_DYNAMIC || | 73 if (!resource || res_type == ANDROID_RESOURCE_TYPE_DYNAMIC || |
| 72 res_type == ANDROID_RESOURCE_TYPE_DYNAMIC_BITMAP) { | 74 res_type == ANDROID_RESOURCE_TYPE_DYNAMIC_BITMAP) { |
| 73 RequestResourceFromJava(res_type, res_id); | 75 RequestResourceFromJava(res_type, res_id); |
| 74 resource = resources_[res_type].Lookup(res_id); | 76 resource = resources_[res_type][res_id].get(); |
| 75 } | 77 } |
| 76 | 78 |
| 77 return resource; | 79 return resource; |
| 78 } | 80 } |
| 79 | 81 |
| 80 void ResourceManagerImpl::RemoveUnusedTints( | 82 void ResourceManagerImpl::RemoveUnusedTints( |
| 81 const std::unordered_set<int>& used_tints) { | 83 const std::unordered_set<int>& used_tints) { |
| 82 // Iterate over the currently cached tints and remove ones that were not | 84 // Iterate over the currently cached tints and remove ones that were not |
| 83 // used as defined in |used_tints|. | 85 // used as defined in |used_tints|. |
| 84 for (auto it = tinted_resources_.cbegin(); it != tinted_resources_.cend();) { | 86 for (auto it = tinted_resources_.cbegin(); it != tinted_resources_.cend();) { |
| 85 if (used_tints.find(it->first) == used_tints.end()) { | 87 if (used_tints.find(it->first) == used_tints.end()) { |
| 86 it = tinted_resources_.erase(it); | 88 it = tinted_resources_.erase(it); |
| 87 } else { | 89 } else { |
| 88 ++it; | 90 ++it; |
| 89 } | 91 } |
| 90 } | 92 } |
| 91 } | 93 } |
| 92 | 94 |
| 93 ResourceManager::Resource* ResourceManagerImpl::GetStaticResourceWithTint( | 95 ResourceManager::Resource* ResourceManagerImpl::GetStaticResourceWithTint( |
| 94 int res_id, | 96 int res_id, |
| 95 int tint_color) { | 97 SkColor tint_color) { |
| 96 if (tinted_resources_.find(tint_color) == tinted_resources_.end()) { | 98 if (tinted_resources_.find(tint_color) == tinted_resources_.end()) { |
| 97 tinted_resources_[tint_color] = base::MakeUnique<ResourceMap>(); | 99 tinted_resources_[tint_color] = base::MakeUnique<ResourceMap>(); |
| 98 } | 100 } |
| 99 ResourceMap* resource_map = tinted_resources_[tint_color].get(); | 101 ResourceMap* resource_map = tinted_resources_[tint_color].get(); |
| 100 | 102 |
| 101 Resource* tinted_resource = resource_map->Lookup(res_id); | 103 // If the resource is already cached, use it. |
| 104 std::unordered_map<int, std::unique_ptr<Resource>>::iterator item = | |
| 105 resource_map->find(res_id); | |
| 106 if (item != resource_map->end()) | |
| 107 return item->second.get(); | |
| 102 | 108 |
| 103 // If the resource is already cached, use it. | 109 std::unique_ptr<Resource> tinted_resource = base::MakeUnique<Resource>(); |
| 104 if (tinted_resource) | |
| 105 return tinted_resource; | |
| 106 | |
| 107 tinted_resource = new Resource(); | |
| 108 | 110 |
| 109 ResourceManager::Resource* base_image = | 111 ResourceManager::Resource* base_image = |
| 110 GetResource(ANDROID_RESOURCE_TYPE_STATIC, res_id); | 112 GetResource(ANDROID_RESOURCE_TYPE_STATIC, res_id); |
| 111 DCHECK(base_image); | 113 DCHECK(base_image); |
| 112 | 114 |
| 113 TRACE_EVENT0("browser", "ResourceManagerImpl::GetStaticResourceWithTint"); | 115 TRACE_EVENT0("browser", "ResourceManagerImpl::GetStaticResourceWithTint"); |
| 114 SkBitmap tinted_bitmap; | 116 SkBitmap tinted_bitmap; |
| 115 tinted_bitmap.allocPixels(SkImageInfo::MakeN32Premul(base_image->size.width(), | 117 tinted_bitmap.allocPixels(SkImageInfo::MakeN32Premul(base_image->size.width(), |
| 116 base_image->size.height())); | 118 base_image->size.height())); |
| 117 | 119 |
| 118 SkCanvas canvas(tinted_bitmap); | 120 SkCanvas canvas(tinted_bitmap); |
| 119 canvas.clear(SK_ColorTRANSPARENT); | 121 canvas.clear(SK_ColorTRANSPARENT); |
| 120 | 122 |
| 121 // Build a color filter to use on the base resource. This filter multiplies | 123 // 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 | 124 // the RGB components by the components of the new color but retains the |
| 123 // alpha of the original image. | 125 // alpha of the original image. |
| 124 SkPaint color_filter; | 126 SkPaint color_filter; |
| 125 color_filter.setColorFilter( | 127 color_filter.setColorFilter( |
| 126 SkColorFilter::MakeModeFilter(tint_color, SkXfermode::kModulate_Mode)); | 128 SkColorFilter::MakeModeFilter(tint_color, SkXfermode::kModulate_Mode)); |
| 127 | 129 |
| 128 // Draw the resource and make it immutable. | 130 // Draw the resource and make it immutable. |
| 129 base_image->ui_resource->GetBitmap(base_image->ui_resource->id(), false) | 131 base_image->ui_resource->GetBitmap(base_image->ui_resource->id(), false) |
| 130 .DrawToCanvas(&canvas, &color_filter); | 132 .DrawToCanvas(&canvas, &color_filter); |
| 131 tinted_bitmap.setImmutable(); | 133 tinted_bitmap.setImmutable(); |
| 132 | 134 |
| 133 // Create a UI resource from the new bitmap. | 135 // 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->size = gfx::Size(base_image->size); |
| 136 tinted_resource->padding = gfx::Rect(base_image->padding); | 137 tinted_resource->padding = gfx::Rect(base_image->padding); |
| 137 tinted_resource->aperture = gfx::Rect(base_image->aperture); | 138 tinted_resource->aperture = gfx::Rect(base_image->aperture); |
| 138 tinted_resource->ui_resource = cc::ScopedUIResource::Create(host_, | 139 tinted_resource->ui_resource = cc::ScopedUIResource::Create(host_, |
| 139 cc::UIResourceBitmap(tinted_bitmap)); | 140 cc::UIResourceBitmap(tinted_bitmap)); |
| 140 | 141 |
| 141 resource_map->AddWithID(tinted_resource, res_id); | 142 (*resource_map)[res_id].swap(tinted_resource); |
| 142 | 143 |
| 143 return tinted_resource; | 144 return (*resource_map)[res_id].get(); |
| 144 } | 145 } |
| 145 | 146 |
| 146 void ResourceManagerImpl::ClearTintedResourceCache(JNIEnv* env, | 147 void ResourceManagerImpl::ClearTintedResourceCache(JNIEnv* env, |
| 147 const JavaRef<jobject>& jobj) { | 148 const JavaRef<jobject>& jobj) { |
| 148 tinted_resources_.clear(); | 149 tinted_resources_.clear(); |
| 149 } | 150 } |
| 150 | 151 |
| 151 void ResourceManagerImpl::PreloadResource(AndroidResourceType res_type, | 152 void ResourceManagerImpl::PreloadResource(AndroidResourceType res_type, |
| 152 int res_id) { | 153 int res_id) { |
| 153 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); | 154 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); |
| 154 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); | 155 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); |
| 155 | 156 |
| 156 // Don't send out a query if the resource is already loaded. | 157 // Don't send out a query if the resource is already loaded. |
| 157 if (resources_[res_type].Lookup(res_id)) | 158 if (resources_[res_type].find(res_id) != resources_[res_type].end()) |
| 158 return; | 159 return; |
| 159 | 160 |
| 160 PreloadResourceFromJava(res_type, res_id); | 161 PreloadResourceFromJava(res_type, res_id); |
| 161 } | 162 } |
| 162 | 163 |
| 163 void ResourceManagerImpl::OnResourceReady(JNIEnv* env, | 164 void ResourceManagerImpl::OnResourceReady(JNIEnv* env, |
| 164 const JavaRef<jobject>& jobj, | 165 const JavaRef<jobject>& jobj, |
| 165 jint res_type, | 166 jint res_type, |
| 166 jint res_id, | 167 jint res_id, |
| 167 const JavaRef<jobject>& bitmap, | 168 const JavaRef<jobject>& bitmap, |
| 168 jint padding_left, | 169 jint padding_left, |
| 169 jint padding_top, | 170 jint padding_top, |
| 170 jint padding_right, | 171 jint padding_right, |
| 171 jint padding_bottom, | 172 jint padding_bottom, |
| 172 jint aperture_left, | 173 jint aperture_left, |
| 173 jint aperture_top, | 174 jint aperture_top, |
| 174 jint aperture_right, | 175 jint aperture_right, |
| 175 jint aperture_bottom) { | 176 jint aperture_bottom) { |
| 176 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); | 177 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); |
| 177 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); | 178 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); |
| 178 TRACE_EVENT2("ui", "ResourceManagerImpl::OnResourceReady", | 179 TRACE_EVENT2("ui", "ResourceManagerImpl::OnResourceReady", |
| 179 "resource_type", res_type, | 180 "resource_type", res_type, |
| 180 "resource_id", res_id); | 181 "resource_id", res_id); |
| 181 | 182 |
| 182 Resource* resource = resources_[res_type].Lookup(res_id); | 183 std::unordered_map<int, std::unique_ptr<Resource>>::iterator item = |
| 183 if (!resource) { | 184 resources_[res_type].find(res_id); |
| 184 resource = new Resource(); | 185 if (item == resources_[res_type].end()) { |
| 185 resources_[res_type].AddWithID(resource, res_id); | 186 resources_[res_type][res_id] = base::MakeUnique<Resource>(); |
|
mdjones
2016/09/12 16:55:09
I think you wanted my to use "item" here to reset
| |
| 186 } | 187 } |
| 187 | 188 |
| 189 Resource* resource = resources_[res_type][res_id].get(); | |
| 190 | |
| 188 gfx::JavaBitmap jbitmap(bitmap.obj()); | 191 gfx::JavaBitmap jbitmap(bitmap.obj()); |
| 189 resource->size = jbitmap.size(); | 192 resource->size = jbitmap.size(); |
| 190 resource->padding.SetRect(padding_left, padding_top, | 193 resource->padding.SetRect(padding_left, padding_top, |
| 191 padding_right - padding_left, | 194 padding_right - padding_left, |
| 192 padding_bottom - padding_top); | 195 padding_bottom - padding_top); |
| 193 resource->aperture.SetRect(aperture_left, aperture_top, | 196 resource->aperture.SetRect(aperture_left, aperture_top, |
| 194 aperture_right - aperture_left, | 197 aperture_right - aperture_left, |
| 195 aperture_bottom - aperture_top); | 198 aperture_bottom - aperture_top); |
| 196 | 199 |
| 197 SkBitmap skbitmap = gfx::CreateSkBitmapFromJavaBitmap(jbitmap); | 200 SkBitmap skbitmap = gfx::CreateSkBitmapFromJavaBitmap(jbitmap); |
| 198 skbitmap.setImmutable(); | 201 skbitmap.setImmutable(); |
| 199 resource->ui_resource = | 202 resource->ui_resource = |
| 200 cc::ScopedUIResource::Create(host_, cc::UIResourceBitmap(skbitmap)); | 203 cc::ScopedUIResource::Create(host_, cc::UIResourceBitmap(skbitmap)); |
| 201 } | 204 } |
| 202 | 205 |
| 203 CrushedSpriteResource* ResourceManagerImpl::GetCrushedSpriteResource( | 206 CrushedSpriteResource* ResourceManagerImpl::GetCrushedSpriteResource( |
| 204 int bitmap_res_id, int metadata_res_id) { | 207 int bitmap_res_id, int metadata_res_id) { |
| 205 CrushedSpriteResource* resource = | 208 |
| 206 crushed_sprite_resources_.Lookup(bitmap_res_id); | 209 CrushedSpriteResource* resource = nullptr; |
| 210 if (crushed_sprite_resources_.find(bitmap_res_id) | |
| 211 != crushed_sprite_resources_.end()) { | |
| 212 resource = crushed_sprite_resources_[bitmap_res_id].get(); | |
| 213 } | |
| 214 | |
| 207 if (!resource) { | 215 if (!resource) { |
| 208 RequestCrushedSpriteResourceFromJava(bitmap_res_id, metadata_res_id, false); | 216 RequestCrushedSpriteResourceFromJava(bitmap_res_id, metadata_res_id, false); |
| 209 resource = crushed_sprite_resources_.Lookup(bitmap_res_id); | 217 resource = crushed_sprite_resources_[bitmap_res_id].get(); |
| 210 } else if (resource->BitmapHasBeenEvictedFromMemory()) { | 218 } else if (resource->BitmapHasBeenEvictedFromMemory()) { |
| 211 RequestCrushedSpriteResourceFromJava(bitmap_res_id, metadata_res_id, true); | 219 RequestCrushedSpriteResourceFromJava(bitmap_res_id, metadata_res_id, true); |
| 212 } | 220 } |
| 213 | 221 |
| 214 return resource; | 222 return resource; |
| 215 } | 223 } |
| 216 | 224 |
| 217 void ResourceManagerImpl::OnCrushedSpriteResourceReady( | 225 void ResourceManagerImpl::OnCrushedSpriteResourceReady( |
| 218 JNIEnv* env, | 226 JNIEnv* env, |
| 219 const JavaRef<jobject>& jobj, | 227 const JavaRef<jobject>& jobj, |
| 220 jint bitmap_res_id, | 228 jint bitmap_res_id, |
| 221 const JavaRef<jobject>& bitmap, | 229 const JavaRef<jobject>& bitmap, |
| 222 const JavaRef<jobjectArray>& frame_rects, | 230 const JavaRef<jobjectArray>& frame_rects, |
| 223 jint unscaled_sprite_width, | 231 jint unscaled_sprite_width, |
| 224 jint unscaled_sprite_height, | 232 jint unscaled_sprite_height, |
| 225 jfloat scaled_sprite_width, | 233 jfloat scaled_sprite_width, |
| 226 jfloat scaled_sprite_height) { | 234 jfloat scaled_sprite_height) { |
| 227 // Construct source and destination rectangles for each frame from | 235 // Construct source and destination rectangles for each frame from |
| 228 // |frame_rects|. | 236 // |frame_rects|. |
| 229 std::vector<std::vector<int>> all_frame_rects_vector; | 237 std::vector<std::vector<int>> all_frame_rects_vector; |
| 230 JavaArrayOfIntArrayToIntVector(env, frame_rects.obj(), | 238 JavaArrayOfIntArrayToIntVector(env, frame_rects.obj(), |
| 231 &all_frame_rects_vector); | 239 &all_frame_rects_vector); |
| 232 CrushedSpriteResource::SrcDstRects src_dst_rects = | 240 CrushedSpriteResource::SrcDstRects src_dst_rects = |
| 233 ProcessCrushedSpriteFrameRects(all_frame_rects_vector); | 241 ProcessCrushedSpriteFrameRects(all_frame_rects_vector); |
| 234 | 242 |
| 235 SkBitmap skbitmap = | 243 SkBitmap skbitmap = |
| 236 gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(bitmap.obj())); | 244 gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(bitmap.obj())); |
| 237 | 245 |
| 238 CrushedSpriteResource* resource = new CrushedSpriteResource( | 246 std::unique_ptr<CrushedSpriteResource> resource = |
| 239 skbitmap, | 247 base::MakeUnique<CrushedSpriteResource>( |
| 240 src_dst_rects, | 248 skbitmap, |
| 241 gfx::Size(unscaled_sprite_width, unscaled_sprite_height), | 249 src_dst_rects, |
| 242 gfx::Size(scaled_sprite_width, scaled_sprite_height)); | 250 gfx::Size(unscaled_sprite_width, unscaled_sprite_height), |
| 251 gfx::Size(scaled_sprite_width, scaled_sprite_height)); | |
| 243 | 252 |
| 244 if (crushed_sprite_resources_.Lookup(bitmap_res_id)) { | 253 crushed_sprite_resources_[bitmap_res_id].swap(resource); |
| 245 crushed_sprite_resources_.Replace(bitmap_res_id, resource); | |
| 246 } else { | |
| 247 crushed_sprite_resources_.AddWithID(resource, bitmap_res_id); | |
| 248 } | |
| 249 } | 254 } |
| 250 | 255 |
| 251 CrushedSpriteResource::SrcDstRects | 256 CrushedSpriteResource::SrcDstRects |
| 252 ResourceManagerImpl::ProcessCrushedSpriteFrameRects( | 257 ResourceManagerImpl::ProcessCrushedSpriteFrameRects( |
| 253 std::vector<std::vector<int>> frame_rects_vector) { | 258 std::vector<std::vector<int>> frame_rects_vector) { |
| 254 CrushedSpriteResource::SrcDstRects src_dst_rects; | 259 CrushedSpriteResource::SrcDstRects src_dst_rects; |
| 255 for (size_t i = 0; i < frame_rects_vector.size(); ++i) { | 260 for (size_t i = 0; i < frame_rects_vector.size(); ++i) { |
| 256 std::vector<int> frame_ints = frame_rects_vector[i]; | 261 std::vector<int> frame_ints = frame_rects_vector[i]; |
| 257 CrushedSpriteResource::FrameSrcDstRects frame_src_dst_rects; | 262 CrushedSpriteResource::FrameSrcDstRects frame_src_dst_rects; |
| 258 | 263 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 275 src_dst_rects.push_back(frame_src_dst_rects); | 280 src_dst_rects.push_back(frame_src_dst_rects); |
| 276 } | 281 } |
| 277 return src_dst_rects; | 282 return src_dst_rects; |
| 278 } | 283 } |
| 279 | 284 |
| 280 void ResourceManagerImpl::OnCrushedSpriteResourceReloaded( | 285 void ResourceManagerImpl::OnCrushedSpriteResourceReloaded( |
| 281 JNIEnv* env, | 286 JNIEnv* env, |
| 282 const JavaRef<jobject>& jobj, | 287 const JavaRef<jobject>& jobj, |
| 283 jint bitmap_res_id, | 288 jint bitmap_res_id, |
| 284 const JavaRef<jobject>& bitmap) { | 289 const JavaRef<jobject>& bitmap) { |
| 285 CrushedSpriteResource* resource = | 290 std::unordered_map<int, std::unique_ptr<CrushedSpriteResource>>::iterator |
| 286 crushed_sprite_resources_.Lookup(bitmap_res_id); | 291 item = crushed_sprite_resources_.find(bitmap_res_id); |
| 287 if (!resource) { | 292 if (item == crushed_sprite_resources_.end()) { |
| 288 // Cannot reload a resource that has not been previously loaded. | 293 // Cannot reload a resource that has not been previously loaded. |
| 289 return; | 294 return; |
| 290 } | 295 } |
| 291 SkBitmap skbitmap = | 296 SkBitmap skbitmap = |
| 292 gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(bitmap.obj())); | 297 gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(bitmap.obj())); |
| 293 resource->SetBitmap(skbitmap); | 298 item->second->SetBitmap(skbitmap); |
| 294 } | 299 } |
| 295 | 300 |
| 296 // static | 301 // static |
| 297 bool ResourceManagerImpl::RegisterResourceManager(JNIEnv* env) { | 302 bool ResourceManagerImpl::RegisterResourceManager(JNIEnv* env) { |
| 298 return RegisterNativesImpl(env); | 303 return RegisterNativesImpl(env); |
| 299 } | 304 } |
| 300 | 305 |
| 301 void ResourceManagerImpl::PreloadResourceFromJava(AndroidResourceType res_type, | 306 void ResourceManagerImpl::PreloadResourceFromJava(AndroidResourceType res_type, |
| 302 int res_id) { | 307 int res_id) { |
| 303 TRACE_EVENT2("ui", "ResourceManagerImpl::PreloadResourceFromJava", | 308 TRACE_EVENT2("ui", "ResourceManagerImpl::PreloadResourceFromJava", |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 321 TRACE_EVENT2("ui", | 326 TRACE_EVENT2("ui", |
| 322 "ResourceManagerImpl::RequestCrushedSpriteResourceFromJava", | 327 "ResourceManagerImpl::RequestCrushedSpriteResourceFromJava", |
| 323 "bitmap_res_id", bitmap_res_id, | 328 "bitmap_res_id", bitmap_res_id, |
| 324 "metadata_res_id", metadata_res_id); | 329 "metadata_res_id", metadata_res_id); |
| 325 Java_ResourceManager_crushedSpriteResourceRequested( | 330 Java_ResourceManager_crushedSpriteResourceRequested( |
| 326 base::android::AttachCurrentThread(), java_obj_, bitmap_res_id, | 331 base::android::AttachCurrentThread(), java_obj_, bitmap_res_id, |
| 327 metadata_res_id, reloading); | 332 metadata_res_id, reloading); |
| 328 } | 333 } |
| 329 | 334 |
| 330 } // namespace ui | 335 } // namespace ui |
| OLD | NEW |