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     status_t start(Buffers *buffers, JpegListener *listener);
     60 
     61     // Compress and block until buffer is complete.
     62     status_t compressSynchronous(Buffers *buffers);
     63 
     64     status_t cancel();
     65 
     66     bool isBusy();
     67     bool isStreamInUse(uint32_t id);
     68 
     69     bool waitForDone(nsecs_t timeout);
     70 
     71     // TODO: Measure this
     72     static const size_t kMaxJpegSize = 300000;
     73 
     74   private:
     75     Mutex mBusyMutex;
     76     bool mIsBusy;
     77     Condition mDone;
     78     bool mSynchronous;
     79 
     80     Mutex mMutex;
     81 
     82     Buffers *mBuffers;
     83     JpegListener *mListener;
     84 
     85     StreamBuffer mJpegBuffer, mAuxBuffer;
     86     bool mFoundJpeg, mFoundAux;
     87 
     88     jpeg_compress_struct mCInfo;
     89 
     90     struct JpegError : public jpeg_error_mgr {
     91         JpegCompressor *parent;
     92     };
     93     j_common_ptr mJpegErrorInfo;
     94 
     95     struct JpegDestination : public jpeg_destination_mgr {
     96         JpegCompressor *parent;
     97     };
     98 
     99     static void jpegErrorHandler(j_common_ptr cinfo);
    100 
    101     static void jpegInitDestination(j_compress_ptr cinfo);
    102     static boolean jpegEmptyOutputBuffer(j_compress_ptr cinfo);
    103     static void jpegTermDestination(j_compress_ptr cinfo);
    104 
    105     bool checkError(const char *msg);
    106     status_t compress();
    107 
    108     void cleanUp();
    109 
    110     /**
    111      * Inherited Thread virtual overrides
    112      */
    113   private:
    114     virtual status_t readyToRun();
    115     virtual bool threadLoop();
    116 };
    117 
    118 } // namespace android
    119 
    120 #endif
    121