TaskImageContainer.java revision 3830d419691ef865f01b362fee9618bac2aa8888
1/* 2 * Copyright (C) 2014 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.camera.processing.imagebackend; 18 19import com.android.camera.app.OrientationManager; 20import com.android.camera.debug.Log; 21import com.android.camera.session.CaptureSession; 22 23import java.util.concurrent.Executor; 24 25/** 26 * TaskImageContainer are the base class of tasks that wish to run with the ImageBackend class. It 27 * contains the basic information required to interact with the ImageBackend class and the ability 28 * to identify itself to the UI backend for updates on its progress. 29 */ 30public abstract class TaskImageContainer implements Runnable { 31 32 33 /** 34 * Simple helper class to encapsulate uncompressed payloads. Could be more complex in 35 * the future. 36 */ 37 static public class UncompressedPayload { 38 final public int [] data; 39 UncompressedPayload(int [] passData) { 40 data = passData; 41 } 42 } 43 44 45 /** 46 * Simple helper class to encapsulate compressed payloads. Could be more complex in 47 * the future. 48 */ 49 static public class CompressedPayload { 50 final public byte [] data; 51 CompressedPayload(byte [] passData) { 52 data = passData; 53 } 54 } 55 56 /** 57 * Simple helper class to encapsulate all necessary image information that is carried with the 58 * data to processing, so that tasks derived off of TaskImageContainer can properly coordinate 59 * and optimize its computation. 60 */ 61 static public class TaskImage { 62 // Addendum to Android-defined image-format 63 public final static int EXTRA_USER_DEFINED_FORMAT_ARGB_8888 = -1; 64 65 // Minimal required knowledge for the image specification. 66 public final OrientationManager.DeviceOrientation orientation; 67 68 public final int height; 69 70 public final int width; 71 72 public final int format; 73 74 TaskImage(OrientationManager.DeviceOrientation anOrientation, int aWidth, int aHeight, 75 int aFormat) { 76 orientation = anOrientation; 77 height = aHeight; 78 width = aWidth; 79 format = aFormat; 80 } 81 82 } 83 84 /** 85 * Simple helper class to encapsulate input and resultant image specification. 86 * TasksImageContainer classes can be uniquely identified by triplet of its content (currently, 87 * the global timestamp of when the object was taken), the image specification of the input and 88 * the desired output image specification. 89 */ 90 static public class TaskInfo { 91 // The unique Id of the image being processed. 92 public final long contentId; 93 94 public final TaskImage input; 95 96 public final TaskImage result; 97 98 TaskInfo(long aContentId, TaskImage inputSpec, TaskImage outputSpec) { 99 contentId = aContentId; 100 input = inputSpec; 101 result = outputSpec; 102 } 103 104 } 105 106 public enum ProcessingPriority { 107 FAST, SLOW 108 } 109 110 protected final static Log.Tag TAG = new Log.Tag("TaskImgContain"); 111 112 final protected ImageBackend mImageBackend; 113 114 final protected Executor mExecutor; 115 116 final protected long mId; 117 118 final protected ProcessingPriority mProcessingPriority; 119 120 final protected ImageToProcess mImage; 121 122 final protected CaptureSession mSession; 123 124 /** 125 * Constructor when releasing the image reference. 126 * 127 * @param otherTask the original task that is spawning this task. 128 * @param processingPriority Priority that the derived task will run at. 129 */ 130 public TaskImageContainer(TaskImageContainer otherTask, ProcessingPriority processingPriority) { 131 mId = otherTask.mId; 132 mExecutor = otherTask.mExecutor; 133 mImageBackend = otherTask.mImageBackend; 134 mProcessingPriority = processingPriority; 135 mSession = otherTask.mSession; 136 mImage = null; 137 } 138 139 /** 140 * Constructor to use when keeping the image reference. 141 * 142 * @param image Image reference that needs to be released. 143 * @param Executor Executor to run the event handling 144 * @param imageBackend a reference to the ImageBackend, in case, you need to spawn other tasks 145 * @param preferredLane Priority that the derived task will run at 146 * @param captureSession Session that handles image processing events 147 */ 148 public TaskImageContainer(ImageToProcess image, Executor Executor, ImageBackend imageBackend, 149 ProcessingPriority preferredLane, CaptureSession captureSession) { 150 mImage = image; 151 mId = mImage.proxy.getTimestamp(); 152 mExecutor = Executor; 153 mImageBackend = imageBackend; 154 mProcessingPriority = preferredLane; 155 mSession = captureSession; 156 } 157 158 /** 159 * Basic listener function to signal ImageBackend that task has started. 160 * 161 * @param id Id for image content 162 * @param input Image specification for task input 163 * @param result Image specification for task result 164 */ 165 public void onStart(long id, TaskImage input, TaskImage result) { 166 TaskInfo job = new TaskInfo(id, input, result); 167 final ImageProcessorListener listener = mImageBackend.getProxyListener(); 168 listener.onStart(job); 169 } 170 171 /** 172 * Getter for Processing Priority 173 * 174 * @return Processing Priority associated with the task. 175 */ 176 public ProcessingPriority getProcessingPriority() { 177 return mProcessingPriority; 178 } 179} 180