blob: 9d042cf19eded4d0dc1c70fdffee345261e782ef [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
Greg Danielbcf612b2017-05-01 13:50:58 +00009#include "GrBackendSurface.h"
jvanverthaf236b52016-05-20 06:01:06 -070010#include "GrContext.h"
jvanverthaf236b52016-05-20 06:01:06 -070011#include "GLWindowContext.h"
12
13#include "gl/GrGLDefines.h"
jvanverthaf236b52016-05-20 06:01:06 -070014#include "gl/GrGLUtil.h"
jvanverthaf236b52016-05-20 06:01:06 -070015
16#include "SkCanvas.h"
17#include "SkImage_Base.h"
csmartdalton578f0642017-02-24 16:04:47 -070018#include "SkMathPriv.h"
Greg Danielbcf612b2017-05-01 13:50:58 +000019#include "SkSurface.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) {
Greg Danielbcf612b2017-05-01 13:50:58 +000059 GrGLFramebufferInfo fbInfo;
jvanverthaf236b52016-05-20 06:01:06 -070060 GrGLint buffer;
Greg Danielbcf612b2017-05-01 13:50:58 +000061 GR_GL_CALL(fBackendContext.get(), GetIntegerv(GR_GL_FRAMEBUFFER_BINDING,
62 &buffer));
63 fbInfo.fFBOID = buffer;
Greg Danielfaa095e2017-12-19 13:15:02 -050064 fbInfo.fFormat = fContext->caps()->srgbSupport() && fDisplayParams.fColorSpace
65 ? GR_GL_SRGB8_ALPHA8 : 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,
77 &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
bsalomonccde4ab2016-07-27 08:50:12 -070088void 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) {
94 this->destroyContext();
bsalomond1bdd1f2016-07-26 12:02:50 -070095 fDisplayParams = params;
96 this->initializeContext();
jvanverthaf236b52016-05-20 06:01:06 -070097}
98
99} //namespace sk_app