blob: 6953d9435d01bd8c8c57ebdbaebefa986b8c840d [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.comed673312009-02-27 16:24:51 +00008#include "Test.h"
9
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000010#include "SkTLazy.h"
11
12#if SK_SUPPORT_GPU
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000013#include "GrContext.h"
tomhudson@google.com6bf38b52012-02-14 15:11:59 +000014#include "gl/SkNativeGLContext.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000015#else
16class GrContext;
17#endif
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000018
robertphillips@google.coma22e2112012-08-16 14:58:06 +000019SK_DEFINE_INST_COUNT(skiatest::Reporter)
20
reed@android.comed673312009-02-27 16:24:51 +000021using namespace skiatest;
22
23Reporter::Reporter() {
24 this->resetReporting();
25}
26
27void Reporter::resetReporting() {
28 fCurrTest = NULL;
29 fTestCount = 0;
reed@android.com4516f472009-06-29 16:25:36 +000030 sk_bzero(fResultCount, sizeof(fResultCount));
reed@android.comed673312009-02-27 16:24:51 +000031}
32
33void Reporter::startTest(Test* test) {
34 SkASSERT(NULL == fCurrTest);
35 fCurrTest = test;
36 this->onStart(test);
37 fTestCount += 1;
reed@android.comeeb3b7f2009-04-09 04:06:54 +000038 fCurrTestSuccess = true; // we're optimistic
reed@android.comed673312009-02-27 16:24:51 +000039}
40
41void Reporter::report(const char desc[], Result result) {
42 if (NULL == desc) {
43 desc = "<no description>";
44 }
45 this->onReport(desc, result);
46 fResultCount[result] += 1;
reed@android.comeeb3b7f2009-04-09 04:06:54 +000047 if (kFailed == result) {
48 fCurrTestSuccess = false;
49 }
reed@android.comed673312009-02-27 16:24:51 +000050}
51
52void Reporter::endTest(Test* test) {
53 SkASSERT(test == fCurrTest);
54 this->onEnd(test);
55 fCurrTest = NULL;
56}
57
58///////////////////////////////////////////////////////////////////////////////
59
60Test::Test() : fReporter(NULL) {}
61
62Test::~Test() {
reed@google.com82065d62011-02-07 15:30:46 +000063 SkSafeUnref(fReporter);
reed@android.comed673312009-02-27 16:24:51 +000064}
65
66void Test::setReporter(Reporter* r) {
67 SkRefCnt_SafeAssign(fReporter, r);
68}
69
70const char* Test::getName() {
71 if (fName.size() == 0) {
72 this->onGetName(&fName);
73 }
74 return fName.c_str();
75}
76
reed@android.comeeb3b7f2009-04-09 04:06:54 +000077bool Test::run() {
reed@android.comed673312009-02-27 16:24:51 +000078 fReporter->startTest(this);
79 this->onRun(fReporter);
80 fReporter->endTest(this);
reed@android.comeeb3b7f2009-04-09 04:06:54 +000081 return fReporter->getCurrSuccess();
reed@android.comed673312009-02-27 16:24:51 +000082}
83
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000084///////////////////////////////////////////////////////////////////////////////
85
djsollen@google.com0945bde2012-11-29 15:28:45 +000086#if SK_SUPPORT_GPU
87 static SkAutoTUnref<SkNativeGLContext> gGLContext;
88 static SkAutoTUnref<GrContext> gGrContext;
89#endif
90
91void GpuTest::DestroyContext() {
92#if SK_SUPPORT_GPU
bsalomon@google.com83a853a2012-11-29 17:27:37 +000093 // preserve this order, we want gGrContext destroyed before gGLContext
djsollen@google.com0945bde2012-11-29 15:28:45 +000094 gGrContext.reset(NULL);
bsalomon@google.com83a853a2012-11-29 17:27:37 +000095 gGLContext.reset(NULL);
djsollen@google.com0945bde2012-11-29 15:28:45 +000096#endif
97}
98
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000099
100GrContext* GpuTest::GetContext() {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000101#if SK_SUPPORT_GPU
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000102 if (NULL == gGrContext.get()) {
djsollen@google.com0945bde2012-11-29 15:28:45 +0000103 gGLContext.reset(new SkNativeGLContext());
bsalomon@google.come2953132011-10-13 13:33:08 +0000104 if (gGLContext.get()->init(800, 600)) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000105 GrBackendContext ctx = reinterpret_cast<GrBackendContext>(gGLContext.get()->gl());
106 gGrContext.reset(GrContext::Create(kOpenGL_GrBackend, ctx));
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000107 }
108 }
bsalomon@google.com57f5d982011-10-24 21:17:53 +0000109 if (gGLContext.get()) {
110 gGLContext.get()->makeCurrent();
111 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000112 return gGrContext.get();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000113#else
114 return NULL;
115#endif
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000116}
117