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 CrushedSpriteResource* ResourceManagerImpl::GetCrushedSpriteResource( |
| 124 int bitmap_res_id, int metadata_res_id) { |
| 125 CrushedSpriteResource* resource = |
| 126 crushed_sprite_resources_.Lookup(bitmap_res_id); |
| 127 if (!resource) { |
| 128 RequestCrushedSpriteResourceFromJava(bitmap_res_id, metadata_res_id, false); |
| 129 resource = crushed_sprite_resources_.Lookup(bitmap_res_id); |
| 130 } else if (resource->BitmapHasBeenEvictedFromMemory()) { |
| 131 RequestCrushedSpriteResourceFromJava(bitmap_res_id, metadata_res_id, true); |
| 132 } |
| 133 |
| 134 return resource; |
| 135 } |
| 136 |
| 137 void ResourceManagerImpl::OnCrushedSpriteResourceReady( |
| 138 JNIEnv* env, |
| 139 jobject jobj, |
| 140 jint bitmap_res_id, |
| 141 jobject bitmap, |
| 142 jobjectArray frame_rects, |
| 143 jint sprite_width, |
| 144 jint sprite_height) { |
| 145 |
| 146 // Construct source and destination rectangles for each frame from |
| 147 // |frame_rects|. |
| 148 std::vector<std::vector<int>> all_frame_rects_vector; |
| 149 JavaArrayOfIntArrayToIntVector(env, frame_rects, &all_frame_rects_vector); |
| 150 CrushedSpriteResource::SrcDstRects src_dst_rects = |
| 151 ProcessCrushedSpriteFrameRects(all_frame_rects_vector); |
| 152 |
| 153 SkBitmap skbitmap = |
| 154 gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(bitmap)); |
| 155 |
| 156 CrushedSpriteResource* resource = new CrushedSpriteResource( |
| 157 skbitmap, |
| 158 src_dst_rects, |
| 159 gfx::Size(sprite_width, sprite_height)); |
| 160 |
| 161 if (crushed_sprite_resources_.Lookup(bitmap_res_id)) { |
| 162 crushed_sprite_resources_.Replace(bitmap_res_id, resource); |
| 163 } else { |
| 164 crushed_sprite_resources_.AddWithID(resource, bitmap_res_id); |
| 165 } |
| 166 } |
| 167 |
| 168 CrushedSpriteResource::SrcDstRects |
| 169 ResourceManagerImpl::ProcessCrushedSpriteFrameRects( |
| 170 std::vector<std::vector<int>> frame_rects_vector) { |
| 171 CrushedSpriteResource::SrcDstRects src_dst_rects; |
| 172 for (size_t i = 0; i < frame_rects_vector.size(); ++i) { |
| 173 std::vector<int> frame_ints = frame_rects_vector[i]; |
| 174 CrushedSpriteResource::FrameSrcDstRects frame_src_dst_rects; |
| 175 |
| 176 // Create source and destination gfx::Rect's for each rectangle in |
| 177 // |frame_ints|. Each rectangle consists of 6 values: |
| 178 // i: destination x i+1: destination y i+2: source x i+3: source y |
| 179 // i+4: width i+5: height |
| 180 for (size_t j = 0; j < frame_ints.size(); j += 6) { |
| 181 gfx::Rect sprite_rect_destination(frame_ints[j], |
| 182 frame_ints[j+1], |
| 183 frame_ints[j+4], |
| 184 frame_ints[j+5]); |
| 185 gfx::Rect sprite_rect_source(frame_ints[j+2], |
| 186 frame_ints[j+3], |
| 187 frame_ints[j+4], |
| 188 frame_ints[j+5]); |
| 189 frame_src_dst_rects.push_back(std::pair<gfx::Rect, gfx::Rect>( |
| 190 sprite_rect_source, sprite_rect_destination)); |
| 191 } |
| 192 src_dst_rects.push_back(frame_src_dst_rects); |
| 193 } |
| 194 return src_dst_rects; |
| 195 } |
| 196 |
| 197 void ResourceManagerImpl::OnCrushedSpriteResourceReloaded( |
| 198 JNIEnv* env, |
| 199 jobject jobj, |
| 200 jint bitmap_res_id, |
| 201 jobject bitmap) { |
| 202 CrushedSpriteResource* resource = |
| 203 crushed_sprite_resources_.Lookup(bitmap_res_id); |
| 204 if (!resource) { |
| 205 // Cannot reload a resource that has not been previously loaded. |
| 206 return; |
| 207 } |
| 208 SkBitmap skbitmap = |
| 209 gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(bitmap)); |
| 210 resource->SetBitmap(skbitmap); |
| 211 } |
| 212 |
116 // static | 213 // static |
117 bool ResourceManagerImpl::RegisterResourceManager(JNIEnv* env) { | 214 bool ResourceManagerImpl::RegisterResourceManager(JNIEnv* env) { |
118 return RegisterNativesImpl(env); | 215 return RegisterNativesImpl(env); |
119 } | 216 } |
120 | 217 |
121 void ResourceManagerImpl::PreloadResourceFromJava(AndroidResourceType res_type, | 218 void ResourceManagerImpl::PreloadResourceFromJava(AndroidResourceType res_type, |
122 int res_id) { | 219 int res_id) { |
123 TRACE_EVENT2("ui", "ResourceManagerImpl::PreloadResourceFromJava", | 220 TRACE_EVENT2("ui", "ResourceManagerImpl::PreloadResourceFromJava", |
124 "resource_type", res_type, | 221 "resource_type", res_type, |
125 "resource_id", res_id); | 222 "resource_id", res_id); |
126 Java_ResourceManager_preloadResource(base::android::AttachCurrentThread(), | 223 Java_ResourceManager_preloadResource(base::android::AttachCurrentThread(), |
127 java_obj_.obj(), res_type, res_id); | 224 java_obj_.obj(), res_type, res_id); |
128 } | 225 } |
129 | 226 |
130 void ResourceManagerImpl::RequestResourceFromJava(AndroidResourceType res_type, | 227 void ResourceManagerImpl::RequestResourceFromJava(AndroidResourceType res_type, |
131 int res_id) { | 228 int res_id) { |
132 TRACE_EVENT2("ui", "ResourceManagerImpl::RequestResourceFromJava", | 229 TRACE_EVENT2("ui", "ResourceManagerImpl::RequestResourceFromJava", |
133 "resource_type", res_type, | 230 "resource_type", res_type, |
134 "resource_id", res_id); | 231 "resource_id", res_id); |
135 Java_ResourceManager_resourceRequested(base::android::AttachCurrentThread(), | 232 Java_ResourceManager_resourceRequested(base::android::AttachCurrentThread(), |
136 java_obj_.obj(), res_type, res_id); | 233 java_obj_.obj(), res_type, res_id); |
137 } | 234 } |
138 | 235 |
| 236 void ResourceManagerImpl::RequestCrushedSpriteResourceFromJava( |
| 237 int bitmap_res_id, int metadata_res_id, bool reloading) { |
| 238 TRACE_EVENT2("ui", |
| 239 "ResourceManagerImpl::RequestCrushedSpriteResourceFromJava", |
| 240 "bitmap_res_id", bitmap_res_id, |
| 241 "metadata_res_id", metadata_res_id); |
| 242 Java_ResourceManager_crushedSpriteResourceRequested( |
| 243 base::android::AttachCurrentThread(), java_obj_.obj(), |
| 244 bitmap_res_id, metadata_res_id, reloading); |
| 245 } |
| 246 |
139 } // namespace ui | 247 } // namespace ui |
OLD | NEW |