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.pipeline; 18 19 import android.graphics.Bitmap; 20 21 import com.android.gallery3d.filtershow.filters.FiltersManager; 22 import com.android.gallery3d.filtershow.imageshow.MasterImage; 23 24 public class UpdatePreviewTask extends ProcessingTask { 25 private static final String LOGTAG = "UpdatePreviewTask"; 26 private CachingPipeline mPreviewPipeline = null; 27 private boolean mHasUnhandledPreviewRequest = false; 28 private boolean mPipelineIsOn = false; 29 30 public UpdatePreviewTask() { 31 mPreviewPipeline = new CachingPipeline( 32 FiltersManager.getPreviewManager(), "Preview"); 33 } 34 35 public void setOriginal(Bitmap bitmap) { 36 mPreviewPipeline.setOriginal(bitmap); 37 mPipelineIsOn = true; 38 } 39 40 public void updatePreview() { 41 if (!mPipelineIsOn) { 42 return; 43 } 44 mHasUnhandledPreviewRequest = true; 45 if (postRequest(null)) { 46 mHasUnhandledPreviewRequest = false; 47 } 48 } 49 50 @Override 51 public boolean isPriorityTask() { 52 return true; 53 } 54 55 @Override 56 public Result doInBackground(Request message) { 57 SharedBuffer buffer = MasterImage.getImage().getPreviewBuffer(); 58 SharedPreset preset = MasterImage.getImage().getPreviewPreset(); 59 ImagePreset renderingPreset = preset.dequeuePreset(); 60 if (renderingPreset != null) { 61 mPreviewPipeline.compute(buffer, renderingPreset, 0); 62 // set the preset we used in the buffer for later inspection UI-side 63 buffer.getProducer().setPreset(renderingPreset); 64 buffer.getProducer().sync(); 65 buffer.swapProducer(); // push back the result 66 } 67 return null; 68 } 69 70 @Override 71 public void onResult(Result message) { 72 MasterImage.getImage().notifyObservers(); 73 if (mHasUnhandledPreviewRequest) { 74 updatePreview(); 75 } 76 } 77 78 public void setPipelineIsOn(boolean pipelineIsOn) { 79 mPipelineIsOn = pipelineIsOn; 80 } 81 } 82