OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/android/resources/resource_manager.h" |
| 6 |
| 7 #include "content/public/browser/android/ui_resource_provider.h" |
| 8 #include "jni/ResourceManager_jni.h" |
| 9 #include "ui/android/resources/ui_resource_android.h" |
| 10 #include "ui/gfx/android/java_bitmap.h" |
| 11 #include "ui/gfx/geometry/insets_f.h" |
| 12 |
| 13 namespace ui { |
| 14 namespace android { |
| 15 |
| 16 ResourceManager::Resource::Resource() { |
| 17 } |
| 18 |
| 19 ResourceManager::Resource::~Resource() { |
| 20 } |
| 21 |
| 22 gfx::Rect ResourceManager::Resource::Border(const gfx::Size& bounds) { |
| 23 return Border(bounds, gfx::InsetsF(1.f, 1.f, 1.f, 1.f)); |
| 24 } |
| 25 |
| 26 gfx::Rect ResourceManager::Resource::Border(const gfx::Size& bounds, |
| 27 const gfx::InsetsF& scale) { |
| 28 // Calculate whether or not we need to scale down the border if the bounds of |
| 29 // the layer are going to be smaller than the aperture padding. |
| 30 float x_scale = std::min((float)bounds.width() / size.width(), 1.f); |
| 31 float y_scale = std::min((float)bounds.height() / size.height(), 1.f); |
| 32 |
| 33 float left_scale = std::min(x_scale * scale.left(), 1.f); |
| 34 float right_scale = std::min(x_scale * scale.right(), 1.f); |
| 35 float top_scale = std::min(y_scale * scale.top(), 1.f); |
| 36 float bottom_scale = std::min(y_scale * scale.bottom(), 1.f); |
| 37 |
| 38 return gfx::Rect(aperture.x() * left_scale, aperture.y() * top_scale, |
| 39 (size.width() - aperture.width()) * right_scale, |
| 40 (size.height() - aperture.height()) * bottom_scale); |
| 41 } |
| 42 |
| 43 // static |
| 44 ResourceManager* ResourceManager::FromJavaObject(jobject jobj) { |
| 45 return reinterpret_cast<ResourceManager*>(Java_ResourceManager_getNativePtr( |
| 46 base::android::AttachCurrentThread(), jobj)); |
| 47 } |
| 48 |
| 49 ResourceManager::ResourceManager( |
| 50 content::UIResourceProvider* ui_resource_provider) |
| 51 : ui_resource_provider_(ui_resource_provider) { |
| 52 JNIEnv* env = base::android::AttachCurrentThread(); |
| 53 java_obj_.Reset(env, Java_ResourceManager_create( |
| 54 env, base::android::GetApplicationContext(), |
| 55 reinterpret_cast<intptr_t>(this)).obj()); |
| 56 DCHECK(!java_obj_.is_null()); |
| 57 } |
| 58 |
| 59 ResourceManager::~ResourceManager() { |
| 60 Java_ResourceManager_destroy(base::android::AttachCurrentThread(), |
| 61 java_obj_.obj()); |
| 62 } |
| 63 |
| 64 base::android::ScopedJavaLocalRef<jobject> ResourceManager::GetJavaObject( |
| 65 JNIEnv* env) { |
| 66 return base::android::ScopedJavaLocalRef<jobject>(java_obj_); |
| 67 } |
| 68 |
| 69 ResourceManager::Resource* ResourceManager::GetResource( |
| 70 AndroidResourceType res_type, |
| 71 int res_id) { |
| 72 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); |
| 73 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); |
| 74 |
| 75 Resource* resource = resources_[res_type].Lookup(res_id); |
| 76 |
| 77 if (!resource || res_type == ANDROID_RESOURCE_TYPE_DYNAMIC || |
| 78 res_type == ANDROID_RESOURCE_TYPE_DYNAMIC_BITMAP) { |
| 79 Java_ResourceManager_resourceRequested(base::android::AttachCurrentThread(), |
| 80 java_obj_.obj(), res_type, res_id); |
| 81 resource = resources_[res_type].Lookup(res_id); |
| 82 } |
| 83 |
| 84 return resource; |
| 85 } |
| 86 |
| 87 void ResourceManager::PreloadResource(AndroidResourceType res_type, |
| 88 int res_id) { |
| 89 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); |
| 90 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); |
| 91 |
| 92 // Don't send out a query if the resource is already loaded. |
| 93 if (resources_[res_type].Lookup(res_id)) |
| 94 return; |
| 95 |
| 96 Java_ResourceManager_preloadResource(base::android::AttachCurrentThread(), |
| 97 java_obj_.obj(), res_type, res_id); |
| 98 } |
| 99 |
| 100 void ResourceManager::OnResourceReady(JNIEnv* env, |
| 101 jobject jobj, |
| 102 jint res_type, |
| 103 jint res_id, |
| 104 jobject bitmap, |
| 105 jint padding_left, |
| 106 jint padding_top, |
| 107 jint padding_right, |
| 108 jint padding_bottom, |
| 109 jint aperture_left, |
| 110 jint aperture_top, |
| 111 jint aperture_right, |
| 112 jint aperture_bottom) { |
| 113 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); |
| 114 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); |
| 115 |
| 116 Resource* resource = resources_[res_type].Lookup(res_id); |
| 117 if (!resource) { |
| 118 resource = new Resource(); |
| 119 resources_[res_type].AddWithID(resource, res_id); |
| 120 } |
| 121 |
| 122 gfx::JavaBitmap jbitmap(bitmap); |
| 123 resource->size = jbitmap.size(); |
| 124 resource->padding.SetRect(padding_left, padding_top, |
| 125 padding_right - padding_left, |
| 126 padding_bottom - padding_top); |
| 127 resource->aperture.SetRect(aperture_left, aperture_top, |
| 128 aperture_right - aperture_left, |
| 129 aperture_bottom - aperture_top); |
| 130 resource->ui_resource = |
| 131 UIResourceAndroid::CreateFromJavaBitmap(ui_resource_provider_, jbitmap); |
| 132 } |
| 133 |
| 134 // static |
| 135 bool ResourceManager::RegisterResourceManager(JNIEnv* env) { |
| 136 return RegisterNativesImpl(env); |
| 137 } |
| 138 |
| 139 } // namespace android |
| 140 } // namespace ui |
OLD | NEW |