• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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 #include "SkTypes.h"
9 
10 #include "GrGpu.h"
11 #include "GrContextPriv.h"
12 #include "GrTexture.h"
13 #include "SkConvertPixels.h"
14 #include "Test.h"
15 #include "TestUtils.h"
16 
testing_only_texture_test(skiatest::Reporter * reporter,GrContext * context,GrColorType ct,bool renderTarget,bool doDataUpload,GrMipMapped mipMapped)17 void testing_only_texture_test(skiatest::Reporter* reporter, GrContext* context, GrColorType ct,
18                                bool renderTarget, bool doDataUpload, GrMipMapped mipMapped) {
19     const int kWidth = 16;
20     const int kHeight = 16;
21     SkAutoTMalloc<GrColor> srcBuffer;
22     if (doDataUpload) {
23         srcBuffer.reset(kWidth * kHeight);
24         fill_pixel_data(kWidth, kHeight, srcBuffer.get());
25     }
26     SkAutoTMalloc<GrColor> dstBuffer(kWidth * kHeight);
27 
28     GrGpu* gpu = context->contextPriv().getGpu();
29 
30     GrPixelConfig config = GrColorTypeToPixelConfig(ct, GrSRGBEncoded::kNo);
31     if (!gpu->caps()->isConfigTexturable(config)) {
32         return;
33     }
34     if (gpu->caps()->supportedReadPixelsColorType(config, ct) != ct) {
35         return;
36     }
37 
38     GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(srcBuffer,
39                                                                        kWidth,
40                                                                        kHeight,
41                                                                        ct,
42                                                                        renderTarget,
43                                                                        mipMapped);
44     if (!backendTex.isValid()) {
45         return;
46     }
47 
48     sk_sp<GrTexture> wrappedTex;
49     if (renderTarget) {
50         wrappedTex = gpu->wrapRenderableBackendTexture(
51                 backendTex, 1, GrWrapOwnership::kAdopt_GrWrapOwnership, GrWrapCacheable::kNo);
52     } else {
53         wrappedTex = gpu->wrapBackendTexture(backendTex, GrWrapOwnership::kAdopt_GrWrapOwnership,
54                                              GrWrapCacheable::kNo, kRead_GrIOType);
55     }
56     REPORTER_ASSERT(reporter, wrappedTex);
57 
58     int rowBytes = GrColorTypeBytesPerPixel(ct) * kWidth;
59     bool result = gpu->readPixels(wrappedTex.get(), 0, 0, kWidth,
60                                   kHeight, ct, dstBuffer, rowBytes);
61 
62     if (!doDataUpload) {
63         // createTestingOnlyBackendTexture will fill the texture with 0's if no data is provided, so
64         // we set the expected result likewise.
65         srcBuffer.reset(kWidth * kHeight);
66         memset(srcBuffer, 0, kWidth * kHeight * sizeof(GrColor));
67     }
68     REPORTER_ASSERT(reporter, result);
69     REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer, dstBuffer,
70                                                                      kWidth, kHeight));
71 }
72 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrTestingBackendTextureUploadTest,reporter,ctxInfo)73 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrTestingBackendTextureUploadTest, reporter, ctxInfo) {
74     for (auto colorType: { GrColorType::kRGBA_8888, GrColorType::kBGRA_8888 }) {
75         for (bool renderable: {true, false}) {
76             for (bool doDataUpload: {true, false}) {
77                 testing_only_texture_test(reporter, ctxInfo.grContext(), colorType,
78                                           renderable, doDataUpload, GrMipMapped::kNo);
79 
80                 if (!doDataUpload) {
81                     testing_only_texture_test(reporter, ctxInfo.grContext(), colorType,
82                                               renderable, doDataUpload, GrMipMapped::kYes);
83                 }
84             }
85         }
86     }
87 }
88 
89