From 90b0a1cc20c91694816a325a45a4498acaf4e315 Mon Sep 17 00:00:00 2001 From: John Reck Date: Thu, 12 Nov 2020 12:37:30 -0500 Subject: [PATCH] Verify for_each is const Test: this Change-Id: I7ffd755b4b762f7e8608cf84b328560783162439 --- libs/hwui/canvas/CanvasOpBuffer.h | 4 ++++ libs/hwui/canvas/CanvasOpRasterizer.cpp | 2 +- libs/hwui/canvas/OpBuffer.h | 2 +- libs/hwui/tests/unit/CanvasOpTests.cpp | 14 ++++++++++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/libs/hwui/canvas/CanvasOpBuffer.h b/libs/hwui/canvas/CanvasOpBuffer.h index b80faeb1a65b8..07e079a7d57f3 100644 --- a/libs/hwui/canvas/CanvasOpBuffer.h +++ b/libs/hwui/canvas/CanvasOpBuffer.h @@ -46,6 +46,10 @@ public: const SkMatrix& transform() const { return mTransform; } CanvasOp* operator->() noexcept { return &mImpl; } + const CanvasOp* operator->() const noexcept { return &mImpl; } + + CanvasOp& op() noexcept { return mImpl; } + const CanvasOp& op() const noexcept { return mImpl; } }; extern template class OpBuffer; diff --git a/libs/hwui/canvas/CanvasOpRasterizer.cpp b/libs/hwui/canvas/CanvasOpRasterizer.cpp index 97c418a3e8d09..25129f641c002 100644 --- a/libs/hwui/canvas/CanvasOpRasterizer.cpp +++ b/libs/hwui/canvas/CanvasOpRasterizer.cpp @@ -32,7 +32,7 @@ void rasterizeCanvasBuffer(const CanvasOpBuffer& source, SkCanvas* destination) std::vector globalMatrixStack; SkMatrix& currentGlobalTransform = globalMatrixStack.emplace_back(SkMatrix::I()); - source.for_each([&](CanvasOpContainer * op) { + source.for_each([&](const CanvasOpContainer * op) { if constexpr (T == CanvasOpType::BeginZ || T == CanvasOpType::EndZ) { // Do beginZ or endZ LOG_ALWAYS_FATAL("TODO"); diff --git a/libs/hwui/canvas/OpBuffer.h b/libs/hwui/canvas/OpBuffer.h index 398e090b8cfab..98e385f37a6e4 100644 --- a/libs/hwui/canvas/OpBuffer.h +++ b/libs/hwui/canvas/OpBuffer.h @@ -156,7 +156,7 @@ private: using F_PTR = decltype(&f); using THUNK = void (*)(F_PTR, void*); static constexpr auto jump = std::array{[](F_PTR fp, void* t) { - (*fp)(reinterpret_cast(I)>*>(t)); + (*fp)(reinterpret_cast(I)>*>(t)); }...}; // Do the actual iteration of each item diff --git a/libs/hwui/tests/unit/CanvasOpTests.cpp b/libs/hwui/tests/unit/CanvasOpTests.cpp index c90d1a4f8cda3..84fc6e6d508a9 100644 --- a/libs/hwui/tests/unit/CanvasOpTests.cpp +++ b/libs/hwui/tests/unit/CanvasOpTests.cpp @@ -138,6 +138,20 @@ TEST(CanvasOp, lifecycleCheckMove) { EXPECT_EQ(tracker.alive(), 0); } +TEST(CanvasOp, verifyConst) { + CanvasOpBuffer buffer; + buffer.push({ + .color = SkColors::kBlack, + .mode = SkBlendMode::kSrcOver, + }); + buffer.for_each([](auto op) { + static_assert(std::is_const_v>, + "Expected container to be const"); + static_assert(std::is_const_vop())>>, + "Expected op to be const"); + }); +} + TEST(CanvasOp, simplePush) { CanvasOpBuffer buffer; EXPECT_EQ(buffer.size(), 0);