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