blob: d29fdd16f05bb9dadcb860c2bbfaeafb1a8fe5f4 [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 Reedac9f0c92020-12-23 10:11:33 -050016#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/core/SkCanvas.h"
18#include "include/core/SkSurface.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040019#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/core/SkImagePriv.h"
Robert Phillips3ec95732017-09-29 15:10:39 -040021
22static bool surface_is_expected_color(SkSurface* surf, const SkImageInfo& ii, SkColor color) {
23 SkBitmap bm;
24 bm.allocPixels(ii);
25
26 surf->readPixels(bm, 0, 0);
27
28 for (int y = 0; y < bm.height(); ++y) {
29 for (int x = 0; x < bm.width(); ++x) {
30 if (bm.getColor(x, y) != color) {
31 return false;
32 }
33 }
34 }
35
36 return true;
37}
38
Robert Phillips58adb342020-07-23 09:41:57 -040039static void basic_test(skiatest::Reporter* reporter, GrRecordingContext* rContext) {
Robert Phillips3ec95732017-09-29 15:10:39 -040040 const SkImageInfo ii = SkImageInfo::Make(64, 64, kN32_SkColorType, kPremul_SkAlphaType);
41
42 SkBitmap bm;
43 bm.allocPixels(ii);
44
45 SkCanvas bmCanvas(bm);
46 bmCanvas.clear(SK_ColorRED);
47
48 // We start off with the raster image being all red.
49 sk_sp<SkImage> img = SkMakeImageFromRasterBitmap(bm, kNever_SkCopyPixelsMode);
50
Robert Phillips58adb342020-07-23 09:41:57 -040051 sk_sp<SkSurface> gpuSurface = SkSurface::MakeRenderTarget(rContext, SkBudgeted::kYes, ii);
Robert Phillips3ec95732017-09-29 15:10:39 -040052 SkCanvas* canvas = gpuSurface->getCanvas();
53
Brian Salomon3650f6d2021-06-08 13:22:41 -040054 // w/o pinning - the gpu draw always reflects the current state of the underlying bitmap
Robert Phillips3ec95732017-09-29 15:10:39 -040055 {
56 canvas->drawImage(img, 0, 0);
57 REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorRED));
58
59 bmCanvas.clear(SK_ColorGREEN);
60
61 canvas->drawImage(img, 0, 0);
Brian Salomon3650f6d2021-06-08 13:22:41 -040062 REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN));
Robert Phillips3ec95732017-09-29 15:10:39 -040063 }
64
65 // w/ pinning - the gpu draw is stuck at the pinned state
66 {
Robert Phillips58adb342020-07-23 09:41:57 -040067 SkImage_pinAsTexture(img.get(), rContext); // pin at blue
Robert Phillips3ec95732017-09-29 15:10:39 -040068
69 canvas->drawImage(img, 0, 0);
Brian Salomon3650f6d2021-06-08 13:22:41 -040070 REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN));
Robert Phillips3ec95732017-09-29 15:10:39 -040071
Brian Salomon3650f6d2021-06-08 13:22:41 -040072 bmCanvas.clear(SK_ColorBLUE);
Robert Phillips3ec95732017-09-29 15:10:39 -040073
74 canvas->drawImage(img, 0, 0);
Brian Salomon3650f6d2021-06-08 13:22:41 -040075 REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN));
Robert Phillips3ec95732017-09-29 15:10:39 -040076
Robert Phillips58adb342020-07-23 09:41:57 -040077 SkImage_unpinAsTexture(img.get(), rContext);
Robert Phillips3ec95732017-09-29 15:10:39 -040078 }
79
Brian Salomon3650f6d2021-06-08 13:22:41 -040080 // once unpinned local changes will be picked up
Robert Phillips3ec95732017-09-29 15:10:39 -040081 {
82 canvas->drawImage(img, 0, 0);
Brian Salomon3650f6d2021-06-08 13:22:41 -040083 REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorBLUE));
Robert Phillips3ec95732017-09-29 15:10:39 -040084 }
85}
86
87// Deleting the context while there are still pinned images shouldn't result in a crash.
88static void cleanup_test(skiatest::Reporter* reporter) {
89
90 const SkImageInfo ii = SkImageInfo::Make(64, 64, kN32_SkColorType, kPremul_SkAlphaType);
91
92 SkBitmap bm;
93 bm.allocPixels(ii);
94
95 SkCanvas bmCanvas(bm);
96 bmCanvas.clear(SK_ColorRED);
97
Brian Salomon490f1922021-02-01 13:14:37 -050098 GrMockOptions options;
99 sk_sp<GrDirectContext> mockContext = GrDirectContext::MakeMock(&options);
100
Robert Phillips3ec95732017-09-29 15:10:39 -0400101 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
102 GrContextFactory::ContextType ctxType = (GrContextFactory::ContextType) i;
103
104 {
105 sk_sp<SkImage> img;
Robert Phillipse94b4e12020-07-23 13:54:35 -0400106 GrDirectContext* dContext = nullptr;
Robert Phillips3ec95732017-09-29 15:10:39 -0400107
108 {
109 GrContextFactory testFactory;
110 ContextInfo info = testFactory.getContextInfo(ctxType);
Robert Phillipse94b4e12020-07-23 13:54:35 -0400111 dContext = info.directContext();
112 if (!dContext) {
Robert Phillips3ec95732017-09-29 15:10:39 -0400113 continue;
114 }
115
116 img = SkMakeImageFromRasterBitmap(bm, kNever_SkCopyPixelsMode);
Robert Phillipse94b4e12020-07-23 13:54:35 -0400117 if (!SkImage_pinAsTexture(img.get(), dContext)) {
Robert Phillips3ec95732017-09-29 15:10:39 -0400118 continue;
119 }
Brian Salomon490f1922021-02-01 13:14:37 -0500120 // Pinning on a second context should be blocked.
121 REPORTER_ASSERT(reporter, !SkImage_pinAsTexture(img.get(), mockContext.get()));
Robert Phillips3ec95732017-09-29 15:10:39 -0400122 }
123
Robert Phillipse94b4e12020-07-23 13:54:35 -0400124 // The context used to pin the image is gone at this point!
Robert Phillips3ec95732017-09-29 15:10:39 -0400125 // "context" isn't technically used in this call but it can't be null!
126 // We don't really want to support this use case but it currently happens.
Robert Phillipse94b4e12020-07-23 13:54:35 -0400127 SkImage_unpinAsTexture(img.get(), dContext);
Robert Phillips3ec95732017-09-29 15:10:39 -0400128 }
129 }
130}
131
132DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PinnedImageTest, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400133 basic_test(reporter, ctxInfo.directContext());
Robert Phillips3ec95732017-09-29 15:10:39 -0400134 cleanup_test(reporter);
135}