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 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 9 | #include "GLWindowContext.h" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 10 | #include "GrBackendSurface.h" |
| 11 | #include "GrCaps.h" |
| 12 | #include "GrContext.h" |
| 13 | #include "GrContextPriv.h" |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 14 | #include "SkCanvas.h" |
| 15 | #include "SkImage_Base.h" |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 16 | #include "SkMathPriv.h" |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 17 | #include "SkSurface.h" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 18 | #include "gl/GrGLDefines.h" |
| 19 | #include "gl/GrGLUtil.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) { |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 59 | GrGLint buffer; |
Brian Osman | 294fe29 | 2018-07-09 10:23:19 -0400 | [diff] [blame] | 60 | GR_GL_CALL(fBackendContext.get(), GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &buffer)); |
| 61 | |
| 62 | GrGLFramebufferInfo fbInfo; |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 63 | fbInfo.fFBOID = buffer; |
Brian Osman | 294fe29 | 2018-07-09 10:23:19 -0400 | [diff] [blame] | 64 | fbInfo.fFormat = GR_GL_RGBA8; |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 65 | |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 66 | GrBackendRenderTarget backendRT(fWidth, |
| 67 | fHeight, |
| 68 | fSampleCount, |
| 69 | fStencilBits, |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 70 | fbInfo); |
| 71 | |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 72 | fSurface = SkSurface::MakeFromBackendRenderTarget(fContext.get(), backendRT, |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 73 | kBottomLeft_GrSurfaceOrigin, |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 74 | kRGBA_8888_SkColorType, |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 75 | fDisplayParams.fColorSpace, |
Ben Wagner | 37c5403 | 2018-04-13 14:30:23 -0400 | [diff] [blame] | 76 | &fDisplayParams.fSurfaceProps); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | return fSurface; |
| 81 | } |
| 82 | |
| 83 | void GLWindowContext::swapBuffers() { |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 84 | this->onSwapBuffers(); |
| 85 | } |
| 86 | |
bsalomon | ccde4ab | 2016-07-27 08:50:12 -0700 | [diff] [blame] | 87 | void GLWindowContext::resize(int w, int h) { |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 88 | this->destroyContext(); |
bsalomon | d1bdd1f | 2016-07-26 12:02:50 -0700 | [diff] [blame] | 89 | this->initializeContext(); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void GLWindowContext::setDisplayParams(const DisplayParams& params) { |
| 93 | this->destroyContext(); |
bsalomon | d1bdd1f | 2016-07-26 12:02:50 -0700 | [diff] [blame] | 94 | fDisplayParams = params; |
| 95 | this->initializeContext(); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | } //namespace sk_app |