jPCT-AE - a 3d engine for Android > Support

Re: multi-thread unsafe operations

(1/2) > >>

AugTech:
In terms of the original question, is object creation and texture loading thread-safe?

I am creating objects and loading textures in a separate thread and then passing the objects to a Handler in the renderer which adds them to the world from a Stack in onDrawFrame(). Randomly these objects are not being displayed (incidentally all the objects are map tiles - 2 triangles with one texture).

Logcat reports that 9 textures are loaded, 9 objects are added to the world/ compiled, but potentially only 4 will actually be displayed.

Should texture's be loaded and/ or objects created in the render thread as well as adding?


EgonOlsen:
Adding has to be done in the render thread or at least synchronized with it. Creating and loading of textures and objects should be fine in another thread. I don't see why this should cause any trouble...can you post the log output and the part of the code that creates the objects?

AugTech:
Creation and texture loading (note that this part is multi-threaded and gets called for each tile loaded);


--- Code: ---ArrayList<ARObject3D> allNewObjects = new ArrayList<ARObject3D>();

RGBColor transCol = (RGBColor)fType.getUserData().get(USER_DATA.COLOR_TRANSPARENT);
boolean makeTrans = transCol!=null && AwilaARView.USE_TRANSPARENT_RASTERS;

// Load the image to the graphics manager
byte[] bitmapData = (byte[]) feature.getAttribute(AttributeTypeImpl.IMAGE_NAME);
Bitmap bmp = BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length);

if (makeTrans && bmp!=null) {
bmp = GraphicsManager.getTransparentBitmap(bmp, transCol);
}

GraphicsManager.loadTexture(bmp, feature.getID(), bmp.getWidth());

// Recycle image as large...
bmp.recycle();

// Transform native bounds to lat/ lon
BoundingBox latLon = null;
try {
latLon = ProjectionUtils.reproject( feature.getBounds(), "epsg:4326");
} catch (Exception e) {
e.printStackTrace();
continue;
}
Object3D tmp = ARObjectBuilder.getPlane(latLon, trasnformECEF );

ARObjectTile obj = new ARObjectTile(tmp, feature.getID() );
feature.addObject3D(obj);
allNewObjects.add(obj);


JpctRenderer.addRemoveObjects(allNewObjects, JpctRenderer.SCENE_ADD_OBJECTS);

--- End code ---

Load texture method;


--- Code: --- public static String loadTexture(Bitmap bitmap, String textureName, int size) {
if (bitmap == null) return null;
if (textureName == null || textureName.equals(TEXTURE_NONE)) {
textureName = UUID.randomUUID().toString();
}
if (TextureManager.getInstance().containsTexture(textureName)) {
return textureName;
}

// Only rescale if we have to
int upper2 = upperPowerOfTwo(size);
if (upper2!=size) bitmap = BitmapHelper.rescale(bitmap, upper2, upper2);

TextureManager.getInstance().addTexture(textureName, new Texture(bitmap) );

return textureName;
}

--- End code ---

The JpctRenderer.addRemoveObjects() method creates a Message and sends to the handler, which adds the new objects in to a Stack<ARObject3D>
During the onDrawFrame() method, the following is called/ run;


--- Code: ---int size = objectAddList.size();
while (!objectAddList.isEmpty()) {

ARObject3D obj = objectAddList.pop();
obj.build();
theWorld.addObject(obj);
objsAdded = true;
Log.d(LOG_TAG, String.format("Added %s 1 of %s", obj.getParentFeatureID(), size ));
}

--- End code ---

The resultant Logcat;


--- Code: ---12-19 13:49:47.165: D/dalvikvm(23790): GC_EXPLICIT freed 1459K, 40% free 26192K/42964K, paused 11ms+5ms, total 85ms
12-19 13:49:47.165: D/Awila Renderer(23790): World was 26, now 1
12-19 13:49:50.027: I/Data Manager(23790): Loading data...
12-19 13:49:50.067: I/Data Manager(23790): osm:OSM Maps requesting 9 tiles..
12-19 13:49:51.379: I/Data Manager(23790): 1 received of 9 in OSM Maps; (1 of 9 total)
12-19 13:49:51.389: I/jPCT-AE(23790): Loading Texture...
12-19 13:49:51.399: I/jPCT-AE(23790): Texture loaded...262144 bytes/256*256 pixels!
12-19 13:49:51.419: I/Data Manager(23790): 2 received of 9 in OSM Maps; (2 of 9 total)
12-19 13:49:51.419: I/jPCT-AE(23790): Loading Texture...
12-19 13:49:51.429: I/jPCT-AE(23790): Texture loaded...262144 bytes/256*256 pixels!
12-19 13:49:51.439: I/Data Manager(23790): 3 received of 9 in OSM Maps; (3 of 9 total)
12-19 13:49:51.439: I/jPCT-AE(23790): Loading Texture...
12-19 13:49:51.439: I/jPCT-AE(23790): Texture loaded...262144 bytes/256*256 pixels!
12-19 13:49:51.449: I/Data Manager(23790): 4 received of 9 in OSM Maps; (4 of 9 total)
12-19 13:49:51.459: I/jPCT-AE(23790): Loading Texture...
12-19 13:49:51.459: I/jPCT-AE(23790): Texture loaded...262144 bytes/256*256 pixels!
12-19 13:49:51.459: I/jPCT-AE(23790): Normal vectors calculated in 0ms!
12-19 13:49:51.459: D/Awila Renderer(23790): Added OSM Maps-49-8094-5393-14 1 of 4
12-19 13:49:51.459: I/jPCT-AE(23790): Normal vectors calculated in 0ms!
12-19 13:49:51.459: D/Awila Renderer(23790): Added OSM Maps-47-8095-5391-14 1 of 4
12-19 13:49:51.459: I/jPCT-AE(23790): Normal vectors calculated in 0ms!
12-19 13:49:51.469: D/Awila Renderer(23790): Added OSM Maps-50-8095-5393-14 1 of 4
12-19 13:49:51.469: I/jPCT-AE(23790): Normal vectors calculated in 0ms!
12-19 13:49:51.469: D/Awila Renderer(23790): Added OSM Maps-48-8095-5392-14 1 of 4
12-19 13:49:51.469: I/Data Manager(23790): 5 received of 9 in OSM Maps; (5 of 9 total)
12-19 13:49:51.479: I/jPCT-AE(23790): Loading Texture...
12-19 13:49:51.479: I/jPCT-AE(23790): Texture loaded...262144 bytes/256*256 pixels!
12-19 13:49:51.489: I/Data Manager(23790): 6 received of 9 in OSM Maps; (6 of 9 total)
12-19 13:49:51.489: I/jPCT-AE(23790): Loading Texture...
12-19 13:49:51.489: I/jPCT-AE(23790): Texture loaded...262144 bytes/256*256 pixels!
12-19 13:49:51.499: I/Data Manager(23790): 7 received of 9 in OSM Maps; (7 of 9 total)
12-19 13:49:51.509: I/jPCT-AE(23790): Loading Texture...
12-19 13:49:51.509: I/jPCT-AE(23790): Texture loaded...262144 bytes/256*256 pixels!
12-19 13:49:51.519: I/Data Manager(23790): 8 received of 9 in OSM Maps; (8 of 9 total)
12-19 13:49:51.519: I/jPCT-AE(23790): Loading Texture...
12-19 13:49:51.519: I/jPCT-AE(23790): Texture loaded...262144 bytes/256*256 pixels!
12-19 13:49:51.529: I/jPCT-AE(23790): Subobject of object 529/object531 compiled to indexed fixed point data using 6/4 vertices in 0ms!
12-19 13:49:51.529: I/jPCT-AE(23790): Object 529/object531 compiled to 1 subobjects in 2ms!
12-19 13:49:51.529: I/jPCT-AE(23790): Subobject of object 527/object529 compiled to indexed fixed point data using 6/4 vertices in 1ms!
12-19 13:49:51.529: I/jPCT-AE(23790): Object 527/object529 compiled to 1 subobjects in 1ms!
12-19 13:49:51.529: I/jPCT-AE(23790): Subobject of object 525/object527 compiled to indexed fixed point data using 6/4 vertices in 0ms!
12-19 13:49:51.529: I/jPCT-AE(23790): Object 525/object527 compiled to 1 subobjects in 1ms!
12-19 13:49:51.529: I/jPCT-AE(23790): Subobject of object 523/object525 compiled to indexed fixed point data using 6/4 vertices in 1ms!
12-19 13:49:51.529: I/jPCT-AE(23790): Object 523/object525 compiled to 1 subobjects in 1ms!
12-19 13:49:51.529: I/jPCT-AE(23790): Normal vectors calculated in 0ms!
12-19 13:49:51.529: D/Awila Renderer(23790): Added OSM Maps-53-8094-5391-14 1 of 4
12-19 13:49:51.529: I/jPCT-AE(23790): Normal vectors calculated in 0ms!
12-19 13:49:51.529: D/Awila Renderer(23790): Added OSM Maps-54-8093-5391-14 1 of 4
12-19 13:49:51.539: I/jPCT-AE(23790): Normal vectors calculated in 0ms!
12-19 13:49:51.539: D/Awila Renderer(23790): Added OSM Maps-52-8094-5392-14 1 of 4
12-19 13:49:51.539: I/jPCT-AE(23790): Normal vectors calculated in 0ms!
12-19 13:49:51.539: D/Awila Renderer(23790): Added OSM Maps-51-8093-5393-14 1 of 4
12-19 13:49:51.539: I/Data Manager(23790): 9 received of 9 in OSM Maps; (9 of 9 total)
12-19 13:49:51.549: I/jPCT-AE(23790): Loading Texture...
12-19 13:49:51.549: I/jPCT-AE(23790): Texture loaded...262144 bytes/256*256 pixels!
12-19 13:49:51.589: I/jPCT-AE(23790): Subobject of object 537/object539 compiled to indexed fixed point data using 6/4 vertices in 0ms!
12-19 13:49:51.599: I/jPCT-AE(23790): Object 537/object539 compiled to 1 subobjects in 1ms!
12-19 13:49:51.599: I/jPCT-AE(23790): Subobject of object 535/object537 compiled to indexed fixed point data using 6/4 vertices in 1ms!
12-19 13:49:51.599: I/jPCT-AE(23790): Object 535/object537 compiled to 1 subobjects in 1ms!
12-19 13:49:51.599: I/jPCT-AE(23790): Subobject of object 533/object535 compiled to indexed fixed point data using 6/4 vertices in 0ms!
12-19 13:49:51.599: I/jPCT-AE(23790): Object 533/object535 compiled to 1 subobjects in 1ms!
12-19 13:49:51.599: I/jPCT-AE(23790): Subobject of object 531/object533 compiled to indexed fixed point data using 6/4 vertices in 0ms!
12-19 13:49:51.599: I/jPCT-AE(23790): Object 531/object533 compiled to 1 subobjects in 0ms!
12-19 13:49:51.599: I/jPCT-AE(23790): Normal vectors calculated in 0ms!
12-19 13:49:51.599: D/Awila Renderer(23790): Added OSM Maps-55-8093-5392-14 1 of 1
12-19 13:49:51.659: I/jPCT-AE(23790): Subobject of object 539/object541 compiled to indexed fixed point data using 6/4 vertices in 1ms!
12-19 13:49:51.659: I/jPCT-AE(23790): Object 539/object541 compiled to 1 subobjects in 1ms!


--- End code ---

The end result;



Originally I did think it was texture loading, but the objects all have obj.setAdditionalColor(128,128,128) applied, and as you can see, nothing is grey!

Mike

EgonOlsen:
How is the final supposed to look? Do you have an example for a correct rendering?

AugTech:
Basically the whole screen as a map;

Navigation

[0] Message Index

[#] Next page

Go to full version