• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/core/SkAlphaType.h"
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkCanvas.h"
13 #include "include/core/SkColor.h"
14 #include "include/core/SkColorType.h"
15 #include "include/core/SkImage.h"
16 #include "include/core/SkImageInfo.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkSurface.h"
19 #include "include/core/SkTypes.h"
20 #include "include/gpu/GpuTypes.h"
21 #include "include/gpu/GrDirectContext.h"
22 #include "include/gpu/mock/GrMockTypes.h"
23 #include "src/core/SkImagePriv.h"
24 #include "tests/CtsEnforcement.h"
25 #include "tests/Test.h"
26 #include "tools/gpu/FenceSync.h"
27 
28 class GrRecordingContext;
29 struct GrContextOptions;
30 
31 using namespace sk_gpu_test;
32 
surface_is_expected_color(SkSurface * surf,const SkImageInfo & ii,SkColor color)33 static bool surface_is_expected_color(SkSurface* surf, const SkImageInfo& ii, SkColor color) {
34     SkBitmap bm;
35     bm.allocPixels(ii);
36 
37     surf->readPixels(bm, 0, 0);
38 
39     for (int y = 0; y < bm.height(); ++y) {
40         for (int x = 0; x < bm.width(); ++x) {
41             if (bm.getColor(x, y) != color) {
42                 return false;
43             }
44         }
45     }
46 
47     return true;
48 }
49 
basic_test(skiatest::Reporter * reporter,GrRecordingContext * rContext)50 static void basic_test(skiatest::Reporter* reporter, GrRecordingContext* rContext) {
51     const SkImageInfo ii = SkImageInfo::Make(64, 64, kN32_SkColorType, kPremul_SkAlphaType);
52 
53     SkBitmap bm;
54     bm.allocPixels(ii);
55 
56     SkCanvas bmCanvas(bm);
57     bmCanvas.clear(SK_ColorRED);
58 
59     // We start off with the raster image being all red.
60     sk_sp<SkImage> img = SkMakeImageFromRasterBitmap(bm, kNever_SkCopyPixelsMode);
61 
62     sk_sp<SkSurface> gpuSurface = SkSurface::MakeRenderTarget(rContext, skgpu::Budgeted::kYes, ii);
63     SkCanvas* canvas = gpuSurface->getCanvas();
64 
65     // w/o pinning - the gpu draw always reflects the current state of the underlying bitmap
66     {
67         canvas->drawImage(img, 0, 0);
68         REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorRED));
69 
70         bmCanvas.clear(SK_ColorGREEN);
71 
72         canvas->drawImage(img, 0, 0);
73         REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN));
74     }
75 
76     // w/ pinning - the gpu draw is stuck at the pinned state
77     {
78         SkImage_pinAsTexture(img.get(), rContext); // pin at blue
79 
80         canvas->drawImage(img, 0, 0);
81         REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN));
82 
83         bmCanvas.clear(SK_ColorBLUE);
84 
85         canvas->drawImage(img, 0, 0);
86         REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN));
87 
88         SkImage_unpinAsTexture(img.get(), rContext);
89     }
90 
91     // once unpinned local changes will be picked up
92     {
93         canvas->drawImage(img, 0, 0);
94         REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorBLUE));
95     }
96 }
97 
98 // Deleting the context while there are still pinned images shouldn't result in a crash.
cleanup_test(skiatest::Reporter * reporter)99 static void cleanup_test(skiatest::Reporter* reporter) {
100 
101     const SkImageInfo ii = SkImageInfo::Make(64, 64, kN32_SkColorType, kPremul_SkAlphaType);
102 
103     SkBitmap bm;
104     bm.allocPixels(ii);
105 
106     SkCanvas bmCanvas(bm);
107     bmCanvas.clear(SK_ColorRED);
108 
109     GrMockOptions options;
110     sk_sp<GrDirectContext> mockContext = GrDirectContext::MakeMock(&options);
111 
112     for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
113         GrContextFactory::ContextType ctxType = (GrContextFactory::ContextType) i;
114 
115         {
116             sk_sp<SkImage> img;
117             GrDirectContext* dContext = nullptr;
118 
119             {
120                 GrContextFactory testFactory;
121                 ContextInfo info = testFactory.getContextInfo(ctxType);
122                 dContext = info.directContext();
123                 if (!dContext) {
124                     continue;
125                 }
126 
127                 img = SkMakeImageFromRasterBitmap(bm, kNever_SkCopyPixelsMode);
128                 if (!SkImage_pinAsTexture(img.get(), dContext)) {
129                     continue;
130                 }
131                 // Pinning on a second context should be blocked.
132                 REPORTER_ASSERT(reporter, !SkImage_pinAsTexture(img.get(), mockContext.get()));
133             }
134 
135             // The context used to pin the image is gone at this point!
136             // "context" isn't technically used in this call but it can't be null!
137             // We don't really want to support this use case but it currently happens.
138             SkImage_unpinAsTexture(img.get(), dContext);
139         }
140     }
141 }
142 
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(PinnedImageTest,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)143 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(PinnedImageTest,
144                                        reporter,
145                                        ctxInfo,
146                                        CtsEnforcement::kApiLevel_T) {
147     basic_test(reporter, ctxInfo.directContext());
148     cleanup_test(reporter);
149 }
150