• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 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 "TestUtils.h"
9 
10 #if SK_SUPPORT_GPU
11 
12 #include "GrSurfaceContext.h"
13 #include "GrSurfaceProxy.h"
14 #include "GrTextureProxy.h"
15 
test_read_pixels(skiatest::Reporter * reporter,GrSurfaceContext * srcContext,uint32_t expectedPixelValues[],const char * testName)16 void test_read_pixels(skiatest::Reporter* reporter,
17                       GrSurfaceContext* srcContext, uint32_t expectedPixelValues[],
18                       const char* testName) {
19     int pixelCnt = srcContext->width() * srcContext->height();
20     SkAutoTMalloc<uint32_t> pixels(pixelCnt);
21     memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt);
22 
23     SkImageInfo ii = SkImageInfo::Make(srcContext->width(), srcContext->height(),
24                                        kRGBA_8888_SkColorType, kPremul_SkAlphaType);
25     bool read = srcContext->readPixels(ii, pixels.get(), 0, 0, 0);
26     if (!read) {
27         ERRORF(reporter, "%s: Error reading from texture.", testName);
28     }
29 
30     for (int i = 0; i < pixelCnt; ++i) {
31         if (pixels.get()[i] != expectedPixelValues[i]) {
32             ERRORF(reporter, "%s: Error, pixel value %d should be 0x%08x, got 0x%08x.",
33                    testName, i, expectedPixelValues[i], pixels.get()[i]);
34             break;
35         }
36     }
37 }
38 
test_write_pixels(skiatest::Reporter * reporter,GrSurfaceContext * dstContext,bool expectedToWork,const char * testName)39 void test_write_pixels(skiatest::Reporter* reporter,
40                        GrSurfaceContext* dstContext, bool expectedToWork,
41                        const char* testName) {
42     int pixelCnt = dstContext->width() * dstContext->height();
43     SkAutoTMalloc<uint32_t> pixels(pixelCnt);
44     for (int y = 0; y < dstContext->width(); ++y) {
45         for (int x = 0; x < dstContext->height(); ++x) {
46             pixels.get()[y * dstContext->width() + x] =
47                 GrPremulColor(GrColorPackRGBA(x, y, x + y, 2*y));
48         }
49     }
50 
51     SkImageInfo ii = SkImageInfo::Make(dstContext->width(), dstContext->height(),
52                                        kRGBA_8888_SkColorType, kPremul_SkAlphaType);
53     bool write = dstContext->writePixels(ii, pixels.get(), 0, 0, 0);
54     if (!write) {
55         if (expectedToWork) {
56             ERRORF(reporter, "%s: Error writing to texture.", testName);
57         }
58         return;
59     }
60 
61     if (write && !expectedToWork) {
62         ERRORF(reporter, "%s: writePixels succeeded when it wasn't supposed to.", testName);
63         return;
64     }
65 
66     test_read_pixels(reporter, dstContext, pixels.get(), testName);
67 }
68 
test_copy_from_surface(skiatest::Reporter * reporter,GrContext * context,GrSurfaceProxy * proxy,uint32_t expectedPixelValues[],bool onlyTestRTConfig,const char * testName)69 void test_copy_from_surface(skiatest::Reporter* reporter, GrContext* context,
70                             GrSurfaceProxy* proxy, uint32_t expectedPixelValues[],
71                             bool onlyTestRTConfig, const char* testName) {
72     GrSurfaceDesc copyDstDesc;
73     copyDstDesc.fConfig = kRGBA_8888_GrPixelConfig;
74     copyDstDesc.fWidth = proxy->width();
75     copyDstDesc.fHeight = proxy->height();
76 
77     for (auto flags : { kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag }) {
78         if (kNone_GrSurfaceFlags == flags && onlyTestRTConfig) {
79             continue;
80         }
81 
82         copyDstDesc.fFlags = flags;
83         copyDstDesc.fOrigin = (kNone_GrSurfaceFlags == flags) ? kTopLeft_GrSurfaceOrigin
84                                                               : kBottomLeft_GrSurfaceOrigin;
85 
86         sk_sp<GrSurfaceContext> dstContext(GrSurfaceProxy::TestCopy(context, copyDstDesc, proxy));
87 
88         test_read_pixels(reporter, dstContext.get(), expectedPixelValues, testName);
89     }
90 }
91 
test_copy_to_surface(skiatest::Reporter * reporter,GrResourceProvider * resourceProvider,GrSurfaceContext * dstContext,const char * testName)92 void test_copy_to_surface(skiatest::Reporter* reporter, GrResourceProvider* resourceProvider,
93                           GrSurfaceContext* dstContext, const char* testName) {
94 
95     int pixelCnt = dstContext->width() * dstContext->height();
96     SkAutoTMalloc<uint32_t> pixels(pixelCnt);
97     for (int y = 0; y < dstContext->width(); ++y) {
98         for (int x = 0; x < dstContext->height(); ++x) {
99             pixels.get()[y * dstContext->width() + x] =
100                 GrPremulColor(GrColorPackRGBA(y, x, x * y, 2*y));
101         }
102     }
103 
104     GrSurfaceDesc copySrcDesc;
105     copySrcDesc.fConfig = kRGBA_8888_GrPixelConfig;
106     copySrcDesc.fWidth = dstContext->width();
107     copySrcDesc.fHeight = dstContext->height();
108 
109     for (auto flags : { kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag }) {
110         copySrcDesc.fFlags = flags;
111         copySrcDesc.fOrigin = (kNone_GrSurfaceFlags == flags) ? kTopLeft_GrSurfaceOrigin
112                                                               : kBottomLeft_GrSurfaceOrigin;
113 
114         sk_sp<GrTextureProxy> src(GrSurfaceProxy::MakeDeferred(resourceProvider,
115                                                                copySrcDesc,
116                                                                SkBudgeted::kYes, pixels.get(), 0));
117         dstContext->copy(src.get());
118 
119         test_read_pixels(reporter, dstContext, pixels.get(), testName);
120     }
121 }
122 
123 #endif
124