blob: ecac2cc0047b44ab2d189dbd4e223148ff3cba40 [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
9#include "GrContext.h"
10#include "SkSurface.h"
11#include "WindowContext.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
22namespace sk_app {
23
24sk_sp<SkSurface> WindowContext::createRenderSurface(sk_sp<GrRenderTarget> rt, int colorBits) {
25 auto flags = (fSurfaceProps.flags() & ~SkSurfaceProps::kGammaCorrect_Flag) |
26 (GrPixelConfigIsSRGB(fPixelConfig) ? SkSurfaceProps::kGammaCorrect_Flag : 0);
27 SkSurfaceProps props(flags, fSurfaceProps.pixelGeometry());
28
29 if (!this->isGpuContext() || colorBits > 24 ||
30 kRGBA_F16_SkColorType == fDisplayParams.fColorType) {
31 // If we're rendering to F16, we need an off-screen surface - the current render
32 // target is most likely the wrong format.
33 //
34 // If we're rendering raster data or using a deep (10-bit or higher) surface, we probably
35 // need an off-screen surface. 10-bit, in particular, has strange gamma behavior.
36 SkImageInfo info = SkImageInfo::Make(fWidth, fHeight,
37 fDisplayParams.fColorType,
38 kUnknown_SkAlphaType,
brianosmanb109b8c2016-06-16 13:03:24 -070039 fDisplayParams.fColorSpace);
jvanverthaf236b52016-05-20 06:01:06 -070040 return SkSurface::MakeRenderTarget(fContext, SkBudgeted::kNo, info,
41 fDisplayParams.fMSAASampleCount, &props);
42 } else {
43 return SkSurface::MakeRenderTargetDirect(rt.get(), &props);
44 }
45}
46
47void WindowContext::presentRenderSurface(sk_sp<SkSurface> renderSurface, sk_sp<GrRenderTarget> rt,
48 int colorBits) {
49 if (!this->isGpuContext() || colorBits > 24 ||
50 kRGBA_F16_SkColorType == fDisplayParams.fColorType) {
51 // We made/have an off-screen surface. Get the contents as an SkImage:
52 SkImageInfo info = SkImageInfo::Make(fWidth, fHeight,
53 fDisplayParams.fColorType,
54 kUnknown_SkAlphaType,
brianosmanb109b8c2016-06-16 13:03:24 -070055 fDisplayParams.fColorSpace);
jvanverthaf236b52016-05-20 06:01:06 -070056 SkBitmap bm;
57 bm.allocPixels(info);
58 renderSurface->getCanvas()->readPixels(&bm, 0, 0);
59 SkPixmap pm;
60 bm.peekPixels(&pm);
61 sk_sp<SkImage> image(SkImage::MakeTextureFromPixmap(fContext, pm,
62 SkBudgeted::kNo));
63 GrTexture* texture = as_IB(image)->peekTexture();
64 SkASSERT(texture);
65
66 // With ten-bit output, we need to manually apply the gamma of the output device
67 // (unless we're in non-gamma correct mode, in which case our data is already
68 // fake-sRGB, like we're expected to put in the 10-bit buffer):
69 bool doGamma = (colorBits == 30) && SkImageInfoIsGammaCorrect(info);
70 fContext->applyGamma(rt.get(), texture, doGamma ? 1.0f / 2.2f : 1.0f);
71 }
72}
73
74} //namespace sk_app