180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2011 Google Inc.
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "Test.h"
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// This is a GPU-backend specific test
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#if SK_SUPPORT_GPU
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#if SK_ANGLE
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "gl/SkANGLEGLContext.h"
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "gl/SkNativeGLContext.h"
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#if SK_MESA
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "gl/SkMesaGLContext.h"
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic void GLInterfaceValidationTest(skiatest::Reporter* reporter) {
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    typedef const GrGLInterface* (*interfaceFactory)();
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    struct {
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru       interfaceFactory fFactory;
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru       const char* fName;
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    } interfaceFactories[] = {
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#if SK_ANGLE
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        {GrGLCreateANGLEInterface, "ANGLE"},
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        {GrGLCreateNativeInterface, "Native"},
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#if SK_MESA
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        {GrGLCreateMesaInterface, "Mesa"},
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        {GrGLCreateDebugInterface, "Debug"},
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        {GrGLCreateNullInterface, "Null"},
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    };
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // On some platforms GrGLCreateNativeInterface will fail unless an OpenGL
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // context has been created. Also, preserve the current context that may
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // be in use by outer test harness.
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkNativeGLContext::AutoContextRestore nglacr;
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkNativeGLContext nglctx;
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static const int gBOGUS_SIZE = 16;
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool nativeContextInit = nglctx.init(gBOGUS_SIZE, gBOGUS_SIZE);
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    REPORTER_ASSERT(reporter, nativeContextInit);
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (!nativeContextInit) {
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return;
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#if SK_MESA
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // We must have a current OSMesa context to initialize an OSMesa
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // GrGLInterface
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkMesaGLContext::AutoContextRestore mglacr;
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkMesaGLContext mglctx;
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool mesaContextInit = mglctx.init(gBOGUS_SIZE, gBOGUS_SIZE);
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    REPORTER_ASSERT(reporter, mesaContextInit);
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if(!mesaContextInit) {
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return;
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkAutoTUnref<const GrGLInterface> iface;
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    for (size_t i = 0; i < SK_ARRAY_COUNT(interfaceFactories); ++i) {
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        iface.reset(interfaceFactories[i].fFactory());
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        REPORTER_ASSERT(reporter, NULL != iface.get());
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (iface.get()) {
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            for (GrGLBinding binding = kFirstGrGLBinding;
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                 binding <= kLastGrGLBinding;
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                 binding = static_cast<GrGLBinding>(binding << 1)) {
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                if (iface.get()->fBindingsExported & binding) {
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    REPORTER_ASSERT(reporter, iface.get()->validate(binding));
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                }
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            }
7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "TestClassDef.h"
8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruDEFINE_TESTCLASS("GLInterfaceValidation",
8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                 GLInterfaceValidationTestClass,
8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                 GLInterfaceValidationTest)
8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
86