• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
35 #include <stdio.h>
36 
37 extern "C" {
38 #include <jpeglib.h>
39 }
40 
41 using ::android::hardware::camera::common::V1_0::helper::CameraMetadata;
42 
43 namespace android {
44 
45 class JpegCompressor: private Thread, public virtual RefBase {
46   public:
47 
48     JpegCompressor();
49     ~JpegCompressor();
50 
51     struct JpegListener {
52         // Called when JPEG compression has finished, or encountered an error
53         virtual void onJpegDone(const StreamBuffer &jpegBuffer,
54                 bool success) = 0;
55         // Called when the input buffer for JPEG is not needed any more,
56         // if the buffer came from the framework.
57         virtual void onJpegInputDone(const StreamBuffer &inputBuffer) = 0;
58         virtual ~JpegListener();
59     };
60 
61     // Start compressing COMPRESSED format buffers; JpegCompressor takes
62     // ownership of the Buffers vector.
63     // Reserve() must be called first.
64     status_t start(Buffers *buffers, JpegListener *listener, CameraMetadata* settings);
65 
66     // Compress and block until buffer is complete.
67     status_t compressSynchronous(Buffers *buffers);
68 
69     status_t cancel();
70 
71     bool isBusy();
72     bool isStreamInUse(uint32_t id);
73 
74     bool waitForDone(nsecs_t timeout);
75 
76     // Reserve the compressor for a later start() call.
77     status_t reserve();
78 
79     // TODO: Measure this
80     static const size_t kMaxJpegSize = 300000;
81 
82   private:
83     Mutex mBusyMutex;
84     bool mIsBusy;
85     Condition mDone;
86     bool mSynchronous;
87 
88     Mutex mMutex;
89 
90     Buffers *mBuffers;
91     JpegListener *mListener;
92 
93     StreamBuffer mJpegBuffer, mAuxBuffer;
94     bool mFoundJpeg, mFoundAux;
95     CameraMetadata mSettings;
96 
97     status_t compress();
98 
99     void cleanUp();
100 
101     /**
102      * Inherited Thread virtual overrides
103      */
104   private:
105     virtual status_t readyToRun();
106     virtual bool threadLoop();
107 };
108 
109 } // namespace android
110 
111 #endif
112