Skip to content

Commit

Permalink
Add support of Textures from SVG vector resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
ComBatVision committed Jul 25, 2022
1 parent 6d66205 commit ec309f0
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 36 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ext {
minSdkVersion = 19
targetSdkVersion = 31
versionCode = 11
versionName = '0.9.0-SNAPSHOT'
versionName = '0.9.0'
}

task clean(type: Delete) {
Expand Down
2 changes: 1 addition & 1 deletion worldwind-examples/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ dependencies {
implementation project(':worldwind')
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.6.0'
implementation 'io.github.missioncommand:mil-sym-android-renderer:0.1.48'
implementation 'io.github.missioncommand:mil-sym-android-renderer:0.1.50'
}
1 change: 1 addition & 0 deletions worldwind/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ android {

dependencies {
implementation 'androidx.annotation:annotation:1.3.0'
implementation 'androidx.appcompat:appcompat-resources:1.4.2'
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.mockito:mockito-core:3.12.4'
// PowerMockito is required to mock static methods like Logger.log
Expand Down
3 changes: 1 addition & 2 deletions worldwind/src/main/java/gov/nasa/worldwind/WorldWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ protected void init(EGLConfigChooser configChooser) {

// Initialize the WorldWindow's render resource cache.
int cacheCapacity = RenderResourceCache.recommendedCapacity();
this.renderResourceCache = new RenderResourceCache(cacheCapacity);
this.renderResourceCache = new RenderResourceCache(getContext(), cacheCapacity);

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

// Configure the frame's Cartesian modelview matrix and eye coordinate projection matrix.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@

package gov.nasa.worldwind.render;

import android.content.res.Resources;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;

import androidx.appcompat.content.res.AppCompatResources;

import java.io.BufferedInputStream;
import java.io.IOException;
Expand All @@ -22,18 +26,11 @@

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

protected Resources resources;
protected Context context;

public ImageRetriever(int maxSimultaneousRetrievals) {
public ImageRetriever(Context context, int maxSimultaneousRetrievals) {
super(maxSimultaneousRetrievals);
}

public Resources getResources() {
return resources;
}

public void setResources(Resources res) {
this.resources = res;
this.context = context;
}

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

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

protected Bitmap decodeResource(int id, ImageOptions imageOptions) {
BitmapFactory.Options factoryOptions = this.bitmapFactoryOptions(imageOptions);
return (this.resources != null) ? BitmapFactory.decodeResource(this.resources, id, factoryOptions) : null;
Bitmap bitmap = BitmapFactory.decodeResource(this.context.getResources(), id, factoryOptions);
if (bitmap == null) {
// Use AppCompatResources to read vector SVG images from resources
Drawable drawable = AppCompatResources.getDrawable(this.context, id);
if (drawable != null) {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
}
}
return bitmap;
}

protected Bitmap decodeFilePath(String pathName, ImageOptions imageOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package gov.nasa.worldwind.render;

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

protected Resources resources;

protected Handler handler;

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

protected static final int TRIM_STALE_RETRIEVALS_DELAY = 6000;

public RenderResourceCache(int capacity) {
public RenderResourceCache(Context context, int capacity) {
super(capacity);
this.init();
this.init(context);
}

public RenderResourceCache(int capacity, int lowWater) {
public RenderResourceCache(Context context, int capacity, int lowWater) {
super(capacity, lowWater);
this.init();
this.init(context);
}

protected void init() {
protected void init(Context context) {
this.handler = new Handler(Looper.myLooper(), this);
this.evictionQueue = new ConcurrentLinkedQueue<>();
this.imageRetriever = new ImageRetriever(2);
this.urlImageRetriever = new ImageRetriever(8);
this.imageRetriever = new ImageRetriever(context, 2);
this.urlImageRetriever = new ImageRetriever(context, 8);
this.imageRetrieverCache = new SynchronizedMemoryCache<>(this.getCapacity() / 8);

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

public Resources getResources() {
return this.resources;
}

public void setResources(Resources res) {
this.resources = res;
((ImageRetriever) this.imageRetriever).setResources(res);
}

public void clear() { // TODO rename as contextLost to clarify this method's purpose for RenderResourceCache
this.handler.removeMessages(TRIM_STALE_RETRIEVALS);
this.entries.clear(); // the cache entries are invalid; clear but don't call entryRemoved
Expand Down

0 comments on commit ec309f0

Please sign in to comment.