| 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 13 matching lines...) Expand all Loading... |
| 24 #include "jni/ResourceManager_jni.h" | 24 #include "jni/ResourceManager_jni.h" |
| 25 #include "third_party/skia/include/core/SkBitmap.h" | 25 #include "third_party/skia/include/core/SkBitmap.h" |
| 26 #include "third_party/skia/include/core/SkCanvas.h" | 26 #include "third_party/skia/include/core/SkCanvas.h" |
| 27 #include "third_party/skia/include/core/SkColorFilter.h" | 27 #include "third_party/skia/include/core/SkColorFilter.h" |
| 28 #include "ui/android/resources/ui_resource_provider.h" | 28 #include "ui/android/resources/ui_resource_provider.h" |
| 29 #include "ui/android/window_android.h" | 29 #include "ui/android/window_android.h" |
| 30 #include "ui/gfx/android/java_bitmap.h" | 30 #include "ui/gfx/android/java_bitmap.h" |
| 31 #include "ui/gfx/geometry/rect.h" | 31 #include "ui/gfx/geometry/rect.h" |
| 32 | 32 |
| 33 using base::android::JavaArrayOfIntArrayToIntVector; | 33 using base::android::JavaArrayOfIntArrayToIntVector; |
| 34 using base::android::JavaParamRef; |
| 34 using base::android::JavaRef; | 35 using base::android::JavaRef; |
| 35 | 36 |
| 36 namespace ui { | 37 namespace ui { |
| 37 | 38 |
| 38 // static | 39 // static |
| 39 ResourceManagerImpl* ResourceManagerImpl::FromJavaObject( | 40 ResourceManagerImpl* ResourceManagerImpl::FromJavaObject( |
| 40 const JavaRef<jobject>& jobj) { | 41 const JavaRef<jobject>& jobj) { |
| 41 return reinterpret_cast<ResourceManagerImpl*>( | 42 return reinterpret_cast<ResourceManagerImpl*>( |
| 42 Java_ResourceManager_getNativePtr(base::android::AttachCurrentThread(), | 43 Java_ResourceManager_getNativePtr(base::android::AttachCurrentThread(), |
| 43 jobj)); | 44 jobj)); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 66 DCHECK(!ui_resource_manager_); | 67 DCHECK(!ui_resource_manager_); |
| 67 DCHECK(ui_resource_manager); | 68 DCHECK(ui_resource_manager); |
| 68 ui_resource_manager_ = ui_resource_manager; | 69 ui_resource_manager_ = ui_resource_manager; |
| 69 } | 70 } |
| 70 | 71 |
| 71 base::android::ScopedJavaLocalRef<jobject> | 72 base::android::ScopedJavaLocalRef<jobject> |
| 72 ResourceManagerImpl::GetJavaObject() { | 73 ResourceManagerImpl::GetJavaObject() { |
| 73 return base::android::ScopedJavaLocalRef<jobject>(java_obj_); | 74 return base::android::ScopedJavaLocalRef<jobject>(java_obj_); |
| 74 } | 75 } |
| 75 | 76 |
| 76 ResourceManager::Resource* ResourceManagerImpl::GetResource( | 77 Resource* ResourceManagerImpl::GetResource(AndroidResourceType res_type, |
| 77 AndroidResourceType res_type, | 78 int res_id) { |
| 78 int res_id) { | |
| 79 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); | 79 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); |
| 80 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); | 80 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); |
| 81 | 81 |
| 82 std::unordered_map<int, std::unique_ptr<Resource>>::iterator item = | 82 std::unordered_map<int, std::unique_ptr<Resource>>::iterator item = |
| 83 resources_[res_type].find(res_id); | 83 resources_[res_type].find(res_id); |
| 84 | 84 |
| 85 if (item == resources_[res_type].end() || | 85 if (item == resources_[res_type].end() || |
| 86 res_type == ANDROID_RESOURCE_TYPE_DYNAMIC || | 86 res_type == ANDROID_RESOURCE_TYPE_DYNAMIC || |
| 87 res_type == ANDROID_RESOURCE_TYPE_DYNAMIC_BITMAP) { | 87 res_type == ANDROID_RESOURCE_TYPE_DYNAMIC_BITMAP) { |
| 88 RequestResourceFromJava(res_type, res_id); | 88 RequestResourceFromJava(res_type, res_id); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 102 // used as defined in |used_tints|. | 102 // used as defined in |used_tints|. |
| 103 for (auto it = tinted_resources_.cbegin(); it != tinted_resources_.cend();) { | 103 for (auto it = tinted_resources_.cbegin(); it != tinted_resources_.cend();) { |
| 104 if (used_tints.find(it->first) == used_tints.end()) { | 104 if (used_tints.find(it->first) == used_tints.end()) { |
| 105 it = tinted_resources_.erase(it); | 105 it = tinted_resources_.erase(it); |
| 106 } else { | 106 } else { |
| 107 ++it; | 107 ++it; |
| 108 } | 108 } |
| 109 } | 109 } |
| 110 } | 110 } |
| 111 | 111 |
| 112 ResourceManager::Resource* ResourceManagerImpl::GetStaticResourceWithTint( | 112 Resource* ResourceManagerImpl::GetStaticResourceWithTint(int res_id, |
| 113 int res_id, | 113 SkColor tint_color) { |
| 114 SkColor tint_color) { | |
| 115 if (tinted_resources_.find(tint_color) == tinted_resources_.end()) { | 114 if (tinted_resources_.find(tint_color) == tinted_resources_.end()) { |
| 116 tinted_resources_[tint_color] = base::MakeUnique<ResourceMap>(); | 115 tinted_resources_[tint_color] = base::MakeUnique<ResourceMap>(); |
| 117 } | 116 } |
| 118 ResourceMap* resource_map = tinted_resources_[tint_color].get(); | 117 ResourceMap* resource_map = tinted_resources_[tint_color].get(); |
| 119 | 118 |
| 120 // If the resource is already cached, use it. | 119 // If the resource is already cached, use it. |
| 121 std::unordered_map<int, std::unique_ptr<Resource>>::iterator item = | 120 std::unordered_map<int, std::unique_ptr<Resource>>::iterator item = |
| 122 resource_map->find(res_id); | 121 resource_map->find(res_id); |
| 123 if (item != resource_map->end()) | 122 if (item != resource_map->end()) |
| 124 return item->second.get(); | 123 return item->second.get(); |
| 125 | 124 |
| 126 std::unique_ptr<Resource> tinted_resource = base::MakeUnique<Resource>(); | 125 Resource* base_image = GetResource(ANDROID_RESOURCE_TYPE_STATIC, res_id); |
| 126 DCHECK(base_image); |
| 127 | 127 |
| 128 ResourceManager::Resource* base_image = | 128 std::unique_ptr<Resource> tinted_resource = base_image->CreateForCopy(); |
| 129 GetResource(ANDROID_RESOURCE_TYPE_STATIC, res_id); | |
| 130 DCHECK(base_image); | |
| 131 | 129 |
| 132 TRACE_EVENT0("browser", "ResourceManagerImpl::GetStaticResourceWithTint"); | 130 TRACE_EVENT0("browser", "ResourceManagerImpl::GetStaticResourceWithTint"); |
| 133 SkBitmap tinted_bitmap; | 131 SkBitmap tinted_bitmap; |
| 134 tinted_bitmap.allocPixels(SkImageInfo::MakeN32Premul(base_image->size.width(), | 132 tinted_bitmap.allocPixels(SkImageInfo::MakeN32Premul( |
| 135 base_image->size.height())); | 133 base_image->size().width(), base_image->size().height())); |
| 136 | 134 |
| 137 SkCanvas canvas(tinted_bitmap); | 135 SkCanvas canvas(tinted_bitmap); |
| 138 canvas.clear(SK_ColorTRANSPARENT); | 136 canvas.clear(SK_ColorTRANSPARENT); |
| 139 | 137 |
| 140 // Build a color filter to use on the base resource. This filter multiplies | 138 // Build a color filter to use on the base resource. This filter multiplies |
| 141 // the RGB components by the components of the new color but retains the | 139 // the RGB components by the components of the new color but retains the |
| 142 // alpha of the original image. | 140 // alpha of the original image. |
| 143 SkPaint color_filter; | 141 SkPaint color_filter; |
| 144 color_filter.setColorFilter( | 142 color_filter.setColorFilter( |
| 145 SkColorFilter::MakeModeFilter(tint_color, SkBlendMode::kModulate)); | 143 SkColorFilter::MakeModeFilter(tint_color, SkBlendMode::kModulate)); |
| 146 | 144 |
| 147 // Draw the resource and make it immutable. | 145 // Draw the resource and make it immutable. |
| 148 base_image->ui_resource->GetBitmap(base_image->ui_resource->id(), false) | 146 base_image->ui_resource() |
| 147 ->GetBitmap(base_image->ui_resource()->id(), false) |
| 149 .DrawToCanvas(&canvas, &color_filter); | 148 .DrawToCanvas(&canvas, &color_filter); |
| 150 tinted_bitmap.setImmutable(); | 149 tinted_bitmap.setImmutable(); |
| 151 | 150 |
| 152 // Create a UI resource from the new bitmap. | 151 // Create a UI resource from the new bitmap. |
| 153 tinted_resource->size = gfx::Size(base_image->size); | 152 tinted_resource->SetUIResource( |
| 154 tinted_resource->padding = gfx::Rect(base_image->padding); | 153 cc::ScopedUIResource::Create(ui_resource_manager_, |
| 155 tinted_resource->aperture = gfx::Rect(base_image->aperture); | 154 cc::UIResourceBitmap(tinted_bitmap)), |
| 156 tinted_resource->ui_resource = cc::ScopedUIResource::Create( | 155 base_image->size()); |
| 157 ui_resource_manager_, cc::UIResourceBitmap(tinted_bitmap)); | |
| 158 | 156 |
| 159 (*resource_map)[res_id].swap(tinted_resource); | 157 (*resource_map)[res_id].swap(tinted_resource); |
| 160 | 158 |
| 161 return (*resource_map)[res_id].get(); | 159 return (*resource_map)[res_id].get(); |
| 162 } | 160 } |
| 163 | 161 |
| 164 void ResourceManagerImpl::ClearTintedResourceCache(JNIEnv* env, | 162 void ResourceManagerImpl::ClearTintedResourceCache(JNIEnv* env, |
| 165 const JavaRef<jobject>& jobj) { | 163 const JavaRef<jobject>& jobj) { |
| 166 tinted_resources_.clear(); | 164 tinted_resources_.clear(); |
| 167 } | 165 } |
| 168 | 166 |
| 169 void ResourceManagerImpl::PreloadResource(AndroidResourceType res_type, | 167 void ResourceManagerImpl::PreloadResource(AndroidResourceType res_type, |
| 170 int res_id) { | 168 int res_id) { |
| 171 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); | 169 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); |
| 172 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); | 170 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); |
| 173 | 171 |
| 174 // Don't send out a query if the resource is already loaded. | 172 // Don't send out a query if the resource is already loaded. |
| 175 if (resources_[res_type].find(res_id) != resources_[res_type].end()) | 173 if (resources_[res_type].find(res_id) != resources_[res_type].end()) |
| 176 return; | 174 return; |
| 177 | 175 |
| 178 PreloadResourceFromJava(res_type, res_id); | 176 PreloadResourceFromJava(res_type, res_id); |
| 179 } | 177 } |
| 180 | 178 |
| 181 void ResourceManagerImpl::OnResourceReady(JNIEnv* env, | 179 void ResourceManagerImpl::OnResourceReady(JNIEnv* env, |
| 182 const JavaRef<jobject>& jobj, | 180 const JavaRef<jobject>& jobj, |
| 183 jint res_type, | 181 jint res_type, |
| 184 jint res_id, | 182 jint res_id, |
| 185 const JavaRef<jobject>& bitmap, | 183 const JavaRef<jobject>& bitmap, |
| 186 jint padding_left, | 184 jlong native_resource) { |
| 187 jint padding_top, | |
| 188 jint padding_right, | |
| 189 jint padding_bottom, | |
| 190 jint aperture_left, | |
| 191 jint aperture_top, | |
| 192 jint aperture_right, | |
| 193 jint aperture_bottom) { | |
| 194 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); | 185 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); |
| 195 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); | 186 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); |
| 196 TRACE_EVENT2("ui", "ResourceManagerImpl::OnResourceReady", | 187 TRACE_EVENT2("ui", "ResourceManagerImpl::OnResourceReady", |
| 197 "resource_type", res_type, | 188 "resource_type", res_type, |
| 198 "resource_id", res_id); | 189 "resource_id", res_id); |
| 199 | 190 |
| 200 std::unordered_map<int, std::unique_ptr<Resource>>::iterator item = | 191 resources_[res_type][res_id] = |
| 201 resources_[res_type].find(res_id); | 192 base::WrapUnique(reinterpret_cast<Resource*>(native_resource)); |
| 202 if (item == resources_[res_type].end()) { | |
| 203 resources_[res_type][res_id] = base::MakeUnique<Resource>(); | |
| 204 } | |
| 205 | |
| 206 Resource* resource = resources_[res_type][res_id].get(); | 193 Resource* resource = resources_[res_type][res_id].get(); |
| 207 | 194 |
| 208 gfx::JavaBitmap jbitmap(bitmap); | 195 gfx::JavaBitmap jbitmap(bitmap); |
| 209 resource->size = jbitmap.size(); | |
| 210 resource->padding.SetRect(padding_left, padding_top, | |
| 211 padding_right - padding_left, | |
| 212 padding_bottom - padding_top); | |
| 213 resource->aperture.SetRect(aperture_left, aperture_top, | |
| 214 aperture_right - aperture_left, | |
| 215 aperture_bottom - aperture_top); | |
| 216 | |
| 217 SkBitmap skbitmap = gfx::CreateSkBitmapFromJavaBitmap(jbitmap); | 196 SkBitmap skbitmap = gfx::CreateSkBitmapFromJavaBitmap(jbitmap); |
| 218 skbitmap.setImmutable(); | 197 skbitmap.setImmutable(); |
| 219 resource->ui_resource = cc::ScopedUIResource::Create( | 198 resource->SetUIResource( |
| 220 ui_resource_manager_, cc::UIResourceBitmap(skbitmap)); | 199 cc::ScopedUIResource::Create(ui_resource_manager_, |
| 200 cc::UIResourceBitmap(skbitmap)), |
| 201 jbitmap.size()); |
| 221 } | 202 } |
| 222 | 203 |
| 223 void ResourceManagerImpl::RemoveResource( | 204 void ResourceManagerImpl::RemoveResource( |
| 224 JNIEnv* env, | 205 JNIEnv* env, |
| 225 const base::android::JavaRef<jobject>& jobj, | 206 const base::android::JavaRef<jobject>& jobj, |
| 226 jint res_type, | 207 jint res_type, |
| 227 jint res_id) { | 208 jint res_id) { |
| 228 resources_[res_type].erase(res_id); | 209 resources_[res_type].erase(res_id); |
| 229 } | 210 } |
| 230 | 211 |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 TRACE_EVENT2("ui", | 357 TRACE_EVENT2("ui", |
| 377 "ResourceManagerImpl::RequestCrushedSpriteResourceFromJava", | 358 "ResourceManagerImpl::RequestCrushedSpriteResourceFromJava", |
| 378 "bitmap_res_id", bitmap_res_id, | 359 "bitmap_res_id", bitmap_res_id, |
| 379 "metadata_res_id", metadata_res_id); | 360 "metadata_res_id", metadata_res_id); |
| 380 Java_ResourceManager_crushedSpriteResourceRequested( | 361 Java_ResourceManager_crushedSpriteResourceRequested( |
| 381 base::android::AttachCurrentThread(), java_obj_, bitmap_res_id, | 362 base::android::AttachCurrentThread(), java_obj_, bitmap_res_id, |
| 382 metadata_res_id, reloading); | 363 metadata_res_id, reloading); |
| 383 } | 364 } |
| 384 | 365 |
| 385 } // namespace ui | 366 } // namespace ui |
| OLD | NEW |