blob: 436ea952866cdf4345b30f49baa1117987145d0e [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,
25 int disallowAA, int disallowDither) {
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 }
33
34 SkSurfaceProps props(flags, geo);
35 if (ctx) {
reede8f30622016-03-23 18:59:25 -070036 return SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
reed4a8126e2014-09-22 07:29:03 -070037 } else {
reede8f30622016-03-23 18:59:25 -070038 return SkSurface::MakeRaster(info, &props);
reed4a8126e2014-09-22 07:29:03 -070039 }
40}
41
42static void test_draw(SkCanvas* canvas, const char label[]) {
43 SkPaint paint;
44
45 paint.setAntiAlias(true);
46 paint.setLCDRenderText(true);
47 paint.setDither(true);
48
reed1a9b9642016-03-13 14:13:58 -070049 paint.setShader(make_shader());
reed4a8126e2014-09-22 07:29:03 -070050 canvas->drawRect(SkRect::MakeWH(W, H), paint);
halcanary96fcdcc2015-08-27 07:41:13 -070051 paint.setShader(nullptr);
reed4a8126e2014-09-22 07:29:03 -070052
53 paint.setColor(SK_ColorWHITE);
54 paint.setTextSize(32);
55 paint.setTextAlign(SkPaint::kCenter_Align);
caryclarkf597c422015-07-28 10:37:53 -070056 sk_tool_utils::set_portable_typeface(&paint);
reed4a8126e2014-09-22 07:29:03 -070057 canvas->drawText(label, strlen(label), W / 2, H * 3 / 4, paint);
58}
59
60class SurfacePropsGM : public skiagm::GM {
61public:
62 SurfacePropsGM() {}
63
64protected:
mtklein36352bf2015-03-25 18:17:31 -070065 SkString onShortName() override {
reed4a8126e2014-09-22 07:29:03 -070066 return SkString("surfaceprops");
67 }
68
mtklein36352bf2015-03-25 18:17:31 -070069 SkISize onISize() override {
reed4a8126e2014-09-22 07:29:03 -070070 return SkISize::Make(W * 4, H * 5);
71 }
72
mtklein36352bf2015-03-25 18:17:31 -070073 void onDraw(SkCanvas* canvas) override {
reed4a8126e2014-09-22 07:29:03 -070074 GrContext* ctx = canvas->getGrContext();
75
76 // must be opaque to have a hope of testing LCD text
77 const SkImageInfo info = SkImageInfo::MakeN32(W, H, kOpaque_SkAlphaType);
78
79 const struct {
80 SkPixelGeometry fGeo;
81 const char* fLabel;
82 } rec[] = {
83 { kUnknown_SkPixelGeometry, "Unknown" },
84 { kRGB_H_SkPixelGeometry, "RGB_H" },
85 { kBGR_H_SkPixelGeometry, "BGR_H" },
86 { kRGB_V_SkPixelGeometry, "RGB_V" },
87 { kBGR_V_SkPixelGeometry, "BGR_V" },
88 };
89
90 SkScalar x = 0;
91 for (int disallowAA = 0; disallowAA <= 1; ++disallowAA) {
92 for (int disallowDither = 0; disallowDither <= 1; ++disallowDither) {
93 SkScalar y = 0;
94 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
reede8f30622016-03-23 18:59:25 -070095 auto surface(make_surface(ctx, info, rec[i].fGeo, disallowAA, disallowDither));
reed4a8126e2014-09-22 07:29:03 -070096 test_draw(surface->getCanvas(), rec[i].fLabel);
halcanary96fcdcc2015-08-27 07:41:13 -070097 surface->draw(canvas, x, y, nullptr);
reed4a8126e2014-09-22 07:29:03 -070098 y += H;
99 }
100 x += W;
101 }
102 }
103 }
104
105private:
106 typedef GM INHERITED;
107};
reed4a8126e2014-09-22 07:29:03 -0700108DEF_GM( return new SurfacePropsGM )
reed4af267b2014-11-21 08:46:37 -0800109
110#ifdef SK_DEBUG
111static bool equal(const SkSurfaceProps& a, const SkSurfaceProps& b) {
112 return a.flags() == b.flags() && a.pixelGeometry() == b.pixelGeometry();
113}
114#endif
115
116class NewSurfaceGM : public skiagm::GM {
117public:
118 NewSurfaceGM() {}
119
120protected:
mtklein36352bf2015-03-25 18:17:31 -0700121 SkString onShortName() override {
reed4af267b2014-11-21 08:46:37 -0800122 return SkString("surfacenew");
123 }
124
mtklein36352bf2015-03-25 18:17:31 -0700125 SkISize onISize() override {
reed4af267b2014-11-21 08:46:37 -0800126 return SkISize::Make(300, 140);
127 }
128
129 static void drawInto(SkCanvas* canvas) {
130 canvas->drawColor(SK_ColorRED);
131 }
132
mtklein36352bf2015-03-25 18:17:31 -0700133 void onDraw(SkCanvas* canvas) override {
reed4af267b2014-11-21 08:46:37 -0800134 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
135
reede8f30622016-03-23 18:59:25 -0700136 auto surf(canvas->makeSurface(info, nullptr));
137 if (!surf) {
138 surf = SkSurface::MakeRaster(info);
reed4af267b2014-11-21 08:46:37 -0800139 }
140 drawInto(surf->getCanvas());
141
reed9ce9d672016-03-17 10:51:11 -0700142 sk_sp<SkImage> image(surf->makeImageSnapshot());
halcanary96fcdcc2015-08-27 07:41:13 -0700143 canvas->drawImage(image, 10, 10, nullptr);
reed4af267b2014-11-21 08:46:37 -0800144
reede8f30622016-03-23 18:59:25 -0700145 auto surf2(surf->makeSurface(info));
reed4af267b2014-11-21 08:46:37 -0800146 drawInto(surf2->getCanvas());
147
148 // Assert that the props were communicated transitively through the first image
149 SkASSERT(equal(surf->props(), surf2->props()));
150
reed9ce9d672016-03-17 10:51:11 -0700151 sk_sp<SkImage> image2(surf2->makeImageSnapshot());
152 canvas->drawImage(image2.get(), 10 + SkIntToScalar(image->width()) + 10, 10, nullptr);
reed4af267b2014-11-21 08:46:37 -0800153 }
154
155private:
156 typedef GM INHERITED;
157};
158DEF_GM( return new NewSurfaceGM )
159