java - Why this code have performance issue even without synchronized keyword? -
why following code have performance issue, frame camera not smooth.
public class videocaptureandroid implements previewcallback, callback{ private integer devicerotation = integer.max_value; public videocaptureandroid(context context, int id, long native_capturer) { devicerotationnotifier = new orientationeventlistener(context, sensormanager.sensor_delay_normal) { public void onorientationchanged(int orientation) { if (orientation == orientation_unknown) { log.d(tag, "the device rotation angle unknown."); return; } synchronized(devicerotation) { if (devicerotation != orientation) { devicerotation = orientation; } } } }; exchanger<handler> handlerexchanger = new exchanger<handler>(); camerathread = new camerathread(handlerexchanger); camerathread.start(); } public synchronized void onpreviewframe(byte[] data, camera callbackcamera) { int framerotation = info.orientation; if (info.facing == camera.camerainfo.camera_facing_front) { framerotation = (info.orientation - devicerotation + 360) % 360; } else if (info.facing == camera.camerainfo.camera_facing_back) { framerotation = (info.orientation + devicerotation) % 360; } onframe(data, data.length, native_capturer, framerotation); camera.addcallbackbuffer(data); } }
seems if comment out following code, frame smooth, , no performance issues. didn't using synchronized
in onpreviewframe
access devicerotation
, why impacted onorientationchanged
?
if (info.facing == camera.camerainfo.camera_facing_front) { framerotation = (info.orientation - devicerotation + 360) % 360; } else if (info.facing == camera.camerainfo.camera_facing_back) { framerotation = (info.orientation + devicerotation) % 360; }
the code have indicated not problem. modulo operations slow, doing single 1 each frame not going have huge impact. framerotation
value being passed onframe()
can assume applying matrix or other transform rotate data. expensive operation , involves use of 1 or more temporary buffers (or bitmap
objects), uses heap , slow. i'm assuming info.orientation
being passed unaltered onframe()
result in no adjustment frame. hence, removal of 2 lines have indicated result in no heavy processing in onframe()
, why jitter disappears.
you can track down better using traceview or systrace confirm.
Comments
Post a Comment