• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = fDisplayParams.fMSAASampleCount ?
28                                       GrNextPow2(fDisplayParams.fMSAASampleCount) :
29                                       0;
30 }
31 
initializeContext()32 void GLWindowContext::initializeContext() {
33     this->onInitializeContext();
34     SkASSERT(nullptr == fContext);
35 
36     fBackendContext.reset(GrGLCreateNativeInterface());
37     fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fBackendContext.get(),
38                                  fDisplayParams.fGrContextOptions);
39     if (!fContext && fDisplayParams.fMSAASampleCount) {
40         fDisplayParams.fMSAASampleCount /= 2;
41         this->initializeContext();
42         return;
43     }
44 
45     if (fContext) {
46         // We may not have real sRGB support (ANGLE, in particular), so check for
47         // that, and fall back to L32:
48         fPixelConfig = fContext->caps()->srgbSupport() && fDisplayParams.fColorSpace
49                        ? kSRGBA_8888_GrPixelConfig : kRGBA_8888_GrPixelConfig;
50     } else {
51         fPixelConfig = kUnknown_GrPixelConfig;
52     }
53 }
54 
destroyContext()55 void GLWindowContext::destroyContext() {
56     fSurface.reset(nullptr);
57 
58     if (fContext) {
59         // in case we have outstanding refs to this guy (lua?)
60         fContext->abandonContext();
61         fContext->unref();
62         fContext = nullptr;
63     }
64 
65     fBackendContext.reset(nullptr);
66 
67     this->onDestroyContext();
68 }
69 
getBackbufferSurface()70 sk_sp<SkSurface> GLWindowContext::getBackbufferSurface() {
71     if (nullptr == fSurface) {
72         if (fContext) {
73             GrGLFramebufferInfo fbInfo;
74             GrGLint buffer;
75             GR_GL_CALL(fBackendContext.get(), GetIntegerv(GR_GL_FRAMEBUFFER_BINDING,
76                                                           &buffer));
77             fbInfo.fFBOID = buffer;
78 
79             GrBackendRenderTarget backendRT(fWidth,
80                                             fHeight,
81                                             fSampleCount,
82                                             fStencilBits,
83                                             fPixelConfig,
84                                             fbInfo);
85 
86             fSurface = SkSurface::MakeFromBackendRenderTarget(fContext, backendRT,
87                                                               kBottomLeft_GrSurfaceOrigin,
88                                                               fDisplayParams.fColorSpace,
89                                                               &fSurfaceProps);
90         }
91     }
92 
93     return fSurface;
94 }
95 
swapBuffers()96 void GLWindowContext::swapBuffers() {
97     this->onSwapBuffers();
98 }
99 
resize(int w,int h)100 void GLWindowContext::resize(int  w, int h) {
101     this->destroyContext();
102     this->initializeContext();
103 }
104 
setDisplayParams(const DisplayParams & params)105 void GLWindowContext::setDisplayParams(const DisplayParams& params) {
106     this->destroyContext();
107     fDisplayParams = params;
108     this->initializeContext();
109 }
110 
111 }   //namespace sk_app
112