jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 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 | |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 9 | #include "GrBackendSurface.h" |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 10 | #include "GrContext.h" |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 11 | #include "GLWindowContext.h" |
| 12 | |
| 13 | #include "gl/GrGLDefines.h" |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 14 | #include "gl/GrGLUtil.h" |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 15 | |
| 16 | #include "SkCanvas.h" |
| 17 | #include "SkImage_Base.h" |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 18 | #include "SkMathPriv.h" |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 19 | #include "SkSurface.h" |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 20 | |
| 21 | namespace sk_app { |
| 22 | |
bsalomon | d1bdd1f | 2016-07-26 12:02:50 -0700 | [diff] [blame] | 23 | GLWindowContext::GLWindowContext(const DisplayParams& params) |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame] | 24 | : WindowContext(params) |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 25 | , fBackendContext(nullptr) |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 26 | , fSurface(nullptr) { |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 27 | fDisplayParams.fMSAASampleCount = fDisplayParams.fMSAASampleCount ? |
| 28 | GrNextPow2(fDisplayParams.fMSAASampleCount) : |
| 29 | 0; |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 30 | } |
| 31 | |
bsalomon | d1bdd1f | 2016-07-26 12:02:50 -0700 | [diff] [blame] | 32 | void GLWindowContext::initializeContext() { |
| 33 | this->onInitializeContext(); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 34 | SkASSERT(nullptr == fContext); |
csmartdalton | 008b9d8 | 2017-02-22 12:00:42 -0700 | [diff] [blame] | 35 | |
csmartdalton | 008b9d8 | 2017-02-22 12:00:42 -0700 | [diff] [blame] | 36 | fBackendContext.reset(GrGLCreateNativeInterface()); |
| 37 | fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fBackendContext.get(), |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 38 | fDisplayParams.fGrContextOptions); |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 39 | if (!fContext && fDisplayParams.fMSAASampleCount) { |
| 40 | fDisplayParams.fMSAASampleCount /= 2; |
| 41 | this->initializeContext(); |
| 42 | return; |
| 43 | } |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 44 | |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 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 | } |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void GLWindowContext::destroyContext() { |
| 56 | fSurface.reset(nullptr); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 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 | } |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 64 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 65 | fBackendContext.reset(nullptr); |
| 66 | |
| 67 | this->onDestroyContext(); |
| 68 | } |
| 69 | |
| 70 | sk_sp<SkSurface> GLWindowContext::getBackbufferSurface() { |
| 71 | if (nullptr == fSurface) { |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 72 | if (fContext) { |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 73 | GrGLFramebufferInfo fbInfo; |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 74 | GrGLint buffer; |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 75 | GR_GL_CALL(fBackendContext.get(), GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, |
| 76 | &buffer)); |
| 77 | fbInfo.fFBOID = buffer; |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 78 | |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 79 | GrBackendRenderTarget backendRT(fWidth, |
| 80 | fHeight, |
| 81 | fSampleCount, |
| 82 | fStencilBits, |
| 83 | fPixelConfig, |
| 84 | fbInfo); |
| 85 | |
| 86 | fSurface = SkSurface::MakeFromBackendRenderTarget(fContext, backendRT, |
| 87 | kBottomLeft_GrSurfaceOrigin, |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 88 | fDisplayParams.fColorSpace, |
| 89 | &fSurfaceProps); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | return fSurface; |
| 94 | } |
| 95 | |
| 96 | void GLWindowContext::swapBuffers() { |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 97 | this->onSwapBuffers(); |
| 98 | } |
| 99 | |
bsalomon | ccde4ab | 2016-07-27 08:50:12 -0700 | [diff] [blame] | 100 | void GLWindowContext::resize(int w, int h) { |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 101 | this->destroyContext(); |
bsalomon | d1bdd1f | 2016-07-26 12:02:50 -0700 | [diff] [blame] | 102 | this->initializeContext(); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void GLWindowContext::setDisplayParams(const DisplayParams& params) { |
| 106 | this->destroyContext(); |
bsalomon | d1bdd1f | 2016-07-26 12:02:50 -0700 | [diff] [blame] | 107 | fDisplayParams = params; |
| 108 | this->initializeContext(); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | } //namespace sk_app |