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/GrDirectContext.h"
13 #include "src/core/SkMathPriv.h"
14 #include "src/gpu/GrCaps.h"
15 #include "src/gpu/GrDirectContextPriv.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 
35     fContext = GrDirectContext::MakeGL(fBackendContext, fDisplayParams.fGrContextOptions);
36     if (!fContext && fDisplayParams.fMSAASampleCount > 1) {
37         fDisplayParams.fMSAASampleCount /= 2;
38         this->initializeContext();
39         return;
40     }
41 }
42 
destroyContext()43 void GLWindowContext::destroyContext() {
44     fSurface.reset(nullptr);
45 
46     if (fContext) {
47         // in case we have outstanding refs to this (lua?)
48         fContext->abandonContext();
49         fContext.reset();
50     }
51 
52     fBackendContext.reset(nullptr);
53 
54     this->onDestroyContext();
55 }
56 
getBackbufferSurface()57 sk_sp<SkSurface> GLWindowContext::getBackbufferSurface() {
58     if (nullptr == fSurface) {
59         if (fContext) {
60             GrGLint buffer;
61             GR_GL_CALL(fBackendContext.get(), GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &buffer));
62 
63             GrGLFramebufferInfo fbInfo;
64             fbInfo.fFBOID = buffer;
65             fbInfo.fFormat = 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                                                               &fDisplayParams.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     fDisplayParams = params;
95     this->destroyContext();
96     this->initializeContext();
97 }
98 
99 }   //namespace sk_app
100