blob: 44a78c51fbc402bda09e254f87aac32e354f6fdd [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
Robert Phillips0bd24dc2018-01-16 08:06:32 -050010#include "GrProxyProvider.h"
Robert Phillips3500b772017-01-27 10:11:42 -050011#include "GrSurfaceContext.h"
Brian Salomon58389b92018-03-07 13:01:25 -050012#include "GrSurfaceContextPriv.h"
Robert Phillips3500b772017-01-27 10:11:42 -050013#include "GrSurfaceProxy.h"
Mike Reed84dd8572017-03-08 22:21:00 -050014#include "GrTextureProxy.h"
Brian Salomon58389b92018-03-07 13:01:25 -050015#include "ProxyUtils.h"
Robert Phillips3500b772017-01-27 10:11:42 -050016
Robert Phillips26c90e02017-03-14 14:39:29 -040017void test_read_pixels(skiatest::Reporter* reporter,
Robert Phillips3500b772017-01-27 10:11:42 -050018 GrSurfaceContext* srcContext, uint32_t expectedPixelValues[],
19 const char* testName) {
20 int pixelCnt = srcContext->width() * srcContext->height();
21 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
22 memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt);
23
24 SkImageInfo ii = SkImageInfo::Make(srcContext->width(), srcContext->height(),
25 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
26 bool read = srcContext->readPixels(ii, pixels.get(), 0, 0, 0);
27 if (!read) {
28 ERRORF(reporter, "%s: Error reading from texture.", testName);
29 }
30
31 for (int i = 0; i < pixelCnt; ++i) {
32 if (pixels.get()[i] != expectedPixelValues[i]) {
33 ERRORF(reporter, "%s: Error, pixel value %d should be 0x%08x, got 0x%08x.",
34 testName, i, expectedPixelValues[i], pixels.get()[i]);
35 break;
36 }
37 }
38}
39
Robert Phillips26c90e02017-03-14 14:39:29 -040040void test_write_pixels(skiatest::Reporter* reporter,
Robert Phillips3500b772017-01-27 10:11:42 -050041 GrSurfaceContext* dstContext, bool expectedToWork,
42 const char* testName) {
43 int pixelCnt = dstContext->width() * dstContext->height();
44 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
45 for (int y = 0; y < dstContext->width(); ++y) {
46 for (int x = 0; x < dstContext->height(); ++x) {
47 pixels.get()[y * dstContext->width() + x] =
48 GrPremulColor(GrColorPackRGBA(x, y, x + y, 2*y));
49 }
50 }
51
52 SkImageInfo ii = SkImageInfo::Make(dstContext->width(), dstContext->height(),
53 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
54 bool write = dstContext->writePixels(ii, pixels.get(), 0, 0, 0);
55 if (!write) {
56 if (expectedToWork) {
57 ERRORF(reporter, "%s: Error writing to texture.", testName);
58 }
59 return;
60 }
61
62 if (write && !expectedToWork) {
63 ERRORF(reporter, "%s: writePixels succeeded when it wasn't supposed to.", testName);
64 return;
65 }
66
Robert Phillips26c90e02017-03-14 14:39:29 -040067 test_read_pixels(reporter, dstContext, pixels.get(), testName);
Robert Phillips3500b772017-01-27 10:11:42 -050068}
69
70void test_copy_from_surface(skiatest::Reporter* reporter, GrContext* context,
71 GrSurfaceProxy* proxy, uint32_t expectedPixelValues[],
72 bool onlyTestRTConfig, const char* testName) {
73 GrSurfaceDesc copyDstDesc;
Robert Phillips3500b772017-01-27 10:11:42 -050074 copyDstDesc.fWidth = proxy->width();
75 copyDstDesc.fHeight = proxy->height();
Robert Phillips16d8ec62017-07-27 16:16:25 -040076 copyDstDesc.fConfig = kRGBA_8888_GrPixelConfig;
Robert Phillips3500b772017-01-27 10:11:42 -050077
78 for (auto flags : { kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag }) {
79 if (kNone_GrSurfaceFlags == flags && onlyTestRTConfig) {
80 continue;
81 }
82
83 copyDstDesc.fFlags = flags;
Brian Salomon2a4f9832018-03-03 22:43:43 -050084 auto origin = (kNone_GrSurfaceFlags == flags) ? kTopLeft_GrSurfaceOrigin
85 : kBottomLeft_GrSurfaceOrigin;
Robert Phillips3500b772017-01-27 10:11:42 -050086
Brian Salomon2a4f9832018-03-03 22:43:43 -050087 sk_sp<GrSurfaceContext> dstContext(
88 GrSurfaceProxy::TestCopy(context, copyDstDesc, origin, proxy));
Robert Phillips3500b772017-01-27 10:11:42 -050089
Robert Phillips26c90e02017-03-14 14:39:29 -040090 test_read_pixels(reporter, dstContext.get(), expectedPixelValues, testName);
Robert Phillips3500b772017-01-27 10:11:42 -050091 }
92}
93
Robert Phillips1afd4cd2018-01-08 13:40:32 -050094void test_copy_to_surface(skiatest::Reporter* reporter, GrProxyProvider* proxyProvider,
Robert Phillips3500b772017-01-27 10:11:42 -050095 GrSurfaceContext* dstContext, const char* testName) {
96
97 int pixelCnt = dstContext->width() * dstContext->height();
98 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
99 for (int y = 0; y < dstContext->width(); ++y) {
100 for (int x = 0; x < dstContext->height(); ++x) {
101 pixels.get()[y * dstContext->width() + x] =
102 GrPremulColor(GrColorPackRGBA(y, x, x * y, 2*y));
103 }
104 }
105
Brian Salomon58389b92018-03-07 13:01:25 -0500106 for (auto isRT : {false, true}) {
107 for (auto origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
108 auto src = sk_gpu_test::MakeTextureProxyFromData(
109 dstContext->surfPriv().getContext(), isRT, dstContext->width(),
110 dstContext->height(), GrColorType::kRGBA_8888, origin, pixels.get(), 0);
111 dstContext->copy(src.get());
112 test_read_pixels(reporter, dstContext, pixels.get(), testName);
113 }
Robert Phillips3500b772017-01-27 10:11:42 -0500114 }
115}