Home | History | Annotate | Download | only in fake-pipeline2
      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 JpegCompressor: private Thread, public virtual RefBase {
     42   public:
     43 
     44     JpegCompressor();
     45     ~JpegCompressor();
     46 
     47     struct JpegListener {
     48         // Called when JPEG compression has finished, or encountered an error
     49         virtual void onJpegDone(const StreamBuffer &jpegBuffer,
     50                 bool success) = 0;
     51         // Called when the input buffer for JPEG is not needed any more,
     52         // if the buffer came from the framework.
     53         virtual void onJpegInputDone(const StreamBuffer &inputBuffer) = 0;
     54         virtual ~JpegListener();
     55     };
     56 
     57     // Start compressing COMPRESSED format buffers; JpegCompressor takes
     58     // ownership of the Buffers vector.
     59     // Reserve() must be called first.
     60     status_t start(Buffers *buffers, JpegListener *listener);
     61 
     62     // Compress and block until buffer is complete.
     63     status_t compressSynchronous(Buffers *buffers);
     64 
     65     status_t cancel();
     66 
     67     bool isBusy();
     68     bool isStreamInUse(uint32_t id);
     69 
     70     bool waitForDone(nsecs_t timeout);
     71 
     72     // Reserve the compressor for a later start() call.
     73     status_t reserve();
     74 
     75     // TODO: Measure this
     76     static const size_t kMaxJpegSize = 300000;
     77 
     78   private:
     79     Mutex mBusyMutex;
     80     bool mIsBusy;
     81     Condition mDone;
     82     bool mSynchronous;
     83 
     84     Mutex mMutex;
     85 
     86     Buffers *mBuffers;
     87     JpegListener *mListener;
     88 
     89     StreamBuffer mJpegBuffer, mAuxBuffer;
     90     bool mFoundJpeg, mFoundAux;
     91 
     92     jpeg_compress_struct mCInfo;
     93 
     94     struct JpegError : public jpeg_error_mgr {
     95         JpegCompressor *parent;
     96     };
     97     j_common_ptr mJpegErrorInfo;
     98 
     99     struct JpegDestination : public jpeg_destination_mgr {
    100         JpegCompressor *parent;
    101     };
    102 
    103     static void jpegErrorHandler(j_common_ptr cinfo);
    104 
    105     static void jpegInitDestination(j_compress_ptr cinfo);
    106     static boolean jpegEmptyOutputBuffer(j_compress_ptr cinfo);
    107     static void jpegTermDestination(j_compress_ptr cinfo);
    108 
    109     bool checkError(const char *msg);
    110     status_t compress();
    111 
    112     void cleanUp();
    113 
    114     /**
    115      * Inherited Thread virtual overrides
    116      */
    117   private:
    118     virtual status_t readyToRun();
    119     virtual bool threadLoop();
    120 };
    121 
    122 } // namespace android
    123 
    124 #endif
    125