Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / android / resources / resource_manager_impl.cc
blobc1c64ba26f8f114b34fc07fa99cd4dbfee7cc68e
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.
5 #include "ui/android/resources/resource_manager_impl.h"
7 #include "base/android/jni_string.h"
8 #include "base/trace_event/trace_event.h"
9 #include "jni/ResourceManager_jni.h"
10 #include "ui/android/resources/ui_resource_android.h"
11 #include "ui/android/resources/ui_resource_provider.h"
12 #include "ui/gfx/android/java_bitmap.h"
14 namespace ui {
16 // static
17 ResourceManagerImpl* ResourceManagerImpl::FromJavaObject(jobject jobj) {
18 return reinterpret_cast<ResourceManagerImpl*>(
19 Java_ResourceManager_getNativePtr(base::android::AttachCurrentThread(),
20 jobj));
23 ResourceManagerImpl::ResourceManagerImpl(
24 ui::UIResourceProvider* ui_resource_provider)
25 : ui_resource_provider_(ui_resource_provider) {
26 JNIEnv* env = base::android::AttachCurrentThread();
27 java_obj_.Reset(env, Java_ResourceManager_create(
28 env, base::android::GetApplicationContext(),
29 reinterpret_cast<intptr_t>(this)).obj());
30 DCHECK(!java_obj_.is_null());
33 ResourceManagerImpl::~ResourceManagerImpl() {
34 Java_ResourceManager_destroy(base::android::AttachCurrentThread(),
35 java_obj_.obj());
38 base::android::ScopedJavaLocalRef<jobject>
39 ResourceManagerImpl::GetJavaObject() {
40 return base::android::ScopedJavaLocalRef<jobject>(java_obj_);
43 ResourceManager::Resource* ResourceManagerImpl::GetResource(
44 AndroidResourceType res_type,
45 int res_id) {
46 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST);
47 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST);
49 Resource* resource = resources_[res_type].Lookup(res_id);
51 if (!resource || res_type == ANDROID_RESOURCE_TYPE_DYNAMIC ||
52 res_type == ANDROID_RESOURCE_TYPE_DYNAMIC_BITMAP) {
53 RequestResourceFromJava(res_type, res_id);
54 resource = resources_[res_type].Lookup(res_id);
57 return resource;
60 void ResourceManagerImpl::PreloadResource(AndroidResourceType res_type,
61 int res_id) {
62 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST);
63 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST);
65 // Don't send out a query if the resource is already loaded.
66 if (resources_[res_type].Lookup(res_id))
67 return;
69 PreloadResourceFromJava(res_type, res_id);
72 void ResourceManagerImpl::OnResourceReady(JNIEnv* env,
73 jobject jobj,
74 jint res_type,
75 jint res_id,
76 jobject bitmap,
77 jint padding_left,
78 jint padding_top,
79 jint padding_right,
80 jint padding_bottom,
81 jint aperture_left,
82 jint aperture_top,
83 jint aperture_right,
84 jint aperture_bottom) {
85 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST);
86 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST);
87 TRACE_EVENT2("ui", "ResourceManagerImpl::OnResourceReady",
88 "resource_type", res_type,
89 "resource_id", res_id);
91 Resource* resource = resources_[res_type].Lookup(res_id);
92 if (!resource) {
93 resource = new Resource();
94 resources_[res_type].AddWithID(resource, res_id);
97 gfx::JavaBitmap jbitmap(bitmap);
98 resource->size = jbitmap.size();
99 resource->padding.SetRect(padding_left, padding_top,
100 padding_right - padding_left,
101 padding_bottom - padding_top);
102 resource->aperture.SetRect(aperture_left, aperture_top,
103 aperture_right - aperture_left,
104 aperture_bottom - aperture_top);
105 resource->ui_resource =
106 UIResourceAndroid::CreateFromJavaBitmap(ui_resource_provider_, jbitmap);
109 // static
110 bool ResourceManagerImpl::RegisterResourceManager(JNIEnv* env) {
111 return RegisterNativesImpl(env);
114 void ResourceManagerImpl::PreloadResourceFromJava(AndroidResourceType res_type,
115 int res_id) {
116 TRACE_EVENT2("ui", "ResourceManagerImpl::PreloadResourceFromJava",
117 "resource_type", res_type,
118 "resource_id", res_id);
119 Java_ResourceManager_preloadResource(base::android::AttachCurrentThread(),
120 java_obj_.obj(), res_type, res_id);
123 void ResourceManagerImpl::RequestResourceFromJava(AndroidResourceType res_type,
124 int res_id) {
125 TRACE_EVENT2("ui", "ResourceManagerImpl::RequestResourceFromJava",
126 "resource_type", res_type,
127 "resource_id", res_id);
128 Java_ResourceManager_resourceRequested(base::android::AttachCurrentThread(),
129 java_obj_.obj(), res_type, res_id);
132 } // namespace ui