skia2/samplecode/SampleTiming.cpp
Brian Salomon 9fa47cc1c6 Make class members that are static constexpr also be inline.
This is in prep for compiling with -std=c++14 and -Wno-c++17-extensions
when building with clang. Chrome has encountered problems with
third_party headers that are included both in Skia and other Chrome
sources that produce different code based on whether preprocessor macros
indicate a C++14 or C++17 compilation.

In C++17 they are already inline implicitly. When compiling with C++14
we can get linker errors unless they're explicitly inlined or defined
outside the class. With -Wno-c++17-extensions we can explicitly inline
them in the C++14 build because the warning that would be generated
about using a C++17 language extension is suppressed.

We cannot do this in public headers because we support compiling with
C++14 without suppressing the C++17 language extension warnings.

Bug: chromium:1257145
Change-Id: Iaf5f4c62a398f98dd4ca9b7dfb86f2d5cab21d66
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/457498
Reviewed-by: Ben Wagner <bungeman@google.com>
Reviewed-by: Herb Derby <herb@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
2021-10-11 16:22:59 +00:00

97 lines
3.5 KiB
C++

/*
* Copyright 2020 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/core/SkCanvas.h"
#include "include/core/SkFont.h"
#include "include/core/SkSurface.h"
#include "samplecode/Sample.h"
#include <chrono>
struct TimingSample : public Sample {
inline static constexpr int W = 24,
H = 16;
sk_sp<SkImage> fImg;
SkString name() override { return SkString("Timing"); }
void onOnceBeforeDraw() override {
sk_sp<SkSurface> surf = SkSurface::MakeRasterN32Premul(W,H);
surf->getCanvas()->drawString("abc", 2,H-4, SkFont{}, SkPaint{});
fImg = surf->makeImageSnapshot();
}
void onDrawContent(SkCanvas* canvas) override {
canvas->scale(8,8);
// Draw normally.
canvas->drawImage(fImg, 0,0);
canvas->translate(0,H);
// Draw one pixel at a time with drawImageRect(),
// timing how long each drawImageRect() call takes.
double cost[H][W];
double min = +INFINITY,
max = -INFINITY;
for (int y = 0; y < H; y++)
for (int x = 0; x < W; x++) {
auto start = std::chrono::steady_clock::now();
canvas->drawImageRect(fImg.get(),
SkRect::MakeXYWH(x,y,1,1), SkRect::MakeXYWH(x,y,1,1),
SkSamplingOptions(), /*paint=*/nullptr,
SkCanvas::kStrict_SrcRectConstraint);
auto elapsed = std::chrono::steady_clock::now() - start;
cost[y][x] = elapsed.count();
min = std::min(min, cost[y][x]);
max = std::max(max, cost[y][x]);
}
canvas->translate(0,H);
// Draw using those per-pixel timings,
// with the slowest pixel scaled to alpha=1, the fastest to alpha=0.
for (int y = 0; y < H; y++)
for (int x = 0; x < W; x++) {
SkPaint p;
p.setAlphaf( (cost[y][x] - min) / (max - min) );
canvas->drawRect(SkRect::MakeXYWH(x,y,1,1), p);
}
canvas->translate(0,H);
// Draw each pixel into offscreen, timing each draw.
SkImageInfo info = canvas->imageInfo().makeWH(1024,1024);
if (sk_sp<SkSurface> offscreen = canvas->makeSurface(info)) {
min = +INFINITY;
max = -INFINITY;
for (int y = 0; y < H; y++)
for (int x = 0; x < W; x++) {
auto start = std::chrono::steady_clock::now();
offscreen->getCanvas()->drawImageRect(fImg,
SkRect::MakeXYWH(x,y,1,1),
SkRect::MakeXYWH(0,0,1024,1024),
SkSamplingOptions(),
/*paint=*/nullptr,
SkCanvas::kStrict_SrcRectConstraint);
auto elapsed = std::chrono::steady_clock::now() - start;
cost[y][x] = elapsed.count();
min = std::min(min, cost[y][x]);
max = std::max(max, cost[y][x]);
}
for (int y = 0; y < H; y++)
for (int x = 0; x < W; x++) {
SkPaint p;
p.setAlphaf( (cost[y][x] - min) / (max - min) );
canvas->drawRect(SkRect::MakeXYWH(x,y,1,1), p);
}
}
}
};
DEF_SAMPLE( return new TimingSample; )