blob: ee4deccacfe821d12c8a1426b46399cd8c0e0d17 [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
reed4a8126e2014-09-22 07:29:03 -070010#include "SkGradientShader.h"
11#include "SkSurface.h"
12#include "SkSurfaceProps.h"
13
14#define W 200
15#define H 100
16
reed1a9b9642016-03-13 14:13:58 -070017static sk_sp<SkShader> make_shader() {
reed4a8126e2014-09-22 07:29:03 -070018 int a = 0x99;
19 int b = 0xBB;
20 SkPoint pts[] = { { 0, 0 }, { W, H } };
21 SkColor colors[] = { SkColorSetRGB(a, a, a), SkColorSetRGB(b, b, b) };
reed1a9b9642016-03-13 14:13:58 -070022 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
reed4a8126e2014-09-22 07:29:03 -070023}
24
reed7c123542016-08-18 09:30:44 -070025static sk_sp<SkSurface> make_surface(GrContext* ctx, const SkImageInfo& info, SkPixelGeometry geo) {
26 SkSurfaceProps props(0, geo);
reed4a8126e2014-09-22 07:29:03 -070027 if (ctx) {
reede8f30622016-03-23 18:59:25 -070028 return SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
reed4a8126e2014-09-22 07:29:03 -070029 } else {
reede8f30622016-03-23 18:59:25 -070030 return SkSurface::MakeRaster(info, &props);
reed4a8126e2014-09-22 07:29:03 -070031 }
32}
33
34static void test_draw(SkCanvas* canvas, const char label[]) {
35 SkPaint paint;
36
37 paint.setAntiAlias(true);
38 paint.setLCDRenderText(true);
39 paint.setDither(true);
40
reed1a9b9642016-03-13 14:13:58 -070041 paint.setShader(make_shader());
reed4a8126e2014-09-22 07:29:03 -070042 canvas->drawRect(SkRect::MakeWH(W, H), paint);
halcanary96fcdcc2015-08-27 07:41:13 -070043 paint.setShader(nullptr);
reed4a8126e2014-09-22 07:29:03 -070044
45 paint.setColor(SK_ColorWHITE);
46 paint.setTextSize(32);
47 paint.setTextAlign(SkPaint::kCenter_Align);
caryclarkf597c422015-07-28 10:37:53 -070048 sk_tool_utils::set_portable_typeface(&paint);
reed4a8126e2014-09-22 07:29:03 -070049 canvas->drawText(label, strlen(label), W / 2, H * 3 / 4, paint);
50}
51
52class SurfacePropsGM : public skiagm::GM {
53public:
54 SurfacePropsGM() {}
55
56protected:
mtklein36352bf2015-03-25 18:17:31 -070057 SkString onShortName() override {
reed4a8126e2014-09-22 07:29:03 -070058 return SkString("surfaceprops");
59 }
60
mtklein36352bf2015-03-25 18:17:31 -070061 SkISize onISize() override {
reed7c123542016-08-18 09:30:44 -070062 return SkISize::Make(W, H * 5);
reed4a8126e2014-09-22 07:29:03 -070063 }
64
mtklein36352bf2015-03-25 18:17:31 -070065 void onDraw(SkCanvas* canvas) override {
reed4a8126e2014-09-22 07:29:03 -070066 GrContext* ctx = canvas->getGrContext();
67
68 // must be opaque to have a hope of testing LCD text
brianosman0e22eb82016-08-30 07:07:59 -070069 const SkImageInfo info = SkImageInfo::MakeN32(W, H, kOpaque_SkAlphaType);
reed4a8126e2014-09-22 07:29:03 -070070
71 const struct {
72 SkPixelGeometry fGeo;
73 const char* fLabel;
scroggoe6cad6b2016-05-09 13:20:58 -070074 } recs[] = {
reed4a8126e2014-09-22 07:29:03 -070075 { kUnknown_SkPixelGeometry, "Unknown" },
76 { kRGB_H_SkPixelGeometry, "RGB_H" },
77 { kBGR_H_SkPixelGeometry, "BGR_H" },
78 { kRGB_V_SkPixelGeometry, "RGB_V" },
79 { kBGR_V_SkPixelGeometry, "BGR_V" },
80 };
halcanary9d524f22016-03-29 09:03:52 -070081
reed4a8126e2014-09-22 07:29:03 -070082 SkScalar x = 0;
reed7c123542016-08-18 09:30:44 -070083 SkScalar y = 0;
84 for (const auto& rec : recs) {
85 auto surface(make_surface(ctx, info, rec.fGeo));
86 if (!surface) {
87 SkDebugf("failed to create surface! label: %s", rec.fLabel);
88 continue;
reed4a8126e2014-09-22 07:29:03 -070089 }
reed7c123542016-08-18 09:30:44 -070090 test_draw(surface->getCanvas(), rec.fLabel);
91 surface->draw(canvas, x, y, nullptr);
92 y += H;
reed4a8126e2014-09-22 07:29:03 -070093 }
94 }
95
96private:
97 typedef GM INHERITED;
98};
reed4a8126e2014-09-22 07:29:03 -070099DEF_GM( return new SurfacePropsGM )
reed4af267b2014-11-21 08:46:37 -0800100
101#ifdef SK_DEBUG
102static bool equal(const SkSurfaceProps& a, const SkSurfaceProps& b) {
103 return a.flags() == b.flags() && a.pixelGeometry() == b.pixelGeometry();
104}
105#endif
106
107class NewSurfaceGM : public skiagm::GM {
108public:
109 NewSurfaceGM() {}
110
111protected:
mtklein36352bf2015-03-25 18:17:31 -0700112 SkString onShortName() override {
reed4af267b2014-11-21 08:46:37 -0800113 return SkString("surfacenew");
114 }
115
mtklein36352bf2015-03-25 18:17:31 -0700116 SkISize onISize() override {
reed4af267b2014-11-21 08:46:37 -0800117 return SkISize::Make(300, 140);
118 }
119
120 static void drawInto(SkCanvas* canvas) {
121 canvas->drawColor(SK_ColorRED);
122 }
123
mtklein36352bf2015-03-25 18:17:31 -0700124 void onDraw(SkCanvas* canvas) override {
reed4af267b2014-11-21 08:46:37 -0800125 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
126
reede8f30622016-03-23 18:59:25 -0700127 auto surf(canvas->makeSurface(info, nullptr));
128 if (!surf) {
129 surf = SkSurface::MakeRaster(info);
reed4af267b2014-11-21 08:46:37 -0800130 }
131 drawInto(surf->getCanvas());
132
reed9ce9d672016-03-17 10:51:11 -0700133 sk_sp<SkImage> image(surf->makeImageSnapshot());
halcanary96fcdcc2015-08-27 07:41:13 -0700134 canvas->drawImage(image, 10, 10, nullptr);
reed4af267b2014-11-21 08:46:37 -0800135
reede8f30622016-03-23 18:59:25 -0700136 auto surf2(surf->makeSurface(info));
reed4af267b2014-11-21 08:46:37 -0800137 drawInto(surf2->getCanvas());
138
139 // Assert that the props were communicated transitively through the first image
140 SkASSERT(equal(surf->props(), surf2->props()));
141
reed9ce9d672016-03-17 10:51:11 -0700142 sk_sp<SkImage> image2(surf2->makeImageSnapshot());
143 canvas->drawImage(image2.get(), 10 + SkIntToScalar(image->width()) + 10, 10, nullptr);
reed4af267b2014-11-21 08:46:37 -0800144 }
145
146private:
147 typedef GM INHERITED;
148};
149DEF_GM( return new NewSurfaceGM )
Mike Reedd94abc52017-03-06 16:37:07 -0500150
151///////////////////////////////////////////////////////////////////////////////////////////////////
152
153DEF_SIMPLE_GM(copy_on_write_retain, canvas, 256, 256) {
154 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
155 sk_sp<SkSurface> surf = canvas->makeSurface(info, nullptr);
156 if (!surf) {
157 surf = SkSurface::MakeRaster(info, nullptr);
158 }
159
160 surf->getCanvas()->clear(SK_ColorRED);
161 // its important that image survives longer than the next draw, so the surface will see
162 // an outstanding image, and have to decide if it should retain or discard those pixels
163 sk_sp<SkImage> image = surf->makeImageSnapshot();
164
165 // normally a clear+opaque should trigger the discard optimization, but since we have a clip
166 // it should not (we need the previous red pixels).
167 surf->getCanvas()->clipRect(SkRect::MakeWH(128, 256));
168 surf->getCanvas()->clear(SK_ColorBLUE);
169
170 // expect to see two rects: blue | red
171 canvas->drawImage(surf->makeImageSnapshot(), 0, 0, nullptr);
172}
Mike Reeda1361362017-03-07 09:37:29 -0500173
174DEF_SIMPLE_GM(copy_on_write_savelayer, canvas, 256, 256) {
175 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
176 sk_sp<SkSurface> surf = canvas->makeSurface(info, nullptr);
177 if (!surf) {
178 surf = SkSurface::MakeRaster(info, nullptr);
179 }
180
181 surf->getCanvas()->clear(SK_ColorRED);
182 // its important that image survives longer than the next draw, so the surface will see
183 // an outstanding image, and have to decide if it should retain or discard those pixels
184 sk_sp<SkImage> image = surf->makeImageSnapshot();
185
186 // now draw into a full-screen layer. This should (a) trigger a copy-on-write, but it should
187 // not trigger discard, even tho its alpha (SK_ColorBLUE) is opaque, since it is in a layer
188 // with a non-opaque paint.
189 SkPaint paint;
190 paint.setAlpha(0x40);
191 surf->getCanvas()->saveLayer({0, 0, 256, 256}, &paint);
192 surf->getCanvas()->clear(SK_ColorBLUE);
193 surf->getCanvas()->restore();
194
195 // expect to see two rects: blue blended on red
196 canvas->drawImage(surf->makeImageSnapshot(), 0, 0, nullptr);
197}