1 /* 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 "GrProxyProvider.h" 13 #include "GrSurfaceContext.h" 14 #include "GrSurfaceProxy.h" 15 #include "GrTextureProxy.h" 16 17 void test_read_pixels(skiatest::Reporter* reporter, 18 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 40 void test_write_pixels(skiatest::Reporter* reporter, 41 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 67 test_read_pixels(reporter, dstContext, pixels.get(), testName); 68 } 69 70 void test_copy_from_surface(skiatest::Reporter* reporter, GrContext* context, 71 GrSurfaceProxy* proxy, uint32_t expectedPixelValues[], 72 bool onlyTestRTConfig, const char* testName) { 73 GrSurfaceDesc copyDstDesc; 74 copyDstDesc.fWidth = proxy->width(); 75 copyDstDesc.fHeight = proxy->height(); 76 copyDstDesc.fConfig = kRGBA_8888_GrPixelConfig; 77 78 for (auto flags : { kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag }) { 79 if (kNone_GrSurfaceFlags == flags && onlyTestRTConfig) { 80 continue; 81 } 82 83 copyDstDesc.fFlags = flags; 84 copyDstDesc.fOrigin = (kNone_GrSurfaceFlags == flags) ? kTopLeft_GrSurfaceOrigin 85 : kBottomLeft_GrSurfaceOrigin; 86 87 sk_sp<GrSurfaceContext> dstContext(GrSurfaceProxy::TestCopy(context, copyDstDesc, proxy)); 88 89 test_read_pixels(reporter, dstContext.get(), expectedPixelValues, testName); 90 } 91 } 92 93 void test_copy_to_surface(skiatest::Reporter* reporter, GrProxyProvider* proxyProvider, 94 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; 106 copySrcDesc.fWidth = dstContext->width(); 107 copySrcDesc.fHeight = dstContext->height(); 108 copySrcDesc.fConfig = kRGBA_8888_GrPixelConfig; 109 110 for (auto flags : { kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag }) { 111 copySrcDesc.fFlags = flags; 112 copySrcDesc.fOrigin = (kNone_GrSurfaceFlags == flags) ? kTopLeft_GrSurfaceOrigin 113 : kBottomLeft_GrSurfaceOrigin; 114 115 sk_sp<GrTextureProxy> src = proxyProvider->createTextureProxy(copySrcDesc, SkBudgeted::kYes, 116 pixels.get(), 0); 117 118 dstContext->copy(src.get()); 119 120 test_read_pixels(reporter, dstContext, pixels.get(), testName); 121 } 122 } 123 124 #endif 125