1/* 2 * Copyright (C) 2010 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 17package com.android.gallery3d.photoeditor.filters; 18 19import android.media.effect.Effect; 20import android.media.effect.EffectContext; 21import android.os.Parcel; 22import android.os.Parcelable; 23 24import com.android.gallery3d.photoeditor.Photo; 25 26import java.util.HashMap; 27 28/** 29 * Image filter for photo editing; all of its methods must be called from a single GL thread. 30 */ 31public abstract class Filter implements Parcelable { 32 33 // TODO: This should be set in MFF instead. 34 private static final int DEFAULT_TILE_SIZE = 640; 35 36 private static final HashMap<Filter, Effect> effects = new HashMap<Filter, Effect>(); 37 private static EffectContext context; 38 39 /** 40 * Filter context should be released before the current GL context is lost. 41 */ 42 public static void releaseContext() { 43 if (context != null) { 44 // Release all effects created with the releasing context. 45 for (Effect effect : effects.values()) { 46 effect.release(); 47 } 48 effects.clear(); 49 context.release(); 50 context = null; 51 } 52 } 53 54 public void release() { 55 Effect effect = effects.remove(this); 56 if (effect != null) { 57 effect.release(); 58 } 59 } 60 61 protected Effect getEffect(String name) { 62 Effect effect = effects.get(this); 63 if (effect == null) { 64 if (context == null) { 65 context = EffectContext.createWithCurrentGlContext(); 66 } 67 effect = context.getFactory().createEffect(name); 68 effect.setParameter("tile_size", DEFAULT_TILE_SIZE); 69 effects.put(this, effect); 70 } 71 return effect; 72 } 73 74 /** 75 * Processes the source bitmap and matrix and output the destination bitmap and matrix. 76 * 77 * @param src source photo as the input. 78 * @param dst destination photo having the same dimension as source photo as the output. 79 */ 80 public abstract void process(Photo src, Photo dst); 81 82 /** 83 * Instantiates CREATOR of subclasses for Parcelable implementations. 84 */ 85 protected static <T extends Filter> Parcelable.Creator<T> creatorOf(Class<T> filterClass) { 86 return new FilterCreator<T>(filterClass); 87 } 88 89 /** 90 * Saves states for restoring filter later; subclasses can override this to persist states. 91 */ 92 protected void writeToParcel(Parcel out) { 93 } 94 95 /** 96 * Restores filter from the saved states; subclasses can override this to persist states. 97 */ 98 protected void readFromParcel(Parcel in) { 99 } 100 101 @Override 102 public int describeContents() { 103 return 0; 104 } 105 106 @Override 107 public void writeToParcel(Parcel dest, int flags) { 108 writeToParcel(dest); 109 } 110} 111