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() { |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 33 | SkASSERT(!fContext); |
csmartdalton | 008b9d8 | 2017-02-22 12:00:42 -0700 | [diff] [blame] | 34 | |
Brian Salomon | 194db17 | 2017-08-17 14:37:06 -0400 | [diff] [blame] | 35 | fBackendContext = this->onInitializeContext(); |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 36 | fContext = GrContext::MakeGL(fBackendContext.get(), fDisplayParams.fGrContextOptions); |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 37 | if (!fContext && fDisplayParams.fMSAASampleCount) { |
| 38 | fDisplayParams.fMSAASampleCount /= 2; |
| 39 | this->initializeContext(); |
| 40 | return; |
| 41 | } |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 42 | |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 43 | if (fContext) { |
| 44 | // We may not have real sRGB support (ANGLE, in particular), so check for |
| 45 | // that, and fall back to L32: |
| 46 | fPixelConfig = fContext->caps()->srgbSupport() && fDisplayParams.fColorSpace |
| 47 | ? kSRGBA_8888_GrPixelConfig : kRGBA_8888_GrPixelConfig; |
| 48 | } else { |
| 49 | fPixelConfig = kUnknown_GrPixelConfig; |
| 50 | } |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void GLWindowContext::destroyContext() { |
| 54 | fSurface.reset(nullptr); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 55 | |
| 56 | if (fContext) { |
| 57 | // in case we have outstanding refs to this guy (lua?) |
| 58 | fContext->abandonContext(); |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 59 | fContext.reset(); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 60 | } |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 61 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 62 | fBackendContext.reset(nullptr); |
| 63 | |
| 64 | this->onDestroyContext(); |
| 65 | } |
| 66 | |
| 67 | sk_sp<SkSurface> GLWindowContext::getBackbufferSurface() { |
| 68 | if (nullptr == fSurface) { |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 69 | if (fContext) { |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 70 | GrGLFramebufferInfo fbInfo; |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 71 | GrGLint buffer; |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 72 | GR_GL_CALL(fBackendContext.get(), GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, |
| 73 | &buffer)); |
| 74 | fbInfo.fFBOID = buffer; |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 75 | |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 76 | GrBackendRenderTarget backendRT(fWidth, |
| 77 | fHeight, |
| 78 | fSampleCount, |
| 79 | fStencilBits, |
| 80 | fPixelConfig, |
| 81 | fbInfo); |
| 82 | |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 83 | fSurface = SkSurface::MakeFromBackendRenderTarget(fContext.get(), backendRT, |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 84 | kBottomLeft_GrSurfaceOrigin, |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 85 | fDisplayParams.fColorSpace, |
| 86 | &fSurfaceProps); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | return fSurface; |
| 91 | } |
| 92 | |
| 93 | void GLWindowContext::swapBuffers() { |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 94 | this->onSwapBuffers(); |
| 95 | } |
| 96 | |
bsalomon | ccde4ab | 2016-07-27 08:50:12 -0700 | [diff] [blame] | 97 | void GLWindowContext::resize(int w, int h) { |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 98 | this->destroyContext(); |
bsalomon | d1bdd1f | 2016-07-26 12:02:50 -0700 | [diff] [blame] | 99 | this->initializeContext(); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void GLWindowContext::setDisplayParams(const DisplayParams& params) { |
| 103 | this->destroyContext(); |
bsalomon | d1bdd1f | 2016-07-26 12:02:50 -0700 | [diff] [blame] | 104 | fDisplayParams = params; |
| 105 | this->initializeContext(); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | } //namespace sk_app |