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