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 android.graphics.Rect;
20
21import com.android.camera.processing.memory.LruResourcePool;
22import com.android.camera.session.CaptureSession;
23import com.android.camera.util.Size;
24
25import java.nio.ByteBuffer;
26import java.util.concurrent.Executor;
27
28/**
29 * Implements the conversion of a YUV_420_888 image to subsampled image
30 * inscribed in a circle.
31 */
32public class TaskPreviewChainedJpeg extends TaskConvertImageToRGBPreview {
33    private final LruResourcePool<Integer, ByteBuffer> mByteBufferDirectPool;
34
35    /**
36     * Constructor
37     *
38     * @param image Image that the computation is dependent on
39     * @param executor Executor to fire off an events
40     * @param imageTaskManager Image task manager that allows reference counting
41     *            and task spawning
42     * @param captureSession Capture session that bound to this image
43     * @param targetSize Approximate viewable pixel demensions of the desired
44     *            preview Image     */
45    TaskPreviewChainedJpeg(ImageToProcess image,
46            Executor executor,
47            ImageTaskManager imageTaskManager,
48            CaptureSession captureSession,
49            Size targetSize,
50            LruResourcePool<Integer, ByteBuffer> byteBufferResourcePool) {
51        super(image, executor, imageTaskManager, ProcessingPriority.AVERAGE, captureSession,
52                targetSize , ThumbnailShape.MAINTAIN_ASPECT_NO_INSET);
53        mByteBufferDirectPool = byteBufferResourcePool;
54    }
55
56    public void logWrapper(String message) {
57        // final Log.Tag TAG = new Log.Tag("TaskPreviewChainedJpeg");
58        // Log.v(TAG, message);
59    }
60
61    @Override
62    public void run() {
63        ImageToProcess img = mImage;
64        Rect safeCrop = guaranteedSafeCrop(img.proxy, img.crop);
65
66        final TaskImage inputImage = calculateInputImage(img, safeCrop);
67        final int subsample = calculateBestSubsampleFactor(
68                new Size(safeCrop.width(), safeCrop.height()),
69                mTargetSize);
70        final TaskImage resultImage = calculateResultImage(img, subsample);
71        final int[] convertedImage;
72
73        try {
74            onStart(mId, inputImage, resultImage, TaskInfo.Destination.INTERMEDIATE_THUMBNAIL);
75
76            logWrapper("TIMER_END Rendering preview YUV buffer available, w=" + img.proxy.getWidth()
77                    / subsample + " h=" + img.proxy.getHeight() / subsample + " of subsample "
78                    + subsample);
79
80            convertedImage = runSelectedConversion(img.proxy, safeCrop, subsample);
81
82            // Chain JPEG task
83            TaskImageContainer jpegTask = new TaskCompressImageToJpeg(img, mExecutor,
84                    mImageTaskManager, mSession, mByteBufferDirectPool);
85            mImageTaskManager.appendTasks(img, jpegTask);
86        } finally {
87            // Signal backend that reference has been released
88            mImageTaskManager.releaseSemaphoreReference(img, mExecutor);
89        }
90
91        onPreviewDone(resultImage, inputImage, convertedImage,
92                TaskInfo.Destination.INTERMEDIATE_THUMBNAIL);
93    }
94
95
96}
97