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_size) { | |
153 CrushedSpriteResourceHolder* resource_holder = | |
154 crushed_sprite_resources_.Lookup(bitmap_res_id); | |
155 if (!resource_holder) { | |
156 resource_holder = new CrushedSpriteResourceHolder(); | |
157 crushed_sprite_resources_.AddWithID(resource_holder, bitmap_res_id); | |
158 } | |
159 | |
160 // Construct source and destination rectangles for each frame from | |
161 // |frame_rects|. | |
162 std::vector<std::vector<int>> all_frame_rects_vector; | |
163 JavaArrayOfIntArrayToIntVector(env, frame_rects, &all_frame_rects_vector); | |
164 CrushedSpriteResource::SrcDstRects src_dst_rects; | |
165 | |
166 for (size_t i = 0; i < all_frame_rects_vector.size(); ++i) { | |
167 std::vector<int> frame_ints = all_frame_rects_vector[i]; | |
168 CrushedSpriteResource::FrameSrcDstRects frame_src_dst_rects; | |
169 | |
170 // Create source and destination gfx::Rect's for each rectangle in | |
171 // |frame_ints|. Each rectangle consists of 6 values: | |
172 // i: destination x i+1: destination y i+2: source x i+3: source y | |
173 // i+4: width i+5: height | |
174 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.
| |
175 gfx::Rect sprite_rect_destination(frame_ints[j], | |
176 frame_ints[j+1], | |
177 frame_ints[j+4], | |
178 frame_ints[j+5]); | |
179 gfx::Rect sprite_rect_source(frame_ints[j+2], | |
180 frame_ints[j+3], | |
181 frame_ints[j+4], | |
182 frame_ints[j+5]); | |
183 frame_src_dst_rects.push_back(std::pair<gfx::Rect, gfx::Rect>( | |
184 sprite_rect_source, sprite_rect_destination)); | |
185 j += 6; | |
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 sprite_size); | |
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 |