Hash :
c3a1cae4
Author :
Date :
2024-04-15T14:58:55
Use angle::SimpleMutex everywhere in libGLESv2 Only cases left that use std::mutex are: - Share group and the context ErrorSet mutexes as they need try_lock() - Anywhere mutexes are used in conjunction with std::condition_variables (as they explicitly require std::mutex) Bug: angleproject:8667 Change-Id: Ib6d68938b0886f9e7c43e023162557990ecfb300 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5453294 Reviewed-by: Roman Lavrov <romanl@google.com> Reviewed-by: Charlie Lao <cclao@google.com> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
//
// Copyright 2022 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// AstcDecompressorImpl.cpp: Decodes ASTC-encoded textures.
#include <array>
#include <future>
#include <unordered_map>
#include "astcenc.h"
#include "common/SimpleMutex.h"
#include "common/WorkerThread.h"
#include "image_util/AstcDecompressor.h"
namespace angle
{
namespace
{
const astcenc_swizzle kSwizzle = {ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A};
// Used by std::unique_ptr to release the context when the pointer is destroyed
struct AstcencContextDeleter
{
void operator()(astcenc_context *c) { astcenc_context_free(c); }
};
using AstcencContextUniquePtr = std::unique_ptr<astcenc_context, AstcencContextDeleter>;
// Returns the max number of threads to use when using multithreaded decompression
uint32_t MaxThreads()
{
static const uint32_t numThreads = std::min(16u, std::thread::hardware_concurrency());
return numThreads;
}
// Creates a new astcenc_context and wraps it in a smart pointer.
// It is not needed to call astcenc_context_free() on the returned pointer.
// blockWith, blockSize: ASTC block size for the context
// Error: (output param) Where to put the error status. Must not be null.
// Returns nullptr in case of error.
AstcencContextUniquePtr MakeDecoderContext(uint32_t blockWidth,
uint32_t blockHeight,
astcenc_error *error)
{
astcenc_config config = {};
*error =
// TODO(gregschlom): Do we need a special case for sRGB images? (And pass
// ASTCENC_PRF_LDR_SRGB here?)
astcenc_config_init(ASTCENC_PRF_LDR, blockWidth, blockHeight, 1, ASTCENC_PRE_FASTEST,
ASTCENC_FLG_DECOMPRESS_ONLY, &config);
if (*error != ASTCENC_SUCCESS)
{
return nullptr;
}
astcenc_context *context;
*error = astcenc_context_alloc(&config, MaxThreads(), &context);
if (*error != ASTCENC_SUCCESS)
{
return nullptr;
}
return AstcencContextUniquePtr(context);
}
// Returns whether the ASTC decompressor can be used on this machine. It might not be available if
// the CPU doesn't support AVX2 instructions for example. Since this call is a bit expensive and
// never changes, the result should be cached.
bool IsAstcDecompressorAvailable()
{
astcenc_error error;
// Try getting an arbitrary context. If it works, the decompressor is available.
AstcencContextUniquePtr context = MakeDecoderContext(5, 5, &error);
return context != nullptr;
}
// Caches and manages astcenc_context objects.
//
// Each context is fairly large (around 30 MB) and takes a while to construct, so it's important to
// reuse them as much as possible.
//
// While context objects can be reused across multiple threads, they must be used sequentially. To
// avoid having to lock and manage access between threads, we keep one cache per thread. This avoids
// any concurrency issues, at the cost of extra memory.
//
// Currently, there is no eviction strategy. Each cache could grow to a maximum of ~400 MB in size
// since they are 13 possible ASTC block sizes.
//
// Thread-safety: not thread safe.
class AstcDecompressorContextCache
{
public:
// Returns a context object for a given ASTC block size, along with the error code if the
// context initialization failed.
// In this case, the context will be null, and the status code will be non-zero.
std::pair<astcenc_context *, astcenc_error> get(uint32_t blockWidth, uint32_t blockHeight)
{
Value &value = mContexts[{blockWidth, blockHeight}];
if (value.context == nullptr)
{
value.context = MakeDecoderContext(blockWidth, blockHeight, &value.error);
}
return {value.context.get(), value.error};
}
private:
// Holds the data we use as the cache key
struct Key
{
uint32_t blockWidth;
uint32_t blockHeight;
bool operator==(const Key &other) const
{
return blockWidth == other.blockWidth && blockHeight == other.blockHeight;
}
};
struct Value
{
AstcencContextUniquePtr context = nullptr;
astcenc_error error = ASTCENC_SUCCESS;
};
// Computes the hash of a Key
struct KeyHash
{
std::size_t operator()(const Key &k) const
{
// blockWidth and blockHeight are < 256 (actually, < 16), so this is safe
return k.blockWidth << 8 | k.blockHeight;
}
};
std::unordered_map<Key, Value, KeyHash> mContexts;
};
struct DecompressTask : public Closure
{
DecompressTask(astcenc_context *context,
uint32_t threadIndex,
const uint8_t *data,
size_t dataLength,
astcenc_image *image)
: context(context),
threadIndex(threadIndex),
data(data),
dataLength(dataLength),
image(image)
{}
void operator()() override
{
result = astcenc_decompress_image(context, data, dataLength, image, &kSwizzle, threadIndex);
}
astcenc_context *context;
uint32_t threadIndex;
const uint8_t *data;
size_t dataLength;
astcenc_image *image;
astcenc_error result;
};
// Performs ASTC decompression of an image on the CPU
class AstcDecompressorImpl : public AstcDecompressor
{
public:
AstcDecompressorImpl()
: AstcDecompressor(), mContextCache(std::make_unique<AstcDecompressorContextCache>())
{
mTasks.reserve(MaxThreads());
mWaitEvents.reserve(MaxThreads());
}
~AstcDecompressorImpl() override = default;
bool available() const override
{
static bool available = IsAstcDecompressorAvailable();
return available;
}
int32_t decompress(std::shared_ptr<WorkerThreadPool> singleThreadPool,
std::shared_ptr<WorkerThreadPool> multiThreadPool,
const uint32_t imgWidth,
const uint32_t imgHeight,
const uint32_t blockWidth,
const uint32_t blockHeight,
const uint8_t *input,
size_t inputLength,
uint8_t *output) override
{
// A given astcenc context can only decompress one image at a time, which we why we keep
// this mutex locked the whole time.
std::lock_guard global_lock(mMutex);
auto [context, context_status] = mContextCache->get(blockWidth, blockHeight);
if (context_status != ASTCENC_SUCCESS)
return context_status;
astcenc_image image;
image.dim_x = imgWidth;
image.dim_y = imgHeight;
image.dim_z = 1;
image.data_type = ASTCENC_TYPE_U8;
image.data = reinterpret_cast<void **>(&output);
// For smaller images the overhead of multithreading exceeds the benefits.
const bool singleThreaded = (imgHeight <= 32 && imgWidth <= 32) || !multiThreadPool;
std::shared_ptr<WorkerThreadPool> &threadPool =
singleThreaded ? singleThreadPool : multiThreadPool;
const uint32_t threadCount = singleThreaded ? 1 : MaxThreads();
mTasks.clear();
mWaitEvents.clear();
for (uint32_t i = 0; i < threadCount; ++i)
{
mTasks.push_back(
std::make_shared<DecompressTask>(context, i, input, inputLength, &image));
mWaitEvents.push_back(threadPool->postWorkerTask(mTasks[i]));
}
WaitableEvent::WaitMany(&mWaitEvents);
astcenc_decompress_reset(context);
for (auto &task : mTasks)
{
if (task->result != ASTCENC_SUCCESS)
return task->result;
}
return ASTCENC_SUCCESS;
}
const char *getStatusString(int32_t statusCode) const override
{
const char *msg = astcenc_get_error_string((astcenc_error)statusCode);
return msg ? msg : "ASTCENC_UNKNOWN_STATUS";
}
private:
std::unique_ptr<AstcDecompressorContextCache> mContextCache;
angle::SimpleMutex mMutex; // Locked while calling `decode()`
std::vector<std::shared_ptr<DecompressTask>> mTasks;
std::vector<std::shared_ptr<WaitableEvent>> mWaitEvents;
};
} // namespace
AstcDecompressor &AstcDecompressor::get()
{
static auto *instance = new AstcDecompressorImpl();
return *instance;
}
} // namespace angle