blob: b960da4824d06eb3f3309f8907948ed40bceaaaf [file] [log] [blame]
jvanverthaf236b52016-05-20 06:01:06 -07001
2/*
3 * Copyright 2015 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 "GrContext.h"
10#include "SkSurface.h"
11#include "GLWindowContext.h"
12
13#include "gl/GrGLDefines.h"
14
15#include "gl/GrGLUtil.h"
16#include "GrRenderTarget.h"
17#include "GrContext.h"
18
19#include "SkCanvas.h"
20#include "SkImage_Base.h"
21
22namespace sk_app {
23
24GLWindowContext::GLWindowContext(void* platformData, const DisplayParams& params)
25 : WindowContext()
26 , fBackendContext(nullptr)
27 , fRenderTarget(nullptr)
28 , fSurface(nullptr) {
29}
30
31void GLWindowContext::initializeContext(void* platformData, const DisplayParams& params) {
32
33 this->onInitializeContext(platformData, params);
34
35 fDisplayParams = params;
36
37 SkAutoTUnref<const GrGLInterface> glInterface;
38 glInterface.reset(GrGLCreateNativeInterface());
39 fBackendContext.reset(GrGLInterfaceRemoveNVPR(glInterface.get()));
40
41 SkASSERT(nullptr == fContext);
42 fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fBackendContext.get());
43
44 // We may not have real sRGB support (ANGLE, in particular), so check for
45 // that, and fall back to L32:
46 //
47 // ... and, if we're using a 10-bit/channel FB0, it doesn't do sRGB conversion on write,
48 // so pretend that it's non-sRGB 8888:
49 fPixelConfig = fContext->caps()->srgbSupport() &&
brianosman6a61a872016-06-16 11:10:03 -070050 SkColorAndColorSpaceAreGammaCorrect(fDisplayParams.fColorType,
51 fDisplayParams.fColorSpace.get()) &&
jvanverthaf236b52016-05-20 06:01:06 -070052 (fColorBits != 30) ? kSkiaGamma8888_GrPixelConfig : kSkia8888_GrPixelConfig;
53}
54
55void GLWindowContext::destroyContext() {
56 fSurface.reset(nullptr);
57 fRenderTarget.reset(nullptr);
58
59 if (fContext) {
60 // in case we have outstanding refs to this guy (lua?)
61 fContext->abandonContext();
62 fContext->unref();
63 fContext = nullptr;
64 }
65
66 fBackendContext.reset(nullptr);
67
68 this->onDestroyContext();
69}
70
71sk_sp<SkSurface> GLWindowContext::getBackbufferSurface() {
72 if (nullptr == fSurface) {
73 fActualColorBits = SkTMax(fColorBits, 24);
74
75 if (fContext) {
76 GrBackendRenderTargetDesc desc;
77 desc.fWidth = this->fWidth;
78 desc.fHeight = this->fHeight;
79 desc.fConfig = fPixelConfig;
80 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
81 desc.fSampleCnt = fSampleCount;
82 desc.fStencilBits = fStencilBits;
83 GrGLint buffer;
84 GR_GL_CALL(fBackendContext, GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &buffer));
85 desc.fRenderTargetHandle = buffer;
86 fRenderTarget.reset(fContext->textureProvider()->wrapBackendRenderTarget(desc));
87
88 fSurface = this->createRenderSurface(fRenderTarget, fActualColorBits);
89 }
90 }
91
92 return fSurface;
93}
94
95void GLWindowContext::swapBuffers() {
96 this->presentRenderSurface(fSurface, fRenderTarget, fActualColorBits);
97 this->onSwapBuffers();
98}
99
100void GLWindowContext::resize(uint32_t w, uint32_t h) {
101 this->destroyContext();
102
103 this->initializeContext(nullptr, fDisplayParams);
104}
105
106void GLWindowContext::setDisplayParams(const DisplayParams& params) {
107 this->destroyContext();
108
109 this->initializeContext(nullptr, params);
110}
111
112} //namespace sk_app