fresco icon indicating copy to clipboard operation
fresco copied to clipboard

Use callback method to get Bitmap reference ?

Open leelei opened this issue 11 years ago • 3 comments

I use Palette.generateAsync , so I need get a Bitmap reference

Postprocessor palettePostprocessor = new Postprocessor() { 
  @Override
  public String getName() {
    return "PalettePostprocessor";
  }

  @Override
  public void process(Bitmap bitmap) {
     //Palette.generateAsync(bitmap, paletteAsyncListener)
    }
  }
}

but , it is a copy of origin Bitmap ,because of performance , so i just use this

 setPostprocessor(new Postprocessor() {
        @Override public CloseableReference<Bitmap> process(Bitmap sourceBitmap,
            PlatformBitmapFactory bitmapFactory) {
          return CloseableReference.of(sourceBitmap, new ResourceReleaser<Bitmap>() {
            @Override public void release(Bitmap value) {
            }
          });
        }

        @Override public String getName() {
          return null;
        }
      })

in release(Bitmap value) do nothing( it will be auto recycle ?), it is ok ?

leelei avatar Apr 19 '15 09:04 leelei

Hi. No, this is not a proper way of doing it as the source bitmap can be recycled immediately after this method finishes. I'd strongly suggest that you don't deal with CloseableReferences as it is easy to get the things wrong. One question to answer first: Do you really have performance problems because the postprocessor makes a copy or is this really a premature optimization? If it's the later, then I suggest that you just override public void process(Bitmap bitmap) and do Palette.generate there. No need for Palette.generateAsync as postprocessors run on the background thread anyway. If you however do see perf issues, then I'd suggest that you don't use postprocessor at all. Postprocessor is intended for modifying the image (in which case we have to make a copy, otherwise you'd overwrite the source bitmap that is cached and may be used by other clients). Instead, use the image pipeline directly to get the bitmap and then generate the palette information from it. See http://frescolib.org/docs/using-image-pipeline.html#_ and how to use BaseBitmapDataSubscriber in http://frescolib.org/docs/datasources-datasubscribers.html#_.

plamenko avatar Apr 19 '15 13:04 plamenko

Thanks, I see dataSource.subscribe(dataSubscriber, executor); use UiThreadExecutorService.getInstance() as executor is OK ? override public void process(Bitmap bitmap) and do Palette.generate, if Palette.generatecost long time, bitmap cannot immediately return , i think

leelei avatar Apr 19 '15 15:04 leelei

I wouldn't use UiThreadExecutorService. You have two options:

  1. You can use some background executor. Once the pipeline has the bitmap ready, it will execute your callback on that background executor. Since the callback is executed on a background thread, it is fine to do a longer operation synchronously. I.e. you can just call Palette.generate directly.
  2. You can use CallerThreadExecutor.getInstance() which will execute your callback on whatever thread the image pipeline was executed last. This is going to be the main (UI) thread if the image is found in the bitmap cache, or some other background thread if the image had to be decoded. Since your callback may get executed on the main thread, you shouldn't do long running operations on it so you shoul use Palette.generateAsync in that case.

If you choose the option 1., don't forget to clone your reference in order to keep it alive even after your callback is executed, as the asynchronous work started from it may still require that reference. Also, don't forget to close the clonned reference properly after your heavy work is complete. See http://frescolib.org/docs/closeable-references.html#_

To summarize, you want to avoid doing any heavy work on the main thread, as that would block the UI. To achieve that, you can either tell the pipeline to execute your callback directly (CallerThreadExecutor) and then you have to manually submit the heavy operation on some background thread (luckily Palette has the generateAsync method). Or, you can tell the pipeline to execute your callback on a background thread, in which case you can do the heavy work in the callback (i.e. call Palette.generate directly).

If you are familiar with executors, I'd suggest using some background executor and just do Palette.generate in your callback. This is definitely the easiest and the safest option.

plamenko avatar Apr 19 '15 16:04 plamenko