1 #pragma once
2 
3 /*
4  * Copyright (C) 2017 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include <cstdint>
20 #include <cstdlib>
21 #include <memory>
22 
23 #include "host/frontend/vnc_server/vnc_utils.h"
24 
25 namespace cuttlefish {
26 namespace vnc {
27 
28 // libjpeg-turbo with jpeg_mem_dest (using memory as a destination) is funky.
29 // If you give it a buffer that is big enough it will use it.
30 // If you give it a buffer that is too small, it will allocate a new buffer
31 // but will NOT free the buffer you gave it.
32 // This class keeps track of the capacity of the working buffer, and frees the
33 // old buffer if libjpeg-turbo silently discards it.
34 class JpegCompressor {
35  public:
36   Message Compress(const Message& frame, int jpeg_quality, std::uint16_t x,
37                    std::uint16_t y, std::uint16_t width, std::uint16_t height,
38                    int screen_width);
39 
40  private:
41   void UpdateBuffer(std::uint8_t* compression_buffer,
42                     unsigned long compression_buffer_size);
43   struct Freer {
operatorFreer44     void operator()(void* p) const { std::free(p); }
45   };
46 
47   std::unique_ptr<std::uint8_t, Freer> buffer_;
48   unsigned long buffer_capacity_{};
49 };
50 
51 }  // namespace vnc
52 }  // namespace cuttlefish
53