blob: 32697d253428445d9dbff8b5dd215ab9820b8bd4 [file] [log] [blame]
Robert Phillips3ec95732017-09-29 15:10:39 -04001/*
2 * Copyright 2017 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// This is a GPU-backend specific test.
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "tests/Test.h"
Robert Phillips3ec95732017-09-29 15:10:39 -040011
Robert Phillips3ec95732017-09-29 15:10:39 -040012using namespace sk_gpu_test;
13
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "tools/gpu/GrContextFactory.h"
Robert Phillips3ec95732017-09-29 15:10:39 -040015
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkCanvas.h"
17#include "include/core/SkSurface.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040018#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkImagePriv.h"
Robert Phillips3ec95732017-09-29 15:10:39 -040020
21static bool surface_is_expected_color(SkSurface* surf, const SkImageInfo& ii, SkColor color) {
22 SkBitmap bm;
23 bm.allocPixels(ii);
24
25 surf->readPixels(bm, 0, 0);
26
27 for (int y = 0; y < bm.height(); ++y) {
28 for (int x = 0; x < bm.width(); ++x) {
29 if (bm.getColor(x, y) != color) {
30 return false;
31 }
32 }
33 }
34
35 return true;
36}
37
38static void basic_test(skiatest::Reporter* reporter, GrContext* context) {
39 const SkImageInfo ii = SkImageInfo::Make(64, 64, kN32_SkColorType, kPremul_SkAlphaType);
40
41 SkBitmap bm;
42 bm.allocPixels(ii);
43
44 SkCanvas bmCanvas(bm);
45 bmCanvas.clear(SK_ColorRED);
46
47 // We start off with the raster image being all red.
48 sk_sp<SkImage> img = SkMakeImageFromRasterBitmap(bm, kNever_SkCopyPixelsMode);
49
50 sk_sp<SkSurface> gpuSurface = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii);
51 SkCanvas* canvas = gpuSurface->getCanvas();
52
53 // w/o pinning - the gpu draw always reflects the current state of the underlying bitmap
54 {
55 canvas->drawImage(img, 0, 0);
56 REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorRED));
57
58 bmCanvas.clear(SK_ColorGREEN);
59
60 canvas->drawImage(img, 0, 0);
61 REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN));
62 }
63
64 // w/ pinning - the gpu draw is stuck at the pinned state
65 {
66 SkImage_pinAsTexture(img.get(), context); // pin at blue
67
68 canvas->drawImage(img, 0, 0);
69 REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN));
70
71 bmCanvas.clear(SK_ColorBLUE);
72
73 canvas->drawImage(img, 0, 0);
74 REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN));
75
76 SkImage_unpinAsTexture(img.get(), context);
77 }
78
79 // once unpinned local changes will be picked up
80 {
81 canvas->drawImage(img, 0, 0);
82 REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorBLUE));
83 }
84}
85
86// Deleting the context while there are still pinned images shouldn't result in a crash.
87static void cleanup_test(skiatest::Reporter* reporter) {
88
89 const SkImageInfo ii = SkImageInfo::Make(64, 64, kN32_SkColorType, kPremul_SkAlphaType);
90
91 SkBitmap bm;
92 bm.allocPixels(ii);
93
94 SkCanvas bmCanvas(bm);
95 bmCanvas.clear(SK_ColorRED);
96
97 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
98 GrContextFactory::ContextType ctxType = (GrContextFactory::ContextType) i;
99
100 {
101 sk_sp<SkImage> img;
Robert Phillips6d344c32020-07-06 10:56:46 -0400102 GrDirectContext* context = nullptr;
Robert Phillips3ec95732017-09-29 15:10:39 -0400103
104 {
105 GrContextFactory testFactory;
106 ContextInfo info = testFactory.getContextInfo(ctxType);
Robert Phillips6d344c32020-07-06 10:56:46 -0400107 context = info.directContext();
Robert Phillips3ec95732017-09-29 15:10:39 -0400108 if (!context) {
109 continue;
110 }
111
112 img = SkMakeImageFromRasterBitmap(bm, kNever_SkCopyPixelsMode);
113 if (!SkImage_pinAsTexture(img.get(), context)) {
114 continue;
115 }
116 }
117
118 // The GrContext used to pin the image is gone at this point!
119 // "context" isn't technically used in this call but it can't be null!
120 // We don't really want to support this use case but it currently happens.
121 SkImage_unpinAsTexture(img.get(), context);
122 }
123 }
124}
125
126DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PinnedImageTest, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400127 basic_test(reporter, ctxInfo.directContext());
Robert Phillips3ec95732017-09-29 15:10:39 -0400128 cleanup_test(reporter);
129}