blob: 6e6130bb1a09942172e8645049af937fd10cfb93 [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"
12#include "include/gpu/GrContext.h"
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"
jvanverthaf236b52016-05-20 06:01:06 -070020
21namespace sk_app {
22
bsalomond1bdd1f2016-07-26 12:02:50 -070023GLWindowContext::GLWindowContext(const DisplayParams& params)
Jim Van Verthfbdc0802017-05-02 16:15:53 -040024 : WindowContext(params)
jvanverthaf236b52016-05-20 06:01:06 -070025 , fBackendContext(nullptr)
jvanverthaf236b52016-05-20 06:01:06 -070026 , 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();
Brian Salomon384fab42017-12-07 12:33:05 -050034 fContext = GrContext::MakeGL(fBackendContext, fDisplayParams.fGrContextOptions);
Brian Salomonbdecacf2018-02-02 20:32:49 -050035 if (!fContext && fDisplayParams.fMSAASampleCount > 1) {
csmartdalton578f0642017-02-24 16:04:47 -070036 fDisplayParams.fMSAASampleCount /= 2;
37 this->initializeContext();
38 return;
39 }
jvanverthaf236b52016-05-20 06:01:06 -070040}
41
42void GLWindowContext::destroyContext() {
43 fSurface.reset(nullptr);
jvanverthaf236b52016-05-20 06:01:06 -070044
45 if (fContext) {
46 // in case we have outstanding refs to this guy (lua?)
47 fContext->abandonContext();
Greg Daniel02611d92017-07-25 10:05:01 -040048 fContext.reset();
jvanverthaf236b52016-05-20 06:01:06 -070049 }
Greg Danielbcf612b2017-05-01 13:50:58 +000050
jvanverthaf236b52016-05-20 06:01:06 -070051 fBackendContext.reset(nullptr);
52
53 this->onDestroyContext();
54}
55
56sk_sp<SkSurface> GLWindowContext::getBackbufferSurface() {
57 if (nullptr == fSurface) {
jvanverthaf236b52016-05-20 06:01:06 -070058 if (fContext) {
jvanverthaf236b52016-05-20 06:01:06 -070059 GrGLint buffer;
Brian Osman294fe292018-07-09 10:23:19 -040060 GR_GL_CALL(fBackendContext.get(), GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &buffer));
61
62 GrGLFramebufferInfo fbInfo;
Greg Danielbcf612b2017-05-01 13:50:58 +000063 fbInfo.fFBOID = buffer;
Brian Osman294fe292018-07-09 10:23:19 -040064 fbInfo.fFormat = GR_GL_RGBA8;
jvanverthaf236b52016-05-20 06:01:06 -070065
Greg Danielbcf612b2017-05-01 13:50:58 +000066 GrBackendRenderTarget backendRT(fWidth,
67 fHeight,
68 fSampleCount,
69 fStencilBits,
Greg Danielbcf612b2017-05-01 13:50:58 +000070 fbInfo);
71
Greg Daniel02611d92017-07-25 10:05:01 -040072 fSurface = SkSurface::MakeFromBackendRenderTarget(fContext.get(), backendRT,
Greg Danielbcf612b2017-05-01 13:50:58 +000073 kBottomLeft_GrSurfaceOrigin,
Greg Danielfaa095e2017-12-19 13:15:02 -050074 kRGBA_8888_SkColorType,
Brian Osmanf750fbc2017-02-08 10:47:28 -050075 fDisplayParams.fColorSpace,
Ben Wagner37c54032018-04-13 14:30:23 -040076 &fDisplayParams.fSurfaceProps);
jvanverthaf236b52016-05-20 06:01:06 -070077 }
78 }
79
80 return fSurface;
81}
82
83void GLWindowContext::swapBuffers() {
jvanverthaf236b52016-05-20 06:01:06 -070084 this->onSwapBuffers();
85}
86
Jim Van Verthd063e8b2019-05-16 10:31:56 -040087void GLWindowContext::resize(int w, int h) {
jvanverthaf236b52016-05-20 06:01:06 -070088 this->destroyContext();
bsalomond1bdd1f2016-07-26 12:02:50 -070089 this->initializeContext();
jvanverthaf236b52016-05-20 06:01:06 -070090}
91
92void GLWindowContext::setDisplayParams(const DisplayParams& params) {
bsalomond1bdd1f2016-07-26 12:02:50 -070093 fDisplayParams = params;
Jim Van Verthaf545d72019-07-11 16:23:49 -040094 this->destroyContext();
bsalomond1bdd1f2016-07-26 12:02:50 -070095 this->initializeContext();
jvanverthaf236b52016-05-20 06:01:06 -070096}
97
98} //namespace sk_app