fresco icon indicating copy to clipboard operation
fresco copied to clipboard

The application may be doing too much work on its main thread.

Open ultimaters opened this issue 11 years ago • 25 comments

I used SimpleDraweeView in adapter, but my app runs slowly, and I saw the log below:

I/Choreographer(1378): Skipped 55 frames! The application may be doing too much work on its main thread.

so I comment the code mSimpleDraw.setImageUri, the app runs fluently. I think the SimpleDraw blocks the main thread when fetching image from network.

ultimaters avatar Apr 03 '15 12:04 ultimaters

Are you using OkHttp networking stack? There is an issue with OkHttp networking stack that it cancels requests on the calling thread (which is going to be the main thread). We have a fix ready for that. Other than that, SimpleDraweeView should not be doing anything heavy on the main thread. If you give us more context on how you use Fresco, we may be able to isolate the issue more easily.

plamenko avatar Apr 03 '15 17:04 plamenko

@plamenko I just used in a CircleView Adapter and load image, very simple like the tutorial. in Application: Fresco.initialize(mContext); in adapter: mImageView.setImageURI(Uri.parse(data.getImage())); that's it.

ultimaters avatar Apr 04 '15 17:04 ultimaters

What's CircleView Adapter?

plamenko avatar Apr 04 '15 19:04 plamenko

The application may be doing too much work on its main thread.

Hitting the same problem here. Pretty basic usage of SimpleDraweeView in a GridView through an adapter:

// MainActivity.java
Fresco.initialize(this);
GridView gv = findViewById(R.id.grid_view);
ImageAdapter ia = new ImageAdapter(this);
gv.setAdapter(ia);
// ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
   ...
   public View getView(int position, View convertView, final ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.grid_item, null);
            convertView.setTag(convertView.findViewById(R.id.grid_item_image_view));
        }
        SimpleDraweeView view = (SimpleDraweeView) convertView.getTag();

        // with the following line: major UI slowdown, "the application may be doing too much work on its main thread."
        // without the following line: performant as expected
        view.setImageURI(Uri.parse("content://media/external/images/media/172699");

        return view;
   }
   ...
}
<!-- grid_item.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/grid_item_image_view"
        android:layout_width="150dp"
        android:layout_height="150dp"
        />

</LinearLayout>

It seems that any parallel calls to setImageURI result in the slowdown.

mikeknoop avatar Apr 06 '15 02:04 mikeknoop

@plamenko My code just the same as above, in an adapter

ultimaters avatar Apr 06 '15 04:04 ultimaters

I'll investigate

plamenko avatar Apr 06 '15 15:04 plamenko

Replacing

view.setImageURI(Uri.parse("content://...");

with

ImageRequest imageRequest =
        ImageRequestBuilder.newBuilderWithSource(Uri.parse("content://...")
                .setResizeOptions(
                        new ResizeOptions(150, 150))
                .build();
DraweeController draweeController = Fresco.newDraweeControllerBuilder()
        .setImageRequest(imageRequest)
        .setOldController(view.getController())
        .setAutoPlayAnimations(true)
        .build();
view.setController(draweeController);

in my code above fixes the performance problem. If I comment out the setResizeOptions line the performance problem comes back. Perhaps something to do with rescaling on the main thread?

mikeknoop avatar Apr 09 '15 04:04 mikeknoop

FWIW I can also hack the Fresco Sample app to Skipped 129 frames! by taking the opposite approach to my last comment:

Replace these lines with

Uri uri = getRandomLocalContentURI();
view.setImageURI(uri);

I've found it only hits performance problems dealing with local content:// URIs. http:// do just fine.

mikeknoop avatar Apr 09 '15 04:04 mikeknoop

That's really helpful information, thanks!

plamenko avatar Apr 09 '15 20:04 plamenko

@fuwaneko , yeah OkHttp cancellation is a known issue and we have a fix for that. However, local content uri is something we yet need to figure.

plamenko avatar Apr 10 '15 12:04 plamenko

One other potential/related clue. Even when using my workaround above (which fixed the performance problem), I still see one error line for every content://... image that is loaded:

04-11 22:00:25.677  29554-29592/com.example.app E/JHEAD﹕ can't open '/external/images/media/172693'
04-11 22:00:25.677  29554-29592/com.example.app E/JHEAD﹕ can't open '/external/images/media/172692'
04-11 22:00:25.677  29554-29592/com.example.app E/JHEAD﹕ can't open '/external/images/media/172691'

The image still successfully loads but an error is generated for each.

The fix from this Picasso thread is to include file:// in front, presumably something that Fresco needs to do internally or an upstream lib needs to be doing.

mikeknoop avatar Apr 12 '15 02:04 mikeknoop

I deleted the two comments related to OkHttp cancellation as they are not related to this issue and has already been fixed.

plamenko avatar Apr 17 '15 00:04 plamenko

@mikeknoop , I missed part of your commend earlier, where you said "If I comment out the setResizeOptions line the performance problem comes back." Can you please check what are the dimensions of those local images? If the image is much bigger than the view than there is going to be a noticeable overhead unless we resize the image first. Resize has to be explicitly specified as calling setImageURI won't do that.

plamenko avatar Apr 17 '15 00:04 plamenko

@plamenko my local images are indeed bigger than the view. They are camera images so likely ~3000x3000px getting scaled to ~150dpi views.

Can Fresco handle this use case?

mikeknoop avatar Apr 17 '15 02:04 mikeknoop

@mikeknoop, yes, please see http://frescolib.org/docs/resizing-rotating.html#_

plamenko avatar Apr 18 '15 00:04 plamenko

Thanks for the info. I imagine Camera images are the 80% use case for content:// URIs, maybe it'd make sense to do resizing in setImageURI.

I feel like I'm really hacking things to get Fresco to work:

ImageRequestBuilder.newBuilderWithSource(mCameraImages.getUris().get(position))
  // I tried this option too
  //.setLocalThumbnailPreviewsEnabled(false)
  .setResizeOptions(new ResizeOptions(1, 1))
  .build();

Resizing to 1x1 seems to pick some minimum size that is suitable. I should add that I haven't been able to beat Picasso w.r.t. loading performance for content:// images yet (and Picasso is pretty slow itself which is why I was checking out Fresco).

mikeknoop avatar Apr 18 '15 03:04 mikeknoop

You should definitely specify resizing, butResizeOptions(1, 1) doesn't seem right, why don't you pass the view's dimensions? Btw, if you do .setLocalThumbnailPreviewsEnabled(true) with .setResizeOptions(new ResizeOptions(1, 1)), and also do setControllerListener with the following listener, what do you get in the adb logcat?

ControllerListener listener = new BaseControllerListener<ImageInfo>() {
  @Override
  public void onIntermediateImageSet(String id, ImageInfo imageInfo) {
    android.util.Log.w(TAG, "intermediate: width " + imageInfo.getWidth() + ", " + "height " + imageInfo.getHeight());
  }
  @Override
  public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable a) {
    android.util.Log.w(TAG, "final: width " + imageInfo.getWidth() + ", " + "height " + imageInfo.getHeight());
  }
};

plamenko avatar Apr 18 '15 12:04 plamenko

Same issue, after using SimpleDraweeView:

The application may be doing too much work on its main thread;

In my case I have init the Fresco in Application, in the adapter use setImageUrl, then the app is closing to ANR with few response; The issue can be fixed setting ResizeOptions to SimpleDraweeView mentioned by @mikeknoop

ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(data.image))
                .setResizeOptions(new ResizeOptions(width, height))
                .build();
        holder.pic.setController(Fresco.newDraweeControllerBuilder()
                .setOldController(holder.pic.getController())
                .setImageRequest(request)
                .build());

avenwu avatar Jul 14 '15 06:07 avenwu

I just get the width and height when setImageUri()

public void setImageURI(Uri uri, @Nullable Object callerContext) { if (!hasHierarchy()) { inflateHierarchy(getContext()); } DraweeController controller; int width = getLayoutWidth() , height = getLayoutHeight(); if (this.mSimpleDraweeControllerBuilder instanceof PipelineDraweeControllerBuilder && width>0 && height >0) { ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri) .setResizeOptions(new ResizeOptions(width, height)) .setAutoRotateEnabled(true) .build(); PipelineDraweeControllerBuilder builder = (PipelineDraweeControllerBuilder) mSimpleDraweeControllerBuilder; controller = builder .setAutoPlayAnimations(true) .setCallerContext(callerContext) .setOldController(this.getController()) .setImageRequest(request) .build();

    } else
        controller = this.mSimpleDraweeControllerBuilder
                .setCallerContext(callerContext)
                .setUri(uri)
                .setOldController(this.getController())
                .build();

    this.setController(controller);

/** * {@inheritDoc} *

* Width is defined by target {@link android.view.View view} parameters, configuration * parameters or device display dimensions.
* Size computing algorithm (go by steps until get non-zero value):
* 1) Get the actual drawn getWidth() of the View
* 2) Get layout_width */ public int getLayoutWidth() { final ViewGroup.LayoutParams params = getLayoutParams(); int width = 0; if (params != null && params.width != ViewGroup.LayoutParams.WRAP_CONTENT) { width = getWidth(); // Get actual image width } if (width <= 0 && params != null) width = params.width; // Get layout width parameter if (width <= 0) { width = getImageViewFieldValue(this, "mMaxWidth"); // Check maxWidth parameter } return width; }

/**
 * {@inheritDoc}
 * <p/>
 * Height is defined by target {@link android.view.View view} parameters, configuration
 * parameters or device display dimensions.<br />
 * Size computing algorithm (go by steps until get non-zero value):<br />
 * 1) Get the actual drawn <b>getHeight()</b> of the View<br />
 * 2) Get <b>layout_height</b>
 */
public int getLayoutHeight() {
    final ViewGroup.LayoutParams params = getLayoutParams();
    int height = 0;
    if (params != null && params.height != ViewGroup.LayoutParams.WRAP_CONTENT) {
        height = getHeight(); // Get actual image height
    }
    if (height <= 0 && params != null) height = params.height; // Get layout height parameter
    if (height <= 0) {
        height = getImageViewFieldValue(this, "mMaxHeight"); // Check maxHeight parameter
    }
    return height;
}

private static int getImageViewFieldValue(Object object, String fieldName) {
    int value = 0;
    try {
        Field field = ImageView.class.getDeclaredField(fieldName);
        field.setAccessible(true);
        int fieldValue = (Integer) field.get(object);
        if (fieldValue > 0 && fieldValue < Integer.MAX_VALUE) {
            value = fieldValue;
        }
    } catch (Exception e) {
    }
    return value;
}

ohshi000 avatar Jul 23 '15 04:07 ohshi000

Setting ResizeOptions is indeed the correct solution. The reason frames were skipped is that Android was struggling to scale such a large image in a small view.

At most we could make this automatic some of the time - when the view is specified with an explicit size - but that won't work for match_parent type layouts.

tyronen avatar Oct 14 '15 16:10 tyronen