Reland "Added AutoreleasePool for managing pool memory in testing apps."

This is a reland of a36e089065

This is only active when Metal is enabled.

Original change's description:
> Added AutoreleasePool for managing pool memory in testing apps.
>
> This is only active on MacOS and iOS -- on other platforms it
> will do nothing as they have no need for autorelease pools.
>
> Bug: skia:8243
> Change-Id: Ib74968dab6e3455a72e726429832101d0d410076
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/217126
> Commit-Queue: Jim Van Verth <jvanverth@google.com>
> Reviewed-by: Brian Osman <brianosman@google.com>

Bug: skia:8243
Change-Id: I743a3dcc93b46387a6a330e855c2e8810b482544
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/217379
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Jim Van Verth <jvanverth@google.com>
This commit is contained in:
Jim Van Verth 2019-05-31 10:49:12 -04:00 committed by Skia Commit-Bot
parent 8a64fa66e7
commit 8a9a371182
10 changed files with 82 additions and 12 deletions

View File

@ -1564,6 +1564,7 @@ if (skia_enable_tools) {
test_lib("tool_utils") {
sources = [
"tools/AndroidSkDebugToStdOut.cpp",
"tools/AutoreleasePool.h",
"tools/CrashHandler.cpp",
"tools/DDLPromiseImageHelper.cpp",
"tools/DDLTileHelper.cpp",
@ -1591,7 +1592,16 @@ if (skia_enable_tools) {
libs = []
if (is_ios) {
sources += [ "tools/ios_utils.m" ]
sources += [ "tools/ios_utils.h" ]
if (skia_use_metal) {
sources += [ "tools/AutoreleasePool.mm" ]
}
libs += [ "Foundation.framework" ]
} else if (is_mac) {
if (skia_use_metal) {
sources += [ "tools/AutoreleasePool.mm" ]
libs += [ "Foundation.framework" ]
}
} else if (is_win) {
libs += [ "DbgHelp.lib" ]
}

View File

@ -37,6 +37,7 @@
#include "src/core/SkTraceEvent.h"
#include "src/utils/SkJSONWriter.h"
#include "src/utils/SkOSPath.h"
#include "tools/AutoreleasePool.h"
#include "tools/CrashHandler.h"
#include "tools/ProcStats.h"
#include "tools/Stats.h"
@ -1235,6 +1236,7 @@ int main(int argc, char** argv) {
int runs = 0;
BenchmarkStream benchStream;
log.beginObject("results");
AutoreleasePool pool;
while (Benchmark* b = benchStream.next()) {
std::unique_ptr<Benchmark> bench(b);
if (CommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
@ -1421,6 +1423,7 @@ int main(int argc, char** argv) {
SkDebugf("%s\n", bench->getUniqueName());
}
cleanup_run(target);
pool.drain();
}
if (!configs.empty()) {
log.endBench();

View File

@ -28,6 +28,7 @@
#include "src/core/SkTaskGroup.h"
#include "src/utils/SkOSPath.h"
#include "tests/Test.h"
#include "tools/AutoreleasePool.h"
#include "tools/HashAndEncode.h"
#include "tools/ProcStats.h"
#include "tools/Resources.h"
@ -1021,6 +1022,7 @@ static Sink* create_via(const SkString& tag, Sink* wrapped) {
static bool gather_sinks(const GrContextOptions& grCtxOptions, bool defaultConfigs) {
SkCommandLineConfigArray configs;
ParseConfigs(FLAGS_config, &configs);
AutoreleasePool pool;
for (int i = 0; i < configs.count(); i++) {
const SkCommandLineConfig& config = *configs[i];
Sink* sink = create_sink(grCtxOptions, &config);
@ -1094,6 +1096,7 @@ struct Task {
const TaggedSink& sink;
static void Run(const Task& task) {
AutoreleasePool pool;
SkString name = task.src->name();
SkString log;
@ -1364,6 +1367,7 @@ static void run_test(skiatest::Test test, const GrContextOptions& grCtxOptions)
} reporter;
if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
AutoreleasePool pool;
GrContextOptions options = grCtxOptions;
test.modifyGrContextOptions(&options);

View File

@ -457,6 +457,7 @@ DM_SRCS_ALL = struct(
"tests/*.cpp",
"tests/*.h",
"tools/ios_utils.h",
"tools/AutoreleasePool.h",
"tools/BinaryAsset.h",
"tools/BigPathBench.inc",
"tools/CrashHandler.cpp",

View File

@ -30,10 +30,6 @@ namespace SkSL {
class Compiler;
}
// Helper macros for autorelease pools
#define SK_BEGIN_AUTORELEASE_BLOCK @autoreleasepool {
#define SK_END_AUTORELEASE_BLOCK }
class GrMtlGpu : public GrGpu {
public:
static sk_sp<GrGpu> Make(GrContext* context, const GrContextOptions& options,

View File

@ -1051,7 +1051,6 @@ bool GrMtlGpu::onReadPixels(GrSurface* surface, int left, int top, int width, in
int bpp = GrColorTypeBytesPerPixel(dstColorType);
size_t transBufferRowBytes = bpp * width;
SK_BEGIN_AUTORELEASE_BLOCK
bool doResolve = get_surface_sample_cnt(surface) > 1;
id<MTLTexture> mtlTexture = GrGetMTLTextureFromSurface(surface, doResolve);
if (!mtlTexture || [mtlTexture isFramebufferOnly]) {
@ -1089,7 +1088,6 @@ bool GrMtlGpu::onReadPixels(GrSurface* surface, int left, int top, int width, in
const void* mappedMemory = transferBuffer.contents;
SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, transBufferRowBytes, height);
SK_END_AUTORELEASE_BLOCK
return true;

36
tools/AutoreleasePool.h Normal file
View File

@ -0,0 +1,36 @@
/*
* Copyright 2019 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkAutoreleasePool_DEFINED
#define SkAutoreleasePool_DEFINED
/*
* Helper class for managing an autorelease pool for Metal. On other platforms this will
* do nothing so there's no need to #ifdef it out.
*/
#ifdef SK_METAL
class AutoreleasePool {
public:
AutoreleasePool();
~AutoreleasePool();
void drain();
private:
void* fPool;
};
#else
class AutoreleasePool {
public:
AutoreleasePool() {}
~AutoreleasePool() = default;
void drain() {}
};
#endif
#endif

25
tools/AutoreleasePool.mm Normal file
View File

@ -0,0 +1,25 @@
/*
* Copyright 2019 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/core/SkTypes.h"
#include "tools/AutoreleasePool.h"
#import <Foundation/NSAutoreleasePool.h>
AutoreleasePool::AutoreleasePool() {
fPool = (void*)[[NSAutoreleasePool alloc] init];
}
AutoreleasePool::~AutoreleasePool() {
[(NSAutoreleasePool*)fPool release];
fPool = nullptr;
}
void AutoreleasePool::drain() {
[(NSAutoreleasePool*)fPool drain];
fPool = (void*)[[NSAutoreleasePool alloc] init];
}

View File

@ -17,6 +17,7 @@
#include "src/gpu/GrContextPriv.h"
#include "src/gpu/GrGpu.h"
#include "src/utils/SkOSPath.h"
#include "tools/AutoreleasePool.h"
#include "tools/CrashHandler.h"
#include "tools/HashAndEncode.h"
#include "tools/ToolUtils.h"
@ -494,6 +495,7 @@ int main(int argc, char** argv) {
: SkColorSpace::MakeRGB(tf,gamut);
const SkImageInfo unsized_info = SkImageInfo::Make(0,0, ct,at,cs);
AutoreleasePool pool;
for (auto source : sources) {
const auto start = std::chrono::steady_clock::now();
fprintf(stdout, "%50s", source.name.c_str());
@ -582,6 +584,7 @@ int main(int argc, char** argv) {
fprintf(stdout, "\t%s\t%7dms\n",
md5.c_str(),
(int)std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count());
pool.drain();
}
if (!FLAGS_writeShaders.isEmpty()) {

View File

@ -16,10 +16,6 @@
#import <Metal/Metal.h>
// Helper macros for autorelease pools
#define SK_BEGIN_AUTORELEASE_BLOCK @autoreleasepool {
#define SK_END_AUTORELEASE_BLOCK }
namespace {
#if GR_METAL_SDK_VERSION >= 200
/**
@ -105,10 +101,8 @@ public:
device = sharedContextImpl->device();
queue = sharedContextImpl->queue();
} else {
SK_BEGIN_AUTORELEASE_BLOCK
device = MTLCreateSystemDefaultDevice();
queue = [device newCommandQueue];
SK_END_AUTORELEASE_BLOCK
}
return new MtlTestContextImpl(device, queue);