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 
18 /**
19  * This class simulates a hardware JPEG compressor.  It receives image buffers
20  * in RGBA_8888 format, processes them in a worker thread, and then pushes them
21  * out to their destination stream.
22  */
23 
24 #ifndef HW_EMULATOR_CAMERA2_JPEG_H
25 #define HW_EMULATOR_CAMERA2_JPEG_H
26 
27 #include "utils/Thread.h"
28 #include "utils/Mutex.h"
29 #include "utils/Timers.h"
30 
31 #include "Base.h"
32 #include "../JpegCompressor.h"
33 #include <CameraMetadata.h>
34 #include <ui/GraphicBufferMapper.h>
35 
36 #include <stdio.h>
37 
38 extern "C" {
39 #include <jpeglib.h>
40 }
41 
42 using ::android::hardware::camera::common::V1_0::helper::CameraMetadata;
43 
44 namespace android {
45 
46 class JpegCompressor: private Thread, public virtual RefBase {
47   public:
48 
49     JpegCompressor(GraphicBufferMapper* gbm);
50     ~JpegCompressor();
51 
52     struct JpegListener {
53         // Called when JPEG compression has finished, or encountered an error
54         virtual void onJpegDone(const StreamBuffer &jpegBuffer,
55                 bool success) = 0;
56         // Called when the input buffer for JPEG is not needed any more,
57         // if the buffer came from the framework.
58         virtual void onJpegInputDone(const StreamBuffer &inputBuffer) = 0;
59         virtual ~JpegListener();
60     };
61 
62     // Start compressing COMPRESSED format buffers; JpegCompressor takes
63     // ownership of the Buffers vector.
64     // Reserve() must be called first.
65     status_t start(Buffers *buffers, JpegListener *listener, CameraMetadata* settings);
66 
67     // Compress and block until buffer is complete.
68     status_t compressSynchronous(Buffers *buffers);
69 
70     status_t cancel();
71 
72     bool isBusy();
73     bool isStreamInUse(uint32_t id);
74 
75     bool waitForDone(nsecs_t timeout);
76 
77     // Reserve the compressor for a later start() call.
78     status_t reserve();
79 
80     // TODO: Measure this
81     static const size_t kMaxJpegSize = 675000;
82 
83   private:
84     Mutex mBusyMutex;
85     bool mIsBusy;
86     Condition mDone;
87     bool mSynchronous;
88 
89     Mutex mMutex;
90 
91     Buffers *mBuffers;
92     JpegListener *mListener;
93     GraphicBufferMapper *mGBM;
94 
95     StreamBuffer mJpegBuffer, mAuxBuffer;
96     bool mFoundJpeg, mFoundAux;
97     CameraMetadata mSettings;
98 
99     status_t compress();
100 
101     void cleanUp();
102 
103     /**
104      * Inherited Thread virtual overrides
105      */
106   private:
107     virtual status_t readyToRun();
108     virtual bool threadLoop();
109 };
110 
111 } // namespace android
112 
113 #endif
114