aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/viewer/sk_app/GLWindowContext.cpp
blob: faa78ab09c5c6b52361c80655b7a7a92348dc3d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "GrBackendSurface.h"
#include "GrContext.h"
#include "GrRenderTarget.h"
#include "GLWindowContext.h"

#include "gl/GrGLDefines.h"
#include "gl/GrGLUtil.h"

#include "SkCanvas.h"
#include "SkImage_Base.h"
#include "SkMathPriv.h"
#include "SkSurface.h"

namespace sk_app {

GLWindowContext::GLWindowContext(const DisplayParams& params)
    : WindowContext(params)
    , fBackendContext(nullptr)
    , fSurface(nullptr) {
    fDisplayParams.fMSAASampleCount = fDisplayParams.fMSAASampleCount ?
                                      GrNextPow2(fDisplayParams.fMSAASampleCount) :
                                      0;
}

void GLWindowContext::initializeContext() {
    this->onInitializeContext();
    SkASSERT(nullptr == fContext);

    fBackendContext.reset(GrGLCreateNativeInterface());
    fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fBackendContext.get(),
                                 fDisplayParams.fGrContextOptions);
    if (!fContext && fDisplayParams.fMSAASampleCount) {
        fDisplayParams.fMSAASampleCount /= 2;
        this->initializeContext();
        return;
    }

    if (fContext) {
        // We may not have real sRGB support (ANGLE, in particular), so check for
        // that, and fall back to L32:
        fPixelConfig = fContext->caps()->srgbSupport() && fDisplayParams.fColorSpace
                       ? kSRGBA_8888_GrPixelConfig : kRGBA_8888_GrPixelConfig;
    } else {
        fPixelConfig = kUnknown_GrPixelConfig;
    }
}

void GLWindowContext::destroyContext() {
    fSurface.reset(nullptr);

    if (fContext) {
        // in case we have outstanding refs to this guy (lua?)
        fContext->abandonContext();
        fContext->unref();
        fContext = nullptr;
    }

    fBackendContext.reset(nullptr);

    this->onDestroyContext();
}

sk_sp<SkSurface> GLWindowContext::getBackbufferSurface() {
    if (nullptr == fSurface) {
        if (fContext) {
            GrGLFramebufferInfo fbInfo;
            GrGLint buffer;
            GR_GL_CALL(fBackendContext.get(), GetIntegerv(GR_GL_FRAMEBUFFER_BINDING,
                                                          &buffer));
            fbInfo.fFBOID = buffer;

            GrBackendRenderTarget backendRT(fWidth,
                                            fHeight,
                                            fSampleCount,
                                            fStencilBits,
                                            fPixelConfig,
                                            fbInfo);

            fSurface = SkSurface::MakeFromBackendRenderTarget(fContext, backendRT,
                                                              kBottomLeft_GrSurfaceOrigin,
                                                              fDisplayParams.fColorSpace,
                                                              &fSurfaceProps);
        }
    }

    return fSurface;
}

void GLWindowContext::swapBuffers() {
    this->onSwapBuffers();
}

void GLWindowContext::resize(int  w, int h) {
    this->destroyContext();
    this->initializeContext();
}

void GLWindowContext::setDisplayParams(const DisplayParams& params) {
    this->destroyContext();
    fDisplayParams = params;
    this->initializeContext();
}

}   //namespace sk_app