skia2/bench/CreateBackendTextureBench.cpp
John Stiles f4bda743ff Rename SkTArray::reserve to reserve_back.
The semantics of `vector::reserve` and `SkTArray::reserve` were not the
same. SkTArray::reserve takes a delta over the current array size,
whereas vector takes a total array size. This could lead to subtle
errors with over- or under-reservation, hurting performance.

This CL renames `SkTArray::reserve` to `SkTArray::reserve_back` to give
the SkTArray behavior a distinct (hopefully easily understandable) name,
leaving its functionality as-is.

Change-Id: Icbd3114bb317fd5f307f393c02ae6fb6f83764e9
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/326956
Commit-Queue: John Stiles <johnstiles@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
2020-10-14 21:42:11 +00:00

58 lines
1.8 KiB
C++

/*
* Copyright 2020 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "bench/Benchmark.h"
#include "include/core/SkCanvas.h"
#include "include/gpu/GrDirectContext.h"
class CreateBackendTextureBench : public Benchmark {
private:
SkString fName;
SkTArray<GrBackendTexture> fBackendTextures;
GrMipmapped fMipmapped;
public:
CreateBackendTextureBench(GrMipmapped mipMapped) : fMipmapped(mipMapped) {
fName.printf("create_backend_texture%s", mipMapped == GrMipmapped::kYes ? "_mipped" : "");
}
private:
bool isSuitableFor(Backend backend) override { return kGPU_Backend == backend; }
const char* onGetName() override { return fName.c_str(); }
void onDraw(int loops, SkCanvas* canvas) override {
auto context = canvas->recordingContext()->asDirectContext();
fBackendTextures.reserve_back(loops);
static const int kSize = 16;
for (int i = 0; i < loops; ++i) {
fBackendTextures.push_back(context->createBackendTexture(
kSize, kSize, kRGBA_8888_SkColorType, SkColors::kRed, fMipmapped,
GrRenderable::kNo, GrProtected::kNo));
}
}
void onPerCanvasPostDraw(SkCanvas* canvas) override {
auto context = canvas->recordingContext()->asDirectContext();
context->flush();
context->submit(true);
for (int i = 0; i < fBackendTextures.count(); ++i) {
if (fBackendTextures[i].isValid()) {
context->deleteBackendTexture(fBackendTextures[i]);
}
}
fBackendTextures.reset();
}
};
DEF_BENCH(return new CreateBackendTextureBench(GrMipmapped::kNo);)
DEF_BENCH(return new CreateBackendTextureBench(GrMipmapped::kYes);)