blob: aca7509748c0ea52a5e22bfeb832590a00c9f126 [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
Robert Phillips26c90e02017-03-14 14:39:29 -040016void test_read_pixels(skiatest::Reporter* reporter,
Robert Phillips3500b772017-01-27 10:11:42 -050017 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
Robert Phillips26c90e02017-03-14 14:39:29 -040039void test_write_pixels(skiatest::Reporter* reporter,
Robert Phillips3500b772017-01-27 10:11:42 -050040 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
Robert Phillips26c90e02017-03-14 14:39:29 -040066 test_read_pixels(reporter, dstContext, pixels.get(), testName);
Robert Phillips3500b772017-01-27 10:11:42 -050067}
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;
Robert Phillipse78b7252017-04-06 07:59:41 -040083 copyDstDesc.fOrigin = (kNone_GrSurfaceFlags == flags) ? kTopLeft_GrSurfaceOrigin
84 : kBottomLeft_GrSurfaceOrigin;
Robert Phillips3500b772017-01-27 10:11:42 -050085
86 sk_sp<GrSurfaceContext> dstContext(GrSurfaceProxy::TestCopy(context, copyDstDesc, proxy));
87
Robert Phillips26c90e02017-03-14 14:39:29 -040088 test_read_pixels(reporter, dstContext.get(), expectedPixelValues, testName);
Robert Phillips3500b772017-01-27 10:11:42 -050089 }
90}
91
Robert Phillips26c90e02017-03-14 14:39:29 -040092void test_copy_to_surface(skiatest::Reporter* reporter, GrResourceProvider* resourceProvider,
Robert Phillips3500b772017-01-27 10:11:42 -050093 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;
Robert Phillipse78b7252017-04-06 07:59:41 -0400111 copySrcDesc.fOrigin = (kNone_GrSurfaceFlags == flags) ? kTopLeft_GrSurfaceOrigin
112 : kBottomLeft_GrSurfaceOrigin;
Robert Phillips3500b772017-01-27 10:11:42 -0500113
Robert Phillips26c90e02017-03-14 14:39:29 -0400114 sk_sp<GrTextureProxy> src(GrSurfaceProxy::MakeDeferred(resourceProvider,
Robert Phillips3500b772017-01-27 10:11:42 -0500115 copySrcDesc,
116 SkBudgeted::kYes, pixels.get(), 0));
117 dstContext->copy(src.get());
118
Robert Phillips26c90e02017-03-14 14:39:29 -0400119 test_read_pixels(reporter, dstContext, pixels.get(), testName);
Robert Phillips3500b772017-01-27 10:11:42 -0500120 }
121}
122
123#endif