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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkSurface.h" |
| 11 | #include "include/gpu/GrBackendSurface.h" |
Robert Phillips | ed65339 | 2020-07-10 13:55:21 -0400 | [diff] [blame^] | 12 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/core/SkMathPriv.h" |
| 14 | #include "src/gpu/GrCaps.h" |
| 15 | #include "src/gpu/GrContextPriv.h" |
| 16 | #include "src/gpu/gl/GrGLDefines.h" |
| 17 | #include "src/gpu/gl/GrGLUtil.h" |
| 18 | #include "src/image/SkImage_Base.h" |
| 19 | #include "tools/sk_app/GLWindowContext.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) |
Robert Phillips | ed65339 | 2020-07-10 13:55:21 -0400 | [diff] [blame^] | 24 | : WindowContext(params) |
| 25 | , fBackendContext(nullptr) |
| 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(); |
Robert Phillips | ed65339 | 2020-07-10 13:55:21 -0400 | [diff] [blame^] | 34 | |
| 35 | // CONTEXT TODO: MakeGL should return an sk_sp<GrDirectContext> |
| 36 | auto tmp = GrContext::MakeGL(fBackendContext, fDisplayParams.fGrContextOptions); |
| 37 | fContext = sk_ref_sp<GrDirectContext>(tmp->asDirectContext()); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 38 | if (!fContext && fDisplayParams.fMSAASampleCount > 1) { |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 39 | fDisplayParams.fMSAASampleCount /= 2; |
| 40 | this->initializeContext(); |
| 41 | return; |
| 42 | } |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void GLWindowContext::destroyContext() { |
| 46 | fSurface.reset(nullptr); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 47 | |
| 48 | if (fContext) { |
| 49 | // in case we have outstanding refs to this guy (lua?) |
| 50 | fContext->abandonContext(); |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 51 | fContext.reset(); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 52 | } |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 53 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 54 | fBackendContext.reset(nullptr); |
| 55 | |
| 56 | this->onDestroyContext(); |
| 57 | } |
| 58 | |
| 59 | sk_sp<SkSurface> GLWindowContext::getBackbufferSurface() { |
| 60 | if (nullptr == fSurface) { |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 61 | if (fContext) { |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 62 | GrGLint buffer; |
Brian Osman | 294fe29 | 2018-07-09 10:23:19 -0400 | [diff] [blame] | 63 | GR_GL_CALL(fBackendContext.get(), GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &buffer)); |
| 64 | |
| 65 | GrGLFramebufferInfo fbInfo; |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 66 | fbInfo.fFBOID = buffer; |
Brian Osman | 294fe29 | 2018-07-09 10:23:19 -0400 | [diff] [blame] | 67 | fbInfo.fFormat = GR_GL_RGBA8; |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 68 | |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 69 | GrBackendRenderTarget backendRT(fWidth, |
| 70 | fHeight, |
| 71 | fSampleCount, |
| 72 | fStencilBits, |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 73 | fbInfo); |
| 74 | |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 75 | fSurface = SkSurface::MakeFromBackendRenderTarget(fContext.get(), backendRT, |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 76 | kBottomLeft_GrSurfaceOrigin, |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 77 | kRGBA_8888_SkColorType, |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 78 | fDisplayParams.fColorSpace, |
Ben Wagner | 37c5403 | 2018-04-13 14:30:23 -0400 | [diff] [blame] | 79 | &fDisplayParams.fSurfaceProps); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | return fSurface; |
| 84 | } |
| 85 | |
| 86 | void GLWindowContext::swapBuffers() { |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 87 | this->onSwapBuffers(); |
| 88 | } |
| 89 | |
Jim Van Verth | d063e8b | 2019-05-16 10:31:56 -0400 | [diff] [blame] | 90 | void GLWindowContext::resize(int w, int h) { |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 91 | this->destroyContext(); |
bsalomon | d1bdd1f | 2016-07-26 12:02:50 -0700 | [diff] [blame] | 92 | this->initializeContext(); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void GLWindowContext::setDisplayParams(const DisplayParams& params) { |
bsalomon | d1bdd1f | 2016-07-26 12:02:50 -0700 | [diff] [blame] | 96 | fDisplayParams = params; |
Jim Van Verth | af545d7 | 2019-07-11 16:23:49 -0400 | [diff] [blame] | 97 | this->destroyContext(); |
bsalomon | d1bdd1f | 2016-07-26 12:02:50 -0700 | [diff] [blame] | 98 | this->initializeContext(); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | } //namespace sk_app |