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