• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2/*
3 * Copyright 2012 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#include "gl/GLTestContext.h"
10#import <OpenGLES/EAGL.h>
11#include <dlfcn.h>
12
13#define EAGLCTX ((EAGLContext*)(fEAGLContext))
14
15namespace {
16
17class IOSGLTestContext : public sk_gpu_test::GLTestContext {
18public:
19    IOSGLTestContext(IOSGLTestContext* shareContext);
20    ~IOSGLTestContext() override;
21
22private:
23    void destroyGLContext();
24
25    void onPlatformMakeCurrent() const override;
26    void onPlatformSwapBuffers() const override;
27    GrGLFuncPtr onPlatformGetProcAddress(const char*) const override;
28
29    EAGLContext* fEAGLContext;
30    void* fGLLibrary;
31};
32
33IOSGLTestContext::IOSGLTestContext(IOSGLTestContext* shareContext)
34    : fEAGLContext(NULL)
35    , fGLLibrary(RTLD_DEFAULT) {
36
37    if (shareContext) {
38        EAGLContext* iosShareContext = shareContext->fEAGLContext;
39        fEAGLContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2
40                                            sharegroup: [iosShareContext sharegroup]];
41    } else {
42        fEAGLContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
43    }
44    [EAGLContext setCurrentContext:fEAGLContext];
45
46    sk_sp<const GrGLInterface> gl(GrGLCreateNativeInterface());
47    if (NULL == gl.get()) {
48        SkDebugf("Failed to create gl interface");
49        this->destroyGLContext();
50        return;
51    }
52    if (!gl->validate()) {
53        SkDebugf("Failed to validate gl interface");
54        this->destroyGLContext();
55        return;
56    }
57
58    fGLLibrary = dlopen(
59        "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib",
60        RTLD_LAZY);
61
62    this->init(gl.release());
63}
64
65IOSGLTestContext::~IOSGLTestContext() {
66    this->teardown();
67    this->destroyGLContext();
68}
69
70void IOSGLTestContext::destroyGLContext() {
71    if (fEAGLContext) {
72        if ([EAGLContext currentContext] == fEAGLContext) {
73            [EAGLContext setCurrentContext:nil];
74        }
75        fEAGLContext = nil;
76    }
77    if (RTLD_DEFAULT != fGLLibrary) {
78        dlclose(fGLLibrary);
79    }
80}
81
82
83void IOSGLTestContext::onPlatformMakeCurrent() const {
84    if (![EAGLContext setCurrentContext:fEAGLContext]) {
85        SkDebugf("Could not set the context.\n");
86    }
87}
88
89void IOSGLTestContext::onPlatformSwapBuffers() const { }
90
91GrGLFuncPtr IOSGLTestContext::onPlatformGetProcAddress(const char* procName) const {
92    return reinterpret_cast<GrGLFuncPtr>(dlsym(fGLLibrary, procName));
93}
94
95}  // anonymous namespace
96
97namespace sk_gpu_test {
98GLTestContext *CreatePlatformGLTestContext(GrGLStandard forcedGpuAPI,
99                                           GLTestContext *shareContext) {
100    if (kGL_GrGLStandard == forcedGpuAPI) {
101        return NULL;
102    }
103    IOSGLTestContext* iosShareContext = reinterpret_cast<IOSGLTestContext*>(shareContext);
104    IOSGLTestContext *ctx = new IOSGLTestContext(iosShareContext);
105    if (!ctx->isValid()) {
106        delete ctx;
107        return NULL;
108    }
109    return ctx;
110}
111}
112