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"
17 ResourceManagerImpl
* ResourceManagerImpl::FromJavaObject(jobject jobj
) {
18 return reinterpret_cast<ResourceManagerImpl
*>(
19 Java_ResourceManager_getNativePtr(base::android::AttachCurrentThread(),
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(),
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
,
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
);
60 void ResourceManagerImpl::PreloadResource(AndroidResourceType res_type
,
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
))
69 PreloadResourceFromJava(res_type
, res_id
);
72 void ResourceManagerImpl::OnResourceReady(JNIEnv
* env
,
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
);
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
);
110 bool ResourceManagerImpl::RegisterResourceManager(JNIEnv
* env
) {
111 return RegisterNativesImpl(env
);
114 void ResourceManagerImpl::PreloadResourceFromJava(AndroidResourceType res_type
,
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
,
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
);