blob: 1b146c2cca0443f4365802af1e32d7292fb71513 [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
Robert Phillips0bd24dc2018-01-16 08:06:32 -050012#include "GrProxyProvider.h"
Robert Phillips3500b772017-01-27 10:11:42 -050013#include "GrSurfaceContext.h"
Brian Salomon58389b92018-03-07 13:01:25 -050014#include "GrSurfaceContextPriv.h"
Robert Phillips3500b772017-01-27 10:11:42 -050015#include "GrSurfaceProxy.h"
Mike Reed84dd8572017-03-08 22:21:00 -050016#include "GrTextureProxy.h"
Brian Salomon58389b92018-03-07 13:01:25 -050017#include "ProxyUtils.h"
Robert Phillips3500b772017-01-27 10:11:42 -050018
Robert Phillips26c90e02017-03-14 14:39:29 -040019void test_read_pixels(skiatest::Reporter* reporter,
Robert Phillips3500b772017-01-27 10:11:42 -050020 GrSurfaceContext* srcContext, uint32_t expectedPixelValues[],
21 const char* testName) {
22 int pixelCnt = srcContext->width() * srcContext->height();
23 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
24 memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt);
25
26 SkImageInfo ii = SkImageInfo::Make(srcContext->width(), srcContext->height(),
27 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
28 bool read = srcContext->readPixels(ii, pixels.get(), 0, 0, 0);
29 if (!read) {
30 ERRORF(reporter, "%s: Error reading from texture.", testName);
31 }
32
33 for (int i = 0; i < pixelCnt; ++i) {
34 if (pixels.get()[i] != expectedPixelValues[i]) {
35 ERRORF(reporter, "%s: Error, pixel value %d should be 0x%08x, got 0x%08x.",
36 testName, i, expectedPixelValues[i], pixels.get()[i]);
37 break;
38 }
39 }
40}
41
Robert Phillips26c90e02017-03-14 14:39:29 -040042void test_write_pixels(skiatest::Reporter* reporter,
Robert Phillips3500b772017-01-27 10:11:42 -050043 GrSurfaceContext* dstContext, bool expectedToWork,
44 const char* testName) {
45 int pixelCnt = dstContext->width() * dstContext->height();
46 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
47 for (int y = 0; y < dstContext->width(); ++y) {
48 for (int x = 0; x < dstContext->height(); ++x) {
49 pixels.get()[y * dstContext->width() + x] =
50 GrPremulColor(GrColorPackRGBA(x, y, x + y, 2*y));
51 }
52 }
53
54 SkImageInfo ii = SkImageInfo::Make(dstContext->width(), dstContext->height(),
55 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
56 bool write = dstContext->writePixels(ii, pixels.get(), 0, 0, 0);
57 if (!write) {
58 if (expectedToWork) {
59 ERRORF(reporter, "%s: Error writing to texture.", testName);
60 }
61 return;
62 }
63
64 if (write && !expectedToWork) {
65 ERRORF(reporter, "%s: writePixels succeeded when it wasn't supposed to.", testName);
66 return;
67 }
68
Robert Phillips26c90e02017-03-14 14:39:29 -040069 test_read_pixels(reporter, dstContext, pixels.get(), testName);
Robert Phillips3500b772017-01-27 10:11:42 -050070}
71
72void test_copy_from_surface(skiatest::Reporter* reporter, GrContext* context,
73 GrSurfaceProxy* proxy, uint32_t expectedPixelValues[],
74 bool onlyTestRTConfig, const char* testName) {
75 GrSurfaceDesc copyDstDesc;
Robert Phillips3500b772017-01-27 10:11:42 -050076 copyDstDesc.fWidth = proxy->width();
77 copyDstDesc.fHeight = proxy->height();
Robert Phillips16d8ec62017-07-27 16:16:25 -040078 copyDstDesc.fConfig = kRGBA_8888_GrPixelConfig;
Robert Phillips3500b772017-01-27 10:11:42 -050079
80 for (auto flags : { kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag }) {
81 if (kNone_GrSurfaceFlags == flags && onlyTestRTConfig) {
82 continue;
83 }
84
85 copyDstDesc.fFlags = flags;
Brian Salomon2a4f9832018-03-03 22:43:43 -050086 auto origin = (kNone_GrSurfaceFlags == flags) ? kTopLeft_GrSurfaceOrigin
87 : kBottomLeft_GrSurfaceOrigin;
Robert Phillips3500b772017-01-27 10:11:42 -050088
Brian Salomon2a4f9832018-03-03 22:43:43 -050089 sk_sp<GrSurfaceContext> dstContext(
90 GrSurfaceProxy::TestCopy(context, copyDstDesc, origin, proxy));
Robert Phillips3500b772017-01-27 10:11:42 -050091
Robert Phillips26c90e02017-03-14 14:39:29 -040092 test_read_pixels(reporter, dstContext.get(), expectedPixelValues, testName);
Robert Phillips3500b772017-01-27 10:11:42 -050093 }
94}
95
Robert Phillips1afd4cd2018-01-08 13:40:32 -050096void test_copy_to_surface(skiatest::Reporter* reporter, GrProxyProvider* proxyProvider,
Robert Phillips3500b772017-01-27 10:11:42 -050097 GrSurfaceContext* dstContext, const char* testName) {
98
99 int pixelCnt = dstContext->width() * dstContext->height();
100 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
101 for (int y = 0; y < dstContext->width(); ++y) {
102 for (int x = 0; x < dstContext->height(); ++x) {
103 pixels.get()[y * dstContext->width() + x] =
104 GrPremulColor(GrColorPackRGBA(y, x, x * y, 2*y));
105 }
106 }
107
Brian Salomon58389b92018-03-07 13:01:25 -0500108 for (auto isRT : {false, true}) {
109 for (auto origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
110 auto src = sk_gpu_test::MakeTextureProxyFromData(
111 dstContext->surfPriv().getContext(), isRT, dstContext->width(),
112 dstContext->height(), GrColorType::kRGBA_8888, origin, pixels.get(), 0);
113 dstContext->copy(src.get());
114 test_read_pixels(reporter, dstContext, pixels.get(), testName);
115 }
Robert Phillips3500b772017-01-27 10:11:42 -0500116 }
117}
118
119#endif