Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(156)

Side by Side Diff: content/browser/android/resource_manager_impl.cc

Issue 731133002: Upstream ResourceManager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix build failure Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #include "ui/gfx/geometry/insets_f.h"
12
13 namespace content {
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 ResourceManagerImpl* ResourceManagerImpl::FromJavaObject(jobject jobj) {
44 return reinterpret_cast<ResourceManagerImpl*>(
45 Java_ResourceManager_getNativePtr(base::android::AttachCurrentThread(),
46 jobj));
47 }
48
49 ResourceManagerImpl::ResourceManagerImpl(
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 ResourceManagerImpl::~ResourceManagerImpl() {
60 Java_ResourceManager_destroy(base::android::AttachCurrentThread(),
61 java_obj_.obj());
62 }
63
64 base::android::ScopedJavaLocalRef<jobject> ResourceManagerImpl::GetJavaObject(
65 JNIEnv* env) {
66 return base::android::ScopedJavaLocalRef<jobject>(java_obj_);
67 }
68
69 ResourceManager::Resource* ResourceManagerImpl::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 ResourceManagerImpl::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 ResourceManagerImpl::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 = UIResourceAndroidImpl::CreateFromJavaBitmap(
131 ui_resource_provider_, jbitmap);
132 }
133
134 // static
135 bool ResourceManagerImpl::RegisterResourceManager(JNIEnv* env) {
136 return RegisterNativesImpl(env);
137 }
138
139 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698