blob: e0c41b3adda994a809ec86566ac9286c7cf1e9c6 [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"
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"
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
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 Salomonbdecacf2018-02-02 20:32:49 -050038 if (!fContext && fDisplayParams.fMSAASampleCount > 1) {
csmartdalton578f0642017-02-24 16:04:47 -070039 fDisplayParams.fMSAASampleCount /= 2;
40 this->initializeContext();
41 return;
42 }
jvanverthaf236b52016-05-20 06:01:06 -070043}
44
45void GLWindowContext::destroyContext() {
46 fSurface.reset(nullptr);
jvanverthaf236b52016-05-20 06:01:06 -070047
48 if (fContext) {
49 // in case we have outstanding refs to this guy (lua?)
50 fContext->abandonContext();
Greg Daniel02611d92017-07-25 10:05:01 -040051 fContext.reset();
jvanverthaf236b52016-05-20 06:01:06 -070052 }
Greg Danielbcf612b2017-05-01 13:50:58 +000053
jvanverthaf236b52016-05-20 06:01:06 -070054 fBackendContext.reset(nullptr);
55
56 this->onDestroyContext();
57}
58
59sk_sp<SkSurface> GLWindowContext::getBackbufferSurface() {
60 if (nullptr == fSurface) {
jvanverthaf236b52016-05-20 06:01:06 -070061 if (fContext) {
jvanverthaf236b52016-05-20 06:01:06 -070062 GrGLint buffer;
Brian Osman294fe292018-07-09 10:23:19 -040063 GR_GL_CALL(fBackendContext.get(), GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &buffer));
64
65 GrGLFramebufferInfo fbInfo;
Greg Danielbcf612b2017-05-01 13:50:58 +000066 fbInfo.fFBOID = buffer;
Brian Osman294fe292018-07-09 10:23:19 -040067 fbInfo.fFormat = GR_GL_RGBA8;
jvanverthaf236b52016-05-20 06:01:06 -070068
Greg Danielbcf612b2017-05-01 13:50:58 +000069 GrBackendRenderTarget backendRT(fWidth,
70 fHeight,
71 fSampleCount,
72 fStencilBits,
Greg Danielbcf612b2017-05-01 13:50:58 +000073 fbInfo);
74
Greg Daniel02611d92017-07-25 10:05:01 -040075 fSurface = SkSurface::MakeFromBackendRenderTarget(fContext.get(), backendRT,
Greg Danielbcf612b2017-05-01 13:50:58 +000076 kBottomLeft_GrSurfaceOrigin,
Greg Danielfaa095e2017-12-19 13:15:02 -050077 kRGBA_8888_SkColorType,
Brian Osmanf750fbc2017-02-08 10:47:28 -050078 fDisplayParams.fColorSpace,
Ben Wagner37c54032018-04-13 14:30:23 -040079 &fDisplayParams.fSurfaceProps);
jvanverthaf236b52016-05-20 06:01:06 -070080 }
81 }
82
83 return fSurface;
84}
85
86void GLWindowContext::swapBuffers() {
jvanverthaf236b52016-05-20 06:01:06 -070087 this->onSwapBuffers();
88}
89
Jim Van Verthd063e8b2019-05-16 10:31:56 -040090void GLWindowContext::resize(int w, int h) {
jvanverthaf236b52016-05-20 06:01:06 -070091 this->destroyContext();
bsalomond1bdd1f2016-07-26 12:02:50 -070092 this->initializeContext();
jvanverthaf236b52016-05-20 06:01:06 -070093}
94
95void GLWindowContext::setDisplayParams(const DisplayParams& params) {
bsalomond1bdd1f2016-07-26 12:02:50 -070096 fDisplayParams = params;
Jim Van Verthaf545d72019-07-11 16:23:49 -040097 this->destroyContext();
bsalomond1bdd1f2016-07-26 12:02:50 -070098 this->initializeContext();
jvanverthaf236b52016-05-20 06:01:06 -070099}
100
101} //namespace sk_app