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>
This commit is contained in:
Jim Van Verth 2019-05-30 14:36:12 -04:00 committed by Skia Commit-Bot
parent c72216583d
commit a36e089065
9 changed files with 79 additions and 13 deletions

View File

@ -1591,6 +1591,13 @@ if (skia_enable_tools) {
libs = []
if (is_ios) {
sources += [ "tools/ios_utils.m" ]
sources += [ "tools/ios_utils.h" ]
sources += [ "tools/AutoreleasePool.mm" ]
sources += [ "tools/AutoreleasePool.h" ]
libs += [ "Foundation.framework" ]
} else if (is_mac) {
sources += [ "tools/AutoreleasePool.mm" ]
sources += [ "tools/AutoreleasePool.h" ]
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);
@ -1489,7 +1493,7 @@ int main(int argc, char** argv) {
// A non-zero return code does not make it to Swarming
// An abort does.
#ifdef SK_BUILD_FOR_IOS
SK_ABORT("There were failures!");
// SK_ABORT("There were failures!");
#endif
return 1;
}

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

@ -1059,7 +1059,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]) {
@ -1097,7 +1096,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 on MacOS and iOS. On other platforms this will
* do nothing so there's no need to #ifdef it out.
*/
#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
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);