OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.ui.resources; | 5 package org.chromium.ui.resources; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.res.Resources; | 8 import android.content.res.Resources; |
9 import android.graphics.Bitmap; | 9 import android.graphics.Bitmap; |
10 import android.graphics.Rect; | 10 import android.graphics.Rect; |
11 import android.util.SparseArray; | 11 import android.util.SparseArray; |
12 | 12 |
13 import org.chromium.base.annotations.CalledByNative; | 13 import org.chromium.base.annotations.CalledByNative; |
14 import org.chromium.base.annotations.JNINamespace; | 14 import org.chromium.base.annotations.JNINamespace; |
15 import org.chromium.ui.resources.ResourceLoader.ResourceLoaderCallback; | 15 import org.chromium.ui.resources.ResourceLoader.ResourceLoaderCallback; |
16 import org.chromium.ui.resources.dynamics.DynamicResource; | 16 import org.chromium.ui.resources.dynamics.DynamicResource; |
17 import org.chromium.ui.resources.dynamics.DynamicResourceLoader; | 17 import org.chromium.ui.resources.dynamics.DynamicResourceLoader; |
| 18 import org.chromium.ui.resources.sprites.CrushedSpriteResource; |
| 19 import org.chromium.ui.resources.sprites.CrushedSpriteResourceLoader; |
18 import org.chromium.ui.resources.statics.StaticResourceLoader; | 20 import org.chromium.ui.resources.statics.StaticResourceLoader; |
19 import org.chromium.ui.resources.system.SystemResourceLoader; | 21 import org.chromium.ui.resources.system.SystemResourceLoader; |
20 | 22 |
21 /** | 23 /** |
22 * The Java component of a manager for all static resources to be loaded and use
d by CC layers. | 24 * The Java component of a manager for all static resources to be loaded and use
d by CC layers. |
23 * This class does not hold any resource state, but passes it directly to native
as they are loaded. | 25 * This class does not hold any resource state, but passes it directly to native
as they are loaded. |
24 */ | 26 */ |
25 @JNINamespace("ui") | 27 @JNINamespace("ui") |
26 public class ResourceManager implements ResourceLoaderCallback { | 28 public class ResourceManager implements ResourceLoaderCallback { |
27 private final SparseArray<ResourceLoader> mResourceLoaders = new SparseArray
<ResourceLoader>(); | 29 private final SparseArray<ResourceLoader> mResourceLoaders = new SparseArray
<ResourceLoader>(); |
28 private final SparseArray<SparseArray<LayoutResource>> mLoadedResources = | 30 private final SparseArray<SparseArray<LayoutResource>> mLoadedResources = |
29 new SparseArray<SparseArray<LayoutResource>>(); | 31 new SparseArray<SparseArray<LayoutResource>>(); |
| 32 private final CrushedSpriteResourceLoader mCrushedSpriteResourceLoader; |
30 | 33 |
31 private final float mPxToDp; | 34 private final float mPxToDp; |
32 | 35 |
33 private long mNativeResourceManagerPtr; | 36 private long mNativeResourceManagerPtr; |
34 | 37 |
35 private ResourceManager(Context context, long staticResourceManagerPtr) { | 38 private ResourceManager(Context context, long staticResourceManagerPtr) { |
36 Resources resources = context.getResources(); | 39 Resources resources = context.getResources(); |
37 mPxToDp = 1.f / resources.getDisplayMetrics().density; | 40 mPxToDp = 1.f / resources.getDisplayMetrics().density; |
38 | 41 |
39 // Register ResourceLoaders | 42 // Register ResourceLoaders |
40 registerResourceLoader(new StaticResourceLoader( | 43 registerResourceLoader(new StaticResourceLoader( |
41 AndroidResourceType.STATIC, this, resources)); | 44 AndroidResourceType.STATIC, this, resources)); |
42 registerResourceLoader(new DynamicResourceLoader( | 45 registerResourceLoader(new DynamicResourceLoader( |
43 AndroidResourceType.DYNAMIC, this)); | 46 AndroidResourceType.DYNAMIC, this)); |
44 registerResourceLoader(new DynamicResourceLoader( | 47 registerResourceLoader(new DynamicResourceLoader( |
45 AndroidResourceType.DYNAMIC_BITMAP, this)); | 48 AndroidResourceType.DYNAMIC_BITMAP, this)); |
46 registerResourceLoader(new SystemResourceLoader( | 49 registerResourceLoader(new SystemResourceLoader( |
47 AndroidResourceType.SYSTEM, this, context)); | 50 AndroidResourceType.SYSTEM, this, context)); |
| 51 mCrushedSpriteResourceLoader = new CrushedSpriteResourceLoader(this, res
ources); |
48 | 52 |
49 mNativeResourceManagerPtr = staticResourceManagerPtr; | 53 mNativeResourceManagerPtr = staticResourceManagerPtr; |
50 } | 54 } |
51 | 55 |
52 /** | 56 /** |
53 * Creates an instance of a {@link ResourceManager}. This will | 57 * Creates an instance of a {@link ResourceManager}. This will |
54 * @param context A {@link Context} instance to grab {@link
Resources} from. | 58 * @param context A {@link Context} instance to grab {@link
Resources} from. |
55 * @param staticResourceManagerPtr A pointer to the native component of this
class. | 59 * @param staticResourceManagerPtr A pointer to the native component of this
class. |
56 * @return A new instance of a {@link ResourceManage
r}. | 60 * @return A new instance of a {@link ResourceManage
r}. |
57 */ | 61 */ |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 /** | 107 /** |
104 * @param resType The type of the Android resource. | 108 * @param resType The type of the Android resource. |
105 * @param resId The id of the Android resource. | 109 * @param resId The id of the Android resource. |
106 * @return The corresponding {@link LayoutResource}. | 110 * @return The corresponding {@link LayoutResource}. |
107 */ | 111 */ |
108 public LayoutResource getResource(int resType, int resId) { | 112 public LayoutResource getResource(int resType, int resId) { |
109 SparseArray<LayoutResource> bucket = mLoadedResources.get(resType); | 113 SparseArray<LayoutResource> bucket = mLoadedResources.get(resType); |
110 return bucket != null ? bucket.get(resId) : null; | 114 return bucket != null ? bucket.get(resId) : null; |
111 } | 115 } |
112 | 116 |
| 117 @SuppressWarnings("cast") |
113 @Override | 118 @Override |
114 public void onResourceLoaded(int resType, int resId, Resource resource) { | 119 public void onResourceLoaded(int resType, int resId, Resource resource) { |
115 if (resource == null) return; | 120 if (resource == null) return; |
116 | 121 |
117 saveMetadataForLoadedResource(resType, resId, resource); | 122 if (resType != AndroidResourceType.CRUSHED_SPRITE) { |
| 123 saveMetadataForLoadedResource(resType, resId, resource); |
| 124 } |
118 | 125 |
119 if (mNativeResourceManagerPtr == 0) return; | 126 if (mNativeResourceManagerPtr == 0) return; |
| 127 if (resType == AndroidResourceType.CRUSHED_SPRITE) { |
| 128 if (resource.getBitmap() != null) { |
| 129 CrushedSpriteResource crushedResource = (CrushedSpriteResource)
resource; |
| 130 nativeOnCrushedSpriteResourceReady(mNativeResourceManagerPtr, re
sId, |
| 131 crushedResource.getBitmap(), crushedResource.getFrameRec
tangles(), |
| 132 crushedResource.getSpriteWidth(), crushedResource.getSpr
iteHeight()); |
| 133 } |
| 134 return; |
| 135 } |
| 136 |
120 Rect padding = resource.getPadding(); | 137 Rect padding = resource.getPadding(); |
121 Rect aperture = resource.getAperture(); | 138 Rect aperture = resource.getAperture(); |
122 | 139 |
123 nativeOnResourceReady(mNativeResourceManagerPtr, resType, resId, resourc
e.getBitmap(), | 140 nativeOnResourceReady(mNativeResourceManagerPtr, resType, resId, resourc
e.getBitmap(), |
124 padding.left, padding.top, padding.right, padding.bottom, | 141 padding.left, padding.top, padding.right, padding.bottom, |
125 aperture.left, aperture.top, aperture.right, aperture.bottom); | 142 aperture.left, aperture.top, aperture.right, aperture.bottom); |
126 } | 143 } |
127 | 144 |
128 private void saveMetadataForLoadedResource(int resType, int resId, Resource
resource) { | 145 private void saveMetadataForLoadedResource(int resType, int resId, Resource
resource) { |
129 SparseArray<LayoutResource> bucket = mLoadedResources.get(resType); | 146 SparseArray<LayoutResource> bucket = mLoadedResources.get(resType); |
(...skipping 16 matching lines...) Expand all Loading... |
146 if (loader != null) loader.loadResource(resId); | 163 if (loader != null) loader.loadResource(resId); |
147 } | 164 } |
148 | 165 |
149 @CalledByNative | 166 @CalledByNative |
150 private void preloadResource(int resType, int resId) { | 167 private void preloadResource(int resType, int resId) { |
151 ResourceLoader loader = mResourceLoaders.get(resType); | 168 ResourceLoader loader = mResourceLoaders.get(resType); |
152 if (loader != null) loader.preloadResource(resId); | 169 if (loader != null) loader.preloadResource(resId); |
153 } | 170 } |
154 | 171 |
155 @CalledByNative | 172 @CalledByNative |
| 173 private void crushedSpriteResourceRequested(int bitmapResId, int metatadataR
esId, |
| 174 boolean reloading) { |
| 175 if (reloading) { |
| 176 Bitmap bitmap = mCrushedSpriteResourceLoader.reloadResource(bitmapRe
sId); |
| 177 if (bitmap != null) { |
| 178 nativeOnCrushedSpriteResourceReloaded(mNativeResourceManagerPtr,
bitmapResId, |
| 179 bitmap); |
| 180 } |
| 181 } else { |
| 182 mCrushedSpriteResourceLoader.loadResource(bitmapResId, metatadataRes
Id); |
| 183 } |
| 184 } |
| 185 |
| 186 @CalledByNative |
156 private long getNativePtr() { | 187 private long getNativePtr() { |
157 return mNativeResourceManagerPtr; | 188 return mNativeResourceManagerPtr; |
158 } | 189 } |
159 | 190 |
160 private void registerResourceLoader(ResourceLoader loader) { | 191 private void registerResourceLoader(ResourceLoader loader) { |
161 mResourceLoaders.put(loader.getResourceType(), loader); | 192 mResourceLoaders.put(loader.getResourceType(), loader); |
162 } | 193 } |
163 | 194 |
164 private native void nativeOnResourceReady(long nativeResourceManagerImpl, in
t resType, | 195 private native void nativeOnResourceReady(long nativeResourceManagerImpl, in
t resType, |
165 int resId, Bitmap bitmap, int paddingLeft, int paddingTop, int paddi
ngRight, | 196 int resId, Bitmap bitmap, int paddingLeft, int paddingTop, int paddi
ngRight, |
166 int paddingBottom, int apertureLeft, int apertureTop, int apertureRi
ght, | 197 int paddingBottom, int apertureLeft, int apertureTop, int apertureRi
ght, |
167 int apertureBottom); | 198 int apertureBottom); |
| 199 private native void nativeOnCrushedSpriteResourceReady(long nativeResourceMa
nagerImpl, |
| 200 int bitmapResId, Bitmap bitmap, int[][] frameRects, int spriteWidth,
int spriteHeight); |
| 201 private native void nativeOnCrushedSpriteResourceReloaded(long nativeResourc
eManagerImpl, |
| 202 int bitmapResId, Bitmap bitmap); |
168 | 203 |
169 } | 204 } |
OLD | NEW |