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