Index: ui/android/resources/resource_manager_impl.cc |
diff --git a/ui/android/resources/resource_manager_impl.cc b/ui/android/resources/resource_manager_impl.cc |
index 37f9aeaedb87a1fddb484ccbb20169d576d81d8b..0220773c24895b2281a7c00a17dc8f4963606fbc 100644 |
--- a/ui/android/resources/resource_manager_impl.cc |
+++ b/ui/android/resources/resource_manager_impl.cc |
@@ -4,12 +4,19 @@ |
#include "ui/android/resources/resource_manager_impl.h" |
+#include <utility> |
+#include <vector> |
+ |
+#include "base/android/jni_array.h" |
#include "base/android/jni_string.h" |
#include "base/trace_event/trace_event.h" |
#include "cc/resources/scoped_ui_resource.h" |
#include "jni/ResourceManager_jni.h" |
#include "ui/android/resources/ui_resource_provider.h" |
#include "ui/gfx/android/java_bitmap.h" |
+#include "ui/gfx/geometry/rect.h" |
+ |
+using base::android::JavaArrayOfIntArrayToIntVector; |
namespace ui { |
@@ -113,6 +120,94 @@ void ResourceManagerImpl::OnResourceReady(JNIEnv* env, |
cc::ScopedUIResource::Create(host_, cc::UIResourceBitmap(skbitmap)); |
} |
+scoped_refptr<CrushedSpriteResource> |
+ResourceManagerImpl::GetCrushedSpriteResource( |
+ int bitmap_res_id, int metadata_res_id) { |
+ CrushedSpriteResourceHolder* resource_holder = |
+ crushed_sprite_resources_.Lookup(bitmap_res_id); |
+ if (!resource_holder) { |
+ RequestCrushedSpriteResourceFromJava(bitmap_res_id, metadata_res_id); |
+ resource_holder = crushed_sprite_resources_.Lookup(bitmap_res_id); |
+ } |
+ |
+ return resource_holder->resource; |
+} |
+ |
+void ResourceManagerImpl::ReloadCrushedSpriteResource(int bitmap_res_id) { |
+ CrushedSpriteResourceHolder* resource_holder = |
+ crushed_sprite_resources_.Lookup(bitmap_res_id); |
+ if (!resource_holder) { |
+ // Cannot reload a resource that has not been previously loaded. |
+ return; |
+ } |
+ ReloadCrushedSpriteResourceFromJava(bitmap_res_id); |
+} |
+ |
+void ResourceManagerImpl::OnCrushedSpriteResourceReady( |
+ JNIEnv* env, |
+ jobject jobj, |
+ jint bitmap_res_id, |
+ jobject bitmap, |
+ jobjectArray frame_rects, |
+ jint sprite_size) { |
+ CrushedSpriteResourceHolder* resource_holder = |
+ crushed_sprite_resources_.Lookup(bitmap_res_id); |
+ if (!resource_holder) { |
+ resource_holder = new CrushedSpriteResourceHolder(); |
+ crushed_sprite_resources_.AddWithID(resource_holder, bitmap_res_id); |
+ } |
+ |
+ // Construct source and destination rectangles for each frame from |
+ // |frame_rects|. |
+ std::vector<std::vector<int>> all_frame_rects_vector; |
+ JavaArrayOfIntArrayToIntVector(env, frame_rects, &all_frame_rects_vector); |
+ CrushedSpriteResource::SrcDstRects src_dst_rects; |
+ |
+ for (size_t i = 0; i < all_frame_rects_vector.size(); ++i) { |
+ std::vector<int> frame_ints = all_frame_rects_vector[i]; |
+ CrushedSpriteResource::FrameSrcDstRects frame_src_dst_rects; |
+ |
+ // Create source and destination gfx::Rect's for each rectangle in |
+ // |frame_ints|. Each rectangle consists of 6 values: |
+ // i: destination x i+1: destination y i+2: source x i+3: source y |
+ // i+4: width i+5: height |
+ for (size_t j = 0; j < frame_ints.size();) { |
David Trainor- moved to gerrit
2015/10/15 21:04:58
; j += 6 here instead?
Theresa
2015/10/24 00:06:46
Done. Yep, the way I had it is just silly.
|
+ gfx::Rect sprite_rect_destination(frame_ints[j], |
+ frame_ints[j+1], |
+ frame_ints[j+4], |
+ frame_ints[j+5]); |
+ gfx::Rect sprite_rect_source(frame_ints[j+2], |
+ frame_ints[j+3], |
+ frame_ints[j+4], |
+ frame_ints[j+5]); |
+ frame_src_dst_rects.push_back(std::pair<gfx::Rect, gfx::Rect>( |
+ sprite_rect_source, sprite_rect_destination)); |
+ j += 6; |
+ } |
+ src_dst_rects.push_back(frame_src_dst_rects); |
+ } |
+ |
+ resource_holder->resource = CrushedSpriteResource::CreateFromJavaBitmap( |
+ bitmap_res_id, |
+ gfx::JavaBitmap(bitmap), |
+ src_dst_rects, |
+ sprite_size); |
+} |
+ |
+void ResourceManagerImpl::OnCrushedSpriteResourceReloaded(JNIEnv* env, |
+ jobject jobj, |
+ jint bitmap_res_id, |
+ jobject bitmap) { |
+ CrushedSpriteResourceHolder* resource_holder = |
+ crushed_sprite_resources_.Lookup(bitmap_res_id); |
+ if (!resource_holder) { |
+ // Cannot reload a resource that has not been previously loaded. |
+ return; |
+ } |
+ |
+ resource_holder->resource->SetBitmapFromJavaBitmap(gfx::JavaBitmap(bitmap)); |
+} |
+ |
// static |
bool ResourceManagerImpl::RegisterResourceManager(JNIEnv* env) { |
return RegisterNativesImpl(env); |
@@ -136,4 +231,33 @@ void ResourceManagerImpl::RequestResourceFromJava(AndroidResourceType res_type, |
java_obj_.obj(), res_type, res_id); |
} |
+void ResourceManagerImpl::RequestCrushedSpriteResourceFromJava( |
+ int bitmap_res_id, int metadata_res_id) { |
+ TRACE_EVENT2("ui", |
+ "ResourceManagerImpl::RequestCrushedSpriteResourceFromJava", |
+ "bitmap_res_id", bitmap_res_id, |
+ "metadata_res_id", metadata_res_id); |
+ Java_ResourceManager_crushedSpriteResourceRequested( |
+ base::android::AttachCurrentThread(), java_obj_.obj(), |
+ bitmap_res_id, metadata_res_id); |
+} |
+ |
+void ResourceManagerImpl::ReloadCrushedSpriteResourceFromJava( |
+ int bitmap_res_id) { |
+ TRACE_EVENT1("ui", |
+ "ResourceManagerImpl::ReloadCrushedSpriteResourceFromJava", |
+ "bitmap_res_id", bitmap_res_id); |
+ Java_ResourceManager_reloadCrushedSpriteResource( |
+ base::android::AttachCurrentThread(), java_obj_.obj(), |
+ bitmap_res_id); |
+} |
+ |
+ResourceManagerImpl::CrushedSpriteResourceHolder::CrushedSpriteResourceHolder() |
+{ |
+} |
+ |
+ResourceManagerImpl::CrushedSpriteResourceHolder::~CrushedSpriteResourceHolder() |
+{ |
+} |
+ |
} // namespace ui |