blob: 2745195313aef92bd72e24532b5804ac69369ece [file] [log] [blame]
#ifndef LIBGAV1_SRC_THREADING_STRATEGY_H_
#define LIBGAV1_SRC_THREADING_STRATEGY_H_
#include <memory>
#include "src/obu_parser.h"
#include "src/utils/compiler_attributes.h"
#include "src/utils/threadpool.h"
namespace libgav1 {
// This class allocates and manages the worker threads among thread pools used
// for multi-threaded decoding.
class ThreadingStrategy {
public:
ThreadingStrategy() = default;
// Not copyable or movable.
ThreadingStrategy(const ThreadingStrategy&) = delete;
ThreadingStrategy& operator=(const ThreadingStrategy&) = delete;
// Creates or re-allocates the thread pools based on the |frame_header| and
// |thread_count|. This function is idempotent if the |frame_header| and
// |thread_count| doesn't change between calls (it will only create new
// threads on the first call and do nothing on the subsequent calls). This
// function also starts the worker threads whenever it creates new thread
// pools.
// The following strategy is used to allocate threads:
// * One thread is allocated for decoding each Tile.
// * Any remaining threads are allocated for superblock row multi-threading
// within each of the tile in a round robin fashion.
LIBGAV1_MUST_USE_RESULT bool Reset(const ObuFrameHeader& frame_header,
int thread_count);
// Returns a pointer to the ThreadPool that is to be used for Tile
// multi-threading.
ThreadPool* tile_thread_pool() const {
return (tile_thread_count_ != 0) ? thread_pool_.get() : nullptr;
}
int tile_thread_count() const { return tile_thread_count_; }
// Returns a pointer to the ThreadPool that is to be used within the Tile at
// index |tile_index| for superblock row multi-threading.
ThreadPool* row_thread_pool(int tile_index) const {
return tile_index < max_tile_index_for_row_threads_ ? thread_pool_.get()
: nullptr;
}
// Returns a pointer to the ThreadPool that is to be used for post filter
// multi-threading.
ThreadPool* post_filter_thread_pool() const { return thread_pool_.get(); }
private:
std::unique_ptr<ThreadPool> thread_pool_;
int tile_thread_count_;
int max_tile_index_for_row_threads_;
};
} // namespace libgav1
#endif // LIBGAV1_SRC_THREADING_STRATEGY_H_