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/ganesh/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::ganesh::SurfaceDrawContext> get_sdc(GrRecordingContext* rContext) {
33 return skgpu::ganesh::SurfaceDrawContext::Make(rContext,
34 GrColorType::kRGBA_8888,
35 nullptr,
36 SkBackingFit::kExact,
37 {kSize, kSize},
38 SkSurfaceProps(),
39 /*label=*/{});
40 }
41
check_instantiation_status(skiatest::Reporter * reporter,skgpu::ganesh::SurfaceDrawContext * sdc,bool wrappedExpectation)42 static void check_instantiation_status(skiatest::Reporter* reporter,
43 skgpu::ganesh::SurfaceDrawContext* sdc,
44 bool wrappedExpectation) {
45 REPORTER_ASSERT(reporter, sdc->asRenderTargetProxy()->isInstantiated() == wrappedExpectation);
46
47 GrTextureProxy* tProxy = sdc->asTextureProxy();
48 REPORTER_ASSERT(reporter, tProxy);
49
50 REPORTER_ASSERT(reporter, tProxy->isInstantiated() == wrappedExpectation);
51 }
52
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SurfaceDrawContextTest,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)53 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SurfaceDrawContextTest,
54 reporter,
55 ctxInfo,
56 CtsEnforcement::kApiLevel_T) {
57 auto dContext = ctxInfo.directContext();
58
59 // Calling instantiate on a SurfaceDrawContext's textureProxy also instantiates the
60 // SurfaceDrawContext
61 {
62 auto sdc = get_sdc(dContext);
63
64 check_instantiation_status(reporter, sdc.get(), false);
65
66 GrTextureProxy* tProxy = sdc->asTextureProxy();
67 REPORTER_ASSERT(reporter, tProxy);
68
69 REPORTER_ASSERT(reporter, tProxy->instantiate(dContext->priv().resourceProvider()));
70
71 check_instantiation_status(reporter, sdc.get(), true);
72 }
73
74 // readPixels switches a deferred sdCtx to wrapped
75 {
76 auto sdc = get_sdc(dContext);
77
78 check_instantiation_status(reporter, sdc.get(), false);
79
80 SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(kSize, kSize);
81 GrPixmap dstPM = GrPixmap::Allocate(dstInfo);
82
83 bool result = sdc->readPixels(dContext, dstPM, {0, 0});
84 REPORTER_ASSERT(reporter, result);
85
86 check_instantiation_status(reporter, sdc.get(), true);
87 }
88
89 // TODO: in a future world we should be able to add a test that the majority of
90 // SurfaceDrawContext calls do not force the instantiation of a deferred
91 // SurfaceDrawContext
92 }
93