| 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 <inttypes.h> | 7 #include <inttypes.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 } | 202 } |
| 203 | 203 |
| 204 void ResourceManagerImpl::RemoveResource( | 204 void ResourceManagerImpl::RemoveResource( |
| 205 JNIEnv* env, | 205 JNIEnv* env, |
| 206 const base::android::JavaRef<jobject>& jobj, | 206 const base::android::JavaRef<jobject>& jobj, |
| 207 jint res_type, | 207 jint res_type, |
| 208 jint res_id) { | 208 jint res_id) { |
| 209 resources_[res_type].erase(res_id); | 209 resources_[res_type].erase(res_id); |
| 210 } | 210 } |
| 211 | 211 |
| 212 CrushedSpriteResource* ResourceManagerImpl::GetCrushedSpriteResource( | |
| 213 int bitmap_res_id, int metadata_res_id) { | |
| 214 | |
| 215 CrushedSpriteResource* resource = nullptr; | |
| 216 if (crushed_sprite_resources_.find(bitmap_res_id) | |
| 217 != crushed_sprite_resources_.end()) { | |
| 218 resource = crushed_sprite_resources_[bitmap_res_id].get(); | |
| 219 } | |
| 220 | |
| 221 if (!resource) { | |
| 222 RequestCrushedSpriteResourceFromJava(bitmap_res_id, metadata_res_id, false); | |
| 223 resource = crushed_sprite_resources_[bitmap_res_id].get(); | |
| 224 } else if (resource->BitmapHasBeenEvictedFromMemory()) { | |
| 225 RequestCrushedSpriteResourceFromJava(bitmap_res_id, metadata_res_id, true); | |
| 226 } | |
| 227 | |
| 228 return resource; | |
| 229 } | |
| 230 | |
| 231 void ResourceManagerImpl::OnCrushedSpriteResourceReady( | |
| 232 JNIEnv* env, | |
| 233 const JavaRef<jobject>& jobj, | |
| 234 jint bitmap_res_id, | |
| 235 const JavaRef<jobject>& bitmap, | |
| 236 const JavaRef<jobjectArray>& frame_rects, | |
| 237 jint unscaled_sprite_width, | |
| 238 jint unscaled_sprite_height, | |
| 239 jfloat scaled_sprite_width, | |
| 240 jfloat scaled_sprite_height) { | |
| 241 // Construct source and destination rectangles for each frame from | |
| 242 // |frame_rects|. | |
| 243 std::vector<std::vector<int>> all_frame_rects_vector; | |
| 244 JavaArrayOfIntArrayToIntVector(env, frame_rects.obj(), | |
| 245 &all_frame_rects_vector); | |
| 246 CrushedSpriteResource::SrcDstRects src_dst_rects = | |
| 247 ProcessCrushedSpriteFrameRects(all_frame_rects_vector); | |
| 248 | |
| 249 SkBitmap skbitmap = | |
| 250 gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(bitmap)); | |
| 251 | |
| 252 std::unique_ptr<CrushedSpriteResource> resource = | |
| 253 base::MakeUnique<CrushedSpriteResource>( | |
| 254 skbitmap, | |
| 255 src_dst_rects, | |
| 256 gfx::Size(unscaled_sprite_width, unscaled_sprite_height), | |
| 257 gfx::Size(scaled_sprite_width, scaled_sprite_height)); | |
| 258 | |
| 259 crushed_sprite_resources_[bitmap_res_id].swap(resource); | |
| 260 } | |
| 261 | |
| 262 CrushedSpriteResource::SrcDstRects | |
| 263 ResourceManagerImpl::ProcessCrushedSpriteFrameRects( | |
| 264 std::vector<std::vector<int>> frame_rects_vector) { | |
| 265 CrushedSpriteResource::SrcDstRects src_dst_rects; | |
| 266 for (size_t i = 0; i < frame_rects_vector.size(); ++i) { | |
| 267 std::vector<int> frame_ints = frame_rects_vector[i]; | |
| 268 CrushedSpriteResource::FrameSrcDstRects frame_src_dst_rects; | |
| 269 | |
| 270 // Create source and destination gfx::Rect's for each rectangle in | |
| 271 // |frame_ints|. Each rectangle consists of 6 values: | |
| 272 // i: destination x i+1: destination y i+2: source x i+3: source y | |
| 273 // i+4: width i+5: height | |
| 274 for (size_t j = 0; j < frame_ints.size(); j += 6) { | |
| 275 gfx::Rect sprite_rect_destination(frame_ints[j], | |
| 276 frame_ints[j+1], | |
| 277 frame_ints[j+4], | |
| 278 frame_ints[j+5]); | |
| 279 gfx::Rect sprite_rect_source(frame_ints[j+2], | |
| 280 frame_ints[j+3], | |
| 281 frame_ints[j+4], | |
| 282 frame_ints[j+5]); | |
| 283 frame_src_dst_rects.push_back(std::pair<gfx::Rect, gfx::Rect>( | |
| 284 sprite_rect_source, sprite_rect_destination)); | |
| 285 } | |
| 286 src_dst_rects.push_back(frame_src_dst_rects); | |
| 287 } | |
| 288 return src_dst_rects; | |
| 289 } | |
| 290 | |
| 291 bool ResourceManagerImpl::OnMemoryDump( | 212 bool ResourceManagerImpl::OnMemoryDump( |
| 292 const base::trace_event::MemoryDumpArgs& args, | 213 const base::trace_event::MemoryDumpArgs& args, |
| 293 base::trace_event::ProcessMemoryDump* pmd) { | 214 base::trace_event::ProcessMemoryDump* pmd) { |
| 294 size_t memory_usage = | 215 size_t memory_usage = |
| 295 base::trace_event::EstimateMemoryUsage(resources_) + | 216 base::trace_event::EstimateMemoryUsage(resources_) + |
| 296 base::trace_event::EstimateMemoryUsage(crushed_sprite_resources_) + | |
| 297 base::trace_event::EstimateMemoryUsage(tinted_resources_); | 217 base::trace_event::EstimateMemoryUsage(tinted_resources_); |
| 298 | 218 |
| 299 base::trace_event::MemoryAllocatorDump* dump = pmd->CreateAllocatorDump( | 219 base::trace_event::MemoryAllocatorDump* dump = pmd->CreateAllocatorDump( |
| 300 base::StringPrintf("ui/resource_manager_0x%" PRIXPTR, | 220 base::StringPrintf("ui/resource_manager_0x%" PRIXPTR, |
| 301 reinterpret_cast<uintptr_t>(this))); | 221 reinterpret_cast<uintptr_t>(this))); |
| 302 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | 222 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 303 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | 223 base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 304 memory_usage); | 224 memory_usage); |
| 305 | 225 |
| 306 const char* system_allocator_name = | 226 const char* system_allocator_name = |
| 307 base::trace_event::MemoryDumpManager::GetInstance() | 227 base::trace_event::MemoryDumpManager::GetInstance() |
| 308 ->system_allocator_pool_name(); | 228 ->system_allocator_pool_name(); |
| 309 if (system_allocator_name) { | 229 if (system_allocator_name) { |
| 310 pmd->AddSuballocation(dump->guid(), system_allocator_name); | 230 pmd->AddSuballocation(dump->guid(), system_allocator_name); |
| 311 } | 231 } |
| 312 | 232 |
| 313 return true; | 233 return true; |
| 314 } | 234 } |
| 315 | 235 |
| 316 void ResourceManagerImpl::OnCrushedSpriteResourceReloaded( | |
| 317 JNIEnv* env, | |
| 318 const JavaRef<jobject>& jobj, | |
| 319 jint bitmap_res_id, | |
| 320 const JavaRef<jobject>& bitmap) { | |
| 321 std::unordered_map<int, std::unique_ptr<CrushedSpriteResource>>::iterator | |
| 322 item = crushed_sprite_resources_.find(bitmap_res_id); | |
| 323 if (item == crushed_sprite_resources_.end()) { | |
| 324 // Cannot reload a resource that has not been previously loaded. | |
| 325 return; | |
| 326 } | |
| 327 SkBitmap skbitmap = | |
| 328 gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(bitmap)); | |
| 329 item->second->SetBitmap(skbitmap); | |
| 330 } | |
| 331 | |
| 332 // static | 236 // static |
| 333 bool ResourceManagerImpl::RegisterResourceManager(JNIEnv* env) { | 237 bool ResourceManagerImpl::RegisterResourceManager(JNIEnv* env) { |
| 334 return RegisterNativesImpl(env); | 238 return RegisterNativesImpl(env); |
| 335 } | 239 } |
| 336 | 240 |
| 337 void ResourceManagerImpl::PreloadResourceFromJava(AndroidResourceType res_type, | 241 void ResourceManagerImpl::PreloadResourceFromJava(AndroidResourceType res_type, |
| 338 int res_id) { | 242 int res_id) { |
| 339 TRACE_EVENT2("ui", "ResourceManagerImpl::PreloadResourceFromJava", | 243 TRACE_EVENT2("ui", "ResourceManagerImpl::PreloadResourceFromJava", |
| 340 "resource_type", res_type, | 244 "resource_type", res_type, |
| 341 "resource_id", res_id); | 245 "resource_id", res_id); |
| 342 Java_ResourceManager_preloadResource(base::android::AttachCurrentThread(), | 246 Java_ResourceManager_preloadResource(base::android::AttachCurrentThread(), |
| 343 java_obj_, res_type, res_id); | 247 java_obj_, res_type, res_id); |
| 344 } | 248 } |
| 345 | 249 |
| 346 void ResourceManagerImpl::RequestResourceFromJava(AndroidResourceType res_type, | 250 void ResourceManagerImpl::RequestResourceFromJava(AndroidResourceType res_type, |
| 347 int res_id) { | 251 int res_id) { |
| 348 TRACE_EVENT2("ui", "ResourceManagerImpl::RequestResourceFromJava", | 252 TRACE_EVENT2("ui", "ResourceManagerImpl::RequestResourceFromJava", |
| 349 "resource_type", res_type, | 253 "resource_type", res_type, |
| 350 "resource_id", res_id); | 254 "resource_id", res_id); |
| 351 Java_ResourceManager_resourceRequested(base::android::AttachCurrentThread(), | 255 Java_ResourceManager_resourceRequested(base::android::AttachCurrentThread(), |
| 352 java_obj_, res_type, res_id); | 256 java_obj_, res_type, res_id); |
| 353 } | 257 } |
| 354 | 258 |
| 355 void ResourceManagerImpl::RequestCrushedSpriteResourceFromJava( | |
| 356 int bitmap_res_id, int metadata_res_id, bool reloading) { | |
| 357 TRACE_EVENT2("ui", | |
| 358 "ResourceManagerImpl::RequestCrushedSpriteResourceFromJava", | |
| 359 "bitmap_res_id", bitmap_res_id, | |
| 360 "metadata_res_id", metadata_res_id); | |
| 361 Java_ResourceManager_crushedSpriteResourceRequested( | |
| 362 base::android::AttachCurrentThread(), java_obj_, bitmap_res_id, | |
| 363 metadata_res_id, reloading); | |
| 364 } | |
| 365 | |
| 366 } // namespace ui | 259 } // namespace ui |
| OLD | NEW |