1/*
2 * Copyright (C) 2012 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#ifndef ANDROID_SERVERS_CAMERA_CAMERA2_ZSLPROCESSOR_H
18#define ANDROID_SERVERS_CAMERA_CAMERA2_ZSLPROCESSOR_H
19
20#include <utils/Thread.h>
21#include <utils/String16.h>
22#include <utils/Vector.h>
23#include <utils/Mutex.h>
24#include <utils/Condition.h>
25#include <gui/BufferItemConsumer.h>
26#include "Parameters.h"
27#include "FrameProcessor.h"
28#include "CameraMetadata.h"
29#include "Camera2Heap.h"
30#include "../Camera2Device.h"
31
32namespace android {
33
34class Camera2Client;
35
36namespace camera2 {
37
38class CaptureSequencer;
39
40/***
41 * ZSL queue processing
42 */
43class ZslProcessor:
44            virtual public Thread,
45            virtual public BufferItemConsumer::FrameAvailableListener,
46            virtual public FrameProcessor::FilteredListener,
47            virtual public Camera2Device::BufferReleasedListener {
48  public:
49    ZslProcessor(wp<Camera2Client> client, wp<CaptureSequencer> sequencer);
50    ~ZslProcessor();
51
52    // From mZslConsumer
53    virtual void onFrameAvailable();
54    // From FrameProcessor
55    virtual void onFrameAvailable(int32_t frameId, const CameraMetadata &frame);
56
57    virtual void onBufferReleased(buffer_handle_t *handle);
58
59    status_t updateStream(const Parameters &params);
60    status_t deleteStream();
61    int getStreamId() const;
62    int getReprocessStreamId() const;
63
64    status_t pushToReprocess(int32_t requestId);
65    status_t clearZslQueue();
66
67    void dump(int fd, const Vector<String16>& args) const;
68  private:
69    static const nsecs_t kWaitDuration = 10000000; // 10 ms
70
71    enum {
72        RUNNING,
73        LOCKED
74    } mState;
75
76    wp<Camera2Client> mClient;
77    wp<CaptureSequencer> mSequencer;
78
79    mutable Mutex mInputMutex;
80    bool mZslBufferAvailable;
81    Condition mZslBufferAvailableSignal;
82
83    enum {
84        NO_STREAM = -1
85    };
86
87    int mZslStreamId;
88    int mZslReprocessStreamId;
89    sp<BufferItemConsumer> mZslConsumer;
90    sp<ANativeWindow>      mZslWindow;
91
92    struct ZslPair {
93        BufferItemConsumer::BufferItem buffer;
94        CameraMetadata frame;
95    };
96
97    static const size_t kZslBufferDepth = 4;
98    static const size_t kFrameListDepth = kZslBufferDepth * 2;
99    Vector<CameraMetadata> mFrameList;
100    size_t mFrameListHead;
101
102    ZslPair mNextPair;
103
104    Vector<ZslPair> mZslQueue;
105    size_t mZslQueueHead;
106    size_t mZslQueueTail;
107
108    CameraMetadata mLatestCapturedRequest;
109
110    virtual bool threadLoop();
111
112    status_t processNewZslBuffer(sp<Camera2Client> &client);
113
114    // Match up entries from frame list to buffers in ZSL queue
115    void findMatchesLocked();
116
117    status_t clearZslQueueLocked();
118
119    void dumpZslQueue(int id) const;
120};
121
122
123}; //namespace camera2
124}; //namespace android
125
126#endif
127