1
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 "GrBackendSurface.h"
10 #include "GrContext.h"
11 #include "GLWindowContext.h"
12
13 #include "gl/GrGLDefines.h"
14 #include "gl/GrGLUtil.h"
15
16 #include "SkCanvas.h"
17 #include "SkImage_Base.h"
18 #include "SkMathPriv.h"
19 #include "SkSurface.h"
20
21 namespace sk_app {
22
GLWindowContext(const DisplayParams & params)23 GLWindowContext::GLWindowContext(const DisplayParams& params)
24 : WindowContext(params)
25 , fBackendContext(nullptr)
26 , fSurface(nullptr) {
27 fDisplayParams.fMSAASampleCount = GrNextPow2(fDisplayParams.fMSAASampleCount);
28 }
29
initializeContext()30 void GLWindowContext::initializeContext() {
31 SkASSERT(!fContext);
32
33 fBackendContext = this->onInitializeContext();
34 fContext = GrContext::MakeGL(fBackendContext, fDisplayParams.fGrContextOptions);
35 if (!fContext && fDisplayParams.fMSAASampleCount > 1) {
36 fDisplayParams.fMSAASampleCount /= 2;
37 this->initializeContext();
38 return;
39 }
40 }
41
destroyContext()42 void GLWindowContext::destroyContext() {
43 fSurface.reset(nullptr);
44
45 if (fContext) {
46 // in case we have outstanding refs to this guy (lua?)
47 fContext->abandonContext();
48 fContext.reset();
49 }
50
51 fBackendContext.reset(nullptr);
52
53 this->onDestroyContext();
54 }
55
getBackbufferSurface()56 sk_sp<SkSurface> GLWindowContext::getBackbufferSurface() {
57 if (nullptr == fSurface) {
58 if (fContext) {
59 GrGLFramebufferInfo fbInfo;
60 GrGLint buffer;
61 GR_GL_CALL(fBackendContext.get(), GetIntegerv(GR_GL_FRAMEBUFFER_BINDING,
62 &buffer));
63 fbInfo.fFBOID = buffer;
64 fbInfo.fFormat = fContext->caps()->srgbSupport() && fDisplayParams.fColorSpace
65 ? GR_GL_SRGB8_ALPHA8 : GR_GL_RGBA8;
66
67 GrBackendRenderTarget backendRT(fWidth,
68 fHeight,
69 fSampleCount,
70 fStencilBits,
71 fbInfo);
72
73 fSurface = SkSurface::MakeFromBackendRenderTarget(fContext.get(), backendRT,
74 kBottomLeft_GrSurfaceOrigin,
75 kRGBA_8888_SkColorType,
76 fDisplayParams.fColorSpace,
77 &fSurfaceProps);
78 }
79 }
80
81 return fSurface;
82 }
83
swapBuffers()84 void GLWindowContext::swapBuffers() {
85 this->onSwapBuffers();
86 }
87
resize(int w,int h)88 void GLWindowContext::resize(int w, int h) {
89 this->destroyContext();
90 this->initializeContext();
91 }
92
setDisplayParams(const DisplayParams & params)93 void GLWindowContext::setDisplayParams(const DisplayParams& params) {
94 this->destroyContext();
95 fDisplayParams = params;
96 this->initializeContext();
97 }
98
99 } //namespace sk_app
100