| Greg Daniel | d207345 | 2018-12-07 11:20:33 -0500 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2018 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 |  | 
| Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" | 
| Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" | 
|  | 10 | #include "include/core/SkSurface.h" | 
| Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 11 | #include "include/gpu/GrDirectContext.h" | 
| Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrDirectContextPriv.h" | 
| Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrGpu.h" | 
|  | 14 | #include "tests/Test.h" | 
| Greg Daniel | d207345 | 2018-12-07 11:20:33 -0500 | [diff] [blame] | 15 |  | 
|  | 16 | static bool check_read(skiatest::Reporter* reporter, const SkBitmap& bitmap) { | 
|  | 17 | bool result = true; | 
|  | 18 | for (int x = 0; x < 1000 && result; ++x) { | 
|  | 19 | const uint32_t srcPixel = *bitmap.getAddr32(x, 0); | 
|  | 20 | if (srcPixel != SK_ColorGREEN) { | 
|  | 21 | ERRORF(reporter, "Expected color of Green, but got 0x%08x, at pixel (%d, 0).", | 
|  | 22 | x, srcPixel); | 
|  | 23 | result = false; | 
|  | 24 | } | 
|  | 25 | } | 
|  | 26 | return result; | 
|  | 27 | } | 
|  | 28 |  | 
| Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 29 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrOpsTaskFlushCount, reporter, ctxInfo) { | 
| Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 30 | auto context = ctxInfo.directContext(); | 
| Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 31 | GrGpu* gpu = context->priv().getGpu(); | 
| Greg Daniel | d207345 | 2018-12-07 11:20:33 -0500 | [diff] [blame] | 32 |  | 
|  | 33 | SkImageInfo imageInfo = SkImageInfo::Make(1000, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType); | 
|  | 34 |  | 
|  | 35 | sk_sp<SkSurface> surface1 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, imageInfo); | 
|  | 36 | if (!surface1) { | 
|  | 37 | return; | 
|  | 38 | } | 
|  | 39 | sk_sp<SkSurface> surface2 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, imageInfo); | 
|  | 40 | if (!surface2) { | 
|  | 41 | return; | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | SkCanvas* canvas1 = surface1->getCanvas(); | 
|  | 45 | SkCanvas* canvas2 = surface2->getCanvas(); | 
|  | 46 |  | 
|  | 47 | canvas1->clear(SK_ColorRED); | 
|  | 48 | canvas2->clear(SK_ColorRED); | 
|  | 49 |  | 
| Mike Reed | 039f136 | 2021-01-27 21:21:08 -0500 | [diff] [blame] | 50 | SkRect srcRect = SkRect::MakeWH(1, 1); | 
| Greg Daniel | d207345 | 2018-12-07 11:20:33 -0500 | [diff] [blame] | 51 | SkRect dstRect = SkRect::MakeWH(1, 1); | 
|  | 52 | SkPaint paint; | 
|  | 53 | paint.setColor(SK_ColorGREEN); | 
|  | 54 | canvas1->drawRect(dstRect, paint); | 
|  | 55 |  | 
|  | 56 | for (int i = 0; i < 1000; ++i) { | 
|  | 57 | srcRect.fLeft = i; | 
|  | 58 | srcRect.fRight = srcRect.fLeft + 1; | 
|  | 59 |  | 
|  | 60 | sk_sp<SkImage> image = surface1->makeImageSnapshot(); | 
| Mike Reed | 039f136 | 2021-01-27 21:21:08 -0500 | [diff] [blame] | 61 | canvas2->drawImageRect(image.get(), srcRect, dstRect, SkSamplingOptions(), nullptr, | 
|  | 62 | SkCanvas::kStrict_SrcRectConstraint); | 
| Greg Daniel | d207345 | 2018-12-07 11:20:33 -0500 | [diff] [blame] | 63 | if (i != 999) { | 
|  | 64 | dstRect.fLeft = i+1; | 
|  | 65 | dstRect.fRight = dstRect.fLeft + 1; | 
|  | 66 | image = surface2->makeImageSnapshot(); | 
| Mike Reed | 039f136 | 2021-01-27 21:21:08 -0500 | [diff] [blame] | 67 | canvas1->drawImageRect(image.get(), srcRect, dstRect, SkSamplingOptions(), nullptr, | 
|  | 68 | SkCanvas::kStrict_SrcRectConstraint); | 
| Greg Daniel | d207345 | 2018-12-07 11:20:33 -0500 | [diff] [blame] | 69 | } | 
|  | 70 | } | 
| Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 71 | context->flushAndSubmit(); | 
| Greg Daniel | d207345 | 2018-12-07 11:20:33 -0500 | [diff] [blame] | 72 |  | 
|  | 73 | // In total we make 2000 oplists. Our current limit on max oplists between flushes is 100, so we | 
|  | 74 | // should do 20 flushes while executing oplists. Additionaly we always do 1 flush at the end of | 
|  | 75 | // executing all oplists. So in total we should see 21 flushes here. | 
| Greg Daniel | fe15962 | 2020-04-10 17:43:51 +0000 | [diff] [blame] | 76 | REPORTER_ASSERT(reporter, gpu->stats()->numSubmitToGpus() == 21); | 
| Greg Daniel | d207345 | 2018-12-07 11:20:33 -0500 | [diff] [blame] | 77 |  | 
|  | 78 | SkBitmap readbackBitmap; | 
|  | 79 | readbackBitmap.allocN32Pixels(1000, 1); | 
|  | 80 | REPORTER_ASSERT(reporter, surface1->readPixels(readbackBitmap, 0, 0)); | 
|  | 81 | REPORTER_ASSERT(reporter, check_read(reporter, readbackBitmap)); | 
|  | 82 |  | 
|  | 83 | REPORTER_ASSERT(reporter, surface2->readPixels(readbackBitmap, 0, 0)); | 
|  | 84 | REPORTER_ASSERT(reporter, check_read(reporter, readbackBitmap)); | 
|  | 85 | } |