• 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 
33 #include <stdio.h>
34 
35 extern "C" {
36 #include <jpeglib.h>
37 }
38 
39 namespace android {
40 
41 class EmulatedFakeCamera2;
42 
43 class JpegCompressor: private Thread, public virtual RefBase {
44   public:
45 
46     JpegCompressor(EmulatedFakeCamera2 *parent);
47     ~JpegCompressor();
48 
49     // Start compressing COMPRESSED format buffers; JpegCompressor takes
50     // ownership of the Buffers vector.
51     status_t start(Buffers *buffers,
52             nsecs_t captureTime);
53 
54     status_t cancel();
55 
56     bool isBusy();
57     bool isStreamInUse(uint32_t id);
58 
59     bool waitForDone(nsecs_t timeout);
60 
61     // TODO: Measure this
62     static const size_t kMaxJpegSize = 300000;
63 
64   private:
65     Mutex mBusyMutex;
66     bool mIsBusy;
67     Condition mDone;
68 
69     Mutex mMutex;
70 
71     EmulatedFakeCamera2 *mParent;
72 
73     Buffers *mBuffers;
74     nsecs_t mCaptureTime;
75 
76     StreamBuffer mJpegBuffer, mAuxBuffer;
77     bool mFoundJpeg, mFoundAux;
78 
79     jpeg_compress_struct mCInfo;
80 
81     struct JpegError : public jpeg_error_mgr {
82         JpegCompressor *parent;
83     };
84     j_common_ptr mJpegErrorInfo;
85 
86     struct JpegDestination : public jpeg_destination_mgr {
87         JpegCompressor *parent;
88     };
89 
90     static void jpegErrorHandler(j_common_ptr cinfo);
91 
92     static void jpegInitDestination(j_compress_ptr cinfo);
93     static boolean jpegEmptyOutputBuffer(j_compress_ptr cinfo);
94     static void jpegTermDestination(j_compress_ptr cinfo);
95 
96     bool checkError(const char *msg);
97     void cleanUp();
98 
99     /**
100      * Inherited Thread virtual overrides
101      */
102   private:
103     virtual status_t readyToRun();
104     virtual bool threadLoop();
105 };
106 
107 } // namespace android
108 
109 #endif
110