blob: 1e03b6be112c91a937fe7c1c211d42695faff3c7 [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"
Michael Ludwige8e10752018-10-01 12:42:53 -040016#include "SkBase64.h"
17#include "SkPngEncoder.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}
Timothy Liang760dbc42018-07-17 13:28:20 -0400118
119void fill_pixel_data(int width, int height, GrColor* data) {
120 for (int j = 0; j < height; ++j) {
121 for (int i = 0; i < width; ++i) {
122 unsigned int red = (unsigned int)(256.f * (i / (float)width));
123 unsigned int green = (unsigned int)(256.f * (j / (float)height));
124 data[i + j * width] = GrColorPackRGBA(red - (red >> 8), green - (green >> 8),
125 0xff, 0xff);
126 }
127 }
128}
129
130bool does_full_buffer_contain_correct_color(GrColor* srcBuffer,
131 GrColor* dstBuffer,
132 int width,
133 int height) {
134 GrColor* srcPtr = srcBuffer;
135 GrColor* dstPtr = dstBuffer;
136 for (int j = 0; j < height; ++j) {
137 for (int i = 0; i < width; ++i) {
138 if (srcPtr[i] != dstPtr[i]) {
139 return false;
140 }
141 }
142 srcPtr += width;
143 dstPtr += width;
144 }
145 return true;
146}
Michael Ludwige8e10752018-10-01 12:42:53 -0400147
148bool bitmap_to_base64_data_uri(const SkBitmap& bitmap, SkString* dst) {
149 SkPixmap pm;
150 if (!bitmap.peekPixels(&pm)) {
151 dst->set("peekPixels failed");
152 return false;
153 }
154
155 // We're going to embed this PNG in a data URI, so make it as small as possible
156 SkPngEncoder::Options options;
157 options.fFilterFlags = SkPngEncoder::FilterFlag::kAll;
158 options.fZLibLevel = 9;
159
160 SkDynamicMemoryWStream wStream;
161 if (!SkPngEncoder::Encode(&wStream, pm, options)) {
162 dst->set("SkPngEncoder::Encode failed");
163 return false;
164 }
165
166 sk_sp<SkData> pngData = wStream.detachAsData();
167 size_t len = SkBase64::Encode(pngData->data(), pngData->size(), nullptr);
168
169 // The PNG can be almost arbitrarily large. We don't want to fill our logs with enormous URLs.
170 // Infra says these can be pretty big, as long as we're only outputting them on failure.
171 static const size_t kMaxBase64Length = 1024 * 1024;
172 if (len > kMaxBase64Length) {
173 dst->printf("Encoded image too large (%u bytes)", static_cast<uint32_t>(len));
174 return false;
175 }
176
177 dst->resize(len);
178 SkBase64::Encode(pngData->data(), pngData->size(), dst->writable_str());
179 dst->prepend("data:image/png;base64,");
180 return true;
181}