Debian Codegraph @

gm/surface.cpp

raw
1 /*
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/gm.h"
9 #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"
24 #include "include/core/SkSurface.h"
25 #include "include/core/SkSurfaceProps.h"
26 #include "include/core/SkTileMode.h"
27 #include "include/core/SkTypeface.h"
28 #include "include/core/SkTypes.h"
29 #include "include/effects/SkGradient.h"
30 #include "include/utils/SkTextUtils.h"
31 #include "tools/ToolUtils.h"
32 #include "tools/fonts/FontToolUtils.h"
33
34 #if defined(SK_GANESH)
35 #include "include/gpu/ganesh/GrDirectContext.h"
36 #include "include/gpu/ganesh/GrRecordingContext.h"
37 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
38 #include "tools/gpu/BackendSurfaceFactory.h"
39 #endif
40
41 #if defined(SK_GRAPHITE)
42 #include "include/gpu/graphite/Surface.h"
43 #include "tools/gpu/BackendSurfaceFactory.h"
44 #endif
45
46 #define W 800
47 #define H 100
48
49 static sk_sp<SkShader> make_shader() {
50 constexpr float a = 0x99/255.f;
51 constexpr float b = 0xBB/255.f;
52 SkPoint pts[] = { { 0, 0 }, { W, H } };
53 const SkColor4f colors[] = { {a, a, a, 1}, {b, b, b, 1} };
54 return SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kClamp}, {}});
55 }
56
57 static sk_sp<SkSurface> make_surface(GrRecordingContext* ctx,
58 skgpu::graphite::Recorder* recorder,
59 const SkImageInfo& info,
60 uint32_t flags,
61 SkPixelGeometry geo,
62 SkScalar contrast,
63 SkScalar gamma) {
64 SkSurfaceProps props(flags, geo, contrast, gamma);
65 #if defined(SK_GRAPHITE)
66 if (recorder) {
67 return SkSurfaces::RenderTarget(recorder, info, skgpu::Mipmapped::kNo, &props);
68 }
69 #endif
70 #if defined(SK_GANESH)
71 if (ctx) {
72 return SkSurfaces::RenderTarget(ctx, skgpu::Budgeted::kNo, info, 0, &props);
73 }
74 #endif
75 return SkSurfaces::Raster(info, &props);
76 }
77
78 static void test_draw(SkCanvas* canvas, const char label[]) {
79 SkPaint paint;
80
81 paint.setAntiAlias(true);
82 paint.setDither(true);
83
84 paint.setShader(make_shader());
85 canvas->drawRect(SkRect::MakeWH(W, H), paint);
86 paint.setShader(nullptr);
87
88 paint.setColor(SK_ColorWHITE);
89 SkFont font(ToolUtils::DefaultPortableTypeface(), 32);
90 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
91 SkTextUtils::DrawString(canvas, label, W / 2, H * 3 / 4, font, paint,
92 SkTextUtils::kCenter_Align);
93 }
94
95 class SurfacePropsGM : public skiagm::GM {
96 public:
97 SurfacePropsGM(uint32_t flags) : fFlags(flags) {
98 recs = {
99 {kUnknown_SkPixelGeometry,
100 "Unknown geometry, default contrast/gamma",
101 SK_GAMMA_CONTRAST,
102 SK_GAMMA_EXPONENT},
103 {kRGB_H_SkPixelGeometry,
104 "RGB_H, default contrast/gamma",
105 SK_GAMMA_CONTRAST,
106 SK_GAMMA_EXPONENT},
107 {kBGR_H_SkPixelGeometry,
108 "BGR_H, default contrast/gamma",
109 SK_GAMMA_CONTRAST,
110 SK_GAMMA_EXPONENT},
111 {kRGB_V_SkPixelGeometry,
112 "RGB_V, default contrast/gamma",
113 SK_GAMMA_CONTRAST,
114 SK_GAMMA_EXPONENT},
115 {kBGR_V_SkPixelGeometry,
116 "BGR_V, default contrast/gamma",
117 SK_GAMMA_CONTRAST,
118 SK_GAMMA_EXPONENT},
119 {kRGB_H_SkPixelGeometry, "RGB_H contrast : 0 gamma: 0", 0, 0},
120 {kRGB_H_SkPixelGeometry, "RGB_H contrast : 1 gamma: 0", 1, 0},
121 {kRGB_H_SkPixelGeometry, "RGB_H contrast : 0 gamma: 3.9", 0, 3.9f},
122 {kRGB_H_SkPixelGeometry, "RGB_H contrast : 1 gamma: 3.9", 1, 3.9f},
123 };
124 }
125
126 protected:
127 SkString getName() const override {
128 return SkStringPrintf("surfaceprops%s",
129 fFlags != 0 ? "_df" : "");
130 }
131
132 SkISize getISize() override { return SkISize::Make(W, H * recs.size()); }
133
134 void onDraw(SkCanvas* canvas) override {
135 auto ctx = canvas->recordingContext();
136 auto recorder = canvas->recorder();
137
138 // must be opaque to have a hope of testing LCD text
139 const SkImageInfo info = SkImageInfo::MakeN32(W, H, kOpaque_SkAlphaType);
140
141 SkScalar x = 0;
142 SkScalar y = 0;
143 for (const auto& rec : recs) {
144 auto surface(make_surface(ctx, recorder, info, fFlags, rec.fGeo, rec.fContrast,
145 rec.fGamma));
146 if (!surface) {
147 SkDebugf("failed to create surface! label: %s", rec.fLabel);
148 continue;
149 }
150 test_draw(surface->getCanvas(), rec.fLabel);
151 surface->draw(canvas, x, y);
152 y += H;
153 }
154 }
155
156 private:
157 struct SurfacePropsInput {
158 SkPixelGeometry fGeo;
159 const char* fLabel;
160 SkScalar fContrast;
161 SkScalar fGamma;
162 };
163 std::vector<SurfacePropsInput> recs;
164
165 uint32_t fFlags;
166
167 using INHERITED = GM;
168 };
169 DEF_GM( return new SurfacePropsGM(0); )
170 DEF_GM( return new SurfacePropsGM(SkSurfaceProps::kUseDeviceIndependentFonts_Flag); )
171
172 #ifdef SK_DEBUG
173 static bool equal(const SkSurfaceProps& a, const SkSurfaceProps& b) {
174 return a == b;
175 }
176 #endif
177
178 class NewSurfaceGM : public skiagm::GM {
179 public:
180 NewSurfaceGM() {}
181
182 protected:
183 SkString getName() const override { return SkString("surfacenew"); }
184
185 SkISize getISize() override { return SkISize::Make(300, 140); }
186
187 static void drawInto(SkCanvas* canvas) {
188 canvas->drawColor(SK_ColorRED);
189 }
190
191 void onDraw(SkCanvas* canvas) override {
192 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
193
194 auto surf(ToolUtils::makeSurface(canvas, info, nullptr));
195 drawInto(surf->getCanvas());
196
197 sk_sp<SkImage> image(surf->makeImageSnapshot());
198 canvas->drawImage(image, 10, 10);
199
200 auto surf2(surf->makeSurface(info));
201 drawInto(surf2->getCanvas());
202
203 // Assert that the props were communicated transitively through the first image
204 SkASSERT(equal(surf->props(), surf2->props()));
205
206 sk_sp<SkImage> image2(surf2->makeImageSnapshot());
207 canvas->drawImage(image2.get(), 10 + SkIntToScalar(image->width()) + 10, 10);
208 }
209
210 private:
211 using INHERITED = GM;
212 };
213 DEF_GM( return new NewSurfaceGM )
214
215 ///////////////////////////////////////////////////////////////////////////////////////////////////
216
217 // The GPU backend may behave differently when images are snapped from wrapped textures and
218 // render targets compared.
219 namespace {
220 enum SurfaceType {
221 kManaged,
222 kBackendTexture,
223 kBackendRenderTarget
224 };
225 }
226
227 static sk_sp<SkSurface> make_surface(const SkImageInfo& ii, SkCanvas* canvas, SurfaceType type) {
228 #if defined(SK_GANESH)
229 GrDirectContext* direct = GrAsDirectContext(canvas->recordingContext());
230 #endif
231 #if defined(SK_GRAPHITE)
232 skgpu::graphite::Recorder* recorder = canvas->recorder();
233 #endif
234 switch (type) {
235 case kManaged:
236 return ToolUtils::makeSurface(canvas, ii);
237 case kBackendTexture:
238 #if defined(SK_GRAPHITE)
239 if (recorder) {
240 return sk_gpu_test::MakeBackendTextureSurface(recorder, ii);
241 }
242 #endif
243 #if defined(SK_GANESH)
244 if (direct) {
245 return sk_gpu_test::MakeBackendTextureSurface(direct,
246 ii,
247 kTopLeft_GrSurfaceOrigin,
248 1);
249 }
250 #endif
251 return nullptr;
252
253 case kBackendRenderTarget:
254 #if defined(SK_GRAPHITE)
255 if (recorder) {
256 return SkSurfaces::RenderTarget(recorder,
257 ii,
258 skgpu::Mipmapped::kNo,
259 /*surfaceProps=*/nullptr);
260 }
261 #endif
262 #if defined(SK_GANESH)
263 if (direct) {
264 return sk_gpu_test::MakeBackendRenderTargetSurface(direct,
265 ii,
266 kTopLeft_GrSurfaceOrigin,
267 1);
268 }
269 #endif
270 return nullptr;
271 }
272 return nullptr;
273 }
274
275 using MakeSurfaceFn = std::function<sk_sp<SkSurface>(const SkImageInfo&)>;
276
277 #define DEF_BASIC_SURFACE_TEST(name, canvas, main, W, H) \
278 DEF_SIMPLE_GM(name, canvas, W, H) { \
279 auto make = [canvas](const SkImageInfo& ii) { \
280 return make_surface(ii, canvas, SurfaceType::kManaged); \
281 }; \
282 main(canvas, MakeSurfaceFn(make)); \
283 }
284
285 #if defined(SK_GANESH)
286 #define DEF_BACKEND_SURFACE_TEST(name, canvas, main, type, W, H) \
287 DEF_SIMPLE_GM_CAN_FAIL(name, canvas, err_msg, W, H) { \
288 GrDirectContext* direct = GrAsDirectContext(canvas->recordingContext()); \
289 skgpu::graphite::Recorder* recorder = canvas->recorder(); \
290 if ((!direct || direct->abandoned()) && !recorder) { \
291 *err_msg = "Requires non-abandoned GrDirectContext or Recorder"; \
292 return skiagm::DrawResult::kSkip; \
293 } \
294 auto make = [canvas](const SkImageInfo& ii) { return make_surface(ii, canvas, type); }; \
295 main(canvas, MakeSurfaceFn(make)); \
296 return skiagm::DrawResult::kOk; \
297 }
298 #else
299 #define DEF_BACKEND_SURFACE_TEST(...)
300 #endif
301
302 #define DEF_BET_SURFACE_TEST(name, canvas, main, W, H) \
303 DEF_BACKEND_SURFACE_TEST(SK_MACRO_CONCAT(name, _bet), canvas, main, \
304 SurfaceType::kBackendTexture, W, H)
305
306 #define DEF_BERT_SURFACE_TEST(name, canvas, main, W, H) \
307 DEF_BACKEND_SURFACE_TEST(SK_MACRO_CONCAT(name, _bert), canvas, main, \
308 SurfaceType::kBackendRenderTarget, W, H)
309
310 // This makes 3 GMs from the same code, normal, wrapped backend texture, and wrapped backend
311 // render target.
312 #define DEF_SURFACE_TESTS(name, canvas, W, H) \
313 static void SK_MACRO_CONCAT(name, _main)(SkCanvas*, const MakeSurfaceFn&); \
314 DEF_BASIC_SURFACE_TEST(name, canvas, SK_MACRO_CONCAT(name, _main), W, H) \
315 DEF_BET_SURFACE_TEST (name, canvas, SK_MACRO_CONCAT(name, _main), W, H) \
316 DEF_BERT_SURFACE_TEST (name, canvas, SK_MACRO_CONCAT(name, _main), W, H) \
317 static void SK_MACRO_CONCAT(name, _main)(SkCanvas * canvas, const MakeSurfaceFn& make)
318
319 DEF_SURFACE_TESTS(copy_on_write_retain, canvas, 256, 256) {
320 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
321 sk_sp<SkSurface> surf = make(info);
322
323 surf->getCanvas()->clear(SK_ColorRED);
324 // its important that image survives longer than the next draw, so the surface will see
325 // an outstanding image, and have to decide if it should retain or discard those pixels
326 sk_sp<SkImage> image = surf->makeImageSnapshot();
327
328 // normally a clear+opaque should trigger the discard optimization, but since we have a clip
329 // it should not (we need the previous red pixels).
330 surf->getCanvas()->clipRect(SkRect::MakeWH(128, 256));
331 surf->getCanvas()->clear(SK_ColorBLUE);
332
333 // expect to see two rects: blue | red
334 canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
335 }
336
337 // Like copy_on_write_retain but draws the snapped image back to the surface it was snapped from.
338 DEF_SURFACE_TESTS(copy_on_write_retain2, canvas, 256, 256) {
339 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
340 sk_sp<SkSurface> surf = make(info);
341
342 surf->getCanvas()->clear(SK_ColorBLUE);
343 // its important that image survives longer than the next draw, so the surface will see
344 // an outstanding image, and have to decide if it should retain or discard those pixels
345 sk_sp<SkImage> image = surf->makeImageSnapshot();
346
347 surf->getCanvas()->clear(SK_ColorRED);
348 // normally a clear+opaque should trigger the discard optimization, but since we have a clip
349 // it should not (we need the previous red pixels).
350 surf->getCanvas()->clipRect(SkRect::MakeWH(128, 256));
351 surf->getCanvas()->drawImage(image, 0, 0);
352
353 // expect to see two rects: blue | red
354 canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
355 }
356
357 DEF_SURFACE_TESTS(simple_snap_image, canvas, 256, 256) {
358 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
359 sk_sp<SkSurface> surf = make(info);
360
361 surf->getCanvas()->clear(SK_ColorRED);
362 sk_sp<SkImage> image = surf->makeImageSnapshot();
363 // expect to see just red
364 canvas->drawImage(std::move(image), 0, 0);
365 }
366
367 // Like simple_snap_image but the surface dies before the image.
368 DEF_SURFACE_TESTS(simple_snap_image2, canvas, 256, 256) {
369 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
370 sk_sp<SkSurface> surf = make(info);
371
372 surf->getCanvas()->clear(SK_ColorRED);
373 sk_sp<SkImage> image = surf->makeImageSnapshot();
374 surf.reset();
375 // expect to see just red
376 canvas->drawImage(std::move(image), 0, 0);
377 }
378
379 DEF_SIMPLE_GM(snap_with_mips, canvas, 80, 75) {
380 auto ct = canvas->imageInfo().colorType() == kUnknown_SkColorType
381 ? kRGBA_8888_SkColorType
382 : canvas->imageInfo().colorType();
383 auto ii = SkImageInfo::Make({32, 32},
384 ct,
385 kPremul_SkAlphaType,
386 canvas->imageInfo().refColorSpace());
387 auto surface = SkSurfaces::Raster(ii);
388
389 auto nextImage = [&](SkColor color) {
390 surface->getCanvas()->clear(color);
391 SkPaint paint;
392 paint.setColor(~color | 0xFF000000);
393 surface->getCanvas()->drawRect(SkRect::MakeLTRB(surface->width() *2/5.f,
394 surface->height()*2/5.f,
395 surface->width() *3/5.f,
396 surface->height()*3/5.f),
397 paint);
398 return surface->makeImageSnapshot()->withDefaultMipmaps();
399 };
400
401 static constexpr int kPad = 8;
402 static const SkSamplingOptions kSampling{SkFilterMode::kLinear, SkMipmapMode::kLinear};
403
404 canvas->save();
405 for (int y = 0; y < 3; ++y) {
406 canvas->save();
407 SkColor kColors[] = {0xFFF0F0F0, SK_ColorBLUE};
408 for (int x = 0; x < 2; ++x) {
409 auto image = nextImage(kColors[x]);
410 canvas->drawImage(image, 0, 0, kSampling);
411 canvas->translate(ii.width() + kPad, 0);
412 }
413 canvas->restore();
414 canvas->translate(0, ii.width() + kPad);
415 canvas->scale(.4f, .4f);
416 }
417 canvas->restore();
418 }
419
420 DEF_SURFACE_TESTS(copy_on_write_savelayer, canvas, 256, 256) {
421 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
422 sk_sp<SkSurface> surf = make(info);
423 surf->getCanvas()->clear(SK_ColorRED);
424 // its important that image survives longer than the next draw, so the surface will see
425 // an outstanding image, and have to decide if it should retain or discard those pixels
426 sk_sp<SkImage> image = surf->makeImageSnapshot();
427
428 // now draw into a full-screen layer. This should (a) trigger a copy-on-write, but it should
429 // not trigger discard, even tho its alpha (SK_ColorBLUE) is opaque, since it is in a layer
430 // with a non-opaque paint.
431 SkPaint paint;
432 paint.setAlphaf(0.25f);
433 surf->getCanvas()->saveLayer({0, 0, 256, 256}, &paint);
434 surf->getCanvas()->clear(SK_ColorBLUE);
435 surf->getCanvas()->restore();
436
437 // expect to see two rects: blue blended on red
438 canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
439 }
440
441 DEF_SURFACE_TESTS(surface_underdraw, canvas, 256, 256) {
442 SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256, nullptr);
443 auto surf = make(info);
444
445 const SkIRect subset = SkIRect::MakeLTRB(180, 0, 256, 256);
446
447 // noisy background
448 {
449 SkPoint pts[] = {{0, 0}, {40, 50}};
450 SkColor4f colors[] = {SkColors::kRed, SkColors::kBlue};
451 auto sh = SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kRepeat}, {}});
452 SkPaint paint;
453 paint.setShader(sh);
454 surf->getCanvas()->drawPaint(paint);
455 }
456
457 // save away the right-hand strip, then clear it
458 sk_sp<SkImage> saveImg = surf->makeImageSnapshot(subset);
459 {
460 SkPaint paint;
461 paint.setBlendMode(SkBlendMode::kClear);
462 surf->getCanvas()->drawRect(SkRect::Make(subset), paint);
463 }
464
465 // draw the "foreground"
466 {
467 SkPaint paint;
468 paint.setColor(SK_ColorGREEN);
469 SkRect r = { 0, 10, 256, 35 };
470 while (r.fBottom < 256) {
471 surf->getCanvas()->drawRect(r, paint);
472 r.offset(0, r.height() * 2);
473 }
474 }
475
476 // apply the "fade"
477 {
478 SkPoint pts[] = {{SkIntToScalar(subset.left()), 0}, {SkIntToScalar(subset.right()), 0}};
479 const SkColor4f colors[] = {{0,0,0,1}, {0,0,0,0}};
480 auto sh = SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kClamp}, {}});
481 SkPaint paint;
482 paint.setShader(sh);
483 paint.setBlendMode(SkBlendMode::kDstIn);
484 surf->getCanvas()->drawRect(SkRect::Make(subset), paint);
485 }
486
487 // restore the original strip, drawing it "under" the current foreground
488 {
489 SkPaint paint;
490 paint.setBlendMode(SkBlendMode::kDstOver);
491 surf->getCanvas()->drawImage(saveImg,
492 SkIntToScalar(subset.left()), SkIntToScalar(subset.top()),
493 SkSamplingOptions(), &paint);
494 }
495
496 // show it on screen
497 surf->draw(canvas, 0, 0);
498 }