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 "include/core/SkCanvas.h"
10 #include "include/core/SkSurface.h"
11 #include "include/gpu/GrBackendSurface.h"
12 #include "include/gpu/GrContext.h"
13 #include "src/core/SkMathPriv.h"
14 #include "src/gpu/GrCaps.h"
15 #include "src/gpu/GrContextPriv.h"
16 #include "src/gpu/gl/GrGLDefines.h"
17 #include "src/gpu/gl/GrGLUtil.h"
18 #include "src/image/SkImage_Base.h"
19 #include "tools/sk_app/GLWindowContext.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 GrGLint buffer;
60 GR_GL_CALL(fBackendContext.get(), GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &buffer));
61
62 GrGLFramebufferInfo fbInfo;
63 fbInfo.fFBOID = buffer;
64 fbInfo.fFormat = GR_GL_RGBA8;
65
66 GrBackendRenderTarget backendRT(fWidth,
67 fHeight,
68 fSampleCount,
69 fStencilBits,
70 fbInfo);
71
72 fSurface = SkSurface::MakeFromBackendRenderTarget(fContext.get(), backendRT,
73 kBottomLeft_GrSurfaceOrigin,
74 kRGBA_8888_SkColorType,
75 fDisplayParams.fColorSpace,
76 &fDisplayParams.fSurfaceProps);
77 }
78 }
79
80 return fSurface;
81 }
82
swapBuffers()83 void GLWindowContext::swapBuffers() {
84 this->onSwapBuffers();
85 }
86
resize(int w,int h)87 void GLWindowContext::resize(int w, int h) {
88 this->destroyContext();
89 this->initializeContext();
90 }
91
setDisplayParams(const DisplayParams & params)92 void GLWindowContext::setDisplayParams(const DisplayParams& params) {
93 fDisplayParams = params;
94 this->destroyContext();
95 this->initializeContext();
96 }
97
98 } //namespace sk_app
99