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
17package com.android.gallery3d.filtershow.pipeline;
18
19import android.content.Context;
20import android.graphics.Bitmap;
21import android.graphics.Rect;
22import com.android.gallery3d.app.Log;
23import com.android.gallery3d.filtershow.FilterShowActivity;
24import com.android.gallery3d.filtershow.cache.BitmapCache;
25import com.android.gallery3d.filtershow.filters.FiltersManager;
26import com.android.gallery3d.filtershow.imageshow.MasterImage;
27
28public class RenderingRequest {
29    private static final String LOGTAG = "RenderingRequest";
30    private boolean mIsDirect = false;
31    private Bitmap mBitmap = null;
32    private ImagePreset mImagePreset = null;
33    private ImagePreset mOriginalImagePreset = null;
34    private RenderingRequestCaller mCaller = null;
35    private float mScaleFactor = 1.0f;
36    private Rect mBounds = null;
37    private Rect mDestination = null;
38    private Rect mIconBounds = null;
39    private int mType = FULL_RENDERING;
40    public static final int FULL_RENDERING = 0;
41    public static final int FILTERS_RENDERING = 1;
42    public static final int GEOMETRY_RENDERING = 2;
43    public static final int ICON_RENDERING = 3;
44    public static final int PARTIAL_RENDERING = 4;
45    public static final int HIGHRES_RENDERING = 5;
46    public static final int STYLE_ICON_RENDERING = 6;
47
48    private static final Bitmap.Config mConfig = Bitmap.Config.ARGB_8888;
49
50    public static void post(Context context, Bitmap source, ImagePreset preset,
51                            int type, RenderingRequestCaller caller) {
52        RenderingRequest.post(context, source, preset, type, caller, null, null);
53    }
54
55    public static void post(Context context, Bitmap source, ImagePreset preset, int type,
56                            RenderingRequestCaller caller, Rect bounds, Rect destination) {
57        if (((type != PARTIAL_RENDERING && type != HIGHRES_RENDERING
58                && type != GEOMETRY_RENDERING && type != FILTERS_RENDERING) && source == null)
59                || preset == null || caller == null) {
60            Log.v(LOGTAG, "something null: source: " + source
61                    + " or preset: " + preset + " or caller: " + caller);
62            return;
63        }
64        RenderingRequest request = new RenderingRequest();
65        Bitmap bitmap = null;
66        if (type == FULL_RENDERING
67                || type == ICON_RENDERING
68                || type == STYLE_ICON_RENDERING) {
69            CachingPipeline pipeline = new CachingPipeline(
70                    FiltersManager.getManager(), "Icon");
71            bitmap = pipeline.renderGeometryIcon(source, preset);
72        } else if (type != PARTIAL_RENDERING && type != HIGHRES_RENDERING
73                && type != GEOMETRY_RENDERING && type != FILTERS_RENDERING) {
74            bitmap = MasterImage.getImage().getBitmapCache().getBitmap(
75                    source.getWidth(), source.getHeight(), BitmapCache.RENDERING_REQUEST);
76        }
77
78        request.setBitmap(bitmap);
79        ImagePreset passedPreset = new ImagePreset(preset);
80        request.setOriginalImagePreset(preset);
81        request.setScaleFactor(MasterImage.getImage().getScaleFactor());
82
83        if (type == PARTIAL_RENDERING) {
84            request.setBounds(bounds);
85            request.setDestination(destination);
86            passedPreset.setPartialRendering(true, bounds);
87        }
88
89        request.setImagePreset(passedPreset);
90        request.setType(type);
91        request.setCaller(caller);
92        request.post(context);
93    }
94
95    public static void postIconRequest(Context context, int w, int h,
96                                       ImagePreset preset,
97                                       RenderingRequestCaller caller) {
98        if (preset == null || caller == null) {
99            Log.v(LOGTAG, "something null, preset: "
100                    + preset + " or caller: " + caller);
101            return;
102        }
103        RenderingRequest request = new RenderingRequest();
104        ImagePreset passedPreset = new ImagePreset(preset);
105        request.setOriginalImagePreset(preset);
106        request.setScaleFactor(MasterImage.getImage().getScaleFactor());
107        request.setImagePreset(passedPreset);
108        request.setType(RenderingRequest.ICON_RENDERING);
109        request.setCaller(caller);
110        request.setIconBounds(new Rect(0, 0, w, h));
111        request.post(context);
112    }
113
114    public void post(Context context) {
115        if (context instanceof FilterShowActivity) {
116            FilterShowActivity activity = (FilterShowActivity) context;
117            ProcessingService service = activity.getProcessingService();
118            service.postRenderingRequest(this);
119        }
120    }
121
122    public void markAvailable() {
123        if (mBitmap == null || mImagePreset == null
124                || mCaller == null) {
125            return;
126        }
127        mCaller.available(this);
128    }
129
130    public boolean isDirect() {
131        return mIsDirect;
132    }
133
134    public void setDirect(boolean isDirect) {
135        mIsDirect = isDirect;
136    }
137
138    public Bitmap getBitmap() {
139        return mBitmap;
140    }
141
142    public void setBitmap(Bitmap bitmap) {
143        mBitmap = bitmap;
144    }
145
146    public ImagePreset getImagePreset() {
147        return mImagePreset;
148    }
149
150    public void setImagePreset(ImagePreset imagePreset) {
151        mImagePreset = imagePreset;
152    }
153
154    public int getType() {
155        return mType;
156    }
157
158    public void setType(int type) {
159        mType = type;
160    }
161
162    public void setCaller(RenderingRequestCaller caller) {
163        mCaller = caller;
164    }
165
166    public Rect getBounds() {
167        return mBounds;
168    }
169
170    public void setBounds(Rect bounds) {
171        mBounds = bounds;
172    }
173
174    public void setScaleFactor(float scaleFactor) {
175        mScaleFactor = scaleFactor;
176    }
177
178    public float getScaleFactor() {
179        return mScaleFactor;
180    }
181
182    public Rect getDestination() {
183        return mDestination;
184    }
185
186    public void setDestination(Rect destination) {
187        mDestination = destination;
188    }
189
190    public void setIconBounds(Rect bounds) {
191        mIconBounds = bounds;
192    }
193
194    public Rect getIconBounds() {
195        return mIconBounds;
196    }
197
198    public ImagePreset getOriginalImagePreset() {
199        return mOriginalImagePreset;
200    }
201
202    public void setOriginalImagePreset(ImagePreset originalImagePreset) {
203        mOriginalImagePreset = originalImagePreset;
204    }
205}
206