aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/viewer/sk_app/GLWindowContext.cpp
blob: 31ba01c292a3229ebdbd72c60cf38188680f095f (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

/*
 * 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 "GrContext.h"
#include "SkSurface.h"
#include "GLWindowContext.h"

#include "gl/GrGLDefines.h"

#include "gl/GrGLUtil.h"
#include "GrRenderTarget.h"
#include "GrContext.h"

#include "SkCanvas.h"
#include "SkImage_Base.h"

namespace sk_app {

GLWindowContext::GLWindowContext(void* platformData, const DisplayParams& params) 
    : WindowContext()
    , fBackendContext(nullptr)
    , fRenderTarget(nullptr)
    , fSurface(nullptr) {
}

void GLWindowContext::initializeContext(void* platformData, const DisplayParams& params) {

    this->onInitializeContext(platformData, params);

    fDisplayParams = params;

    SkAutoTUnref<const GrGLInterface> glInterface;
    glInterface.reset(GrGLCreateNativeInterface());
    fBackendContext.reset(GrGLInterfaceRemoveNVPR(glInterface.get()));

    SkASSERT(nullptr == fContext);
    fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fBackendContext.get());

    // We may not have real sRGB support (ANGLE, in particular), so check for
    // that, and fall back to L32:
    //
    // ... and, if we're using a 10-bit/channel FB0, it doesn't do sRGB conversion on write,
    // so pretend that it's non-sRGB 8888:
    fPixelConfig = fContext->caps()->srgbSupport() &&
                   SkColorAndColorSpaceAreGammaCorrect(fDisplayParams.fColorType,
                                                       fDisplayParams.fColorSpace.get()) &&
                   (fColorBits != 30) ? kSkiaGamma8888_GrPixelConfig : kSkia8888_GrPixelConfig;
}

void GLWindowContext::destroyContext() {
    fSurface.reset(nullptr);
    fRenderTarget.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) {
        fActualColorBits = SkTMax(fColorBits, 24);

        if (fContext) {
            GrBackendRenderTargetDesc desc;
            desc.fWidth = this->fWidth;
            desc.fHeight = this->fHeight;
            desc.fConfig = fPixelConfig;
            desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
            desc.fSampleCnt = fSampleCount;
            desc.fStencilBits = fStencilBits;
            GrGLint buffer;
            GR_GL_CALL(fBackendContext, GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &buffer));
            desc.fRenderTargetHandle = buffer;

            fSurface = this->createRenderSurface(desc, fActualColorBits);
        }
    }

    return fSurface;
}

void GLWindowContext::swapBuffers() {
    this->presentRenderSurface(fSurface, fRenderTarget, fActualColorBits);
    this->onSwapBuffers();
}

void GLWindowContext::resize(uint32_t w, uint32_t h) {
    this->destroyContext();

    this->initializeContext(nullptr, fDisplayParams);
}

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

    this->initializeContext(nullptr, params);
}

}   //namespace sk_app