StripCache.h
Go to the documentation of this file.
#pragma once
#include <vector>
#include <mutex>
#include <atomic>
#include "grok.h"
#include "MinHeap.h"
namespace grk
{
struct GrkIOBuf : public grk_io_buf
{
public:
GrkIOBuf() : GrkIOBuf(nullptr, 0, 0, 0, false, 0) {}
GrkIOBuf(uint8_t* data, size_t offset, size_t dataLen, size_t allocLen, bool pooled,
uint32_t index)
{
data_ = data;
offset_ = offset;
len_ = dataLen;
allocLen_ = allocLen;
pooled_ = pooled;
index_ = index;
}
explicit GrkIOBuf(const grk_io_buf rhs)
{
data_ = rhs.data_;
offset_ = rhs.offset_;
len_ = rhs.len_;
allocLen_ = rhs.allocLen_;
pooled_ = rhs.pooled_;
index_ = rhs.index_;
}
uint32_t getIndex(void) const
{
return index_;
}
bool alloc(size_t len)
{
dealloc();
data_ = (uint8_t*)grk_aligned_malloc(len);
if(data_)
{
len_ = len;
allocLen_ = len;
}
return (data_ != nullptr);
}
void dealloc()
{
grk_aligned_free(data_);
data_ = nullptr;
}
};
class BufPool
{
public:
~BufPool(void)
{
for(auto& b : pool)
b.second.dealloc();
}
GrkIOBuf get(size_t len)
{
for(auto iter = pool.begin(); iter != pool.end(); ++iter)
{
if(iter->second.allocLen_ >= len)
{
auto b = iter->second;
b.len_ = len;
pool.erase(iter);
return b;
}
}
GrkIOBuf rc;
rc.alloc(len);
return rc;
}
void put(GrkIOBuf b)
{
assert(b.data_);
assert(pool.find(b.data_) == pool.end());
pool[b.data_] = b;
}
private:
std::map<uint8_t*, GrkIOBuf> pool;
};
struct Strip
{
Strip(GrkImage* outputImage, uint16_t index, uint32_t nominalHeight, uint8_t reduce);
~Strip(void);
uint32_t getIndex(void);
uint32_t reduceDim(uint32_t dim);
bool allocInterleavedLocked(uint64_t len, BufPool* pool);
bool allocInterleaved(uint64_t len, BufPool* pool);
GrkImage* stripImg;
std::atomic<uint32_t> tileCounter; // count number of tiles added to strip
uint8_t reduce_; // resolution reduction
mutable std::mutex interleaveMutex_;
mutable std::atomic<bool> allocatedInterleaved_;
};
class StripCache
{
public:
StripCache(void);
virtual ~StripCache();
void init(uint32_t concurrency, uint16_t numTiles_, uint32_t numStrips,
uint32_t nominalStripHeight, uint8_t reduce, GrkImage* outputImg,
grk_io_pixels_callback ioBufferCallback, void* ioUserData,
grk_io_register_reclaim_callback grkRegisterReclaimCallback);
bool ingestTile(uint32_t threadId, GrkImage* src);
bool ingestTile(GrkImage* src);
bool ingestStrip(uint32_t threadId, Tile* src, uint32_t yBegin, uint32_t yEnd);
void returnBufferToPool(uint32_t threadId, GrkIOBuf b);
bool isInitialized(void);
bool isMultiTile(void);
private:
bool serialize(uint32_t threadId, GrkIOBuf buf);
std::vector<BufPool*> pools_;
Strip** strips;
uint16_t numTiles_;
uint32_t numStrips_;
uint32_t nominalStripHeight_;
uint32_t imageY0_;
uint64_t packedRowBytes_;
void* ioUserData_;
grk_io_pixels_callback ioBufferCallback_;
mutable std::mutex serializeMutex_;
MinHeap<GrkIOBuf, uint32_t, MinHeapFakeLocker> serializeHeap;
mutable std::mutex heapMutex_;
bool initialized_;
bool multiTile_;
};
} // namespace grk
MinHeap.h
grk::BufPool
Definition StripCache.h:59
grk::BufPool::pool
std::map< uint8_t *, GrkIOBuf > pool
Definition StripCache.h:91
grk::BufPool::~BufPool
~BufPool(void)
Definition StripCache.h:61
grk::BufPool::put
void put(GrkIOBuf b)
Definition StripCache.h:83
grk::BufPool::get
GrkIOBuf get(size_t len)
Definition StripCache.h:66
grk::GrkImage
Definition GrkImage.h:25
grk::MinHeap
Definition MinHeap.h:34
grk::StripCache
Definition StripCache.h:110
grk::StripCache::init
void init(uint32_t concurrency, uint16_t numTiles_, uint32_t numStrips, uint32_t nominalStripHeight, uint8_t reduce, GrkImage *outputImg, grk_io_pixels_callback ioBufferCallback, void *ioUserData, grk_io_register_reclaim_callback grkRegisterReclaimCallback)
Definition StripCache.cpp:73
grk::StripCache::numTiles_
uint16_t numTiles_
Definition StripCache.h:130
grk::StripCache::ioUserData_
void * ioUserData_
Definition StripCache.h:135
grk::StripCache::StripCache
StripCache(void)
Definition StripCache.cpp:52
grk::StripCache::numStrips_
uint32_t numStrips_
Definition StripCache.h:131
grk::StripCache::ingestTile
bool ingestTile(uint32_t threadId, GrkImage *src)
Definition StripCache.cpp:133
grk::StripCache::serializeHeap
MinHeap< GrkIOBuf, uint32_t, MinHeapFakeLocker > serializeHeap
Definition StripCache.h:138
grk::StripCache::serializeMutex_
std::mutex serializeMutex_
Definition StripCache.h:137
grk::StripCache::ingestStrip
bool ingestStrip(uint32_t threadId, Tile *src, uint32_t yBegin, uint32_t yEnd)
Definition StripCache.cpp:103
grk::StripCache::~StripCache
virtual ~StripCache()
Definition StripCache.cpp:57
grk::StripCache::returnBufferToPool
void returnBufferToPool(uint32_t threadId, GrkIOBuf b)
Definition StripCache.cpp:215
grk::StripCache::isMultiTile
bool isMultiTile(void)
Definition StripCache.cpp:69
grk::StripCache::multiTile_
bool multiTile_
Definition StripCache.h:141
grk::StripCache::pools_
std::vector< BufPool * > pools_
Definition StripCache.h:128
grk::StripCache::initialized_
bool initialized_
Definition StripCache.h:140
grk::StripCache::packedRowBytes_
uint64_t packedRowBytes_
Definition StripCache.h:134
grk::StripCache::nominalStripHeight_
uint32_t nominalStripHeight_
Definition StripCache.h:132
grk::StripCache::imageY0_
uint32_t imageY0_
Definition StripCache.h:133
grk::StripCache::heapMutex_
std::mutex heapMutex_
Definition StripCache.h:139
grk::StripCache::strips
Strip ** strips
Definition StripCache.h:129
grk::StripCache::serialize
bool serialize(uint32_t threadId, GrkIOBuf buf)
Definition StripCache.cpp:167
grk::StripCache::isInitialized
bool isInitialized(void)
Definition StripCache.cpp:65
grk::StripCache::ioBufferCallback_
grk_io_pixels_callback ioBufferCallback_
Definition StripCache.h:136
grok.h
grk_io_register_reclaim_callback
void(* grk_io_register_reclaim_callback)(grk_io_init io_init, grk_io_callback reclaim_callback, void *io_user_data, void *reclaim_user_data)
Definition grok.h:452
grk_io_pixels_callback
bool(* grk_io_pixels_callback)(uint32_t threadId, grk_io_buf buffer, void *user_data)
Definition grok.h:455
grk
Copyright (C) 2016-2023 Grok Image Compression Inc.
Definition ICacheable.h:20
grk::grk_read
void grk_read(const uint8_t *buffer, TYPE *value, uint32_t numBytes)
Definition BufferedStream.h:239
grk::grk_aligned_malloc
void * grk_aligned_malloc(size_t size)
Allocate memory aligned to a 16 byte boundary.
Definition MemManager.cpp:76
grk::grk_aligned_free
void grk_aligned_free(void *ptr)
Definition MemManager.cpp:80
_grk_io_buf
Definition grok.h:436
_grk_io_buf::pooled_
bool pooled_
Definition grok.h:441
_grk_io_buf::offset_
size_t offset_
Definition grok.h:438
_grk_io_buf::allocLen_
size_t allocLen_
Definition grok.h:440
_grk_io_buf::data_
uint8_t * data_
Definition grok.h:437
_grk_io_buf::len_
size_t len_
Definition grok.h:439
_grk_io_buf::index_
uint32_t index_
Definition grok.h:442
grk::GrkIOBuf
Definition StripCache.h:13
grk::GrkIOBuf::GrkIOBuf
GrkIOBuf(uint8_t *data, size_t offset, size_t dataLen, size_t allocLen, bool pooled, uint32_t index)
Definition StripCache.h:16
grk::GrkIOBuf::GrkIOBuf
GrkIOBuf()
Definition StripCache.h:15
grk::GrkIOBuf::getIndex
uint32_t getIndex(void) const
Definition StripCache.h:35
grk::GrkIOBuf::dealloc
void dealloc()
Definition StripCache.h:51
grk::GrkIOBuf::GrkIOBuf
GrkIOBuf(const grk_io_buf rhs)
Definition StripCache.h:26
grk::GrkIOBuf::alloc
bool alloc(size_t len)
Definition StripCache.h:39
grk::Strip
Definition StripCache.h:95
grk::Strip::tileCounter
std::atomic< uint32_t > tileCounter
Definition StripCache.h:103
grk::Strip::getIndex
uint32_t getIndex(void)
grk::Strip::~Strip
~Strip(void)
Definition StripCache.cpp:26
grk::Strip::stripImg
GrkImage * stripImg
Definition StripCache.h:102
grk::Strip::reduceDim
uint32_t reduceDim(uint32_t dim)
Definition StripCache.cpp:30
grk::Strip::allocatedInterleaved_
std::atomic< bool > allocatedInterleaved_
Definition StripCache.h:106
grk::Strip::interleaveMutex_
std::mutex interleaveMutex_
Definition StripCache.h:105
grk::Strip::reduce_
uint8_t reduce_
Definition StripCache.h:104
grk::Strip::allocInterleaved
bool allocInterleaved(uint64_t len, BufPool *pool)
Definition StripCache.cpp:46
grk::Strip::allocInterleavedLocked
bool allocInterleavedLocked(uint64_t len, BufPool *pool)
Definition StripCache.cpp:34
grk::Tile
Definition TileProcessor.h:39