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