Skip to content

Commit ec309f0

Browse files
committed
Add support of Textures from SVG vector resources.
1 parent 6d66205 commit ec309f0

6 files changed

Lines changed: 33 additions & 36 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ ext {
1919
minSdkVersion = 19
2020
targetSdkVersion = 31
2121
versionCode = 11
22-
versionName = '0.9.0-SNAPSHOT'
22+
versionName = '0.9.0'
2323
}
2424

2525
task clean(type: Delete) {

worldwind-examples/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ dependencies {
3131
implementation project(':worldwind')
3232
implementation 'androidx.appcompat:appcompat:1.4.1'
3333
implementation 'com.google.android.material:material:1.6.0'
34-
implementation 'io.github.missioncommand:mil-sym-android-renderer:0.1.48'
34+
implementation 'io.github.missioncommand:mil-sym-android-renderer:0.1.50'
3535
}

worldwind/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ android {
4343

4444
dependencies {
4545
implementation 'androidx.annotation:annotation:1.3.0'
46+
implementation 'androidx.appcompat:appcompat-resources:1.4.2'
4647
testImplementation 'junit:junit:4.13.2'
4748
testImplementation 'org.mockito:mockito-core:3.12.4'
4849
// PowerMockito is required to mock static methods like Logger.log

worldwind/src/main/java/gov/nasa/worldwind/WorldWindow.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ protected void init(EGLConfigChooser configChooser) {
184184

185185
// Initialize the WorldWindow's render resource cache.
186186
int cacheCapacity = RenderResourceCache.recommendedCapacity();
187-
this.renderResourceCache = new RenderResourceCache(cacheCapacity);
187+
this.renderResourceCache = new RenderResourceCache(getContext(), cacheCapacity);
188188

189189
// Set up to render on demand to an OpenGL ES 2.x context
190190
// TODO Investigate and use the EGL chooser submitted by jgiovino
@@ -907,7 +907,6 @@ protected void renderFrame(Frame frame) {
907907
this.rc.camera = this.camera;
908908
this.rc.cameraPoint = this.globe.geographicToCartesian(this.rc.camera.position.latitude, this.rc.camera.position.longitude, this.rc.camera.position.altitude, this.rc.cameraPoint);
909909
this.rc.renderResourceCache = this.renderResourceCache;
910-
this.rc.renderResourceCache.setResources(this.getContext().getResources());
911910
this.rc.resources = this.getContext().getResources();
912911

913912
// Configure the frame's Cartesian modelview matrix and eye coordinate projection matrix.

worldwind/src/main/java/gov/nasa/worldwind/render/ImageRetriever.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55

66
package gov.nasa.worldwind.render;
77

8-
import android.content.res.Resources;
8+
import android.content.Context;
99
import android.graphics.Bitmap;
1010
import android.graphics.BitmapFactory;
11+
import android.graphics.Canvas;
12+
import android.graphics.drawable.Drawable;
13+
14+
import androidx.appcompat.content.res.AppCompatResources;
1115

1216
import java.io.BufferedInputStream;
1317
import java.io.IOException;
@@ -22,18 +26,11 @@
2226

2327
public class ImageRetriever extends Retriever<ImageSource, ImageOptions, Bitmap> {
2428

25-
protected Resources resources;
29+
protected Context context;
2630

27-
public ImageRetriever(int maxSimultaneousRetrievals) {
31+
public ImageRetriever(Context context, int maxSimultaneousRetrievals) {
2832
super(maxSimultaneousRetrievals);
29-
}
30-
31-
public Resources getResources() {
32-
return resources;
33-
}
34-
35-
public void setResources(Resources res) {
36-
this.resources = res;
33+
this.context = context;
3734
}
3835

3936
@Override
@@ -42,7 +39,7 @@ protected void retrieveAsync(ImageSource imageSource, ImageOptions imageOptions,
4239
try {
4340
Bitmap bitmap = this.decodeImage(imageSource, imageOptions);
4441

45-
if (bitmap != null) {
42+
if (bitmap != null && !bitmap.isRecycled()) {
4643
callback.retrievalSucceeded(this, imageSource, imageOptions, bitmap);
4744
} else {
4845
callback.retrievalFailed(this, imageSource, null); // failed but no exception
@@ -80,7 +77,18 @@ protected Bitmap decodeImage(ImageSource imageSource, ImageOptions imageOptions)
8077

8178
protected Bitmap decodeResource(int id, ImageOptions imageOptions) {
8279
BitmapFactory.Options factoryOptions = this.bitmapFactoryOptions(imageOptions);
83-
return (this.resources != null) ? BitmapFactory.decodeResource(this.resources, id, factoryOptions) : null;
80+
Bitmap bitmap = BitmapFactory.decodeResource(this.context.getResources(), id, factoryOptions);
81+
if (bitmap == null) {
82+
// Use AppCompatResources to read vector SVG images from resources
83+
Drawable drawable = AppCompatResources.getDrawable(this.context, id);
84+
if (drawable != null) {
85+
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
86+
Canvas canvas = new Canvas(bitmap);
87+
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
88+
drawable.draw(canvas);
89+
}
90+
}
91+
return bitmap;
8492
}
8593

8694
protected Bitmap decodeFilePath(String pathName, ImageOptions imageOptions) {

worldwind/src/main/java/gov/nasa/worldwind/render/RenderResourceCache.java

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package gov.nasa.worldwind.render;
77

8-
import android.content.res.Resources;
8+
import android.content.Context;
99
import android.graphics.Bitmap;
1010
import android.opengl.GLES20;
1111
import android.os.Handler;
@@ -28,8 +28,6 @@
2828
public class RenderResourceCache extends LruMemoryCache<Object, RenderResource>
2929
implements Retriever.Callback<ImageSource, ImageOptions, Bitmap>, Handler.Callback {
3030

31-
protected Resources resources;
32-
3331
protected Handler handler;
3432

3533
protected Queue<RenderResource> evictionQueue;
@@ -46,21 +44,21 @@ public class RenderResourceCache extends LruMemoryCache<Object, RenderResource>
4644

4745
protected static final int TRIM_STALE_RETRIEVALS_DELAY = 6000;
4846

49-
public RenderResourceCache(int capacity) {
47+
public RenderResourceCache(Context context, int capacity) {
5048
super(capacity);
51-
this.init();
49+
this.init(context);
5250
}
5351

54-
public RenderResourceCache(int capacity, int lowWater) {
52+
public RenderResourceCache(Context context, int capacity, int lowWater) {
5553
super(capacity, lowWater);
56-
this.init();
54+
this.init(context);
5755
}
5856

59-
protected void init() {
57+
protected void init(Context context) {
6058
this.handler = new Handler(Looper.myLooper(), this);
6159
this.evictionQueue = new ConcurrentLinkedQueue<>();
62-
this.imageRetriever = new ImageRetriever(2);
63-
this.urlImageRetriever = new ImageRetriever(8);
60+
this.imageRetriever = new ImageRetriever(context, 2);
61+
this.urlImageRetriever = new ImageRetriever(context, 8);
6462
this.imageRetrieverCache = new SynchronizedMemoryCache<>(this.getCapacity() / 8);
6563

6664
Logger.log(Logger.INFO, String.format(Locale.US, "RenderResourceCache initialized %,.0f KB (%,.0f KB retrieval cache)",
@@ -71,15 +69,6 @@ public static int recommendedCapacity() {
7169
return (int) (Runtime.getRuntime().maxMemory() * 0.75); // Use maximum 75% of available application heap
7270
}
7371

74-
public Resources getResources() {
75-
return this.resources;
76-
}
77-
78-
public void setResources(Resources res) {
79-
this.resources = res;
80-
((ImageRetriever) this.imageRetriever).setResources(res);
81-
}
82-
8372
public void clear() { // TODO rename as contextLost to clarify this method's purpose for RenderResourceCache
8473
this.handler.removeMessages(TRIM_STALE_RETRIEVALS);
8574
this.entries.clear(); // the cache entries are invalid; clear but don't call entryRemoved

0 commit comments

Comments
 (0)