Home | History | Annotate | Download | only in presets
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.gallery3d.filtershow.presets;
     18 
     19 import android.graphics.Bitmap;
     20 import android.support.v8.renderscript.Allocation;
     21 import android.util.Log;
     22 
     23 import com.android.gallery3d.filtershow.cache.CachingPipeline;
     24 import com.android.gallery3d.filtershow.filters.FilterRepresentation;
     25 import com.android.gallery3d.filtershow.filters.FiltersManager;
     26 import com.android.gallery3d.filtershow.filters.ImageFilter;
     27 
     28 import java.lang.ref.WeakReference;
     29 import java.util.HashMap;
     30 
     31 public class FilterEnvironment {
     32     private static final String LOGTAG = "FilterEnvironment";
     33     private ImagePreset mImagePreset;
     34     private float mScaleFactor;
     35     private int mQuality;
     36     private FiltersManager mFiltersManager;
     37     private CachingPipeline mCachingPipeline;
     38     private volatile boolean mStop = false;
     39 
     40     public synchronized boolean needsStop() {
     41         return mStop;
     42     }
     43 
     44     public synchronized void setStop(boolean stop) {
     45         this.mStop = stop;
     46     }
     47 
     48     private HashMap<Long, WeakReference<Bitmap>>
     49             bitmapCach = new HashMap<Long, WeakReference<Bitmap>>();
     50 
     51     public void cache(Bitmap bitmap) {
     52         if (bitmap == null) {
     53             return;
     54         }
     55         Long key = calcKey(bitmap.getWidth(), bitmap.getHeight());
     56         bitmapCach.put(key, new WeakReference<Bitmap>(bitmap));
     57     }
     58 
     59     public Bitmap getBitmap(int w, int h) {
     60         Long key = calcKey(w, h);
     61         WeakReference<Bitmap> ref = bitmapCach.remove(key);
     62         Bitmap bitmap = null;
     63         if (ref != null) {
     64             bitmap = ref.get();
     65         }
     66         if (bitmap == null) {
     67             bitmap = Bitmap.createBitmap(
     68                     w, h, Bitmap.Config.ARGB_8888);
     69         }
     70         return bitmap;
     71     }
     72 
     73     private Long calcKey(long w, long h) {
     74         return (w << 32) | (h << 32);
     75     }
     76 
     77     public void setImagePreset(ImagePreset imagePreset) {
     78         mImagePreset = imagePreset;
     79     }
     80 
     81     public ImagePreset getImagePreset() {
     82         return mImagePreset;
     83     }
     84 
     85     public void setScaleFactor(float scaleFactor) {
     86         mScaleFactor = scaleFactor;
     87     }
     88 
     89     public float getScaleFactor() {
     90         return mScaleFactor;
     91     }
     92 
     93     public void setQuality(int quality) {
     94         mQuality = quality;
     95     }
     96 
     97     public int getQuality() {
     98         return mQuality;
     99     }
    100 
    101     public void setFiltersManager(FiltersManager filtersManager) {
    102         mFiltersManager = filtersManager;
    103     }
    104 
    105     public FiltersManager getFiltersManager() {
    106         return mFiltersManager;
    107     }
    108 
    109     public void applyRepresentation(FilterRepresentation representation,
    110                                     Allocation in, Allocation out) {
    111         ImageFilter filter = mFiltersManager.getFilterForRepresentation(representation);
    112         filter.useRepresentation(representation);
    113         filter.setEnvironment(this);
    114         if (filter.supportsAllocationInput()) {
    115             filter.apply(in, out);
    116         }
    117         filter.setEnvironment(null);
    118     }
    119 
    120     public Bitmap applyRepresentation(FilterRepresentation representation, Bitmap bitmap) {
    121         ImageFilter filter = mFiltersManager.getFilterForRepresentation(representation);
    122         filter.useRepresentation(representation);
    123         filter.setEnvironment(this);
    124         Bitmap ret = filter.apply(bitmap, mScaleFactor, mQuality);
    125         filter.setEnvironment(null);
    126         return ret;
    127     }
    128 
    129     public CachingPipeline getCachingPipeline() {
    130         return mCachingPipeline;
    131     }
    132 
    133     public void setCachingPipeline(CachingPipeline cachingPipeline) {
    134         mCachingPipeline = cachingPipeline;
    135     }
    136 
    137 }
    138