blob: 2f0d5dc74b550ccbc48f7995806e0fc6455d8fe3 [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() {
33 this->onInitializeContext();
jvanverthaf236b52016-05-20 06:01:06 -070034 SkASSERT(nullptr == fContext);
csmartdalton008b9d82017-02-22 12:00:42 -070035
csmartdalton008b9d82017-02-22 12:00:42 -070036 fBackendContext.reset(GrGLCreateNativeInterface());
37 fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fBackendContext.get(),
csmartdalton61cd31a2017-02-27 17:00:53 -070038 fDisplayParams.fGrContextOptions);
csmartdalton578f0642017-02-24 16:04:47 -070039 if (!fContext && fDisplayParams.fMSAASampleCount) {
40 fDisplayParams.fMSAASampleCount /= 2;
41 this->initializeContext();
42 return;
43 }
jvanverthaf236b52016-05-20 06:01:06 -070044
csmartdalton578f0642017-02-24 16:04:47 -070045 if (fContext) {
46 // We may not have real sRGB support (ANGLE, in particular), so check for
47 // that, and fall back to L32:
48 fPixelConfig = fContext->caps()->srgbSupport() && fDisplayParams.fColorSpace
49 ? kSRGBA_8888_GrPixelConfig : kRGBA_8888_GrPixelConfig;
50 } else {
51 fPixelConfig = kUnknown_GrPixelConfig;
52 }
jvanverthaf236b52016-05-20 06:01:06 -070053}
54
55void GLWindowContext::destroyContext() {
56 fSurface.reset(nullptr);
jvanverthaf236b52016-05-20 06:01:06 -070057
58 if (fContext) {
59 // in case we have outstanding refs to this guy (lua?)
60 fContext->abandonContext();
61 fContext->unref();
62 fContext = nullptr;
63 }
Greg Danielbcf612b2017-05-01 13:50:58 +000064
jvanverthaf236b52016-05-20 06:01:06 -070065 fBackendContext.reset(nullptr);
66
67 this->onDestroyContext();
68}
69
70sk_sp<SkSurface> GLWindowContext::getBackbufferSurface() {
71 if (nullptr == fSurface) {
jvanverthaf236b52016-05-20 06:01:06 -070072 if (fContext) {
Greg Danielbcf612b2017-05-01 13:50:58 +000073 GrGLFramebufferInfo fbInfo;
jvanverthaf236b52016-05-20 06:01:06 -070074 GrGLint buffer;
Greg Danielbcf612b2017-05-01 13:50:58 +000075 GR_GL_CALL(fBackendContext.get(), GetIntegerv(GR_GL_FRAMEBUFFER_BINDING,
76 &buffer));
77 fbInfo.fFBOID = buffer;
jvanverthaf236b52016-05-20 06:01:06 -070078
Greg Danielbcf612b2017-05-01 13:50:58 +000079 GrBackendRenderTarget backendRT(fWidth,
80 fHeight,
81 fSampleCount,
82 fStencilBits,
83 fPixelConfig,
84 fbInfo);
85
86 fSurface = SkSurface::MakeFromBackendRenderTarget(fContext, backendRT,
87 kBottomLeft_GrSurfaceOrigin,
Brian Osmanf750fbc2017-02-08 10:47:28 -050088 fDisplayParams.fColorSpace,
89 &fSurfaceProps);
jvanverthaf236b52016-05-20 06:01:06 -070090 }
91 }
92
93 return fSurface;
94}
95
96void GLWindowContext::swapBuffers() {
jvanverthaf236b52016-05-20 06:01:06 -070097 this->onSwapBuffers();
98}
99
bsalomonccde4ab2016-07-27 08:50:12 -0700100void GLWindowContext::resize(int w, int h) {
jvanverthaf236b52016-05-20 06:01:06 -0700101 this->destroyContext();
bsalomond1bdd1f2016-07-26 12:02:50 -0700102 this->initializeContext();
jvanverthaf236b52016-05-20 06:01:06 -0700103}
104
105void GLWindowContext::setDisplayParams(const DisplayParams& params) {
106 this->destroyContext();
bsalomond1bdd1f2016-07-26 12:02:50 -0700107 fDisplayParams = params;
108 this->initializeContext();
jvanverthaf236b52016-05-20 06:01:06 -0700109}
110
111} //namespace sk_app