blob: 742b72db160a3107e3ece45607bdd3255d8c27b1 [file] [log] [blame]
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +00001/*
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
mtkleincfa90a12015-05-18 13:44:35 -07008#include "SkCanvas.h"
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +00009#include "SkPicture.h"
10#include "SkPictureRecorder.h"
Florin Malitab00a3602017-07-13 22:34:04 -040011#include "SkPictureShader.h"
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000012#include "SkShader.h"
Florin Malitab00a3602017-07-13 22:34:04 -040013#include "SkSurface.h"
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000014#include "Test.h"
15
halcanary96fcdcc2015-08-27 07:41:13 -070016// Test that attempting to create a picture shader with a nullptr picture or
mtkleincfa90a12015-05-18 13:44:35 -070017// empty picture returns a shader that draws nothing.
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000018DEF_TEST(PictureShader_empty, reporter) {
mtkleincfa90a12015-05-18 13:44:35 -070019 SkPaint paint;
20
21 SkBitmap bitmap;
22 bitmap.allocN32Pixels(1,1);
23
24 SkCanvas canvas(bitmap);
25 canvas.clear(SK_ColorGREEN);
26
reed1a9b9642016-03-13 14:13:58 -070027 paint.setShader(SkShader::MakePictureShader(
28 nullptr, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, nullptr, nullptr));
mtkleincfa90a12015-05-18 13:44:35 -070029
30 canvas.drawRect(SkRect::MakeWH(1,1), paint);
31 REPORTER_ASSERT(reporter, *bitmap.getAddr32(0,0) == SK_ColorGREEN);
32
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000033
34 SkPictureRecorder factory;
halcanary96fcdcc2015-08-27 07:41:13 -070035 factory.beginRecording(0, 0, nullptr, 0);
reedca2622b2016-03-18 07:25:55 -070036 paint.setShader(SkShader::MakePictureShader(factory.finishRecordingAsPicture(),
37 SkShader::kClamp_TileMode,
reed1a9b9642016-03-13 14:13:58 -070038 SkShader::kClamp_TileMode, nullptr, nullptr));
mtkleincfa90a12015-05-18 13:44:35 -070039
40 canvas.drawRect(SkRect::MakeWH(1,1), paint);
41 REPORTER_ASSERT(reporter, *bitmap.getAddr32(0,0) == SK_ColorGREEN);
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000042}
Florin Malitab00a3602017-07-13 22:34:04 -040043
44// Test that the SkPictureShader cache is purged on shader deletion.
45DEF_TEST(PictureShader_caching, reporter) {
46 auto makePicture = [] () {
47 SkPictureRecorder recorder;
48 recorder.beginRecording(100, 100)->drawColor(SK_ColorGREEN);
49 return recorder.finishRecordingAsPicture();
50 };
51
52 sk_sp<SkPicture> picture = makePicture();
53 REPORTER_ASSERT(reporter, picture->unique());
54
55 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(100, 100);
56
57 {
58 SkPaint paint;
59 paint.setShader(SkPictureShader::Make(picture,
60 SkShader::kRepeat_TileMode,
61 SkShader::kRepeat_TileMode, nullptr, nullptr));
62 surface->getCanvas()->drawPaint(paint);
63
64 // We should have about 3 refs by now: local + shader + shader cache.
65 REPORTER_ASSERT(reporter, !picture->unique());
66 }
67
68 // Draw another picture shader to have a chance to purge.
69 {
70 SkPaint paint;
71 paint.setShader(SkPictureShader::Make(makePicture(),
72 SkShader::kRepeat_TileMode,
73 SkShader::kRepeat_TileMode, nullptr, nullptr));
74 surface->getCanvas()->drawPaint(paint);
75
76 }
77
78 // All but the local ref should be gone now.
79 REPORTER_ASSERT(reporter, picture->unique());
80}