1cb93a386Sopenharmony_ci
2cb93a386Sopenharmony_ci/*
3cb93a386Sopenharmony_ci * Copyright 2015 Google Inc.
4cb93a386Sopenharmony_ci *
5cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
6cb93a386Sopenharmony_ci * found in the LICENSE file.
7cb93a386Sopenharmony_ci */
8cb93a386Sopenharmony_ci
9cb93a386Sopenharmony_ci#include "include/core/SkCanvas.h"
10cb93a386Sopenharmony_ci#include "include/core/SkSurface.h"
11cb93a386Sopenharmony_ci#include "include/gpu/GrBackendSurface.h"
12cb93a386Sopenharmony_ci#include "include/gpu/GrDirectContext.h"
13cb93a386Sopenharmony_ci#include "src/core/SkMathPriv.h"
14cb93a386Sopenharmony_ci#include "src/gpu/GrCaps.h"
15cb93a386Sopenharmony_ci#include "src/gpu/GrDirectContextPriv.h"
16cb93a386Sopenharmony_ci#include "src/gpu/gl/GrGLDefines.h"
17cb93a386Sopenharmony_ci#include "src/gpu/gl/GrGLUtil.h"
18cb93a386Sopenharmony_ci#include "src/image/SkImage_Base.h"
19cb93a386Sopenharmony_ci#include "tools/sk_app/GLWindowContext.h"
20cb93a386Sopenharmony_ci
21cb93a386Sopenharmony_cinamespace sk_app {
22cb93a386Sopenharmony_ci
23cb93a386Sopenharmony_ciGLWindowContext::GLWindowContext(const DisplayParams& params)
24cb93a386Sopenharmony_ci        : WindowContext(params)
25cb93a386Sopenharmony_ci        , fBackendContext(nullptr)
26cb93a386Sopenharmony_ci        , fSurface(nullptr) {
27cb93a386Sopenharmony_ci    fDisplayParams.fMSAASampleCount = GrNextPow2(fDisplayParams.fMSAASampleCount);
28cb93a386Sopenharmony_ci}
29cb93a386Sopenharmony_ci
30cb93a386Sopenharmony_civoid GLWindowContext::initializeContext() {
31cb93a386Sopenharmony_ci    SkASSERT(!fContext);
32cb93a386Sopenharmony_ci
33cb93a386Sopenharmony_ci    fBackendContext = this->onInitializeContext();
34cb93a386Sopenharmony_ci
35cb93a386Sopenharmony_ci    fContext = GrDirectContext::MakeGL(fBackendContext, fDisplayParams.fGrContextOptions);
36cb93a386Sopenharmony_ci    if (!fContext && fDisplayParams.fMSAASampleCount > 1) {
37cb93a386Sopenharmony_ci        fDisplayParams.fMSAASampleCount /= 2;
38cb93a386Sopenharmony_ci        this->initializeContext();
39cb93a386Sopenharmony_ci        return;
40cb93a386Sopenharmony_ci    }
41cb93a386Sopenharmony_ci}
42cb93a386Sopenharmony_ci
43cb93a386Sopenharmony_civoid GLWindowContext::destroyContext() {
44cb93a386Sopenharmony_ci    fSurface.reset(nullptr);
45cb93a386Sopenharmony_ci
46cb93a386Sopenharmony_ci    if (fContext) {
47cb93a386Sopenharmony_ci        // in case we have outstanding refs to this (lua?)
48cb93a386Sopenharmony_ci        fContext->abandonContext();
49cb93a386Sopenharmony_ci        fContext.reset();
50cb93a386Sopenharmony_ci    }
51cb93a386Sopenharmony_ci
52cb93a386Sopenharmony_ci    fBackendContext.reset(nullptr);
53cb93a386Sopenharmony_ci
54cb93a386Sopenharmony_ci    this->onDestroyContext();
55cb93a386Sopenharmony_ci}
56cb93a386Sopenharmony_ci
57cb93a386Sopenharmony_cisk_sp<SkSurface> GLWindowContext::getBackbufferSurface() {
58cb93a386Sopenharmony_ci    if (nullptr == fSurface) {
59cb93a386Sopenharmony_ci        if (fContext) {
60cb93a386Sopenharmony_ci            GrGLint buffer;
61cb93a386Sopenharmony_ci            GR_GL_CALL(fBackendContext.get(), GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &buffer));
62cb93a386Sopenharmony_ci
63cb93a386Sopenharmony_ci            GrGLFramebufferInfo fbInfo;
64cb93a386Sopenharmony_ci            fbInfo.fFBOID = buffer;
65cb93a386Sopenharmony_ci            fbInfo.fFormat = GR_GL_RGBA8;
66cb93a386Sopenharmony_ci
67cb93a386Sopenharmony_ci            GrBackendRenderTarget backendRT(fWidth,
68cb93a386Sopenharmony_ci                                            fHeight,
69cb93a386Sopenharmony_ci                                            fSampleCount,
70cb93a386Sopenharmony_ci                                            fStencilBits,
71cb93a386Sopenharmony_ci                                            fbInfo);
72cb93a386Sopenharmony_ci
73cb93a386Sopenharmony_ci            fSurface = SkSurface::MakeFromBackendRenderTarget(fContext.get(), backendRT,
74cb93a386Sopenharmony_ci                                                              kBottomLeft_GrSurfaceOrigin,
75cb93a386Sopenharmony_ci                                                              kRGBA_8888_SkColorType,
76cb93a386Sopenharmony_ci                                                              fDisplayParams.fColorSpace,
77cb93a386Sopenharmony_ci                                                              &fDisplayParams.fSurfaceProps);
78cb93a386Sopenharmony_ci        }
79cb93a386Sopenharmony_ci    }
80cb93a386Sopenharmony_ci
81cb93a386Sopenharmony_ci    return fSurface;
82cb93a386Sopenharmony_ci}
83cb93a386Sopenharmony_ci
84cb93a386Sopenharmony_civoid GLWindowContext::swapBuffers() {
85cb93a386Sopenharmony_ci    this->onSwapBuffers();
86cb93a386Sopenharmony_ci}
87cb93a386Sopenharmony_ci
88cb93a386Sopenharmony_civoid GLWindowContext::resize(int w, int h) {
89cb93a386Sopenharmony_ci    this->destroyContext();
90cb93a386Sopenharmony_ci    this->initializeContext();
91cb93a386Sopenharmony_ci}
92cb93a386Sopenharmony_ci
93cb93a386Sopenharmony_civoid GLWindowContext::setDisplayParams(const DisplayParams& params) {
94cb93a386Sopenharmony_ci    fDisplayParams = params;
95cb93a386Sopenharmony_ci    this->destroyContext();
96cb93a386Sopenharmony_ci    this->initializeContext();
97cb93a386Sopenharmony_ci}
98cb93a386Sopenharmony_ci
99cb93a386Sopenharmony_ci}   //namespace sk_app
100