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 "tests/Test.h"
11 
12 #include "include/gpu/GrDirectContext.h"
13 #include "src/gpu/GrDirectContextPriv.h"
14 #include "src/gpu/GrImageInfo.h"
15 #include "src/gpu/GrSurfaceDrawContext.h"
16 #include "src/gpu/GrTextureProxy.h"
17 
18 static const int kSize = 64;
19 
get_rtc(GrRecordingContext * rContext)20 static std::unique_ptr<GrSurfaceDrawContext> get_rtc(GrRecordingContext* rContext) {
21     return GrSurfaceDrawContext::Make(rContext, GrColorType::kRGBA_8888, nullptr,
22                                       SkBackingFit::kExact, {kSize, kSize}, SkSurfaceProps());
23 }
24 
check_instantiation_status(skiatest::Reporter * reporter,GrSurfaceDrawContext * rtCtx,bool wrappedExpectation)25 static void check_instantiation_status(skiatest::Reporter* reporter,
26                                        GrSurfaceDrawContext* rtCtx,
27                                        bool wrappedExpectation) {
28     REPORTER_ASSERT(reporter, rtCtx->asRenderTargetProxy()->isInstantiated() == wrappedExpectation);
29 
30     GrTextureProxy* tProxy = rtCtx->asTextureProxy();
31     REPORTER_ASSERT(reporter, tProxy);
32 
33     REPORTER_ASSERT(reporter, tProxy->isInstantiated() == wrappedExpectation);
34 }
35 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RenderTargetContextTest,reporter,ctxInfo)36 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RenderTargetContextTest, reporter, ctxInfo) {
37     auto dContext = ctxInfo.directContext();
38 
39     // Calling instantiate on a GrSurfaceDrawContext's textureProxy also instantiates the
40     // GrSurfaceDrawContext
41     {
42         auto rtCtx = get_rtc(dContext);
43 
44         check_instantiation_status(reporter, rtCtx.get(), false);
45 
46         GrTextureProxy* tProxy = rtCtx->asTextureProxy();
47         REPORTER_ASSERT(reporter, tProxy);
48 
49         REPORTER_ASSERT(reporter, tProxy->instantiate(dContext->priv().resourceProvider()));
50 
51         check_instantiation_status(reporter, rtCtx.get(), true);
52     }
53 
54     // readPixels switches a deferred rtCtx to wrapped
55     {
56         auto rtCtx = get_rtc(dContext);
57 
58         check_instantiation_status(reporter, rtCtx.get(), false);
59 
60         SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(kSize, kSize);
61         GrPixmap dstPM = GrPixmap::Allocate(dstInfo);
62 
63         bool result = rtCtx->readPixels(dContext, dstPM, {0, 0});
64         REPORTER_ASSERT(reporter, result);
65 
66         check_instantiation_status(reporter, rtCtx.get(), true);
67     }
68 
69     // TODO: in a future world we should be able to add a test that the majority of
70     // GrSurfaceDrawContext calls do not force the instantiation of a deferred
71     // GrSurfaceDrawContext
72 }
73