1 /*
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 
10 #include "tests/Test.h"
11 
12 using namespace sk_gpu_test;
13 
14 #include "tools/gpu/GrContextFactory.h"
15 
16 #include "include/core/SkCanvas.h"
17 #include "include/core/SkSurface.h"
18 #include "include/gpu/GrDirectContext.h"
19 #include "src/core/SkImagePriv.h"
20 
surface_is_expected_color(SkSurface * surf,const SkImageInfo & ii,SkColor color)21 static 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 
basic_test(skiatest::Reporter * reporter,GrRecordingContext * rContext)38 static void basic_test(skiatest::Reporter* reporter, GrRecordingContext* rContext) {
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(rContext, 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(), rContext); // 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(), rContext);
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.
cleanup_test(skiatest::Reporter * reporter)87 static 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;
102             GrDirectContext* dContext = nullptr;
103 
104             {
105                 GrContextFactory testFactory;
106                 ContextInfo info = testFactory.getContextInfo(ctxType);
107                 dContext = info.directContext();
108                 if (!dContext) {
109                     continue;
110                 }
111 
112                 img = SkMakeImageFromRasterBitmap(bm, kNever_SkCopyPixelsMode);
113                 if (!SkImage_pinAsTexture(img.get(), dContext)) {
114                     continue;
115                 }
116             }
117 
118             // The context 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(), dContext);
122         }
123     }
124 }
125 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PinnedImageTest,reporter,ctxInfo)126 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PinnedImageTest, reporter, ctxInfo) {
127     basic_test(reporter, ctxInfo.directContext());
128     cleanup_test(reporter);
129 }
130