Move GR_MAKE_BITFIELD_OPS and GrAlignTo to non-GPU files

This is a more cautious attempt of https://skia-review.googlesource.com/c/skia/+/464876

GrAlignTo renamed to SkAlignTo and moved to SkTypes.h
(with the rest of our align helpers).

GR_MAKE_BITFIELD_OPS and GR_DECL_BITFIELD_OPS_FRIENDS
renamed to SK_* and moved to private/SkMacros.h

This avoids our public includes using a src header file
and messing with SkUtils.h, which apparently was used
by Android. That should be cleaned up in a separate effort.

Change-Id: I86d5e1fb6a7834034534266a6c340bc7757f9abb
Bug: skia:12584
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/466176
Reviewed-by: Brian Osman <brianosman@google.com>
This commit is contained in:
Kevin Lubick 2021-11-01 11:45:19 -04:00
parent c807847427
commit afa657d6ab
17 changed files with 85 additions and 73 deletions

View File

@ -30,7 +30,7 @@ static_assert(sizeof(Unaligned) == 30);
static_assert(sizeof(Unaligned) % GrMemoryPool::kAlignment != 0);
// When max_align_t == 16, 8, or 4 the padded Unaligned will also be 32
static_assert(GrAlignTo(sizeof(Unaligned), GrMemoryPool::kAlignment) == sizeof(Aligned));
static_assert(SkAlignTo(sizeof(Unaligned), GrMemoryPool::kAlignment) == sizeof(Aligned));
// All benchmarks create and delete the same number of objects. The key difference is the order
// of operations, the size of the objects being allocated, and the size of the pool.

View File

@ -557,6 +557,15 @@ template <typename T> static constexpr bool SkIsAlignPtr(T x) {
return sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x);
}
/**
* align up to a power of 2
*/
static inline constexpr size_t SkAlignTo(size_t x, size_t alignment) {
// The same as alignment && SkIsPow2(value), w/o a dependency cycle.
SkASSERT(alignment && (alignment & (alignment - 1)) == 0);
return (x + alignment - 1) & ~(alignment - 1);
}
typedef uint32_t SkFourByteTag;
static inline constexpr SkFourByteTag SkSetFourByteTag(char a, char b, char c, char d) {
return (((uint32_t)a << 24) | ((uint32_t)b << 16) | ((uint32_t)c << 8) | (uint32_t)d);

View File

@ -18,45 +18,6 @@ class SkSurface;
////////////////////////////////////////////////////////////////////////////////
/**
* Defines overloaded bitwise operators to make it easier to use an enum as a
* bitfield.
*/
#define GR_MAKE_BITFIELD_OPS(X) \
inline X operator |(X a, X b) { \
return (X) (+a | +b); \
} \
inline X& operator |=(X& a, X b) { \
return (a = a | b); \
} \
inline X operator &(X a, X b) { \
return (X) (+a & +b); \
} \
inline X& operator &=(X& a, X b) { \
return (a = a & b); \
} \
template <typename T> \
inline X operator &(T a, X b) { \
return (X) (+a & +b); \
} \
template <typename T> \
inline X operator &(X a, T b) { \
return (X) (+a & +b); \
} \
#define GR_DECL_BITFIELD_OPS_FRIENDS(X) \
friend X operator |(X a, X b); \
friend X& operator |=(X& a, X b); \
\
friend X operator &(X a, X b); \
friend X& operator &=(X& a, X b); \
\
template <typename T> \
friend X operator &(T a, X b); \
\
template <typename T> \
friend X operator &(X a, T b); \
/**
* Wraps a C++11 enum that we use as a bitfield, and enables a limited amount of
* masking with type safety. Instantiated with the ~ operator.

View File

@ -15,6 +15,7 @@
#include "include/core/SkRefCnt.h"
#include "include/gpu/GrTypes.h"
#include "include/private/SkImageInfoPriv.h"
#include "include/private/SkMacros.h"
class GrBackendFormat;
class GrCaps;
@ -35,14 +36,6 @@ using GrStdSteadyClock = std::chrono::steady_clock;
static inline constexpr size_t GrSizeDivRoundUp(size_t x, size_t y) { return (x + (y - 1)) / y; }
/**
* align up to a power of 2
*/
static inline constexpr size_t GrAlignTo(size_t x, size_t alignment) {
SkASSERT(alignment && SkIsPow2(alignment));
return (x + alignment - 1) & ~(alignment - 1);
}
/**
* Geometric primitives used for drawing.
*/
@ -373,7 +366,7 @@ enum GrShaderFlags {
kTessEvaluation_GrShaderFlag = 1 << 2,
kFragment_GrShaderFlag = 1 << 3
};
GR_MAKE_BITFIELD_OPS(GrShaderFlags)
SK_MAKE_BITFIELD_OPS(GrShaderFlags)
/** Is the shading language type float (including vectors/matrices)? */
static constexpr bool GrSLTypeIsFloatType(GrSLType type) {

View File

@ -40,4 +40,45 @@
#define SK_INIT_TO_AVOID_WARNING = 0
////////////////////////////////////////////////////////////////////////////////
/**
* Defines overloaded bitwise operators to make it easier to use an enum as a
* bitfield.
*/
#define SK_MAKE_BITFIELD_OPS(X) \
inline X operator |(X a, X b) { \
return (X) (+a | +b); \
} \
inline X& operator |=(X& a, X b) { \
return (a = a | b); \
} \
inline X operator &(X a, X b) { \
return (X) (+a & +b); \
} \
inline X& operator &=(X& a, X b) { \
return (a = a & b); \
} \
template <typename T> \
inline X operator &(T a, X b) { \
return (X) (+a & +b); \
} \
template <typename T> \
inline X operator &(X a, T b) { \
return (X) (+a & +b); \
} \
#define SK_DECL_BITFIELD_OPS_FRIENDS(X) \
friend X operator |(X a, X b); \
friend X& operator |=(X& a, X b); \
\
friend X operator &(X a, X b); \
friend X& operator &=(X& a, X b); \
\
template <typename T> \
friend X operator &(T a, X b); \
\
template <typename T> \
friend X operator &(X a, T b); \
#endif // SkMacros_DEFINED

View File

@ -16,7 +16,7 @@ SkBlockAllocator::SkBlockAllocator(GrowthPolicy policy, size_t blockIncrementByt
: fTail(&fHead)
// Round up to the nearest max-aligned value, and then divide so that fBlockSizeIncrement
// can effectively fit higher byte counts in its 16 bits of storage
, fBlockIncrement(SkTo<uint16_t>(GrAlignTo(blockIncrementBytes, kAddressAlign)
, fBlockIncrement(SkTo<uint16_t>(SkAlignTo(blockIncrementBytes, kAddressAlign)
/ kAddressAlign))
, fGrowthPolicy(static_cast<uint64_t>(policy))
, fN0((policy == GrowthPolicy::kLinear || policy == GrowthPolicy::kExponential) ? 1 : 0)

View File

@ -8,12 +8,17 @@
#ifndef SkBlockAllocator_DEFINED
#define SkBlockAllocator_DEFINED
#include "include/private/GrTypesPriv.h"
#include "include/core/SkMath.h"
#include "include/core/SkTypes.h"
#include "include/private/SkMacros.h"
#include "include/private/SkNoncopyable.h"
#include "include/private/SkTo.h"
#include "src/core/SkASAN.h"
#include <memory> // std::unique_ptr
#include <cstddef> // max_align_t
#include <limits> // numeric_limits
#include <algorithm> // max
/**
* SkBlockAllocator provides low-level support for a block allocated arena with a dynamic tail that
@ -509,12 +514,12 @@ private:
///////////////////////////////////////////////////////////////////////////////////////////////////
// Template and inline implementations
GR_MAKE_BITFIELD_OPS(SkBlockAllocator::ReserveFlags)
SK_MAKE_BITFIELD_OPS(SkBlockAllocator::ReserveFlags)
template<size_t Align, size_t Padding>
constexpr size_t SkBlockAllocator::BlockOverhead() {
static_assert(GrAlignTo(kDataStart + Padding, Align) >= sizeof(Block));
return GrAlignTo(kDataStart + Padding, Align);
static_assert(SkAlignTo(kDataStart + Padding, Align) >= sizeof(Block));
return SkAlignTo(kDataStart + Padding, Align);
}
template<size_t Align, size_t Padding>
@ -562,7 +567,7 @@ SkBlockAllocator::ByteRange SkBlockAllocator::allocate(size_t size) {
static constexpr int kBlockOverhead = (int) BlockOverhead<Align, Padding>();
// Ensures 'offset' and 'end' calculations will be valid
static_assert((kMaxAllocationSize + GrAlignTo(MaxBlockSize<Align, Padding>(), Align))
static_assert((kMaxAllocationSize + SkAlignTo(MaxBlockSize<Align, Padding>(), Align))
<= (size_t) std::numeric_limits<int32_t>::max());
// Ensures size + blockOverhead + addBlock's alignment operations will be valid
static_assert(kMaxAllocationSize + kBlockOverhead + ((1 << 12) - 1) // 4K align for large blocks
@ -625,7 +630,7 @@ int SkBlockAllocator::Block::alignedOffset(int offset) const {
<= (size_t) std::numeric_limits<int32_t>::max());
if /* constexpr */ (Align <= kAddressAlign) {
// Same as GrAlignTo, but operates on ints instead of size_t
// Same as SkAlignTo, but operates on ints instead of size_t
return (offset + Padding + Align - 1) & ~(Align - 1);
} else {
// Must take into account that 'this' may be starting at a pointer that doesn't satisfy the

View File

@ -8,6 +8,7 @@
#ifndef GrFragmentProcessor_DEFINED
#define GrFragmentProcessor_DEFINED
#include "include/private/SkMacros.h"
#include "include/private/SkSLSampleUsage.h"
#include "include/private/SkSLString.h"
#include "src/gpu/GrProcessor.h"
@ -336,7 +337,7 @@ protected:
kPreservesOpaqueInput_OptimizationFlag |
kConstantOutputForConstantInput_OptimizationFlag
};
GR_DECL_BITFIELD_OPS_FRIENDS(OptimizationFlags)
SK_DECL_BITFIELD_OPS_FRIENDS(OptimizationFlags)
/**
* Can be used as a helper to decide which fragment processor OptimizationFlags should be set.
@ -675,7 +676,7 @@ private:
//////////////////////////////////////////////////////////////////////////////
GR_MAKE_BITFIELD_OPS(GrFragmentProcessor::OptimizationFlags)
SK_MAKE_BITFIELD_OPS(GrFragmentProcessor::OptimizationFlags)
static inline GrFPResult GrFPFailure(std::unique_ptr<GrFragmentProcessor> fp) {
return {false, std::move(fp)};

View File

@ -50,7 +50,7 @@ size_t GrRingBuffer::getAllocationOffset(size_t size) {
return fTotalSize;
}
fHead = GrAlignTo(head + size, fAlignment);
fHead = SkAlignTo(head + size, fAlignment);
return modHead;
}

View File

@ -768,7 +768,7 @@ void SurfaceContext::asyncReadPixels(GrDirectContext* dContext,
auto manager = context->fMappedBufferManager;
auto result = std::make_unique<AsyncReadResult>(manager->owningDirectContext());
size_t rowBytes =
GrAlignTo(context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType),
SkAlignTo(context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType),
context->fBufferAlignment);
if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
manager)) {
@ -1002,14 +1002,14 @@ void SurfaceContext::asyncRescaleAndReadPixelsYUV420(GrDirectContext* dContext,
auto manager = context->fMappedBufferManager;
auto result = std::make_unique<AsyncReadResult>(manager->owningDirectContext());
size_t rowBytes = SkToSizeT(context->fSize.width());
rowBytes = GrAlignTo(rowBytes, context->fBufferAlignment);
rowBytes = SkAlignTo(rowBytes, context->fBufferAlignment);
if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
(*context->fClientCallback)(context->fClientContext, nullptr);
delete context;
return;
}
rowBytes = SkToSizeT(context->fSize.width()) / 2;
rowBytes = GrAlignTo(rowBytes, context->fBufferAlignment);
rowBytes = SkAlignTo(rowBytes, context->fBufferAlignment);
SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
(*context->fClientCallback)(context->fClientContext, nullptr);
@ -1262,7 +1262,7 @@ SurfaceContext::PixelTransferResult SurfaceContext::transferPixels(GrColorType d
}
size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
rowBytes = GrAlignTo(rowBytes, this->caps()->transferBufferAlignment());
rowBytes = SkAlignTo(rowBytes, this->caps()->transferBufferAlignment());
size_t size = rowBytes * rect.height();
// By using kStream_GrAccessPattern here, we are not able to cache and reuse the buffer for
// multiple reads. Switching to kDynamic_GrAccessPattern would allow for this, however doing

View File

@ -895,7 +895,7 @@ GrCaps::SupportedWrite GrD3DCaps::supportedWritePixelsColorType(
}
// Any buffer data needs to be aligned to 512 bytes and that of a single texel.
size_t offsetAlignment = GrAlignTo(GrDxgiFormatBytesPerBlock(dxgiFormat),
size_t offsetAlignment = SkAlignTo(GrDxgiFormatBytesPerBlock(dxgiFormat),
D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
const auto& info = this->getFormatInfo(dxgiFormat);

View File

@ -268,7 +268,7 @@ D3D12_GPU_VIRTUAL_ADDRESS GrD3DResourceProvider::uploadConstantData(void* data,
constexpr int kConstantAlignment = 256;
// upload the data
size_t paddedSize = GrAlignTo(size, kConstantAlignment);
size_t paddedSize = SkAlignTo(size, kConstantAlignment);
GrRingBuffer::Slice slice = fGpu->uniformsRingBuffer()->suballocate(paddedSize);
char* destPtr = static_cast<char*>(slice.fBuffer->map()) + slice.fOffset;
memcpy(destPtr, data, size);

View File

@ -112,7 +112,7 @@ private:
kLast_Property = kCanTweakAlphaForCoverage_Property
};
GR_DECL_BITFIELD_OPS_FRIENDS(Properties)
SK_DECL_BITFIELD_OPS_FRIENDS(Properties)
/**
* Deduce the properties of a BlendFormula.
@ -140,7 +140,7 @@ private:
static_assert(4 == sizeof(BlendFormula));
GR_MAKE_BITFIELD_OPS(BlendFormula::Properties)
SK_MAKE_BITFIELD_OPS(BlendFormula::Properties)
constexpr BlendFormula::Properties BlendFormula::GetProperties(OutputType PrimaryOut,
OutputType SecondaryOut,

View File

@ -10,6 +10,7 @@
#include "include/core/SkBlendMode.h"
#include "include/gpu/GrTypes.h"
#include "include/private/SkMacros.h"
#include "src/gpu/GrXferProcessor.h"
// See the comment above GrXPFactory's definition about this warning suppression.

View File

@ -65,7 +65,7 @@ GrMtlBuffer::GrMtlBuffer(GrMtlGpu* gpu, size_t size, GrGpuBufferType intendedTyp
}
}
size = GrAlignTo(size, gpu->mtlCaps().getMinBufferAlignment());
size = SkAlignTo(size, gpu->mtlCaps().getMinBufferAlignment());
fMtlBuffer = size == 0 ? nil :
[gpu->device() newBufferWithLength: size
options: options];

View File

@ -10,6 +10,7 @@
#include "include/gpu/GrTypes.h"
#include "include/gpu/vk/GrVkTypes.h"
#include "include/private/SkMacros.h"
#include "src/gpu/vk/GrVkManagedResource.h"
#include <cinttypes>
@ -87,7 +88,7 @@ public:
// at least have a color attachment.
kExternal_AttachmentFlag = 0x8,
};
GR_DECL_BITFIELD_OPS_FRIENDS(AttachmentFlags);
SK_DECL_BITFIELD_OPS_FRIENDS(AttachmentFlags);
enum class SelfDependencyFlags {
kNone = 0,
@ -201,7 +202,7 @@ private:
using INHERITED = GrVkManagedResource;
};
GR_MAKE_BITFIELD_OPS(GrVkRenderPass::AttachmentFlags)
SK_MAKE_BITFIELD_OPS(GrVkRenderPass::AttachmentFlags)
GR_MAKE_BITFIELD_CLASS_OPS(GrVkRenderPass::SelfDependencyFlags)
#endif

View File

@ -155,7 +155,7 @@ void basic_transfer_to_test(skiatest::Reporter* reporter,
if (!allowedSrc.fOffsetAlignmentForTransferBuffer) {
return;
}
size_t srcRowBytes = GrAlignTo(GrColorTypeBytesPerPixel(allowedSrc.fColorType) * srcBufferWidth,
size_t srcRowBytes = SkAlignTo(GrColorTypeBytesPerPixel(allowedSrc.fColorType) * srcBufferWidth,
caps->transferBufferAlignment());
std::unique_ptr<char[]> srcData(new char[kTexDims.fHeight * srcRowBytes]);
@ -341,8 +341,8 @@ void basic_transfer_from_test(skiatest::Reporter* reporter, const sk_gpu_test::C
GrImageInfo readInfo(allowedRead.fColorType, kUnpremul_SkAlphaType, nullptr, kTexDims);
size_t bpp = GrColorTypeBytesPerPixel(allowedRead.fColorType);
size_t fullBufferRowBytes = GrAlignTo(kTexDims.fWidth * bpp, caps->transferBufferAlignment());
size_t partialBufferRowBytes = GrAlignTo(kPartialWidth * bpp, caps->transferBufferAlignment());
size_t fullBufferRowBytes = SkAlignTo(kTexDims.fWidth * bpp, caps->transferBufferAlignment());
size_t partialBufferRowBytes = SkAlignTo(kPartialWidth * bpp, caps->transferBufferAlignment());
size_t offsetAlignment = allowedRead.fOffsetAlignmentForTransferBuffer;
SkASSERT(offsetAlignment);