blob: 463a8e9e34a7d1739e521679627727fb72eda739 [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"
14#include "GrSurfaceProxy.h"
Mike Reed84dd8572017-03-08 22:21:00 -050015#include "GrTextureProxy.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;
Robert Phillipse78b7252017-04-06 07:59:41 -040084 copyDstDesc.fOrigin = (kNone_GrSurfaceFlags == flags) ? kTopLeft_GrSurfaceOrigin
85 : kBottomLeft_GrSurfaceOrigin;
Robert Phillips3500b772017-01-27 10:11:42 -050086
87 sk_sp<GrSurfaceContext> dstContext(GrSurfaceProxy::TestCopy(context, copyDstDesc, proxy));
88
Robert Phillips26c90e02017-03-14 14:39:29 -040089 test_read_pixels(reporter, dstContext.get(), expectedPixelValues, testName);
Robert Phillips3500b772017-01-27 10:11:42 -050090 }
91}
92
Robert Phillips1afd4cd2018-01-08 13:40:32 -050093void test_copy_to_surface(skiatest::Reporter* reporter, GrProxyProvider* proxyProvider,
Robert Phillips3500b772017-01-27 10:11:42 -050094 GrSurfaceContext* dstContext, const char* testName) {
95
96 int pixelCnt = dstContext->width() * dstContext->height();
97 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
98 for (int y = 0; y < dstContext->width(); ++y) {
99 for (int x = 0; x < dstContext->height(); ++x) {
100 pixels.get()[y * dstContext->width() + x] =
101 GrPremulColor(GrColorPackRGBA(y, x, x * y, 2*y));
102 }
103 }
104
105 GrSurfaceDesc copySrcDesc;
Robert Phillips3500b772017-01-27 10:11:42 -0500106 copySrcDesc.fWidth = dstContext->width();
107 copySrcDesc.fHeight = dstContext->height();
Robert Phillips16d8ec62017-07-27 16:16:25 -0400108 copySrcDesc.fConfig = kRGBA_8888_GrPixelConfig;
Robert Phillips3500b772017-01-27 10:11:42 -0500109
110 for (auto flags : { kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag }) {
111 copySrcDesc.fFlags = flags;
Robert Phillipse78b7252017-04-06 07:59:41 -0400112 copySrcDesc.fOrigin = (kNone_GrSurfaceFlags == flags) ? kTopLeft_GrSurfaceOrigin
113 : kBottomLeft_GrSurfaceOrigin;
Robert Phillips3500b772017-01-27 10:11:42 -0500114
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500115 sk_sp<GrTextureProxy> src = proxyProvider->createTextureProxy(copySrcDesc, SkBudgeted::kYes,
116 pixels.get(), 0);
117
Robert Phillips3500b772017-01-27 10:11:42 -0500118 dstContext->copy(src.get());
119
Robert Phillips26c90e02017-03-14 14:39:29 -0400120 test_read_pixels(reporter, dstContext, pixels.get(), testName);
Robert Phillips3500b772017-01-27 10:11:42 -0500121 }
122}
123
124#endif