• 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 "src/core/SkAutoPixmapStorage.h"
9 #include "src/gpu/GrContextPriv.h"
10 #include "src/gpu/GrGpu.h"
11 #include "src/gpu/GrSurfaceContext.h"
12 #include "src/gpu/SkGr.h"
13 #include "tests/Test.h"
14 #include "tests/TestUtils.h"
15 
testing_only_texture_test(skiatest::Reporter * reporter,GrContext * context,SkColorType ct,GrRenderable renderable,bool doDataUpload,GrMipMapped mipMapped)16 void testing_only_texture_test(skiatest::Reporter* reporter, GrContext* context, SkColorType ct,
17                                GrRenderable renderable, bool doDataUpload, GrMipMapped mipMapped) {
18 
19     const int kWidth = 16;
20     const int kHeight = 16;
21 
22     SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
23 
24     SkAutoPixmapStorage expectedPixels, actualPixels;
25     expectedPixels.alloc(ii);
26     actualPixels.alloc(ii);
27 
28     const GrCaps* caps = context->priv().caps();
29 
30     GrColorType grCT = SkColorTypeToGrColorType(ct);
31 
32     GrBackendFormat backendFormat = context->defaultBackendFormat(ct, renderable);
33     if (!backendFormat.isValid()) {
34         return;
35     }
36 
37     GrBackendTexture backendTex;
38 
39     if (doDataUpload) {
40         SkASSERT(GrMipMapped::kNo == mipMapped);
41 
42         fill_pixel_data(kWidth, kHeight, expectedPixels.writable_addr32(0, 0));
43 
44         backendTex = context->priv().createBackendTexture(&expectedPixels, 1,
45                                                           renderable, GrProtected::kNo);
46     } else {
47         backendTex = context->createBackendTexture(kWidth, kHeight, ct, SkColors::kTransparent,
48                                                    mipMapped, renderable, GrProtected::kNo);
49 
50         size_t allocSize = SkAutoPixmapStorage::AllocSize(ii, nullptr);
51         // createBackendTexture will fill the texture with 0's if no data is provided, so
52         // we set the expected result likewise.
53         memset(expectedPixels.writable_addr32(0, 0), 0, allocSize);
54     }
55     if (!backendTex.isValid()) {
56         return;
57     }
58     // skbug.com/9165
59     auto supportedRead =
60             caps->supportedReadPixelsColorType(grCT, backendTex.getBackendFormat(), grCT);
61     if (supportedRead.fColorType != grCT) {
62         return;
63     }
64 
65     sk_sp<GrTextureProxy> wrappedProxy;
66     if (GrRenderable::kYes == renderable) {
67         wrappedProxy = context->priv().proxyProvider()->wrapRenderableBackendTexture(
68                 backendTex, kTopLeft_GrSurfaceOrigin, 1, grCT, kAdopt_GrWrapOwnership,
69                 GrWrapCacheable::kNo);
70     } else {
71         wrappedProxy = context->priv().proxyProvider()->wrapBackendTexture(
72                 backendTex, grCT, kTopLeft_GrSurfaceOrigin, kAdopt_GrWrapOwnership,
73                 GrWrapCacheable::kNo, GrIOType::kRW_GrIOType);
74     }
75     REPORTER_ASSERT(reporter, wrappedProxy);
76 
77     auto surfaceContext = context->priv().makeWrappedSurfaceContext(std::move(wrappedProxy), grCT,
78                                                                     kPremul_SkAlphaType);
79     REPORTER_ASSERT(reporter, surfaceContext);
80 
81     bool result = surfaceContext->readPixels({grCT, kPremul_SkAlphaType, nullptr, kWidth, kHeight},
82                                              actualPixels.writable_addr(), actualPixels.rowBytes(),
83                                              {0, 0}, context);
84 
85     REPORTER_ASSERT(reporter, result);
86     REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(expectedPixels.addr32(),
87                                                                      actualPixels.addr32(),
88                                                                      kWidth, kHeight));
89 }
90 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrTestingBackendTextureUploadTest,reporter,ctxInfo)91 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrTestingBackendTextureUploadTest, reporter, ctxInfo) {
92     for (auto colorType: { kRGBA_8888_SkColorType, kBGRA_8888_SkColorType }) {
93         for (auto renderable: { GrRenderable::kYes, GrRenderable::kNo }) {
94             for (bool doDataUpload: {true, false}) {
95                 testing_only_texture_test(reporter, ctxInfo.grContext(), colorType,
96                                           renderable, doDataUpload, GrMipMapped::kNo);
97 
98                 if (!doDataUpload) {
99                     testing_only_texture_test(reporter, ctxInfo.grContext(), colorType,
100                                               renderable, doDataUpload, GrMipMapped::kYes);
101                 }
102             }
103         }
104     }
105 }
106 
107