1 /*
2  * Copyright 2016 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/SkColorSpace.h"
11 #include "include/core/SkImageInfo.h"
12 #include "include/core/SkSurfaceProps.h"
13 #include "include/core/SkTypes.h"
14 #include "include/gpu/GrDirectContext.h"
15 #include "include/private/gpu/ganesh/GrTypesPriv.h"
16 #include "src/gpu/SkBackingFit.h"
17 #include "src/gpu/ganesh/GrDirectContextPriv.h"
18 #include "src/gpu/ganesh/GrPixmap.h"
19 #include "src/gpu/ganesh/GrRenderTargetProxy.h"
20 #include "src/gpu/ganesh/GrTextureProxy.h"
21 #include "src/gpu/ganesh/SurfaceDrawContext.h"
22 #include "tests/CtsEnforcement.h"
23 #include "tests/Test.h"
24 
25 #include <memory>
26 
27 class GrRecordingContext;
28 struct GrContextOptions;
29 
30 static const int kSize = 64;
31 
get_sdc(GrRecordingContext * rContext)32 static std::unique_ptr<skgpu::v1::SurfaceDrawContext> get_sdc(GrRecordingContext* rContext) {
33     return skgpu::v1::SurfaceDrawContext::Make(rContext, GrColorType::kRGBA_8888, nullptr,
34                                                SkBackingFit::kExact, {kSize, kSize},
35                                                SkSurfaceProps(), /*label=*/{});
36 }
37 
check_instantiation_status(skiatest::Reporter * reporter,skgpu::v1::SurfaceDrawContext * sdc,bool wrappedExpectation)38 static void check_instantiation_status(skiatest::Reporter* reporter,
39                                        skgpu::v1::SurfaceDrawContext* sdc,
40                                        bool wrappedExpectation) {
41     REPORTER_ASSERT(reporter, sdc->asRenderTargetProxy()->isInstantiated() == wrappedExpectation);
42 
43     GrTextureProxy* tProxy = sdc->asTextureProxy();
44     REPORTER_ASSERT(reporter, tProxy);
45 
46     REPORTER_ASSERT(reporter, tProxy->isInstantiated() == wrappedExpectation);
47 }
48 
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SurfaceDrawContextTest,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)49 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SurfaceDrawContextTest,
50                                        reporter,
51                                        ctxInfo,
52                                        CtsEnforcement::kApiLevel_T) {
53     auto dContext = ctxInfo.directContext();
54 
55     // Calling instantiate on a SurfaceDrawContext's textureProxy also instantiates the
56     // SurfaceDrawContext
57     {
58         auto sdc = get_sdc(dContext);
59 
60         check_instantiation_status(reporter, sdc.get(), false);
61 
62         GrTextureProxy* tProxy = sdc->asTextureProxy();
63         REPORTER_ASSERT(reporter, tProxy);
64 
65         REPORTER_ASSERT(reporter, tProxy->instantiate(dContext->priv().resourceProvider()));
66 
67         check_instantiation_status(reporter, sdc.get(), true);
68     }
69 
70     // readPixels switches a deferred sdCtx to wrapped
71     {
72         auto sdc = get_sdc(dContext);
73 
74         check_instantiation_status(reporter, sdc.get(), false);
75 
76         SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(kSize, kSize);
77         GrPixmap dstPM = GrPixmap::Allocate(dstInfo);
78 
79         bool result = sdc->readPixels(dContext, dstPM, {0, 0});
80         REPORTER_ASSERT(reporter, result);
81 
82         check_instantiation_status(reporter, sdc.get(), true);
83     }
84 
85     // TODO: in a future world we should be able to add a test that the majority of
86     // SurfaceDrawContext calls do not force the instantiation of a deferred
87     // SurfaceDrawContext
88 }
89