2017-09-16 11:27:31 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
2020-04-18 03:39:50 -05:00
|
|
|
|
|
|
|
#pragma once
|
2017-09-16 11:27:31 -05:00
|
|
|
|
|
|
|
#include <vector>
|
2017-09-16 11:32:20 -05:00
|
|
|
#include <assert.h>
|
2022-01-01 16:55:15 -06:00
|
|
|
#include <zlib.h>
|
2022-03-25 13:32:01 -05:00
|
|
|
#include <Log.hpp>
|
|
|
|
#include <Common.hpp>
|
|
|
|
|
2022-05-25 04:33:27 -05:00
|
|
|
#define ENABLE_DELTAS 1
|
2017-09-16 11:27:31 -05:00
|
|
|
|
2017-09-16 11:32:20 -05:00
|
|
|
#ifndef TILE_WIRE_ID
|
2017-09-16 11:27:31 -05:00
|
|
|
# define TILE_WIRE_ID
|
|
|
|
typedef uint32_t TileWireId;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/// A quick and dirty delta generator for last tile changes
|
|
|
|
class DeltaGenerator {
|
|
|
|
|
2022-03-17 14:22:55 -05:00
|
|
|
/// Bitmap row with a CRC for quick vertical shift detection
|
2017-09-28 03:45:46 -05:00
|
|
|
struct DeltaBitmapRow {
|
2022-05-31 09:27:55 -05:00
|
|
|
// FIXME: add "whole row the same" flag.
|
|
|
|
uint64_t _crc;
|
|
|
|
const uint32_t *_pixels; // FIXME: remove me.
|
|
|
|
size_t _pixSize;
|
2017-09-28 03:45:46 -05:00
|
|
|
|
|
|
|
bool identical(const DeltaBitmapRow &other) const
|
|
|
|
{
|
|
|
|
if (_crc != other._crc)
|
|
|
|
return false;
|
2022-05-31 09:27:55 -05:00
|
|
|
return !std::memcmp(_pixels, other._pixels, _pixSize * 4);
|
2018-11-08 02:16:50 -06:00
|
|
|
}
|
2017-09-28 03:45:46 -05:00
|
|
|
};
|
|
|
|
|
2022-03-17 14:22:55 -05:00
|
|
|
/// A bitmap tile with annotated rows and details on its location
|
2017-09-16 11:27:31 -05:00
|
|
|
struct DeltaData {
|
2022-05-31 07:58:55 -05:00
|
|
|
DeltaData (TileWireId wid,
|
|
|
|
unsigned char* pixmap, size_t startX, size_t startY,
|
|
|
|
int width, int height,
|
|
|
|
int tileLeft, int tileTop, int tileSize, int tilePart,
|
|
|
|
int bufferWidth, int bufferHeight
|
|
|
|
) :
|
2022-03-25 13:32:01 -05:00
|
|
|
// in TWIPS
|
2022-05-02 11:39:01 -05:00
|
|
|
_left(tileLeft),
|
|
|
|
_top(tileTop),
|
2022-03-25 13:32:01 -05:00
|
|
|
_size(tileSize),
|
2022-05-02 11:39:01 -05:00
|
|
|
_part(tilePart),
|
|
|
|
_wid(wid),
|
2022-03-25 13:32:01 -05:00
|
|
|
// in Pixels
|
2022-05-02 11:39:01 -05:00
|
|
|
_width(width),
|
|
|
|
_height(height),
|
2022-05-31 09:27:55 -05:00
|
|
|
_rows(new DeltaBitmapRow[height])
|
2022-05-31 07:58:55 -05:00
|
|
|
{
|
|
|
|
assert (startX + width <= (size_t)bufferWidth);
|
|
|
|
assert (startY + height <= (size_t)bufferHeight);
|
|
|
|
|
|
|
|
(void)bufferHeight;
|
|
|
|
|
|
|
|
LOG_TRC("Converting pixel data to delta data of size "
|
|
|
|
<< (width * height * 4) << " width " << width
|
|
|
|
<< " height " << height);
|
|
|
|
|
2022-05-31 09:27:55 -05:00
|
|
|
_pixels = (uint32_t *)malloc((size_t)width * height * 4);
|
2022-05-31 07:58:55 -05:00
|
|
|
for (int y = 0; y < height; ++y)
|
|
|
|
{
|
|
|
|
DeltaBitmapRow &row = _rows[y];
|
|
|
|
size_t position = ((startY + y) * bufferWidth * 4) + (startX * 4);
|
2022-05-31 09:27:55 -05:00
|
|
|
uint32_t *src = reinterpret_cast<uint32_t *>(pixmap + position);
|
|
|
|
uint32_t *dest = _pixels + width * y;
|
2022-05-31 07:58:55 -05:00
|
|
|
|
|
|
|
// We get the hash ~for free as we copy - with a cheap hash.
|
|
|
|
uint64_t crc = 0x7fffffff - 1;
|
2022-05-31 09:27:55 -05:00
|
|
|
row._pixels = dest;
|
|
|
|
row._pixSize = width;
|
2022-05-31 07:58:55 -05:00
|
|
|
for (int x = 0; x < width; ++x)
|
|
|
|
{
|
|
|
|
crc = (crc << 7) + crc + src[x];
|
2022-05-31 09:27:55 -05:00
|
|
|
dest[x] = src[x];
|
2022-05-31 07:58:55 -05:00
|
|
|
}
|
2022-05-31 09:27:55 -05:00
|
|
|
row._crc = crc;
|
2022-05-31 07:58:55 -05:00
|
|
|
}
|
|
|
|
}
|
2022-05-02 11:39:01 -05:00
|
|
|
|
2022-05-31 09:27:55 -05:00
|
|
|
~DeltaData()
|
|
|
|
{
|
|
|
|
delete[] _rows;
|
|
|
|
if (_pixels)
|
|
|
|
free (_pixels);
|
|
|
|
}
|
|
|
|
|
2018-11-07 02:09:01 -06:00
|
|
|
void setWid(TileWireId wid)
|
|
|
|
{
|
|
|
|
_wid = wid;
|
|
|
|
}
|
|
|
|
|
|
|
|
TileWireId getWid() const
|
|
|
|
{
|
|
|
|
return _wid;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setWidth(int width)
|
|
|
|
{
|
|
|
|
_width = width;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getWidth() const
|
|
|
|
{
|
|
|
|
return _width;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setHeight(int height)
|
|
|
|
{
|
|
|
|
_height = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getHeight() const
|
|
|
|
{
|
|
|
|
return _height;
|
|
|
|
}
|
|
|
|
|
2021-12-31 13:37:10 -06:00
|
|
|
void setTilePos(int left, int top, int part)
|
|
|
|
{
|
|
|
|
_left = left;
|
|
|
|
_top = top;
|
|
|
|
_part = part;
|
|
|
|
}
|
|
|
|
|
2022-05-31 07:58:55 -05:00
|
|
|
const DeltaBitmapRow& getRow(int y) const
|
2018-11-07 02:09:01 -06:00
|
|
|
{
|
2022-05-31 07:58:55 -05:00
|
|
|
return _rows[y];
|
2018-11-07 02:09:01 -06:00
|
|
|
}
|
|
|
|
|
2022-05-31 09:27:55 -05:00
|
|
|
void replaceAndFree(std::shared_ptr<DeltaData> &repl)
|
2022-01-05 08:24:18 -06:00
|
|
|
{
|
2022-03-25 13:32:01 -05:00
|
|
|
assert (_left == repl->_left && _top == repl->_top &&
|
|
|
|
_size == repl->_size && _part == repl->_part);
|
2022-01-05 08:24:18 -06:00
|
|
|
_wid = repl->_wid;
|
|
|
|
_width = repl->_width;
|
|
|
|
_height = repl->_height;
|
2022-05-31 09:27:55 -05:00
|
|
|
delete[] _rows;
|
|
|
|
_rows = repl->_rows;
|
|
|
|
repl->_rows = nullptr;
|
|
|
|
free (_pixels);
|
|
|
|
_pixels = repl->_pixels;
|
|
|
|
repl->_pixels = nullptr;
|
|
|
|
repl.reset();
|
2022-01-05 08:24:18 -06:00
|
|
|
}
|
|
|
|
|
2021-12-31 13:37:10 -06:00
|
|
|
int _left;
|
|
|
|
int _top;
|
2022-03-25 13:32:01 -05:00
|
|
|
int _size;
|
2021-12-31 13:37:10 -06:00
|
|
|
int _part;
|
2018-11-07 02:09:01 -06:00
|
|
|
private:
|
2017-09-16 11:27:31 -05:00
|
|
|
TileWireId _wid;
|
2017-09-28 03:45:46 -05:00
|
|
|
int _width;
|
|
|
|
int _height;
|
2022-05-31 09:27:55 -05:00
|
|
|
uint32_t *_pixels;
|
|
|
|
DeltaBitmapRow *_rows;
|
2017-09-16 11:27:31 -05:00
|
|
|
};
|
2022-03-17 14:22:55 -05:00
|
|
|
|
|
|
|
/// The last several bitmap entries as a cache
|
2017-09-28 03:45:46 -05:00
|
|
|
std::vector<std::shared_ptr<DeltaData>> _deltaEntries;
|
2017-09-16 11:27:31 -05:00
|
|
|
|
2022-03-08 08:27:20 -06:00
|
|
|
// Unpremultiplies data and converts native endian ARGB => RGBA bytes
|
|
|
|
static void
|
2022-03-28 11:43:24 -05:00
|
|
|
unpremult_copy (unsigned char *dest, const unsigned char *srcBytes, unsigned int count)
|
2022-03-08 08:27:20 -06:00
|
|
|
{
|
2022-03-28 11:43:24 -05:00
|
|
|
const uint32_t *src = reinterpret_cast<const uint32_t *>(srcBytes);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < count; ++i)
|
2022-03-08 08:27:20 -06:00
|
|
|
{
|
2022-03-28 11:43:24 -05:00
|
|
|
// Profile me: avoid math for runs of duplicate pixels
|
|
|
|
// possibly we should RLE earlier ?
|
|
|
|
if (i > 0 && src[i-1] == src[i])
|
|
|
|
{
|
|
|
|
dest[0] = dest[-4];
|
|
|
|
dest[1] = dest[-3];
|
|
|
|
dest[2] = dest[-2];
|
|
|
|
dest[3] = dest[-1];
|
|
|
|
dest += 4;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-03-08 08:27:20 -06:00
|
|
|
uint32_t pix;
|
|
|
|
uint8_t alpha;
|
|
|
|
|
|
|
|
std::memcpy (&pix, src + i, sizeof (uint32_t));
|
|
|
|
|
|
|
|
alpha = (pix & 0xff000000) >> 24;
|
|
|
|
if (alpha == 255)
|
|
|
|
{
|
|
|
|
dest[0] = ((pix & 0xff0000) >> 16);
|
|
|
|
dest[1] = ((pix & 0x00ff00) >> 8);
|
|
|
|
dest[2] = ((pix & 0x0000ff) >> 0);
|
|
|
|
dest[3] = 255;
|
|
|
|
}
|
|
|
|
else if (alpha == 0)
|
|
|
|
dest[0] = dest[1] = dest[2] = dest[3] = 0;
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dest[0] = (((pix & 0xff0000) >> 16) * 255 + alpha / 2) / alpha;
|
|
|
|
dest[1] = (((pix & 0x00ff00) >> 8) * 255 + alpha / 2) / alpha;
|
|
|
|
dest[2] = (((pix & 0x0000ff) >> 0) * 255 + alpha / 2) / alpha;
|
|
|
|
dest[3] = alpha;
|
|
|
|
}
|
|
|
|
dest += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-16 11:27:31 -05:00
|
|
|
bool makeDelta(
|
2017-09-28 03:45:46 -05:00
|
|
|
const DeltaData &prev,
|
|
|
|
const DeltaData &cur,
|
2022-01-01 16:55:15 -06:00
|
|
|
std::vector<char>& outStream)
|
2017-09-16 11:27:31 -05:00
|
|
|
{
|
2017-09-28 03:45:46 -05:00
|
|
|
// TODO: should we split and compress alpha separately ?
|
2018-11-07 02:09:01 -06:00
|
|
|
if (prev.getWidth() != cur.getWidth() || prev.getHeight() != cur.getHeight())
|
2017-09-16 11:27:31 -05:00
|
|
|
{
|
2020-05-24 08:10:18 -05:00
|
|
|
LOG_ERR("mis-sized delta: " << prev.getWidth() << 'x' << prev.getHeight() << " vs "
|
|
|
|
<< cur.getWidth() << 'x' << cur.getHeight());
|
2017-09-16 11:27:31 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-25 13:32:01 -05:00
|
|
|
LOG_TRC("building delta of a " << cur.getWidth() << 'x' << cur.getHeight() << " bitmap " <<
|
|
|
|
"between old wid " << prev.getWid() << " and " << cur.getWid());
|
2017-09-28 03:45:46 -05:00
|
|
|
|
2022-01-01 16:55:15 -06:00
|
|
|
std::vector<char> output;
|
|
|
|
// guestimated upper-bound delta size
|
|
|
|
output.reserve(cur.getWidth() * (cur.getHeight() + 4) * 4);
|
|
|
|
|
2017-09-28 03:45:46 -05:00
|
|
|
// row move/copy src/dest is a byte.
|
2018-11-07 02:09:01 -06:00
|
|
|
assert (prev.getHeight() <= 256);
|
2017-09-28 03:45:46 -05:00
|
|
|
// column position is a byte.
|
2018-11-07 02:09:01 -06:00
|
|
|
assert (prev.getWidth() <= 256);
|
2017-09-28 03:45:46 -05:00
|
|
|
|
|
|
|
// How do the rows look against each other ?
|
|
|
|
size_t lastMatchOffset = 0;
|
2017-10-23 14:59:10 -05:00
|
|
|
size_t lastCopy = 0;
|
2018-11-07 02:09:01 -06:00
|
|
|
for (int y = 0; y < prev.getHeight(); ++y)
|
2017-09-16 11:27:31 -05:00
|
|
|
{
|
2017-09-28 03:45:46 -05:00
|
|
|
// Life is good where rows match:
|
2022-05-31 07:58:55 -05:00
|
|
|
if (prev.getRow(y).identical(cur.getRow(y)))
|
2017-09-28 03:45:46 -05:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Hunt for other rows
|
|
|
|
bool matched = false;
|
2018-11-07 02:09:01 -06:00
|
|
|
for (int yn = 0; yn < prev.getHeight() && !matched; ++yn)
|
2017-09-16 11:27:31 -05:00
|
|
|
{
|
2018-11-07 02:09:01 -06:00
|
|
|
size_t match = (y + lastMatchOffset + yn) % prev.getHeight();
|
2022-05-31 07:58:55 -05:00
|
|
|
if (prev.getRow(match).identical(cur.getRow(y)))
|
2017-09-16 11:27:31 -05:00
|
|
|
{
|
2017-09-28 03:45:46 -05:00
|
|
|
// TODO: if offsets are >256 - use 16bits?
|
2017-10-23 14:59:10 -05:00
|
|
|
if (lastCopy > 0)
|
|
|
|
{
|
|
|
|
char cnt = output[lastCopy];
|
|
|
|
if (output[lastCopy + 1] + cnt == (char)(match) &&
|
|
|
|
output[lastCopy + 2] + cnt == (char)(y))
|
|
|
|
{
|
|
|
|
output[lastCopy]++;
|
|
|
|
matched = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2017-09-28 03:45:46 -05:00
|
|
|
|
|
|
|
lastMatchOffset = match - y;
|
2017-10-23 14:59:10 -05:00
|
|
|
output.push_back('c'); // copy-row
|
|
|
|
lastCopy = output.size();
|
|
|
|
output.push_back(1); // count
|
2017-09-28 03:45:46 -05:00
|
|
|
output.push_back(match); // src
|
2017-10-23 14:59:10 -05:00
|
|
|
output.push_back(y); // dest
|
|
|
|
|
2017-09-28 03:45:46 -05:00
|
|
|
matched = true;
|
|
|
|
continue;
|
2017-09-16 11:27:31 -05:00
|
|
|
}
|
|
|
|
}
|
2017-09-28 03:45:46 -05:00
|
|
|
if (matched)
|
|
|
|
continue;
|
2017-09-16 11:27:31 -05:00
|
|
|
|
2017-09-28 03:45:46 -05:00
|
|
|
// Our row is just that different:
|
2022-05-31 07:58:55 -05:00
|
|
|
const DeltaBitmapRow &curRow = cur.getRow(y);
|
|
|
|
const DeltaBitmapRow &prevRow = prev.getRow(y);
|
2018-11-07 02:09:01 -06:00
|
|
|
for (int x = 0; x < prev.getWidth();)
|
2017-09-16 11:27:31 -05:00
|
|
|
{
|
2017-09-28 03:45:46 -05:00
|
|
|
int same;
|
2018-11-07 02:09:01 -06:00
|
|
|
for (same = 0; same + x < prev.getWidth() &&
|
2022-05-31 09:27:55 -05:00
|
|
|
prevRow._pixels[x+same] == curRow._pixels[x+same];)
|
2017-09-28 03:45:46 -05:00
|
|
|
++same;
|
|
|
|
|
|
|
|
x += same;
|
|
|
|
|
|
|
|
int diff;
|
2018-11-07 02:09:01 -06:00
|
|
|
for (diff = 0; diff + x < prev.getWidth() &&
|
2022-05-31 09:27:55 -05:00
|
|
|
(prevRow._pixels[x+diff] != curRow._pixels[x+diff] || diff < 3) &&
|
2017-09-28 03:45:46 -05:00
|
|
|
diff < 254;)
|
|
|
|
++diff;
|
|
|
|
if (diff > 0)
|
2017-09-16 11:27:31 -05:00
|
|
|
{
|
2017-09-28 03:45:46 -05:00
|
|
|
output.push_back('d');
|
|
|
|
output.push_back(y);
|
|
|
|
output.push_back(x);
|
|
|
|
output.push_back(diff);
|
|
|
|
|
|
|
|
size_t dest = output.size();
|
|
|
|
output.resize(dest + diff * 4);
|
2022-03-08 08:27:20 -06:00
|
|
|
|
|
|
|
unpremult_copy(reinterpret_cast<unsigned char *>(&output[dest]),
|
2022-05-31 09:27:55 -05:00
|
|
|
(const unsigned char *)(curRow._pixels + x),
|
2022-03-28 11:43:24 -05:00
|
|
|
diff);
|
2017-09-16 11:27:31 -05:00
|
|
|
|
2022-03-25 13:32:01 -05:00
|
|
|
LOG_TRC("row " << y << " different " << diff << "pixels");
|
2017-09-28 03:45:46 -05:00
|
|
|
x += diff;
|
|
|
|
}
|
2017-09-16 11:27:31 -05:00
|
|
|
}
|
|
|
|
}
|
2022-01-01 16:55:15 -06:00
|
|
|
LOG_TRC("Created delta of size " << output.size());
|
2022-03-25 13:32:01 -05:00
|
|
|
if (output.size() == 0)
|
|
|
|
{
|
|
|
|
// The tile content is identical to what the client already has, so skip it
|
|
|
|
LOG_TRC("Identical / un-changed tile");
|
|
|
|
// Return a zero byte image to inform WSD we didn't need that.
|
|
|
|
// This allows WSD side TileCache to free up waiting subscribers.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !ENABLE_DELTAS
|
|
|
|
return false; // Disable transmission for now; just send keyframes.
|
|
|
|
#endif
|
2022-01-01 16:55:15 -06:00
|
|
|
|
|
|
|
z_stream zstr;
|
|
|
|
memset((void *)&zstr, 0, sizeof (zstr));
|
|
|
|
|
|
|
|
if (deflateInit2 (&zstr, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
|
|
|
|
-MAX_WBITS, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
|
|
|
|
LOG_ERR("Failed to init deflate");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: avoid allocation & make this more efficient.
|
|
|
|
uLong maxCompressed = compressBound(output.size());
|
|
|
|
Bytef *compressed = (Bytef *)malloc(maxCompressed);
|
|
|
|
|
|
|
|
zstr.next_in = (Bytef *)output.data();
|
|
|
|
zstr.avail_in = output.size();
|
|
|
|
zstr.next_out = compressed;
|
|
|
|
zstr.avail_out = maxCompressed;
|
|
|
|
|
|
|
|
if (!compressed || deflate(&zstr, Z_FINISH) != Z_STREAM_END) {
|
|
|
|
LOG_ERR("Failed to compress delta of size " << output.size());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
deflateEnd(&zstr);
|
|
|
|
|
|
|
|
uLong compSize = maxCompressed - zstr.avail_out;
|
|
|
|
LOG_TRC("Compressed delta of size " << output.size() << " to size " << compSize);
|
|
|
|
// << Util::dumpHex(std::string((char *)compressed, compSize)));
|
|
|
|
|
|
|
|
// FIXME: should get zlib to drop it directly in really.
|
|
|
|
outStream.push_back('D');
|
|
|
|
size_t oldSize = outStream.size();
|
|
|
|
outStream.resize(oldSize + compSize);
|
|
|
|
memcpy(&outStream[oldSize], compressed, compSize);
|
|
|
|
free (compressed);
|
2017-09-28 03:45:46 -05:00
|
|
|
|
2017-09-16 11:27:31 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
DeltaGenerator() {}
|
|
|
|
|
|
|
|
/**
|
2022-03-25 13:32:01 -05:00
|
|
|
* Creates a delta if possible:
|
2017-09-16 11:27:31 -05:00
|
|
|
* if so - returns @true and appends the delta to @output
|
|
|
|
* stores @pixmap, and other data to accelerate delta
|
|
|
|
* creation in a limited size cache.
|
|
|
|
*/
|
|
|
|
bool createDelta(
|
|
|
|
unsigned char* pixmap, size_t startX, size_t startY,
|
|
|
|
int width, int height,
|
|
|
|
int bufferWidth, int bufferHeight,
|
2022-03-25 13:32:01 -05:00
|
|
|
int tileLeft, int tileTop, int tileSize, int tilePart,
|
2017-09-16 11:27:31 -05:00
|
|
|
std::vector<char>& output,
|
2022-03-25 13:32:01 -05:00
|
|
|
TileWireId wid, bool forceKeyframe,
|
2022-01-05 08:24:18 -06:00
|
|
|
std::mutex &pngMutex)
|
2017-09-16 11:27:31 -05:00
|
|
|
{
|
2022-05-31 07:58:55 -05:00
|
|
|
if ((width & 0x1) != 0) // power of two - RGBA
|
|
|
|
{
|
|
|
|
LOG_TRC("Bad width to create deltas " << width);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-05 08:24:18 -06:00
|
|
|
// FIXME: why duplicate this ? we could overwrite
|
|
|
|
// as we make the delta into an existing cache entry,
|
|
|
|
// and just do this as/when there is no entry.
|
2022-05-31 07:58:55 -05:00
|
|
|
std::shared_ptr<DeltaData> update(
|
|
|
|
new DeltaData(
|
|
|
|
wid, pixmap, startX, startY, width, height,
|
|
|
|
tileLeft, tileTop, tileSize, tilePart,
|
|
|
|
bufferWidth, bufferHeight));
|
2022-01-05 08:24:18 -06:00
|
|
|
std::shared_ptr<DeltaData> cacheEntry;
|
|
|
|
|
2017-09-16 11:27:31 -05:00
|
|
|
{
|
2022-01-05 08:24:18 -06:00
|
|
|
// protect _deltaEntries
|
|
|
|
std::unique_lock<std::mutex> pngLock(pngMutex);
|
|
|
|
|
|
|
|
if (_deltaEntries.size() > 16) // FIXME: hard-coded & not per-view
|
|
|
|
_deltaEntries.erase(_deltaEntries.begin());
|
|
|
|
|
2021-12-31 13:37:10 -06:00
|
|
|
for (auto &old : _deltaEntries)
|
|
|
|
{
|
2022-03-25 13:32:01 -05:00
|
|
|
if (old->_left == tileLeft && old->_top == tileTop &&
|
|
|
|
old->_size == tileSize && old->_part == tilePart)
|
2021-12-31 13:37:10 -06:00
|
|
|
{
|
2022-01-05 08:24:18 -06:00
|
|
|
cacheEntry = old;
|
|
|
|
break;
|
2021-12-31 13:37:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 13:32:01 -05:00
|
|
|
if (!cacheEntry)
|
2022-01-05 08:24:18 -06:00
|
|
|
{
|
2022-03-25 13:32:01 -05:00
|
|
|
_deltaEntries.push_back(update);
|
|
|
|
return false;
|
2022-01-05 08:24:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 13:32:01 -05:00
|
|
|
// interestingly cacheEntry may no longer be in the cache by here.
|
|
|
|
// but no other thread can touch the same tile at the same time.
|
|
|
|
assert (cacheEntry);
|
|
|
|
|
|
|
|
bool delta = false;
|
|
|
|
if (!forceKeyframe)
|
|
|
|
delta = makeDelta(*cacheEntry, *update, output);
|
2022-05-31 09:27:55 -05:00
|
|
|
|
|
|
|
// no two threads can be working on the same DeltaData.
|
|
|
|
cacheEntry->replaceAndFree(update);
|
2022-03-25 13:32:01 -05:00
|
|
|
return delta;
|
2017-09-16 11:27:31 -05:00
|
|
|
}
|
2022-02-26 15:29:36 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Compress the relevant pixmap data either to a delta if we can
|
|
|
|
* or a plain deflated stream if we cannot.
|
|
|
|
*/
|
2022-04-11 08:23:50 -05:00
|
|
|
size_t compressOrDelta(
|
2022-02-26 15:29:36 -06:00
|
|
|
unsigned char* pixmap, size_t startX, size_t startY,
|
|
|
|
int width, int height,
|
|
|
|
int bufferWidth, int bufferHeight,
|
2022-03-25 13:32:01 -05:00
|
|
|
int tileLeft, int tileTop, int tileSize, int tilePart,
|
2022-02-26 15:29:36 -06:00
|
|
|
std::vector<char>& output,
|
2022-03-25 13:32:01 -05:00
|
|
|
TileWireId wid, bool forceKeyframe,
|
2022-02-26 15:29:36 -06:00
|
|
|
std::mutex &pngMutex)
|
|
|
|
{
|
|
|
|
if (!createDelta(pixmap, startX, startY, width, height, bufferWidth, bufferHeight,
|
2022-03-25 13:32:01 -05:00
|
|
|
tileLeft, tileTop, tileSize, tilePart, output, wid, forceKeyframe, pngMutex))
|
2022-02-26 15:29:36 -06:00
|
|
|
{
|
|
|
|
// FIXME: should stream it in =)
|
|
|
|
|
|
|
|
// FIXME: get sizes right [!] ...
|
|
|
|
z_stream zstr;
|
|
|
|
memset((void *)&zstr, 0, sizeof (zstr));
|
|
|
|
|
2022-05-28 04:23:08 -05:00
|
|
|
if (deflateInit2 (&zstr, Z_BEST_SPEED + 1, Z_DEFLATED,
|
2022-02-26 15:29:36 -06:00
|
|
|
-MAX_WBITS, 8, Z_DEFAULT_STRATEGY) != Z_OK)
|
|
|
|
{
|
|
|
|
LOG_ERR("Failed to init deflate");
|
2022-04-11 08:23:50 -05:00
|
|
|
return 0;
|
2022-02-26 15:29:36 -06:00
|
|
|
}
|
|
|
|
|
2022-05-10 04:22:21 -05:00
|
|
|
uLong maxCompressed = compressBound((uLong)width * height * 4);
|
2022-02-26 15:29:36 -06:00
|
|
|
Bytef *compressed = (Bytef *)malloc(maxCompressed);
|
|
|
|
if (!compressed)
|
|
|
|
{
|
|
|
|
LOG_ERR("Failed to allocate buffer of size " << maxCompressed << " to compress into");
|
2022-04-11 08:23:50 -05:00
|
|
|
return 0;
|
2022-02-26 15:29:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
zstr.next_out = compressed;
|
|
|
|
zstr.avail_out = maxCompressed;
|
|
|
|
|
2022-03-08 08:27:20 -06:00
|
|
|
unsigned char fixedupLine[width * 4];
|
|
|
|
|
2022-02-26 15:29:36 -06:00
|
|
|
// FIXME: should we RLE in pixels first ?
|
|
|
|
for (int y = 0; y < height; ++y)
|
|
|
|
{
|
2022-03-28 11:43:24 -05:00
|
|
|
unpremult_copy(fixedupLine, (Bytef *)pixmap + ((startY + y) * bufferWidth * 4) + (startX * 4), width);
|
2022-03-08 08:27:20 -06:00
|
|
|
|
|
|
|
zstr.next_in = fixedupLine;
|
2022-02-26 15:29:36 -06:00
|
|
|
zstr.avail_in = width * 4;
|
|
|
|
|
|
|
|
bool lastRow = (y == height - 1);
|
|
|
|
int flushFlag = lastRow ? Z_FINISH : Z_NO_FLUSH;
|
|
|
|
int expected = lastRow ? Z_STREAM_END : Z_OK;
|
|
|
|
if (deflate(&zstr, flushFlag) != expected)
|
|
|
|
{
|
|
|
|
LOG_ERR("failed to compress image ");
|
2022-04-11 08:23:50 -05:00
|
|
|
return 0;
|
2022-02-26 15:29:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deflateEnd(&zstr);
|
|
|
|
|
|
|
|
uLong compSize = maxCompressed - zstr.avail_out;
|
2022-03-25 13:32:01 -05:00
|
|
|
LOG_TRC("Compressed image of size " << (width * height * 4) << " to size " << compSize);
|
|
|
|
// << Util::dumpHex(std::string((char *)compressed, compSize)));
|
2022-02-26 15:29:36 -06:00
|
|
|
|
|
|
|
// FIXME: get zlib to drop it directly into this buffer really.
|
|
|
|
output.push_back('Z');
|
|
|
|
size_t oldSize = output.size();
|
|
|
|
output.resize(oldSize + compSize);
|
|
|
|
memcpy(&output[oldSize], compressed, compSize);
|
|
|
|
free (compressed);
|
|
|
|
}
|
2022-04-11 08:23:50 -05:00
|
|
|
|
|
|
|
return output.size();
|
2022-02-26 15:29:36 -06:00
|
|
|
}
|
2022-03-25 13:32:01 -05:00
|
|
|
|
|
|
|
static Blob expand(const Blob &blob)
|
|
|
|
{
|
|
|
|
Blob img = std::make_shared<BlobData>();
|
|
|
|
img->resize(1024*1024*4); // lots of extra space.
|
|
|
|
|
|
|
|
z_stream zstr;
|
|
|
|
memset((void *)&zstr, 0, sizeof (zstr));
|
|
|
|
|
|
|
|
zstr.next_in = (Bytef *)blob->data();
|
|
|
|
zstr.avail_in = blob->size();
|
|
|
|
zstr.next_out = (Bytef *)img->data();
|
|
|
|
zstr.avail_out = img->size();
|
|
|
|
|
|
|
|
if (inflateInit2 (&zstr, -MAX_WBITS) != Z_OK)
|
|
|
|
{
|
|
|
|
LOG_ERR("Failed to expand zimage");
|
|
|
|
return Blob();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inflate (&zstr, Z_SYNC_FLUSH) != Z_STREAM_END)
|
|
|
|
{
|
|
|
|
LOG_ERR("Failed to inflate zimage");
|
|
|
|
return Blob();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inflateEnd(&zstr) != Z_OK)
|
|
|
|
return Blob();
|
|
|
|
|
|
|
|
img->resize(img->size() - zstr.avail_out);
|
|
|
|
|
|
|
|
return img;
|
|
|
|
}
|
2017-09-16 11:27:31 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|