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 <utility> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/android/jni_array.h" |
7 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
8 #include "base/trace_event/trace_event.h" | 12 #include "base/trace_event/trace_event.h" |
9 #include "cc/resources/scoped_ui_resource.h" | 13 #include "cc/resources/scoped_ui_resource.h" |
10 #include "jni/ResourceManager_jni.h" | 14 #include "jni/ResourceManager_jni.h" |
11 #include "ui/android/resources/ui_resource_provider.h" | 15 #include "ui/android/resources/ui_resource_provider.h" |
12 #include "ui/gfx/android/java_bitmap.h" | 16 #include "ui/gfx/android/java_bitmap.h" |
| 17 #include "ui/gfx/geometry/rect.h" |
| 18 |
| 19 using base::android::JavaArrayOfIntArrayToIntVector; |
13 | 20 |
14 namespace ui { | 21 namespace ui { |
15 | 22 |
16 // static | 23 // static |
17 ResourceManagerImpl* ResourceManagerImpl::FromJavaObject(jobject jobj) { | 24 ResourceManagerImpl* ResourceManagerImpl::FromJavaObject(jobject jobj) { |
18 return reinterpret_cast<ResourceManagerImpl*>( | 25 return reinterpret_cast<ResourceManagerImpl*>( |
19 Java_ResourceManager_getNativePtr(base::android::AttachCurrentThread(), | 26 Java_ResourceManager_getNativePtr(base::android::AttachCurrentThread(), |
20 jobj)); | 27 jobj)); |
21 } | 28 } |
22 | 29 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 resource->aperture.SetRect(aperture_left, aperture_top, | 113 resource->aperture.SetRect(aperture_left, aperture_top, |
107 aperture_right - aperture_left, | 114 aperture_right - aperture_left, |
108 aperture_bottom - aperture_top); | 115 aperture_bottom - aperture_top); |
109 | 116 |
110 SkBitmap skbitmap = gfx::CreateSkBitmapFromJavaBitmap(jbitmap); | 117 SkBitmap skbitmap = gfx::CreateSkBitmapFromJavaBitmap(jbitmap); |
111 skbitmap.setImmutable(); | 118 skbitmap.setImmutable(); |
112 resource->ui_resource = | 119 resource->ui_resource = |
113 cc::ScopedUIResource::Create(host_, cc::UIResourceBitmap(skbitmap)); | 120 cc::ScopedUIResource::Create(host_, cc::UIResourceBitmap(skbitmap)); |
114 } | 121 } |
115 | 122 |
| 123 scoped_refptr<CrushedSpriteResource> |
| 124 ResourceManagerImpl::GetCrushedSpriteResource( |
| 125 int bitmap_res_id, int metadata_res_id) { |
| 126 CrushedSpriteResourceHolder* resource_holder = |
| 127 crushed_sprite_resources_.Lookup(bitmap_res_id); |
| 128 if (!resource_holder) { |
| 129 RequestCrushedSpriteResourceFromJava(bitmap_res_id, metadata_res_id); |
| 130 resource_holder = crushed_sprite_resources_.Lookup(bitmap_res_id); |
| 131 } |
| 132 |
| 133 return resource_holder->resource; |
| 134 } |
| 135 |
| 136 void ResourceManagerImpl::ReloadCrushedSpriteResource(int bitmap_res_id) { |
| 137 CrushedSpriteResourceHolder* resource_holder = |
| 138 crushed_sprite_resources_.Lookup(bitmap_res_id); |
| 139 if (!resource_holder) { |
| 140 // Cannot reload a resource that has not been previously loaded. |
| 141 return; |
| 142 } |
| 143 ReloadCrushedSpriteResourceFromJava(bitmap_res_id); |
| 144 } |
| 145 |
| 146 void ResourceManagerImpl::OnCrushedSpriteResourceReady( |
| 147 JNIEnv* env, |
| 148 jobject jobj, |
| 149 jint bitmap_res_id, |
| 150 jobject bitmap, |
| 151 jobjectArray frame_rects, |
| 152 jint sprite_width, |
| 153 jint sprite_height) { |
| 154 CrushedSpriteResourceHolder* resource_holder = |
| 155 crushed_sprite_resources_.Lookup(bitmap_res_id); |
| 156 if (!resource_holder) { |
| 157 resource_holder = new CrushedSpriteResourceHolder(); |
| 158 crushed_sprite_resources_.AddWithID(resource_holder, bitmap_res_id); |
| 159 } |
| 160 |
| 161 // Construct source and destination rectangles for each frame from |
| 162 // |frame_rects|. |
| 163 std::vector<std::vector<int>> all_frame_rects_vector; |
| 164 JavaArrayOfIntArrayToIntVector(env, frame_rects, &all_frame_rects_vector); |
| 165 CrushedSpriteResource::SrcDstRects src_dst_rects; |
| 166 |
| 167 for (size_t i = 0; i < all_frame_rects_vector.size(); ++i) { |
| 168 std::vector<int> frame_ints = all_frame_rects_vector[i]; |
| 169 CrushedSpriteResource::FrameSrcDstRects frame_src_dst_rects; |
| 170 |
| 171 // Create source and destination gfx::Rect's for each rectangle in |
| 172 // |frame_ints|. Each rectangle consists of 6 values: |
| 173 // i: destination x i+1: destination y i+2: source x i+3: source y |
| 174 // i+4: width i+5: height |
| 175 for (size_t j = 0; j < frame_ints.size(); j += 6) { |
| 176 gfx::Rect sprite_rect_destination(frame_ints[j], |
| 177 frame_ints[j+1], |
| 178 frame_ints[j+4], |
| 179 frame_ints[j+5]); |
| 180 gfx::Rect sprite_rect_source(frame_ints[j+2], |
| 181 frame_ints[j+3], |
| 182 frame_ints[j+4], |
| 183 frame_ints[j+5]); |
| 184 frame_src_dst_rects.push_back(std::pair<gfx::Rect, gfx::Rect>( |
| 185 sprite_rect_source, sprite_rect_destination)); |
| 186 } |
| 187 src_dst_rects.push_back(frame_src_dst_rects); |
| 188 } |
| 189 |
| 190 resource_holder->resource = CrushedSpriteResource::CreateFromJavaBitmap( |
| 191 bitmap_res_id, |
| 192 gfx::JavaBitmap(bitmap), |
| 193 src_dst_rects, |
| 194 gfx::Size(sprite_width, sprite_height)); |
| 195 } |
| 196 |
| 197 void ResourceManagerImpl::OnCrushedSpriteResourceReloaded(JNIEnv* env, |
| 198 jobject jobj, |
| 199 jint bitmap_res_id, |
| 200 jobject bitmap) { |
| 201 CrushedSpriteResourceHolder* resource_holder = |
| 202 crushed_sprite_resources_.Lookup(bitmap_res_id); |
| 203 if (!resource_holder) { |
| 204 // Cannot reload a resource that has not been previously loaded. |
| 205 return; |
| 206 } |
| 207 |
| 208 resource_holder->resource->SetBitmapFromJavaBitmap(gfx::JavaBitmap(bitmap)); |
| 209 } |
| 210 |
116 // static | 211 // static |
117 bool ResourceManagerImpl::RegisterResourceManager(JNIEnv* env) { | 212 bool ResourceManagerImpl::RegisterResourceManager(JNIEnv* env) { |
118 return RegisterNativesImpl(env); | 213 return RegisterNativesImpl(env); |
119 } | 214 } |
120 | 215 |
121 void ResourceManagerImpl::PreloadResourceFromJava(AndroidResourceType res_type, | 216 void ResourceManagerImpl::PreloadResourceFromJava(AndroidResourceType res_type, |
122 int res_id) { | 217 int res_id) { |
123 TRACE_EVENT2("ui", "ResourceManagerImpl::PreloadResourceFromJava", | 218 TRACE_EVENT2("ui", "ResourceManagerImpl::PreloadResourceFromJava", |
124 "resource_type", res_type, | 219 "resource_type", res_type, |
125 "resource_id", res_id); | 220 "resource_id", res_id); |
126 Java_ResourceManager_preloadResource(base::android::AttachCurrentThread(), | 221 Java_ResourceManager_preloadResource(base::android::AttachCurrentThread(), |
127 java_obj_.obj(), res_type, res_id); | 222 java_obj_.obj(), res_type, res_id); |
128 } | 223 } |
129 | 224 |
130 void ResourceManagerImpl::RequestResourceFromJava(AndroidResourceType res_type, | 225 void ResourceManagerImpl::RequestResourceFromJava(AndroidResourceType res_type, |
131 int res_id) { | 226 int res_id) { |
132 TRACE_EVENT2("ui", "ResourceManagerImpl::RequestResourceFromJava", | 227 TRACE_EVENT2("ui", "ResourceManagerImpl::RequestResourceFromJava", |
133 "resource_type", res_type, | 228 "resource_type", res_type, |
134 "resource_id", res_id); | 229 "resource_id", res_id); |
135 Java_ResourceManager_resourceRequested(base::android::AttachCurrentThread(), | 230 Java_ResourceManager_resourceRequested(base::android::AttachCurrentThread(), |
136 java_obj_.obj(), res_type, res_id); | 231 java_obj_.obj(), res_type, res_id); |
137 } | 232 } |
138 | 233 |
| 234 void ResourceManagerImpl::RequestCrushedSpriteResourceFromJava( |
| 235 int bitmap_res_id, int metadata_res_id) { |
| 236 TRACE_EVENT2("ui", |
| 237 "ResourceManagerImpl::RequestCrushedSpriteResourceFromJava", |
| 238 "bitmap_res_id", bitmap_res_id, |
| 239 "metadata_res_id", metadata_res_id); |
| 240 Java_ResourceManager_crushedSpriteResourceRequested( |
| 241 base::android::AttachCurrentThread(), java_obj_.obj(), |
| 242 bitmap_res_id, metadata_res_id); |
| 243 } |
| 244 |
| 245 void ResourceManagerImpl::ReloadCrushedSpriteResourceFromJava( |
| 246 int bitmap_res_id) { |
| 247 TRACE_EVENT1("ui", |
| 248 "ResourceManagerImpl::ReloadCrushedSpriteResourceFromJava", |
| 249 "bitmap_res_id", bitmap_res_id); |
| 250 Java_ResourceManager_reloadCrushedSpriteResource( |
| 251 base::android::AttachCurrentThread(), java_obj_.obj(), |
| 252 bitmap_res_id); |
| 253 } |
| 254 |
| 255 ResourceManagerImpl::CrushedSpriteResourceHolder::CrushedSpriteResourceHolder() |
| 256 { |
| 257 } |
| 258 |
| 259 ResourceManagerImpl::CrushedSpriteResourceHolder::~CrushedSpriteResourceHolder() |
| 260 { |
| 261 } |
| 262 |
139 } // namespace ui | 263 } // namespace ui |
OLD | NEW |