bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 "Test.h" |
| 9 | #if SK_SUPPORT_GPU |
| 10 | #include "GrContext.h" |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 11 | #include "GrContextPriv.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 12 | #include "GrRenderTargetContext.h" |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 13 | #include "gl/GrGLGpu.h" |
| 14 | #include "gl/GrGLUtil.h" |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 15 | #include "gl/GLTestContext.h" |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 16 | |
Robert Phillips | 2b270e5 | 2017-01-27 14:28:29 +0000 | [diff] [blame] | 17 | static void test_read_pixels(skiatest::Reporter* reporter, GrContext* context, |
| 18 | GrSurfaceContext* srcContext, uint32_t expectedPixelValues[]) { |
| 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, "Error reading rectangle texture."); |
| 28 | } |
| 29 | |
| 30 | for (int i = 0; i < pixelCnt; ++i) { |
| 31 | if (pixels.get()[i] != expectedPixelValues[i]) { |
| 32 | ERRORF(reporter, "Error, pixel value %d should be 0x%08x, got 0x%08x.", i, |
| 33 | expectedPixelValues[i], pixels.get()[i]); |
| 34 | break; |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | static void test_write_pixels(skiatest::Reporter* reporter, GrContext* context, |
| 40 | GrSurfaceContext* rectSurfaceContext) { |
| 41 | int pixelCnt = rectSurfaceContext->width() * rectSurfaceContext->height(); |
| 42 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 43 | for (int y = 0; y < rectSurfaceContext->width(); ++y) { |
| 44 | for (int x = 0; x < rectSurfaceContext->height(); ++x) { |
| 45 | pixels.get()[y * rectSurfaceContext->width() + x] = GrColorPackRGBA(x, y, x + y, x * y); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | SkImageInfo ii = SkImageInfo::Make(rectSurfaceContext->width(), rectSurfaceContext->height(), |
| 50 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 51 | bool write = rectSurfaceContext->writePixels(ii, pixels.get(), 0, 0, 0); |
| 52 | if (!write) { |
| 53 | ERRORF(reporter, "Error writing to rectangle texture."); |
| 54 | } |
| 55 | |
| 56 | test_read_pixels(reporter, context, rectSurfaceContext, pixels.get()); |
| 57 | } |
| 58 | |
| 59 | static void test_copy_surface_src(skiatest::Reporter* reporter, GrContext* context, |
| 60 | GrSurfaceProxy* rectProxy, uint32_t expectedPixelValues[]) { |
| 61 | GrSurfaceDesc copyDstDesc; |
| 62 | copyDstDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 63 | copyDstDesc.fWidth = rectProxy->width(); |
| 64 | copyDstDesc.fHeight = rectProxy->height(); |
| 65 | |
| 66 | for (auto flags : {kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag}) { |
| 67 | copyDstDesc.fFlags = flags; |
| 68 | |
| 69 | sk_sp<GrSurfaceContext> dstContext(GrSurfaceProxy::TestCopy(context, copyDstDesc, |
| 70 | rectProxy)); |
| 71 | |
| 72 | test_read_pixels(reporter, context, dstContext.get(), expectedPixelValues); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | static void test_copy_surface_dst(skiatest::Reporter* reporter, GrContext* context, |
| 77 | GrSurfaceContext* rectContext) { |
| 78 | |
| 79 | int pixelCnt = rectContext->width() * rectContext->height(); |
| 80 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 81 | for (int y = 0; y < rectContext->width(); ++y) { |
| 82 | for (int x = 0; x < rectContext->height(); ++x) { |
| 83 | pixels.get()[y * rectContext->width() + x] = GrColorPackRGBA(y, x, x * y, x *+ y); |
| 84 | } |
| 85 | } |
| 86 | for (auto flags : {kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag}) { |
| 87 | GrSurfaceDesc copySrcDesc; |
| 88 | copySrcDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 89 | copySrcDesc.fWidth = rectContext->width(); |
| 90 | copySrcDesc.fHeight = rectContext->height(); |
| 91 | copySrcDesc.fFlags = flags; |
| 92 | |
| 93 | sk_sp<GrSurfaceProxy> src(GrSurfaceProxy::MakeDeferred(*context->caps(), |
| 94 | context->textureProvider(), |
| 95 | copySrcDesc, |
| 96 | SkBudgeted::kYes, pixels.get(), 0)); |
| 97 | rectContext->copy(src.get()); |
| 98 | |
| 99 | test_read_pixels(reporter, context, rectContext, pixels.get()); |
| 100 | } |
| 101 | } |
| 102 | |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 103 | // skbug.com/5932 |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 104 | static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrContext* context, |
| 105 | sk_sp<GrSurfaceProxy> rectProxy, uint32_t expectedPixelValues[]) { |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 106 | sk_sp<GrRenderTargetContext> rtContext( |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 107 | context->makeRenderTargetContext(SkBackingFit::kExact, rectProxy->width(), |
| 108 | rectProxy->height(), rectProxy->config(), |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 109 | nullptr)); |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 110 | for (auto filter : {GrSamplerParams::kNone_FilterMode, |
| 111 | GrSamplerParams::kBilerp_FilterMode, |
| 112 | GrSamplerParams::kMipMap_FilterMode}) { |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 113 | rtContext->clear(nullptr, 0xDDCCBBAA, true); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 114 | sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make( |
| 115 | context, |
| 116 | sk_ref_sp(rectProxy->asTextureProxy()), |
| 117 | nullptr, |
| 118 | SkMatrix::I(), filter)); |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 119 | GrPaint paint; |
| 120 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 121 | paint.addColorFragmentProcessor(std::move(fp)); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 122 | rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I()); |
Robert Phillips | 2b270e5 | 2017-01-27 14:28:29 +0000 | [diff] [blame] | 123 | test_read_pixels(reporter, context, rtContext.get(), expectedPixelValues); |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 127 | static void test_clear(skiatest::Reporter* reporter, GrContext* context, |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 128 | GrSurfaceContext* rectContext) { |
| 129 | if (GrRenderTargetContext* rtc = rectContext->asRenderTargetContext()) { |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 130 | // Clear the whole thing. |
| 131 | GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 132 | rtc->clear(nullptr, color0, false); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 133 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 134 | int w = rtc->width(); |
| 135 | int h = rtc->height(); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 136 | int pixelCnt = w * h; |
| 137 | SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt); |
| 138 | |
| 139 | // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different. |
benjaminwagner | 2a641ee | 2016-01-15 06:21:18 -0800 | [diff] [blame] | 140 | uint32_t expectedColor0 = 0; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 141 | uint8_t* expectedBytes0 = SkTCast<uint8_t*>(&expectedColor0); |
| 142 | expectedBytes0[0] = GrColorUnpackR(color0); |
| 143 | expectedBytes0[1] = GrColorUnpackG(color0); |
| 144 | expectedBytes0[2] = GrColorUnpackB(color0); |
| 145 | expectedBytes0[3] = GrColorUnpackA(color0); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 146 | for (int i = 0; i < rtc->width() * rtc->height(); ++i) { |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 147 | expectedPixels.get()[i] = expectedColor0; |
| 148 | } |
| 149 | |
| 150 | // Clear the the top to a different color. |
| 151 | GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4); |
| 152 | SkIRect rect = SkIRect::MakeWH(w, h/2); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 153 | rtc->clear(&rect, color1, false); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 154 | |
benjaminwagner | 2a641ee | 2016-01-15 06:21:18 -0800 | [diff] [blame] | 155 | uint32_t expectedColor1 = 0; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 156 | uint8_t* expectedBytes1 = SkTCast<uint8_t*>(&expectedColor1); |
| 157 | expectedBytes1[0] = GrColorUnpackR(color1); |
| 158 | expectedBytes1[1] = GrColorUnpackG(color1); |
| 159 | expectedBytes1[2] = GrColorUnpackB(color1); |
| 160 | expectedBytes1[3] = GrColorUnpackA(color1); |
| 161 | |
| 162 | for (int y = 0; y < h/2; ++y) { |
| 163 | for (int x = 0; x < w; ++x) { |
| 164 | expectedPixels.get()[y * h + x] = expectedColor1; |
| 165 | } |
| 166 | } |
| 167 | |
Robert Phillips | 2b270e5 | 2017-01-27 14:28:29 +0000 | [diff] [blame] | 168 | test_read_pixels(reporter, context, rtc, expectedPixels.get()); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 172 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 173 | GrContext* context = ctxInfo.grContext(); |
| 174 | sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext(); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 175 | static const int kWidth = 13; |
| 176 | static const int kHeight = 13; |
| 177 | |
| 178 | GrColor pixels[kWidth * kHeight]; |
| 179 | for (int y = 0; y < kHeight; ++y) { |
| 180 | for (int x = 0; x < kWidth; ++x) { |
| 181 | pixels[y * kWidth + x] = y * kWidth + x; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | for (int origin = 0; origin < 2; ++origin) { |
| 186 | GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA, |
| 187 | GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, |
| 188 | pixels); |
| 189 | |
| 190 | if (!rectTexID) { |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | // Let GrContext know that we messed with the GL context directly. |
| 195 | context->resetContext(); |
| 196 | |
| 197 | // Wrap the rectangle texture ID in a GrTexture |
| 198 | GrGLTextureInfo rectangleInfo; |
| 199 | rectangleInfo.fID = rectTexID; |
| 200 | rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE; |
| 201 | |
| 202 | GrBackendTextureDesc rectangleDesc; |
| 203 | rectangleDesc.fFlags = kRenderTarget_GrBackendTextureFlag; |
| 204 | rectangleDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 205 | rectangleDesc.fWidth = kWidth; |
| 206 | rectangleDesc.fHeight = kHeight; |
| 207 | rectangleDesc.fOrigin = origin ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; |
| 208 | rectangleDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&rectangleInfo); |
| 209 | |
| 210 | GrColor refPixels[kWidth * kHeight]; |
| 211 | bool flipRef = rectangleDesc.fOrigin == kBottomLeft_GrSurfaceOrigin; |
| 212 | for (int y = 0; y < kHeight; ++y) { |
| 213 | for (int x = 0; x < kWidth; ++x) { |
| 214 | int y0 = flipRef ? kHeight - y - 1 : y; |
| 215 | refPixels[y * kWidth + x] = pixels[y0 * kWidth + x]; |
| 216 | } |
| 217 | } |
| 218 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 219 | sk_sp<GrSurfaceProxy> rectProxy; |
| 220 | |
| 221 | { |
| 222 | sk_sp<GrTexture> rectangleTexture( |
| 223 | context->textureProvider()->wrapBackendTexture(rectangleDesc)); |
| 224 | if (!rectangleTexture) { |
| 225 | ERRORF(reporter, "Error wrapping rectangle texture in GrTexture."); |
| 226 | GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID)); |
| 227 | continue; |
| 228 | } |
| 229 | |
| 230 | rectProxy = GrSurfaceProxy::MakeWrapped(std::move(rectangleTexture)); |
| 231 | if (!rectProxy) { |
| 232 | ERRORF(reporter, "Error creating proxy for rectangle texture."); |
| 233 | GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID)); |
| 234 | continue; |
| 235 | } |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 238 | test_basic_draw_as_src(reporter, context, rectProxy, refPixels); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 239 | |
Robert Phillips | 2b270e5 | 2017-01-27 14:28:29 +0000 | [diff] [blame] | 240 | test_copy_surface_src(reporter, context, rectProxy.get(), refPixels); |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 241 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 242 | sk_sp<GrSurfaceContext> rectContext = context->contextPriv().makeWrappedSurfaceContext( |
| 243 | std::move(rectProxy), nullptr); |
| 244 | SkASSERT(rectContext); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 245 | |
Robert Phillips | 2b270e5 | 2017-01-27 14:28:29 +0000 | [diff] [blame] | 246 | test_read_pixels(reporter, context, rectContext.get(), refPixels); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 247 | |
Robert Phillips | 2b270e5 | 2017-01-27 14:28:29 +0000 | [diff] [blame] | 248 | test_copy_surface_dst(reporter, context, rectContext.get()); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 249 | |
Robert Phillips | 2b270e5 | 2017-01-27 14:28:29 +0000 | [diff] [blame] | 250 | test_write_pixels(reporter, context, rectContext.get()); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 251 | |
| 252 | test_clear(reporter, context, rectContext.get()); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 253 | |
| 254 | GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID)); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | #endif |