blob: 63a8d948715b2fa30369221066bb9ea68af94fc7 [file] [log] [blame]
Robert Phillips3500b772017-01-27 10:11:42 -05001/*
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"
Mike Reed84dd8572017-03-08 22:21:00 -050014#include "GrTextureProxy.h"
Robert Phillips3500b772017-01-27 10:11:42 -050015
16void test_read_pixels(skiatest::Reporter* reporter, GrContext* context,
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
39void test_write_pixels(skiatest::Reporter* reporter, GrContext* context,
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, context, dstContext, pixels.get(), testName);
67}
68
69void 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
84 sk_sp<GrSurfaceContext> dstContext(GrSurfaceProxy::TestCopy(context, copyDstDesc, proxy));
85
86 test_read_pixels(reporter, context, dstContext.get(), expectedPixelValues, testName);
87 }
88}
89
90void test_copy_to_surface(skiatest::Reporter* reporter, GrContext* context,
91 GrSurfaceContext* dstContext, const char* testName) {
92
93 int pixelCnt = dstContext->width() * dstContext->height();
94 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
95 for (int y = 0; y < dstContext->width(); ++y) {
96 for (int x = 0; x < dstContext->height(); ++x) {
97 pixels.get()[y * dstContext->width() + x] =
98 GrPremulColor(GrColorPackRGBA(y, x, x * y, 2*y));
99 }
100 }
101
102 GrSurfaceDesc copySrcDesc;
103 copySrcDesc.fConfig = kRGBA_8888_GrPixelConfig;
104 copySrcDesc.fWidth = dstContext->width();
105 copySrcDesc.fHeight = dstContext->height();
106
107 for (auto flags : { kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag }) {
108 copySrcDesc.fFlags = flags;
109
Robert Phillips2f493142017-03-02 18:18:38 -0500110 sk_sp<GrTextureProxy> src(GrSurfaceProxy::MakeDeferred(*context->caps(),
Brian Osman32342f02017-03-04 08:12:46 -0500111 context->resourceProvider(),
Robert Phillips3500b772017-01-27 10:11:42 -0500112 copySrcDesc,
113 SkBudgeted::kYes, pixels.get(), 0));
114 dstContext->copy(src.get());
115
116 test_read_pixels(reporter, context, dstContext, pixels.get(), testName);
117 }
118}
119
120#endif