2016-09-08 19:35:32 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "bench/Benchmark.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
2020-07-06 17:45:34 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2020-10-14 15:23:11 +00:00
|
|
|
#include "src/gpu/GrDirectContextPriv.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2016-09-08 19:35:32 +00:00
|
|
|
|
2016-09-15 15:33:02 +00:00
|
|
|
|
2018-06-18 19:11:00 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2016-09-08 19:35:32 +00:00
|
|
|
/** These benchmarks were designed to measure changes to GrResourceCache's replacement policy */
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// The width/height of the images to draw. The small size underestimates the value of a good
|
|
|
|
// replacement strategy since the texture uploads are quite small. However, the effects are still
|
|
|
|
// significant and this lets the benchmarks complete a lot faster, especially on mobile.
|
|
|
|
static constexpr int kS = 25;
|
|
|
|
|
|
|
|
static void make_images(sk_sp<SkImage> imgs[], int cnt) {
|
|
|
|
for (int i = 0; i < cnt; ++i) {
|
2020-12-23 15:11:33 +00:00
|
|
|
imgs[i] = ToolUtils::create_checkerboard_image(kS, kS, SK_ColorBLACK, SK_ColorCYAN, 10);
|
2016-09-08 19:35:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_image(SkCanvas* canvas, SkImage* img) {
|
|
|
|
// Make the paint transparent to avoid any issues of deferred tiler blending
|
|
|
|
// optmizations
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAlpha(0x10);
|
2021-01-28 02:21:08 +00:00
|
|
|
canvas->drawImage(img, 0, 0, SkSamplingOptions(), &paint);
|
2016-09-08 19:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_cache_budget(SkCanvas* canvas, int approxImagesInBudget) {
|
|
|
|
// This is inexact but we attempt to figure out a baseline number of resources GrContext needs
|
|
|
|
// to render an SkImage and add one additional resource for each image we'd like to fit.
|
2020-07-06 17:45:34 +00:00
|
|
|
auto context = canvas->recordingContext()->asDirectContext();
|
2016-09-08 19:35:32 +00:00
|
|
|
SkASSERT(context);
|
2020-05-14 19:45:44 +00:00
|
|
|
context->flushAndSubmit();
|
2019-02-06 20:12:53 +00:00
|
|
|
context->priv().testingOnly_purgeAllUnlockedResources();
|
2016-09-08 19:35:32 +00:00
|
|
|
sk_sp<SkImage> image;
|
|
|
|
make_images(&image, 1);
|
|
|
|
draw_image(canvas, image.get());
|
2020-05-14 19:45:44 +00:00
|
|
|
context->flushAndSubmit();
|
2016-09-08 19:35:32 +00:00
|
|
|
int baselineCount;
|
|
|
|
context->getResourceCacheUsage(&baselineCount, nullptr);
|
|
|
|
baselineCount -= 1; // for the image's textures.
|
|
|
|
context->setResourceCacheLimits(baselineCount + approxImagesInBudget, 1 << 30);
|
2019-02-06 20:12:53 +00:00
|
|
|
context->priv().testingOnly_purgeAllUnlockedResources();
|
2016-09-08 19:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests repeatedly drawing the same set of images in each frame. Different instances of the bench
|
|
|
|
* run with different cache sizes and either repeat the image order each frame or use a random
|
|
|
|
* order. Every variation of this bench draws the same image set, only the budget and order of
|
|
|
|
* images differs. Since the total fill is the same they can be cross-compared.
|
|
|
|
*/
|
|
|
|
class ImageCacheBudgetBench : public Benchmark {
|
|
|
|
public:
|
|
|
|
/** budgetSize is the number of images that can fit in the cache. 100 images will be drawn. */
|
|
|
|
ImageCacheBudgetBench(int budgetSize, bool shuffle)
|
|
|
|
: fBudgetSize(budgetSize)
|
|
|
|
, fShuffle(shuffle)
|
|
|
|
, fIndices(nullptr) {
|
|
|
|
float imagesOverBudget = float(kImagesToDraw) / budgetSize;
|
|
|
|
// Make the benchmark name contain the percentage of the budget that is used in each
|
|
|
|
// simulated frame.
|
|
|
|
fName.printf("image_cache_budget_%.0f%s", imagesOverBudget * 100,
|
|
|
|
(shuffle ? "_shuffle" : ""));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isSuitableFor(Backend backend) override { return kGPU_Backend == backend; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const char* onGetName() override {
|
|
|
|
return fName.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void onPerCanvasPreDraw(SkCanvas* canvas) override {
|
2020-07-06 17:45:34 +00:00
|
|
|
auto context = canvas->recordingContext()->asDirectContext();
|
2016-09-08 19:35:32 +00:00
|
|
|
SkASSERT(context);
|
2019-09-03 14:29:20 +00:00
|
|
|
fOldBytes = context->getResourceCacheLimit();
|
2016-09-08 19:35:32 +00:00
|
|
|
set_cache_budget(canvas, fBudgetSize);
|
|
|
|
make_images(fImages, kImagesToDraw);
|
|
|
|
if (fShuffle) {
|
|
|
|
SkRandom random;
|
|
|
|
fIndices.reset(new int[kSimulatedFrames * kImagesToDraw]);
|
|
|
|
for (int frame = 0; frame < kSimulatedFrames; ++frame) {
|
|
|
|
int* base = fIndices.get() + frame * kImagesToDraw;
|
|
|
|
for (int i = 0; i < kImagesToDraw; ++i) {
|
|
|
|
base[i] = i;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < kImagesToDraw - 1; ++i) {
|
|
|
|
int other = random.nextULessThan(kImagesToDraw - i) + i;
|
2018-06-18 19:11:00 +00:00
|
|
|
using std::swap;
|
|
|
|
swap(base[i], base[other]);
|
2016-09-08 19:35:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onPerCanvasPostDraw(SkCanvas* canvas) override {
|
2020-07-06 17:45:34 +00:00
|
|
|
auto context = canvas->recordingContext()->asDirectContext();
|
2016-09-08 19:35:32 +00:00
|
|
|
SkASSERT(context);
|
2019-09-03 14:29:20 +00:00
|
|
|
context->setResourceCacheLimit(fOldBytes);
|
2016-09-08 19:35:32 +00:00
|
|
|
for (int i = 0; i < kImagesToDraw; ++i) {
|
|
|
|
fImages[i].reset();
|
|
|
|
}
|
|
|
|
fIndices.reset(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(int loops, SkCanvas* canvas) override {
|
2020-11-20 18:49:37 +00:00
|
|
|
auto dContext = GrAsDirectContext(canvas->recordingContext());
|
|
|
|
|
2016-09-08 19:35:32 +00:00
|
|
|
for (int i = 0; i < loops; ++i) {
|
|
|
|
for (int frame = 0; frame < kSimulatedFrames; ++frame) {
|
|
|
|
for (int j = 0; j < kImagesToDraw; ++j) {
|
|
|
|
int idx;
|
|
|
|
if (fShuffle) {
|
|
|
|
idx = fIndices[frame * kImagesToDraw + j];
|
|
|
|
} else {
|
|
|
|
idx = j;
|
|
|
|
}
|
|
|
|
draw_image(canvas, fImages[idx].get());
|
|
|
|
}
|
|
|
|
// Simulate a frame boundary by flushing. This should notify GrResourceCache.
|
2020-11-20 18:49:37 +00:00
|
|
|
if (dContext) {
|
|
|
|
dContext->flush();
|
|
|
|
}
|
2016-09-08 19:35:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static constexpr int kImagesToDraw = 100;
|
|
|
|
static constexpr int kSimulatedFrames = 5;
|
|
|
|
|
|
|
|
int fBudgetSize;
|
|
|
|
bool fShuffle;
|
|
|
|
SkString fName;
|
|
|
|
sk_sp<SkImage> fImages[kImagesToDraw];
|
2016-11-02 21:07:33 +00:00
|
|
|
std::unique_ptr<int[]> fIndices;
|
2016-09-08 19:35:32 +00:00
|
|
|
size_t fOldBytes;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Benchmark;
|
2016-09-08 19:35:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DEF_BENCH( return new ImageCacheBudgetBench(105, false); )
|
|
|
|
|
|
|
|
DEF_BENCH( return new ImageCacheBudgetBench(90, false); )
|
|
|
|
|
|
|
|
DEF_BENCH( return new ImageCacheBudgetBench(80, false); )
|
|
|
|
|
|
|
|
DEF_BENCH( return new ImageCacheBudgetBench(50, false); )
|
|
|
|
|
|
|
|
DEF_BENCH( return new ImageCacheBudgetBench(105, true); )
|
|
|
|
|
|
|
|
DEF_BENCH( return new ImageCacheBudgetBench(90, true); )
|
|
|
|
|
|
|
|
DEF_BENCH( return new ImageCacheBudgetBench(80, true); )
|
|
|
|
|
|
|
|
DEF_BENCH( return new ImageCacheBudgetBench(50, true); )
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Similar to above but changes between being over and under budget by varying the number of images
|
|
|
|
* rendered. This is not directly comparable to the non-dynamic benchmarks.
|
|
|
|
*/
|
|
|
|
class ImageCacheBudgetDynamicBench : public Benchmark {
|
|
|
|
public:
|
|
|
|
enum class Mode {
|
|
|
|
// Increase from min to max images drawn gradually over simulated frames and then back.
|
|
|
|
kPingPong,
|
|
|
|
// Alternate between under and over budget every other simulated frame.
|
|
|
|
kFlipFlop
|
|
|
|
};
|
|
|
|
|
|
|
|
ImageCacheBudgetDynamicBench(Mode mode) : fMode(mode) {}
|
|
|
|
|
|
|
|
bool isSuitableFor(Backend backend) override { return kGPU_Backend == backend; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const char* onGetName() override {
|
|
|
|
switch (fMode) {
|
|
|
|
case Mode::kPingPong:
|
|
|
|
return "image_cache_budget_dynamic_ping_pong";
|
|
|
|
case Mode::kFlipFlop:
|
|
|
|
return "image_cache_budget_dynamic_flip_flop";
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void onPerCanvasPreDraw(SkCanvas* canvas) override {
|
2020-07-06 17:45:34 +00:00
|
|
|
auto context = canvas->recordingContext()->asDirectContext();
|
2016-09-08 19:35:32 +00:00
|
|
|
SkASSERT(context);
|
|
|
|
context->getResourceCacheLimits(&fOldCount, &fOldBytes);
|
|
|
|
make_images(fImages, kMaxImagesToDraw);
|
|
|
|
set_cache_budget(canvas, kImagesInBudget);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onPerCanvasPostDraw(SkCanvas* canvas) override {
|
2020-07-06 17:45:34 +00:00
|
|
|
auto context = canvas->recordingContext()->asDirectContext();
|
2016-09-08 19:35:32 +00:00
|
|
|
SkASSERT(context);
|
|
|
|
context->setResourceCacheLimits(fOldCount, fOldBytes);
|
|
|
|
for (int i = 0; i < kMaxImagesToDraw; ++i) {
|
|
|
|
fImages[i].reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(int loops, SkCanvas* canvas) override {
|
2020-11-20 18:49:37 +00:00
|
|
|
auto dContext = GrAsDirectContext(canvas->recordingContext());
|
|
|
|
|
2016-09-08 19:35:32 +00:00
|
|
|
int delta = 0;
|
|
|
|
switch (fMode) {
|
|
|
|
case Mode::kPingPong:
|
|
|
|
delta = 1;
|
|
|
|
break;
|
|
|
|
case Mode::kFlipFlop:
|
|
|
|
delta = kMaxImagesToDraw - kMinImagesToDraw;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < loops; ++i) {
|
|
|
|
int imgsToDraw = kMinImagesToDraw;
|
|
|
|
for (int frame = 0; frame < kSimulatedFrames; ++frame) {
|
|
|
|
for (int j = 0; j < imgsToDraw; ++j) {
|
|
|
|
draw_image(canvas, fImages[j].get());
|
|
|
|
}
|
|
|
|
imgsToDraw += delta;
|
|
|
|
if (imgsToDraw > kMaxImagesToDraw || imgsToDraw < kMinImagesToDraw) {
|
|
|
|
delta = -delta;
|
|
|
|
imgsToDraw += 2 * delta;
|
|
|
|
}
|
|
|
|
// Simulate a frame boundary by flushing. This should notify GrResourceCache.
|
2020-11-20 18:49:37 +00:00
|
|
|
if (dContext) {
|
|
|
|
dContext->flush();
|
|
|
|
}
|
2016-09-08 19:35:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static constexpr int kImagesInBudget = 25;
|
|
|
|
static constexpr int kMinImagesToDraw = 15;
|
|
|
|
static constexpr int kMaxImagesToDraw = 35;
|
|
|
|
static constexpr int kSimulatedFrames = 80;
|
|
|
|
|
|
|
|
Mode fMode;
|
|
|
|
sk_sp<SkImage> fImages[kMaxImagesToDraw];
|
|
|
|
size_t fOldBytes;
|
|
|
|
int fOldCount;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Benchmark;
|
2016-09-08 19:35:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DEF_BENCH( return new ImageCacheBudgetDynamicBench(ImageCacheBudgetDynamicBench::Mode::kPingPong); )
|
|
|
|
DEF_BENCH( return new ImageCacheBudgetDynamicBench(ImageCacheBudgetDynamicBench::Mode::kFlipFlop); )
|