blob: bdfa12a8ecaf8f6effa8b376a1a948516b965ca8 [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) {
csmartdalton578f0642017-02-24 16:04:47 -070027 fDisplayParams.fMSAASampleCount = fDisplayParams.fMSAASampleCount ?
28 GrNextPow2(fDisplayParams.fMSAASampleCount) :
29 0;
jvanverthaf236b52016-05-20 06:01:06 -070030}
31
bsalomond1bdd1f2016-07-26 12:02:50 -070032void GLWindowContext::initializeContext() {
Greg Daniel02611d92017-07-25 10:05:01 -040033 SkASSERT(!fContext);
csmartdalton008b9d82017-02-22 12:00:42 -070034
Brian Salomon194db172017-08-17 14:37:06 -040035 fBackendContext = this->onInitializeContext();
Greg Daniel02611d92017-07-25 10:05:01 -040036 fContext = GrContext::MakeGL(fBackendContext.get(), fDisplayParams.fGrContextOptions);
csmartdalton578f0642017-02-24 16:04:47 -070037 if (!fContext && fDisplayParams.fMSAASampleCount) {
38 fDisplayParams.fMSAASampleCount /= 2;
39 this->initializeContext();
40 return;
41 }
jvanverthaf236b52016-05-20 06:01:06 -070042
csmartdalton578f0642017-02-24 16:04:47 -070043 if (fContext) {
44 // We may not have real sRGB support (ANGLE, in particular), so check for
45 // that, and fall back to L32:
46 fPixelConfig = fContext->caps()->srgbSupport() && fDisplayParams.fColorSpace
47 ? kSRGBA_8888_GrPixelConfig : kRGBA_8888_GrPixelConfig;
48 } else {
49 fPixelConfig = kUnknown_GrPixelConfig;
50 }
jvanverthaf236b52016-05-20 06:01:06 -070051}
52
53void GLWindowContext::destroyContext() {
54 fSurface.reset(nullptr);
jvanverthaf236b52016-05-20 06:01:06 -070055
56 if (fContext) {
57 // in case we have outstanding refs to this guy (lua?)
58 fContext->abandonContext();
Greg Daniel02611d92017-07-25 10:05:01 -040059 fContext.reset();
jvanverthaf236b52016-05-20 06:01:06 -070060 }
Greg Danielbcf612b2017-05-01 13:50:58 +000061
jvanverthaf236b52016-05-20 06:01:06 -070062 fBackendContext.reset(nullptr);
63
64 this->onDestroyContext();
65}
66
67sk_sp<SkSurface> GLWindowContext::getBackbufferSurface() {
68 if (nullptr == fSurface) {
jvanverthaf236b52016-05-20 06:01:06 -070069 if (fContext) {
Greg Danielbcf612b2017-05-01 13:50:58 +000070 GrGLFramebufferInfo fbInfo;
jvanverthaf236b52016-05-20 06:01:06 -070071 GrGLint buffer;
Greg Danielbcf612b2017-05-01 13:50:58 +000072 GR_GL_CALL(fBackendContext.get(), GetIntegerv(GR_GL_FRAMEBUFFER_BINDING,
73 &buffer));
74 fbInfo.fFBOID = buffer;
jvanverthaf236b52016-05-20 06:01:06 -070075
Greg Danielbcf612b2017-05-01 13:50:58 +000076 GrBackendRenderTarget backendRT(fWidth,
77 fHeight,
78 fSampleCount,
79 fStencilBits,
80 fPixelConfig,
81 fbInfo);
82
Greg Daniel02611d92017-07-25 10:05:01 -040083 fSurface = SkSurface::MakeFromBackendRenderTarget(fContext.get(), backendRT,
Greg Danielbcf612b2017-05-01 13:50:58 +000084 kBottomLeft_GrSurfaceOrigin,
Brian Osmanf750fbc2017-02-08 10:47:28 -050085 fDisplayParams.fColorSpace,
86 &fSurfaceProps);
jvanverthaf236b52016-05-20 06:01:06 -070087 }
88 }
89
90 return fSurface;
91}
92
93void GLWindowContext::swapBuffers() {
jvanverthaf236b52016-05-20 06:01:06 -070094 this->onSwapBuffers();
95}
96
bsalomonccde4ab2016-07-27 08:50:12 -070097void GLWindowContext::resize(int w, int h) {
jvanverthaf236b52016-05-20 06:01:06 -070098 this->destroyContext();
bsalomond1bdd1f2016-07-26 12:02:50 -070099 this->initializeContext();
jvanverthaf236b52016-05-20 06:01:06 -0700100}
101
102void GLWindowContext::setDisplayParams(const DisplayParams& params) {
103 this->destroyContext();
bsalomond1bdd1f2016-07-26 12:02:50 -0700104 fDisplayParams = params;
105 this->initializeContext();
jvanverthaf236b52016-05-20 06:01:06 -0700106}
107
108} //namespace sk_app