blob: 0c1019b60d232bdab092e133e342afdae97bd437 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBlendMode.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
12#include "include/core/SkColorSpace.h"
13#include "include/core/SkFont.h"
14#include "include/core/SkImage.h"
15#include "include/core/SkImageInfo.h"
16#include "include/core/SkPaint.h"
17#include "include/core/SkPoint.h"
18#include "include/core/SkRect.h"
19#include "include/core/SkRefCnt.h"
20#include "include/core/SkScalar.h"
21#include "include/core/SkShader.h"
22#include "include/core/SkSize.h"
23#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/core/SkSurface.h"
25#include "include/core/SkSurfaceProps.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040026#include "include/core/SkTileMode.h"
27#include "include/core/SkTypeface.h"
28#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "include/effects/SkGradientShader.h"
30#include "include/utils/SkTextUtils.h"
31#include "tools/ToolUtils.h"
reed4a8126e2014-09-22 07:29:03 -070032
33#define W 200
34#define H 100
35
reed1a9b9642016-03-13 14:13:58 -070036static sk_sp<SkShader> make_shader() {
reed4a8126e2014-09-22 07:29:03 -070037 int a = 0x99;
38 int b = 0xBB;
39 SkPoint pts[] = { { 0, 0 }, { W, H } };
40 SkColor colors[] = { SkColorSetRGB(a, a, a), SkColorSetRGB(b, b, b) };
Mike Reedfae8fce2019-04-03 10:27:45 -040041 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
reed4a8126e2014-09-22 07:29:03 -070042}
43
Robert Phillips16bf7d32020-07-07 10:20:27 -040044static sk_sp<SkSurface> make_surface(GrRecordingContext* ctx,
45 const SkImageInfo& info,
46 SkPixelGeometry geo) {
reed7c123542016-08-18 09:30:44 -070047 SkSurfaceProps props(0, geo);
reed4a8126e2014-09-22 07:29:03 -070048 if (ctx) {
reede8f30622016-03-23 18:59:25 -070049 return SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
reed4a8126e2014-09-22 07:29:03 -070050 } else {
reede8f30622016-03-23 18:59:25 -070051 return SkSurface::MakeRaster(info, &props);
reed4a8126e2014-09-22 07:29:03 -070052 }
53}
54
55static void test_draw(SkCanvas* canvas, const char label[]) {
56 SkPaint paint;
57
58 paint.setAntiAlias(true);
reed4a8126e2014-09-22 07:29:03 -070059 paint.setDither(true);
60
reed1a9b9642016-03-13 14:13:58 -070061 paint.setShader(make_shader());
reed4a8126e2014-09-22 07:29:03 -070062 canvas->drawRect(SkRect::MakeWH(W, H), paint);
halcanary96fcdcc2015-08-27 07:41:13 -070063 paint.setShader(nullptr);
reed4a8126e2014-09-22 07:29:03 -070064
65 paint.setColor(SK_ColorWHITE);
Mike Kleinea3f0142019-03-20 11:12:10 -050066 SkFont font(ToolUtils::create_portable_typeface(), 32);
Mike Reedb579f072019-01-03 15:45:53 -050067 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
68 SkTextUtils::DrawString(canvas, label, W / 2, H * 3 / 4, font, paint,
69 SkTextUtils::kCenter_Align);
reed4a8126e2014-09-22 07:29:03 -070070}
71
72class SurfacePropsGM : public skiagm::GM {
73public:
74 SurfacePropsGM() {}
75
76protected:
mtklein36352bf2015-03-25 18:17:31 -070077 SkString onShortName() override {
reed4a8126e2014-09-22 07:29:03 -070078 return SkString("surfaceprops");
79 }
80
mtklein36352bf2015-03-25 18:17:31 -070081 SkISize onISize() override {
reed7c123542016-08-18 09:30:44 -070082 return SkISize::Make(W, H * 5);
reed4a8126e2014-09-22 07:29:03 -070083 }
84
mtklein36352bf2015-03-25 18:17:31 -070085 void onDraw(SkCanvas* canvas) override {
Robert Phillips16bf7d32020-07-07 10:20:27 -040086 auto ctx = canvas->recordingContext();
reed4a8126e2014-09-22 07:29:03 -070087
88 // must be opaque to have a hope of testing LCD text
brianosman0e22eb82016-08-30 07:07:59 -070089 const SkImageInfo info = SkImageInfo::MakeN32(W, H, kOpaque_SkAlphaType);
reed4a8126e2014-09-22 07:29:03 -070090
91 const struct {
92 SkPixelGeometry fGeo;
93 const char* fLabel;
scroggoe6cad6b2016-05-09 13:20:58 -070094 } recs[] = {
reed4a8126e2014-09-22 07:29:03 -070095 { kUnknown_SkPixelGeometry, "Unknown" },
96 { kRGB_H_SkPixelGeometry, "RGB_H" },
97 { kBGR_H_SkPixelGeometry, "BGR_H" },
98 { kRGB_V_SkPixelGeometry, "RGB_V" },
99 { kBGR_V_SkPixelGeometry, "BGR_V" },
100 };
halcanary9d524f22016-03-29 09:03:52 -0700101
reed4a8126e2014-09-22 07:29:03 -0700102 SkScalar x = 0;
reed7c123542016-08-18 09:30:44 -0700103 SkScalar y = 0;
104 for (const auto& rec : recs) {
105 auto surface(make_surface(ctx, info, rec.fGeo));
106 if (!surface) {
107 SkDebugf("failed to create surface! label: %s", rec.fLabel);
108 continue;
reed4a8126e2014-09-22 07:29:03 -0700109 }
reed7c123542016-08-18 09:30:44 -0700110 test_draw(surface->getCanvas(), rec.fLabel);
111 surface->draw(canvas, x, y, nullptr);
112 y += H;
reed4a8126e2014-09-22 07:29:03 -0700113 }
114 }
115
116private:
117 typedef GM INHERITED;
118};
reed4a8126e2014-09-22 07:29:03 -0700119DEF_GM( return new SurfacePropsGM )
reed4af267b2014-11-21 08:46:37 -0800120
121#ifdef SK_DEBUG
122static bool equal(const SkSurfaceProps& a, const SkSurfaceProps& b) {
123 return a.flags() == b.flags() && a.pixelGeometry() == b.pixelGeometry();
124}
125#endif
126
127class NewSurfaceGM : public skiagm::GM {
128public:
129 NewSurfaceGM() {}
130
131protected:
mtklein36352bf2015-03-25 18:17:31 -0700132 SkString onShortName() override {
reed4af267b2014-11-21 08:46:37 -0800133 return SkString("surfacenew");
134 }
135
mtklein36352bf2015-03-25 18:17:31 -0700136 SkISize onISize() override {
reed4af267b2014-11-21 08:46:37 -0800137 return SkISize::Make(300, 140);
138 }
139
140 static void drawInto(SkCanvas* canvas) {
141 canvas->drawColor(SK_ColorRED);
142 }
143
mtklein36352bf2015-03-25 18:17:31 -0700144 void onDraw(SkCanvas* canvas) override {
reed4af267b2014-11-21 08:46:37 -0800145 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
146
Mike Kleinea3f0142019-03-20 11:12:10 -0500147 auto surf(ToolUtils::makeSurface(canvas, info, nullptr));
reed4af267b2014-11-21 08:46:37 -0800148 drawInto(surf->getCanvas());
149
reed9ce9d672016-03-17 10:51:11 -0700150 sk_sp<SkImage> image(surf->makeImageSnapshot());
halcanary96fcdcc2015-08-27 07:41:13 -0700151 canvas->drawImage(image, 10, 10, nullptr);
reed4af267b2014-11-21 08:46:37 -0800152
reede8f30622016-03-23 18:59:25 -0700153 auto surf2(surf->makeSurface(info));
reed4af267b2014-11-21 08:46:37 -0800154 drawInto(surf2->getCanvas());
155
156 // Assert that the props were communicated transitively through the first image
157 SkASSERT(equal(surf->props(), surf2->props()));
158
reed9ce9d672016-03-17 10:51:11 -0700159 sk_sp<SkImage> image2(surf2->makeImageSnapshot());
160 canvas->drawImage(image2.get(), 10 + SkIntToScalar(image->width()) + 10, 10, nullptr);
reed4af267b2014-11-21 08:46:37 -0800161 }
162
163private:
164 typedef GM INHERITED;
165};
166DEF_GM( return new NewSurfaceGM )
Mike Reedd94abc52017-03-06 16:37:07 -0500167
168///////////////////////////////////////////////////////////////////////////////////////////////////
169
170DEF_SIMPLE_GM(copy_on_write_retain, canvas, 256, 256) {
171 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
Mike Kleinea3f0142019-03-20 11:12:10 -0500172 sk_sp<SkSurface> surf = ToolUtils::makeSurface(canvas, info);
Mike Reedd94abc52017-03-06 16:37:07 -0500173
174 surf->getCanvas()->clear(SK_ColorRED);
175 // its important that image survives longer than the next draw, so the surface will see
176 // an outstanding image, and have to decide if it should retain or discard those pixels
177 sk_sp<SkImage> image = surf->makeImageSnapshot();
178
179 // normally a clear+opaque should trigger the discard optimization, but since we have a clip
180 // it should not (we need the previous red pixels).
181 surf->getCanvas()->clipRect(SkRect::MakeWH(128, 256));
182 surf->getCanvas()->clear(SK_ColorBLUE);
183
184 // expect to see two rects: blue | red
185 canvas->drawImage(surf->makeImageSnapshot(), 0, 0, nullptr);
186}
Mike Reeda1361362017-03-07 09:37:29 -0500187
188DEF_SIMPLE_GM(copy_on_write_savelayer, canvas, 256, 256) {
189 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
Mike Kleinea3f0142019-03-20 11:12:10 -0500190 sk_sp<SkSurface> surf = ToolUtils::makeSurface(canvas, info);
Mike Reeda1361362017-03-07 09:37:29 -0500191 surf->getCanvas()->clear(SK_ColorRED);
192 // its important that image survives longer than the next draw, so the surface will see
193 // an outstanding image, and have to decide if it should retain or discard those pixels
194 sk_sp<SkImage> image = surf->makeImageSnapshot();
195
196 // now draw into a full-screen layer. This should (a) trigger a copy-on-write, but it should
197 // not trigger discard, even tho its alpha (SK_ColorBLUE) is opaque, since it is in a layer
198 // with a non-opaque paint.
199 SkPaint paint;
Mike Reed9407e242019-02-15 16:13:57 -0500200 paint.setAlphaf(0.25f);
Mike Reeda1361362017-03-07 09:37:29 -0500201 surf->getCanvas()->saveLayer({0, 0, 256, 256}, &paint);
202 surf->getCanvas()->clear(SK_ColorBLUE);
203 surf->getCanvas()->restore();
204
205 // expect to see two rects: blue blended on red
206 canvas->drawImage(surf->makeImageSnapshot(), 0, 0, nullptr);
207}
Mike Reed114bde82018-11-21 09:12:09 -0500208
209DEF_SIMPLE_GM(surface_underdraw, canvas, 256, 256) {
210 SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256, nullptr);
Mike Kleinea3f0142019-03-20 11:12:10 -0500211 auto surf = ToolUtils::makeSurface(canvas, info);
Mike Reed114bde82018-11-21 09:12:09 -0500212
213 const SkIRect subset = SkIRect::MakeLTRB(180, 0, 256, 256);
214
215 // noisy background
216 {
217 SkPoint pts[] = {{0, 0}, {40, 50}};
218 SkColor colors[] = {SK_ColorRED, SK_ColorBLUE};
Mike Reedfae8fce2019-04-03 10:27:45 -0400219 auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kRepeat);
Mike Reed114bde82018-11-21 09:12:09 -0500220 SkPaint paint;
221 paint.setShader(sh);
222 surf->getCanvas()->drawPaint(paint);
223 }
224
225 // save away the right-hand strip, then clear it
226 sk_sp<SkImage> saveImg = surf->makeImageSnapshot(subset);
227 {
228 SkPaint paint;
229 paint.setBlendMode(SkBlendMode::kClear);
230 surf->getCanvas()->drawRect(SkRect::Make(subset), paint);
231 }
232
233 // draw the "foreground"
234 {
235 SkPaint paint;
236 paint.setColor(SK_ColorGREEN);
237 SkRect r = { 0, 10, 256, 35 };
238 while (r.fBottom < 256) {
239 surf->getCanvas()->drawRect(r, paint);
240 r.offset(0, r.height() * 2);
241 }
242 }
243
244 // apply the "fade"
245 {
246 SkPoint pts[] = {{SkIntToScalar(subset.left()), 0}, {SkIntToScalar(subset.right()), 0}};
247 SkColor colors[] = {0xFF000000, 0};
Mike Reedfae8fce2019-04-03 10:27:45 -0400248 auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
Mike Reed114bde82018-11-21 09:12:09 -0500249 SkPaint paint;
250 paint.setShader(sh);
251 paint.setBlendMode(SkBlendMode::kDstIn);
252 surf->getCanvas()->drawRect(SkRect::Make(subset), paint);
253 }
254
255 // restore the original strip, drawing it "under" the current foreground
256 {
257 SkPaint paint;
258 paint.setBlendMode(SkBlendMode::kDstOver);
259 surf->getCanvas()->drawImage(saveImg,
260 SkIntToScalar(subset.left()), SkIntToScalar(subset.top()),
261 &paint);
262 }
263
264 // show it on screen
265 surf->draw(canvas, 0, 0, nullptr);
266}