blob: 15d362efd5942d1112b805656c676f69a05460a4 [file] [log] [blame]
reed4a8126e2014-09-22 07:29:03 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "gm.h"
9#include "SkGradientShader.h"
10#include "SkSurface.h"
11#include "SkSurfaceProps.h"
12
13#define W 200
14#define H 100
15
reed1a9b9642016-03-13 14:13:58 -070016static sk_sp<SkShader> make_shader() {
reed4a8126e2014-09-22 07:29:03 -070017 int a = 0x99;
18 int b = 0xBB;
19 SkPoint pts[] = { { 0, 0 }, { W, H } };
20 SkColor colors[] = { SkColorSetRGB(a, a, a), SkColorSetRGB(b, b, b) };
reed1a9b9642016-03-13 14:13:58 -070021 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
reed4a8126e2014-09-22 07:29:03 -070022}
23
reede8f30622016-03-23 18:59:25 -070024static sk_sp<SkSurface> make_surface(GrContext* ctx, const SkImageInfo& info, SkPixelGeometry geo,
brianosmanb461d342016-04-13 13:10:14 -070025 int disallowAA, int disallowDither, bool gammaCorrect) {
reed4a8126e2014-09-22 07:29:03 -070026 uint32_t flags = 0;
27 if (disallowAA) {
28 flags |= SkSurfaceProps::kDisallowAntiAlias_Flag;
29 }
30 if (disallowDither) {
31 flags |= SkSurfaceProps::kDisallowDither_Flag;
32 }
brianosmanb461d342016-04-13 13:10:14 -070033 if (gammaCorrect) {
34 flags |= SkSurfaceProps::kGammaCorrect_Flag;
brianosman898235c2016-04-06 07:38:23 -070035 }
reed4a8126e2014-09-22 07:29:03 -070036
37 SkSurfaceProps props(flags, geo);
38 if (ctx) {
reede8f30622016-03-23 18:59:25 -070039 return SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
reed4a8126e2014-09-22 07:29:03 -070040 } else {
reede8f30622016-03-23 18:59:25 -070041 return SkSurface::MakeRaster(info, &props);
reed4a8126e2014-09-22 07:29:03 -070042 }
43}
44
45static void test_draw(SkCanvas* canvas, const char label[]) {
46 SkPaint paint;
47
48 paint.setAntiAlias(true);
49 paint.setLCDRenderText(true);
50 paint.setDither(true);
51
reed1a9b9642016-03-13 14:13:58 -070052 paint.setShader(make_shader());
reed4a8126e2014-09-22 07:29:03 -070053 canvas->drawRect(SkRect::MakeWH(W, H), paint);
halcanary96fcdcc2015-08-27 07:41:13 -070054 paint.setShader(nullptr);
reed4a8126e2014-09-22 07:29:03 -070055
56 paint.setColor(SK_ColorWHITE);
57 paint.setTextSize(32);
58 paint.setTextAlign(SkPaint::kCenter_Align);
caryclarkf597c422015-07-28 10:37:53 -070059 sk_tool_utils::set_portable_typeface(&paint);
reed4a8126e2014-09-22 07:29:03 -070060 canvas->drawText(label, strlen(label), W / 2, H * 3 / 4, paint);
61}
62
63class SurfacePropsGM : public skiagm::GM {
64public:
65 SurfacePropsGM() {}
66
67protected:
mtklein36352bf2015-03-25 18:17:31 -070068 SkString onShortName() override {
reed4a8126e2014-09-22 07:29:03 -070069 return SkString("surfaceprops");
70 }
71
mtklein36352bf2015-03-25 18:17:31 -070072 SkISize onISize() override {
reed4a8126e2014-09-22 07:29:03 -070073 return SkISize::Make(W * 4, H * 5);
74 }
75
mtklein36352bf2015-03-25 18:17:31 -070076 void onDraw(SkCanvas* canvas) override {
reed4a8126e2014-09-22 07:29:03 -070077 GrContext* ctx = canvas->getGrContext();
78
79 // must be opaque to have a hope of testing LCD text
brianosman898235c2016-04-06 07:38:23 -070080 const SkImageInfo info = SkImageInfo::MakeN32(W, H, kOpaque_SkAlphaType,
81 canvas->imageInfo().profileType());
82 SkSurfaceProps canvasProps(SkSurfaceProps::kLegacyFontHost_InitType);
brianosman0e3c5542016-04-13 13:56:21 -070083 bool gammaCorrect = canvas->getProps(&canvasProps) && canvasProps.isGammaCorrect();
reed4a8126e2014-09-22 07:29:03 -070084
85 const struct {
86 SkPixelGeometry fGeo;
87 const char* fLabel;
scroggoe6cad6b2016-05-09 13:20:58 -070088 } recs[] = {
reed4a8126e2014-09-22 07:29:03 -070089 { kUnknown_SkPixelGeometry, "Unknown" },
90 { kRGB_H_SkPixelGeometry, "RGB_H" },
91 { kBGR_H_SkPixelGeometry, "BGR_H" },
92 { kRGB_V_SkPixelGeometry, "RGB_V" },
93 { kBGR_V_SkPixelGeometry, "BGR_V" },
94 };
halcanary9d524f22016-03-29 09:03:52 -070095
reed4a8126e2014-09-22 07:29:03 -070096 SkScalar x = 0;
97 for (int disallowAA = 0; disallowAA <= 1; ++disallowAA) {
98 for (int disallowDither = 0; disallowDither <= 1; ++disallowDither) {
99 SkScalar y = 0;
scroggoe6cad6b2016-05-09 13:20:58 -0700100 for (const auto& rec : recs) {
101 auto surface(make_surface(ctx, info, rec.fGeo, disallowAA, disallowDither,
brianosman0e3c5542016-04-13 13:56:21 -0700102 gammaCorrect));
scroggoe6cad6b2016-05-09 13:20:58 -0700103 if (!surface) {
104 SkDebugf("failed to create surface! label: %s AA: %s dither: %s\n",
105 rec.fLabel, (disallowAA == 1 ? "disallowed" : "allowed"),
106 (disallowDither == 1 ? "disallowed" : "allowed"));
107 continue;
108 }
109 test_draw(surface->getCanvas(), rec.fLabel);
halcanary96fcdcc2015-08-27 07:41:13 -0700110 surface->draw(canvas, x, y, nullptr);
reed4a8126e2014-09-22 07:29:03 -0700111 y += H;
112 }
113 x += W;
114 }
115 }
116 }
117
118private:
119 typedef GM INHERITED;
120};
reed4a8126e2014-09-22 07:29:03 -0700121DEF_GM( return new SurfacePropsGM )
reed4af267b2014-11-21 08:46:37 -0800122
123#ifdef SK_DEBUG
124static bool equal(const SkSurfaceProps& a, const SkSurfaceProps& b) {
125 return a.flags() == b.flags() && a.pixelGeometry() == b.pixelGeometry();
126}
127#endif
128
129class NewSurfaceGM : public skiagm::GM {
130public:
131 NewSurfaceGM() {}
132
133protected:
mtklein36352bf2015-03-25 18:17:31 -0700134 SkString onShortName() override {
reed4af267b2014-11-21 08:46:37 -0800135 return SkString("surfacenew");
136 }
137
mtklein36352bf2015-03-25 18:17:31 -0700138 SkISize onISize() override {
reed4af267b2014-11-21 08:46:37 -0800139 return SkISize::Make(300, 140);
140 }
141
142 static void drawInto(SkCanvas* canvas) {
143 canvas->drawColor(SK_ColorRED);
144 }
145
mtklein36352bf2015-03-25 18:17:31 -0700146 void onDraw(SkCanvas* canvas) override {
reed4af267b2014-11-21 08:46:37 -0800147 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
148
reede8f30622016-03-23 18:59:25 -0700149 auto surf(canvas->makeSurface(info, nullptr));
150 if (!surf) {
151 surf = SkSurface::MakeRaster(info);
reed4af267b2014-11-21 08:46:37 -0800152 }
153 drawInto(surf->getCanvas());
154
reed9ce9d672016-03-17 10:51:11 -0700155 sk_sp<SkImage> image(surf->makeImageSnapshot());
halcanary96fcdcc2015-08-27 07:41:13 -0700156 canvas->drawImage(image, 10, 10, nullptr);
reed4af267b2014-11-21 08:46:37 -0800157
reede8f30622016-03-23 18:59:25 -0700158 auto surf2(surf->makeSurface(info));
reed4af267b2014-11-21 08:46:37 -0800159 drawInto(surf2->getCanvas());
160
161 // Assert that the props were communicated transitively through the first image
162 SkASSERT(equal(surf->props(), surf2->props()));
163
reed9ce9d672016-03-17 10:51:11 -0700164 sk_sp<SkImage> image2(surf2->makeImageSnapshot());
165 canvas->drawImage(image2.get(), 10 + SkIntToScalar(image->width()) + 10, 10, nullptr);
reed4af267b2014-11-21 08:46:37 -0800166 }
167
168private:
169 typedef GM INHERITED;
170};
171DEF_GM( return new NewSurfaceGM )