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" |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 12 | #include "GrDrawContext.h" |
| 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 | |
| 17 | static void test_read_pixels(skiatest::Reporter* reporter, GrContext* context, |
| 18 | GrTexture* rectangleTexture, uint32_t expectedPixelValues[]) { |
| 19 | int pixelCnt = rectangleTexture->width() * rectangleTexture->height(); |
| 20 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 21 | memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt); |
| 22 | bool read = rectangleTexture->readPixels(0, 0, rectangleTexture->width(), |
| 23 | rectangleTexture->height(), kRGBA_8888_GrPixelConfig, |
| 24 | pixels.get()); |
| 25 | if (!read) { |
| 26 | ERRORF(reporter, "Error reading rectangle texture."); |
| 27 | } |
| 28 | for (int i = 0; i < pixelCnt; ++i) { |
| 29 | if (pixels.get()[i] != expectedPixelValues[i]) { |
| 30 | ERRORF(reporter, "Error, rectangle texture pixel value %d should be 0x%08x," |
| 31 | " got 0x%08x.", i, expectedPixelValues[i], pixels.get()[i]); |
| 32 | break; |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | static void test_write_pixels(skiatest::Reporter* reporter, GrContext* context, |
| 38 | GrTexture* rectangleTexture) { |
| 39 | int pixelCnt = rectangleTexture->width() * rectangleTexture->height(); |
| 40 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 41 | for (int y = 0; y < rectangleTexture->width(); ++y) { |
| 42 | for (int x = 0; x < rectangleTexture->height(); ++x) { |
| 43 | pixels.get()[y * rectangleTexture->width() + x] = GrColorPackRGBA(x, y, x + y, x * y); |
| 44 | } |
| 45 | } |
| 46 | bool write = rectangleTexture->writePixels(0, 0, rectangleTexture->width(), |
| 47 | rectangleTexture->height(), kRGBA_8888_GrPixelConfig, |
| 48 | pixels.get()); |
| 49 | if (!write) { |
| 50 | ERRORF(reporter, "Error writing to rectangle texture."); |
| 51 | } |
| 52 | test_read_pixels(reporter, context, rectangleTexture, pixels.get()); |
| 53 | } |
| 54 | |
| 55 | static void test_copy_surface_src(skiatest::Reporter* reporter, GrContext* context, |
| 56 | GrTexture* rectangleTexture, uint32_t expectedPixelValues[]) { |
| 57 | GrSurfaceDesc copyDstDesc; |
| 58 | copyDstDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 59 | copyDstDesc.fWidth = rectangleTexture->width(); |
| 60 | copyDstDesc.fHeight = rectangleTexture->height(); |
| 61 | copyDstDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 62 | SkAutoTUnref<GrTexture> dst(context->textureProvider()->createTexture( |
| 63 | copyDstDesc, SkBudgeted::kYes)); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 64 | context->copySurface(dst, rectangleTexture); |
| 65 | test_read_pixels(reporter, context, dst, expectedPixelValues); |
| 66 | } |
| 67 | |
| 68 | static void test_copy_surface_dst(skiatest::Reporter* reporter, GrContext* context, |
| 69 | GrTexture* rectangleTexture) { |
| 70 | int pixelCnt = rectangleTexture->width() * rectangleTexture->height(); |
| 71 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 72 | for (int y = 0; y < rectangleTexture->width(); ++y) { |
| 73 | for (int x = 0; x < rectangleTexture->height(); ++x) { |
| 74 | pixels.get()[y * rectangleTexture->width() + x] = GrColorPackRGBA(y, x, x * y, x *+ y); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | GrSurfaceDesc copySrcDesc; |
| 79 | copySrcDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 80 | copySrcDesc.fWidth = rectangleTexture->width(); |
| 81 | copySrcDesc.fHeight = rectangleTexture->height(); |
| 82 | copySrcDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 83 | SkAutoTUnref<GrTexture> src(context->textureProvider()->createTexture( |
| 84 | copySrcDesc, SkBudgeted::kYes, pixels.get(), 0)); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 85 | |
| 86 | context->copySurface(rectangleTexture, src); |
| 87 | test_read_pixels(reporter, context, rectangleTexture, pixels.get()); |
| 88 | } |
| 89 | |
| 90 | static void test_clear(skiatest::Reporter* reporter, GrContext* context, |
| 91 | GrTexture* rectangleTexture) { |
| 92 | if (rectangleTexture->asRenderTarget()) { |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 93 | sk_sp<GrDrawContext> dc(context->contextPriv().makeWrappedDrawContext( |
| 94 | sk_ref_sp(rectangleTexture->asRenderTarget()), |
| 95 | nullptr)); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 96 | if (!dc) { |
| 97 | ERRORF(reporter, "Could not get GrDrawContext for rectangle texture."); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | // Clear the whole thing. |
| 102 | GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD); |
| 103 | dc->clear(nullptr, color0, false); |
| 104 | |
| 105 | int w = rectangleTexture->width(); |
| 106 | int h = rectangleTexture->height(); |
| 107 | int pixelCnt = w * h; |
| 108 | SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt); |
| 109 | |
| 110 | // 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] | 111 | uint32_t expectedColor0 = 0; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 112 | uint8_t* expectedBytes0 = SkTCast<uint8_t*>(&expectedColor0); |
| 113 | expectedBytes0[0] = GrColorUnpackR(color0); |
| 114 | expectedBytes0[1] = GrColorUnpackG(color0); |
| 115 | expectedBytes0[2] = GrColorUnpackB(color0); |
| 116 | expectedBytes0[3] = GrColorUnpackA(color0); |
| 117 | for (int i = 0; i < rectangleTexture->width() * rectangleTexture->height(); ++i) { |
| 118 | expectedPixels.get()[i] = expectedColor0; |
| 119 | } |
| 120 | |
| 121 | // Clear the the top to a different color. |
| 122 | GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4); |
| 123 | SkIRect rect = SkIRect::MakeWH(w, h/2); |
| 124 | dc->clear(&rect, color1, false); |
| 125 | |
benjaminwagner | 2a641ee | 2016-01-15 06:21:18 -0800 | [diff] [blame] | 126 | uint32_t expectedColor1 = 0; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 127 | uint8_t* expectedBytes1 = SkTCast<uint8_t*>(&expectedColor1); |
| 128 | expectedBytes1[0] = GrColorUnpackR(color1); |
| 129 | expectedBytes1[1] = GrColorUnpackG(color1); |
| 130 | expectedBytes1[2] = GrColorUnpackB(color1); |
| 131 | expectedBytes1[3] = GrColorUnpackA(color1); |
| 132 | |
| 133 | for (int y = 0; y < h/2; ++y) { |
| 134 | for (int x = 0; x < w; ++x) { |
| 135 | expectedPixels.get()[y * h + x] = expectedColor1; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | test_read_pixels(reporter, context, rectangleTexture, expectedPixels.get()); |
| 140 | } |
| 141 | } |
| 142 | |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 143 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 144 | GrContext* context = ctxInfo.grContext(); |
| 145 | sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext(); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 146 | static const int kWidth = 13; |
| 147 | static const int kHeight = 13; |
| 148 | |
| 149 | GrColor pixels[kWidth * kHeight]; |
| 150 | for (int y = 0; y < kHeight; ++y) { |
| 151 | for (int x = 0; x < kWidth; ++x) { |
| 152 | pixels[y * kWidth + x] = y * kWidth + x; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | for (int origin = 0; origin < 2; ++origin) { |
| 157 | GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA, |
| 158 | GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, |
| 159 | pixels); |
| 160 | |
| 161 | if (!rectTexID) { |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | // Let GrContext know that we messed with the GL context directly. |
| 166 | context->resetContext(); |
| 167 | |
| 168 | // Wrap the rectangle texture ID in a GrTexture |
| 169 | GrGLTextureInfo rectangleInfo; |
| 170 | rectangleInfo.fID = rectTexID; |
| 171 | rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE; |
| 172 | |
| 173 | GrBackendTextureDesc rectangleDesc; |
| 174 | rectangleDesc.fFlags = kRenderTarget_GrBackendTextureFlag; |
| 175 | rectangleDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 176 | rectangleDesc.fWidth = kWidth; |
| 177 | rectangleDesc.fHeight = kHeight; |
| 178 | rectangleDesc.fOrigin = origin ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; |
| 179 | rectangleDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&rectangleInfo); |
| 180 | |
| 181 | GrColor refPixels[kWidth * kHeight]; |
| 182 | bool flipRef = rectangleDesc.fOrigin == kBottomLeft_GrSurfaceOrigin; |
| 183 | for (int y = 0; y < kHeight; ++y) { |
| 184 | for (int x = 0; x < kWidth; ++x) { |
| 185 | int y0 = flipRef ? kHeight - y - 1 : y; |
| 186 | refPixels[y * kWidth + x] = pixels[y0 * kWidth + x]; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | SkAutoTUnref<GrTexture> rectangleTexture( |
| 191 | context->textureProvider()->wrapBackendTexture(rectangleDesc)); |
| 192 | if (!rectangleTexture) { |
| 193 | ERRORF(reporter, "Error wrapping rectangle texture in GrTexture."); |
| 194 | GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID)); |
| 195 | continue; |
| 196 | } |
| 197 | |
| 198 | test_read_pixels(reporter, context, rectangleTexture, refPixels); |
| 199 | |
| 200 | test_copy_surface_src(reporter, context, rectangleTexture, refPixels); |
| 201 | |
| 202 | test_copy_surface_dst(reporter, context, rectangleTexture); |
| 203 | |
| 204 | test_write_pixels(reporter, context, rectangleTexture); |
| 205 | |
| 206 | test_clear(reporter, context, rectangleTexture); |
| 207 | |
| 208 | GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID)); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | #endif |