blob: 5bcc759dabc47e5440289dbb8c0b9077ac5c9e34 [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
jvanverthaf236b52016-05-20 06:01:06 -07009#include "GLWindowContext.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040010#include "GrBackendSurface.h"
11#include "GrCaps.h"
12#include "GrContext.h"
13#include "GrContextPriv.h"
jvanverthaf236b52016-05-20 06:01:06 -070014#include "SkCanvas.h"
15#include "SkImage_Base.h"
csmartdalton578f0642017-02-24 16:04:47 -070016#include "SkMathPriv.h"
Greg Danielbcf612b2017-05-01 13:50:58 +000017#include "SkSurface.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040018#include "gl/GrGLDefines.h"
19#include "gl/GrGLUtil.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
bsalomonccde4ab2016-07-27 08:50:12 -070087void 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) {
93 this->destroyContext();
bsalomond1bdd1f2016-07-26 12:02:50 -070094 fDisplayParams = params;
95 this->initializeContext();
jvanverthaf236b52016-05-20 06:01:06 -070096}
97
98} //namespace sk_app