ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 11 | #include "GrContext.h" |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 12 | #include "GrContextPriv.h" |
ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 13 | #include "GrGpu.h" |
| 14 | #include "GrTextureStripAtlas.h" |
| 15 | #include "GrTypes.h" |
ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 16 | |
| 17 | // This tests that GrTextureStripAtlas flushes pending IO on the texture it acquires. |
| 18 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrTextureStripAtlasFlush, reporter, ctxInfo) { |
| 19 | GrContext* context = ctxInfo.grContext(); |
| 20 | GrSurfaceDesc desc; |
| 21 | desc.fWidth = 32; |
| 22 | desc.fHeight = 32; |
| 23 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 24 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 25 | sk_sp<GrSurfaceProxy> srcProxy; |
ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 26 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 27 | { |
| 28 | SkAutoTMalloc<uint32_t> pixels(desc.fWidth * desc.fHeight); |
| 29 | memset(pixels.get(), 0xFF, sizeof(uint32_t) * desc.fWidth * desc.fHeight); |
ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 30 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 31 | srcProxy = GrSurfaceProxy::MakeDeferred(*context->caps(), context->textureProvider(), |
| 32 | desc, SkBudgeted::kYes, |
| 33 | pixels.get(), 0); |
| 34 | } |
| 35 | |
| 36 | // Add a pending read to the src texture, and then make it available for reuse. |
| 37 | sk_sp<GrSurfaceProxy> targetProxy; |
| 38 | GrSurface* srcSurface; |
| 39 | |
| 40 | { |
| 41 | GrSurfaceDesc targetDesc = desc; |
| 42 | targetDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 43 | |
| 44 | // We can't use GrSurfaceProxy::Copy bc we may be changing the dst proxy type |
| 45 | sk_sp<GrSurfaceContext> dstContext(context->contextPriv().makeDeferredSurfaceContext( |
| 46 | targetDesc, |
| 47 | SkBackingFit::kExact, |
| 48 | SkBudgeted::kYes)); |
| 49 | REPORTER_ASSERT(reporter, dstContext); |
| 50 | |
| 51 | if (!dstContext->copy(srcProxy.get())) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | targetProxy = sk_ref_sp(dstContext->asDeferredSurface()); |
| 56 | |
| 57 | srcSurface = srcProxy->instantiate(context->textureProvider()); |
| 58 | srcProxy.reset(); |
| 59 | } |
ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 60 | |
| 61 | // Create an atlas with parameters that allow it to reuse the texture. |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 62 | GrTextureStripAtlas* atlas; |
| 63 | |
| 64 | { |
| 65 | GrTextureStripAtlas::Desc atlasDesc; |
| 66 | atlasDesc.fContext = context; |
| 67 | atlasDesc.fConfig = desc.fConfig; |
| 68 | atlasDesc.fWidth = desc.fWidth; |
| 69 | atlasDesc.fHeight = desc.fHeight; |
| 70 | atlasDesc.fRowHeight = 1; |
| 71 | atlas = GrTextureStripAtlas::GetAtlas(atlasDesc); |
| 72 | } |
ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 73 | |
| 74 | // Write to the atlas' texture. |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 75 | int lockedRow; |
| 76 | |
| 77 | { |
| 78 | SkImageInfo info = SkImageInfo::MakeN32(desc.fWidth, desc.fHeight, kPremul_SkAlphaType); |
| 79 | size_t rowBytes = desc.fWidth * GrBytesPerPixel(desc.fConfig); |
| 80 | SkBitmap bitmap; |
| 81 | bitmap.allocPixels(info, rowBytes); |
| 82 | memset(bitmap.getPixels(), 1, rowBytes * desc.fHeight); |
| 83 | lockedRow = atlas->lockRow(bitmap); |
| 84 | } |
ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 85 | |
| 86 | // The atlas' use of its texture shouldn't change which pixels got copied to the target. |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 87 | { |
| 88 | SkAutoTMalloc<uint8_t> actualPixels(sizeof(uint32_t) * desc.fWidth * desc.fHeight); |
| 89 | |
| 90 | // TODO: move readPixels to GrSurfaceProxy? |
| 91 | GrSurface* surf = targetProxy->instantiate(context->textureProvider()); |
| 92 | |
| 93 | bool success = surf->readPixels(0, 0, desc.fWidth, desc.fHeight, |
| 94 | kRGBA_8888_GrPixelConfig, actualPixels.get()); |
| 95 | REPORTER_ASSERT(reporter, success); |
| 96 | |
| 97 | bool good = true; |
| 98 | |
| 99 | const uint8_t* bytes = actualPixels.get(); |
| 100 | for (size_t i = 0; i < sizeof(uint32_t) * desc.fWidth * desc.fHeight; ++i, ++bytes) { |
| 101 | if (0xFF != *bytes) { |
| 102 | good = false; |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | REPORTER_ASSERT(reporter, good); |
| 108 | } |
| 109 | |
| 110 | if (!context->caps()->preferVRAMUseOverFlushes()) { |
| 111 | // This is kindof dodgy since we released it! |
| 112 | REPORTER_ASSERT(reporter, srcSurface == atlas->getTexture()); |
| 113 | } |
| 114 | |
| 115 | atlas->unlockRow(lockedRow); |
ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | #endif |