blob: d01cb02af2ad833e256b61f5e87ba4d348bf4f7d [file] [log] [blame]
bsalomon18a2f9d2016-05-11 10:09:18 -07001
2/*
3 * Copyright 2016 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 */
8
9#ifndef TestContext_DEFINED
10#define TestContext_DEFINED
11
csmartdalton421a3c12016-10-04 11:08:45 -070012#include "FenceSync.h"
bsalomon18a2f9d2016-05-11 10:09:18 -070013#include "GrTypes.h"
bsalomon18a2f9d2016-05-11 10:09:18 -070014#include "../private/SkTemplates.h"
15
16namespace sk_gpu_test {
17/**
18 * An offscreen 3D context. This class is intended for Skia's internal testing needs and not
19 * for general use.
20 */
21class TestContext : public SkNoncopyable {
22public:
23 virtual ~TestContext();
24
25 virtual bool isValid() const = 0;
26
27 bool fenceSyncSupport() const { return fFenceSync != nullptr; }
csmartdalton421a3c12016-10-04 11:08:45 -070028 FenceSync* fenceSync() { SkASSERT(fFenceSync); return fFenceSync; }
bsalomon18a2f9d2016-05-11 10:09:18 -070029
30 bool getMaxGpuFrameLag(int *maxFrameLag) const {
31 if (!fFenceSync) {
32 return false;
33 }
34 *maxFrameLag = kMaxFrameLag;
35 return true;
36 }
37
38 void makeCurrent() const;
39
40 virtual GrBackend backend() = 0;
41 virtual GrBackendContext backendContext() = 0;
42
43 /** Swaps front and back buffer (if the context has such buffers) */
44 void swapBuffers();
45
46 /**
47 * The only purpose of this function it to provide a means of scheduling
48 * work on the GPU (since all of the subclasses create primary buffers for
49 * testing that are small and not meant to be rendered to the screen).
50 *
51 * If the platform supports fence syncs (OpenGL 3.2+ or EGL_KHR_fence_sync),
52 * this will not swap any buffers, but rather emulate triple buffer synchronization
53 * using fences.
54 *
55 * Otherwise it will call the platform SwapBuffers method. This may or may
56 * not perform some sort of synchronization, depending on whether the
57 * drawing surface provided by the platform is double buffered.
bsalomonc8699322016-05-11 11:55:36 -070058 *
59 * Implicitly performs a submit().
bsalomon18a2f9d2016-05-11 10:09:18 -070060 */
61 void waitOnSyncOrSwap();
62
63 /**
64 * This notifies the context that we are deliberately testing abandoning
65 * the context. It is useful for debugging contexts that would otherwise
66 * test that GPU resources are properly deleted. It also allows a debugging
67 * context to test that further API calls are not made by Skia GPU code.
68 */
69 virtual void testAbandon();
70
bsalomonc8699322016-05-11 11:55:36 -070071 /** Ensures all work is submitted to the GPU for execution. */
72 virtual void submit() = 0;
73
74 /** Wait until all GPU work is finished. */
75 virtual void finish() = 0;
76
bsalomon18a2f9d2016-05-11 10:09:18 -070077protected:
csmartdalton421a3c12016-10-04 11:08:45 -070078 FenceSync* fFenceSync;
bsalomon18a2f9d2016-05-11 10:09:18 -070079
80 TestContext();
81
82 /** This should destroy the 3D context. */
83 virtual void teardown();
84
85 virtual void onPlatformMakeCurrent() const = 0;
86 virtual void onPlatformSwapBuffers() const = 0;
87
88private:
89 enum {
90 kMaxFrameLag = 3
91 };
92
csmartdalton421a3c12016-10-04 11:08:45 -070093 PlatformFence fFrameFences[kMaxFrameLag - 1];
bsalomon18a2f9d2016-05-11 10:09:18 -070094 int fCurrentFenceIdx;
95
96 typedef SkNoncopyable INHERITED;
97};
98} // namespace sk_gpu_test
99#endif