Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(102)

Side by Side Diff: tools/viewer/sk_app/GLWindowContext.cpp

Issue 1978573003: Add OpenGL context to Viewer. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fix Android stuff Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/viewer/sk_app/GLWindowContext.h ('k') | tools/viewer/sk_app/VulkanWindowContext.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1
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
9 #include "GrContext.h"
10 #include "SkSurface.h"
11 #include "GLWindowContext.h"
12
13 #include "gl/GrGLDefines.h"
14
15 #include "gl/GrGLUtil.h"
16 #include "GrRenderTarget.h"
17 #include "GrContext.h"
18
19 #include "SkCanvas.h"
20 #include "SkImage_Base.h"
21
22 namespace sk_app {
23
24 GLWindowContext::GLWindowContext(void* platformData, const DisplayParams& params )
25 : WindowContext()
26 , fBackendContext(nullptr)
27 , fRenderTarget(nullptr)
28 , fBackbuffer(nullptr) {
29 }
30
31 void GLWindowContext::initializeContext(void* platformData, const DisplayParams& params) {
32 this->onInitializeContext(platformData, params);
33
34 fDisplayParams = params;
35
36 SkAutoTUnref<const GrGLInterface> glInterface;
37 glInterface.reset(GrGLCreateNativeInterface());
38 fBackendContext.reset(GrGLInterfaceRemoveNVPR(glInterface.get()));
39
40 SkASSERT(nullptr == fContext);
41 fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fBackendCo ntext.get());
42 }
43
44 void GLWindowContext::destroyContext() {
45 fBackbuffer.reset(nullptr);
46 fRenderTarget.reset(nullptr);
47
48 if (fContext) {
49 // in case we have outstanding refs to this guy (lua?)
50 fContext->abandonContext();
51 fContext->unref();
52 fContext = nullptr;
53 }
54
55 fBackendContext.reset(nullptr);
56
57 this->onDestroyContext();
58 }
59
60 sk_sp<SkSurface> GLWindowContext::getBackbufferSurface() {
61 if (nullptr == fBackbuffer) {
62 fActualColorBits = SkTMax(fColorBits, 24);
63
64 GrBackendRenderTargetDesc desc;
65 desc.fWidth = this->fWidth;
66 desc.fHeight = this->fHeight;
67 // TODO: Query the actual framebuffer for sRGB capable. However, to
68 // preserve old (fake-linear) behavior, we don't do this. Instead, rely
69 // on the flag (currently driven via 'C' mode in SampleApp).
70 //
71 // Also, we may not have real sRGB support (ANGLE, in particular), so ch eck for
72 // that, and fall back to L32:
73 //
74 // ... and, if we're using a 10-bit/channel FB0, it doesn't do sRGB conv ersion on write,
75 // so pretend that it's non-sRGB 8888:
76 if (fContext) {
77 desc.fConfig = fContext->caps()->srgbSupport() &&
78 SkColorAndProfileAreGammaCorrect(fDisplayParams.fColorType,
79 fDisplayParams.fProfileType) &&
80 (fColorBits != 30) ? kSkiaGamma 8888_GrPixelConfig
81 : kSkia8888_ GrPixelConfig;
82 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
83 desc.fSampleCnt = fSampleCount;
84 desc.fStencilBits = fStencilBits;
85 GrGLint buffer;
86 GR_GL_CALL(fBackendContext, GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, & buffer));
87 desc.fRenderTargetHandle = buffer;
88 fRenderTarget.reset(fContext->textureProvider()->wrapBackendRenderTa rget(desc));
89
90 SkSurfaceProps props(fSurfaceProps);
Brian Osman 2016/05/16 18:59:43 We should be setting the kGammaCorrect_Flag on the
jvanverth1 2016/05/17 15:37:18 Done.
91 if (// !IsGpuDeviceType(type) ||
92 kRGBA_F16_SkColorType == fDisplayParams.fColorType || fActualCol orBits > 24) {
93 // If we're rendering to F16, we need an off-screen surface - th e current render
94 // target is most likely the wrong format.
95 //
96 // If we're using a deep (10-bit or higher) surface, we probably need an off-screen
97 // surface. 10-bit, in particular, has strange gamma behavior.
98 SkImageInfo info = SkImageInfo::Make(desc.fWidth, desc.fHeight,
99 fDisplayParams.fColorType,
100 kUnknown_SkAlphaType,
101 fDisplayParams.fProfileType );
102 fBackbuffer = SkSurface::MakeRenderTarget(fContext, SkBudgeted:: kNo, info,
103 fDisplayParams.fMSAASa mpleCount, &props);
104 } else {
105 fBackbuffer = SkSurface::MakeRenderTargetDirect(fRenderTarget.ge t(), &props);
106 }
107 }
108 }
109
110 return fBackbuffer;
111 }
112
113 void GLWindowContext::swapBuffers() {
114 if (//!IsGpuDeviceType(dType) ||
115 kRGBA_F16_SkColorType == fDisplayParams.fColorType || fActualColorBits > 24) {
116 // We made/have an off-screen surface. Get the contents as an SkImage:
117 SkImageInfo info = SkImageInfo::Make(fWidth, fHeight,
118 fDisplayParams.fColorType,
119 kUnknown_SkAlphaType,
120 fDisplayParams.fProfileType);
121 SkBitmap bm;
122 bm.allocPixels(info);
123 fBackbuffer->getCanvas()->readPixels(&bm, 0, 0);
124 SkPixmap pm;
125 bm.peekPixels(&pm);
126 sk_sp<SkImage> image(SkImage::MakeTextureFromPixmap(fContext, pm,
127 SkBudgeted::kNo));
128 GrTexture* texture = as_IB(image)->peekTexture();
129 SkASSERT(texture);
130
131 // With ten-bit output, we need to manually apply the gamma of the outpu t device
132 // (unless we're in non-gamma correct mode, in which case our data is al ready
133 // fake-sRGB, like we're expected to put in the 10-bit buffer):
134 bool doGamma = (fActualColorBits == 30) && SkImageInfoIsGammaCorrect(inf o);
135 fContext->applyGamma(fRenderTarget.get(), texture, doGamma ? 1.0f / 2.2f : 1.0f);
136 }
137
138 onSwapBuffers();
139 }
140
141 void GLWindowContext::resize(uint32_t w, uint32_t h) {
142 destroyContext();
143
144 initializeContext(nullptr, fDisplayParams);
145 }
146
147 void GLWindowContext::setDisplayParams(const DisplayParams& params) {
148 destroyContext();
149
150 initializeContext(nullptr, params);
151 }
152
153 } //namespace sk_app
OLDNEW
« no previous file with comments | « tools/viewer/sk_app/GLWindowContext.h ('k') | tools/viewer/sk_app/VulkanWindowContext.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698