blob: ba658af3cdd84ad83e44ce95e3e2a791c6c5d35a [file] [log] [blame]
jvanverthaf236b52016-05-20 06:01:06 -07001
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 Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkCanvas.h"
10#include "include/core/SkSurface.h"
11#include "include/gpu/GrBackendSurface.h"
Robert Phillipsed653392020-07-10 13:55:21 -040012#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/core/SkMathPriv.h"
14#include "src/gpu/GrCaps.h"
Adlai Hollera0693042020-10-14 11:23:11 -040015#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#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"
jvanverthaf236b52016-05-20 06:01:06 -070020
21namespace sk_app {
22
bsalomond1bdd1f2016-07-26 12:02:50 -070023GLWindowContext::GLWindowContext(const DisplayParams& params)
Robert Phillipsed653392020-07-10 13:55:21 -040024 : WindowContext(params)
25 , fBackendContext(nullptr)
26 , fSurface(nullptr) {
Brian Salomonbdecacf2018-02-02 20:32:49 -050027 fDisplayParams.fMSAASampleCount = GrNextPow2(fDisplayParams.fMSAASampleCount);
jvanverthaf236b52016-05-20 06:01:06 -070028}
29
bsalomond1bdd1f2016-07-26 12:02:50 -070030void GLWindowContext::initializeContext() {
Greg Daniel02611d92017-07-25 10:05:01 -040031 SkASSERT(!fContext);
csmartdalton008b9d82017-02-22 12:00:42 -070032
Brian Salomon194db172017-08-17 14:37:06 -040033 fBackendContext = this->onInitializeContext();
Robert Phillipsed653392020-07-10 13:55:21 -040034
Robert Phillipsf4f80112020-07-13 16:13:31 -040035 fContext = GrDirectContext::MakeGL(fBackendContext, fDisplayParams.fGrContextOptions);
Brian Salomonbdecacf2018-02-02 20:32:49 -050036 if (!fContext && fDisplayParams.fMSAASampleCount > 1) {
csmartdalton578f0642017-02-24 16:04:47 -070037 fDisplayParams.fMSAASampleCount /= 2;
38 this->initializeContext();
39 return;
40 }
jvanverthaf236b52016-05-20 06:01:06 -070041}
42
43void GLWindowContext::destroyContext() {
44 fSurface.reset(nullptr);
jvanverthaf236b52016-05-20 06:01:06 -070045
46 if (fContext) {
Leon Scroggins IIIa4c80982020-07-28 10:20:58 -040047 // in case we have outstanding refs to this (lua?)
jvanverthaf236b52016-05-20 06:01:06 -070048 fContext->abandonContext();
Greg Daniel02611d92017-07-25 10:05:01 -040049 fContext.reset();
jvanverthaf236b52016-05-20 06:01:06 -070050 }
Greg Danielbcf612b2017-05-01 13:50:58 +000051
jvanverthaf236b52016-05-20 06:01:06 -070052 fBackendContext.reset(nullptr);
53
54 this->onDestroyContext();
55}
56
57sk_sp<SkSurface> GLWindowContext::getBackbufferSurface() {
58 if (nullptr == fSurface) {
jvanverthaf236b52016-05-20 06:01:06 -070059 if (fContext) {
jvanverthaf236b52016-05-20 06:01:06 -070060 GrGLint buffer;
Brian Osman294fe292018-07-09 10:23:19 -040061 GR_GL_CALL(fBackendContext.get(), GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &buffer));
62
63 GrGLFramebufferInfo fbInfo;
Greg Danielbcf612b2017-05-01 13:50:58 +000064 fbInfo.fFBOID = buffer;
Brian Osman294fe292018-07-09 10:23:19 -040065 fbInfo.fFormat = GR_GL_RGBA8;
jvanverthaf236b52016-05-20 06:01:06 -070066
Greg Danielbcf612b2017-05-01 13:50:58 +000067 GrBackendRenderTarget backendRT(fWidth,
68 fHeight,
69 fSampleCount,
70 fStencilBits,
Greg Danielbcf612b2017-05-01 13:50:58 +000071 fbInfo);
72
Greg Daniel02611d92017-07-25 10:05:01 -040073 fSurface = SkSurface::MakeFromBackendRenderTarget(fContext.get(), backendRT,
Greg Danielbcf612b2017-05-01 13:50:58 +000074 kBottomLeft_GrSurfaceOrigin,
Greg Danielfaa095e2017-12-19 13:15:02 -050075 kRGBA_8888_SkColorType,
Brian Osmanf750fbc2017-02-08 10:47:28 -050076 fDisplayParams.fColorSpace,
Ben Wagner37c54032018-04-13 14:30:23 -040077 &fDisplayParams.fSurfaceProps);
jvanverthaf236b52016-05-20 06:01:06 -070078 }
79 }
80
81 return fSurface;
82}
83
84void GLWindowContext::swapBuffers() {
jvanverthaf236b52016-05-20 06:01:06 -070085 this->onSwapBuffers();
86}
87
Jim Van Verthd063e8b2019-05-16 10:31:56 -040088void GLWindowContext::resize(int w, int h) {
jvanverthaf236b52016-05-20 06:01:06 -070089 this->destroyContext();
bsalomond1bdd1f2016-07-26 12:02:50 -070090 this->initializeContext();
jvanverthaf236b52016-05-20 06:01:06 -070091}
92
93void GLWindowContext::setDisplayParams(const DisplayParams& params) {
bsalomond1bdd1f2016-07-26 12:02:50 -070094 fDisplayParams = params;
Jim Van Verthaf545d72019-07-11 16:23:49 -040095 this->destroyContext();
bsalomond1bdd1f2016-07-26 12:02:50 -070096 this->initializeContext();
jvanverthaf236b52016-05-20 06:01:06 -070097}
98
99} //namespace sk_app