Home | History | Annotate | Download | only in gpu
      1 
      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 
     12 #include "FenceSync.h"
     13 #include "GrTypes.h"
     14 #include "SkRefCnt.h"
     15 #include "../private/SkTemplates.h"
     16 
     17 class GrContext;
     18 struct GrContextOptions;
     19 
     20 namespace sk_gpu_test {
     21 
     22 class GpuTimer;
     23 
     24 /**
     25  * An offscreen 3D context. This class is intended for Skia's internal testing needs and not
     26  * for general use.
     27  */
     28 class TestContext : public SkNoncopyable {
     29 public:
     30     virtual ~TestContext();
     31 
     32     bool fenceSyncSupport() const { return fFenceSync != nullptr; }
     33     FenceSync* fenceSync() { SkASSERT(fFenceSync); return fFenceSync.get(); }
     34 
     35     bool gpuTimingSupport() const { return fGpuTimer != nullptr; }
     36     GpuTimer* gpuTimer() const { SkASSERT(fGpuTimer); return fGpuTimer.get(); }
     37 
     38     bool getMaxGpuFrameLag(int *maxFrameLag) const {
     39         if (!fFenceSync) {
     40             return false;
     41         }
     42         *maxFrameLag = kMaxFrameLag;
     43         return true;
     44     }
     45 
     46     void makeCurrent() const;
     47 
     48     virtual GrBackend backend() = 0;
     49     virtual GrBackendContext backendContext() = 0;
     50 
     51     virtual sk_sp<GrContext> makeGrContext(const GrContextOptions&);
     52 
     53     /** Swaps front and back buffer (if the context has such buffers) */
     54     void swapBuffers();
     55 
     56     /**
     57      * The only purpose of this function it to provide a means of scheduling
     58      * work on the GPU (since all of the subclasses create primary buffers for
     59      * testing that are small and not meant to be rendered to the screen).
     60      *
     61      * If the platform supports fence syncs (OpenGL 3.2+ or EGL_KHR_fence_sync),
     62      * this will not swap any buffers, but rather emulate triple buffer synchronization
     63      * using fences.
     64      *
     65      * Otherwise it will call the platform SwapBuffers method. This may or may
     66      * not perform some sort of synchronization, depending on whether the
     67      * drawing surface provided by the platform is double buffered.
     68      *
     69      * Implicitly performs a submit().
     70      */
     71     void waitOnSyncOrSwap();
     72 
     73     /**
     74      * This notifies the context that we are deliberately testing abandoning
     75      * the context. It is useful for debugging contexts that would otherwise
     76      * test that GPU resources are properly deleted. It also allows a debugging
     77      * context to test that further API calls are not made by Skia GPU code.
     78      */
     79     virtual void testAbandon();
     80 
     81     /** Ensures all work is submitted to the GPU for execution. */
     82     virtual void submit() = 0;
     83 
     84     /** Wait until all GPU work is finished. */
     85     virtual void finish() = 0;
     86 
     87 protected:
     88     std::unique_ptr<FenceSync> fFenceSync;
     89     std::unique_ptr<GpuTimer>  fGpuTimer;
     90 
     91     TestContext();
     92 
     93     /** This should destroy the 3D context. */
     94     virtual void teardown();
     95 
     96     virtual void onPlatformMakeCurrent() const = 0;
     97     virtual void onPlatformSwapBuffers() const = 0;
     98 
     99 private:
    100     enum {
    101         kMaxFrameLag = 3
    102     };
    103 
    104     PlatformFence fFrameFences[kMaxFrameLag - 1];
    105     int fCurrentFenceIdx;
    106 
    107     typedef SkNoncopyable INHERITED;
    108 };
    109 }  // namespace sk_gpu_test
    110 #endif
    111