blob: 370a2bd88bba30ac0acc50086db7a71f994f8e7c [file] [log] [blame]
Greg Danield2073452018-12-07 11:20:33 -05001/*
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 Reedac9f0c92020-12-23 10:11:33 -05008#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkCanvas.h"
10#include "include/core/SkSurface.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040011#include "include/gpu/GrDirectContext.h"
Adlai Hollera0693042020-10-14 11:23:11 -040012#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrGpu.h"
14#include "tests/Test.h"
Greg Danield2073452018-12-07 11:20:33 -050015
16static 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 Danielf41b2bd2019-08-22 16:19:24 -040029DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrOpsTaskFlushCount, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040030 auto context = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -050031 GrGpu* gpu = context->priv().getGpu();
Greg Danield2073452018-12-07 11:20:33 -050032
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 Reed039f1362021-01-27 21:21:08 -050050 SkRect srcRect = SkRect::MakeWH(1, 1);
Greg Danield2073452018-12-07 11:20:33 -050051 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 Reed039f1362021-01-27 21:21:08 -050061 canvas2->drawImageRect(image.get(), srcRect, dstRect, SkSamplingOptions(), nullptr,
62 SkCanvas::kStrict_SrcRectConstraint);
Greg Danield2073452018-12-07 11:20:33 -050063 if (i != 999) {
64 dstRect.fLeft = i+1;
65 dstRect.fRight = dstRect.fLeft + 1;
66 image = surface2->makeImageSnapshot();
Mike Reed039f1362021-01-27 21:21:08 -050067 canvas1->drawImageRect(image.get(), srcRect, dstRect, SkSamplingOptions(), nullptr,
68 SkCanvas::kStrict_SrcRectConstraint);
Greg Danield2073452018-12-07 11:20:33 -050069 }
70 }
Greg Daniel0a2464f2020-05-14 15:45:44 -040071 context->flushAndSubmit();
Greg Danield2073452018-12-07 11:20:33 -050072
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 Danielfe159622020-04-10 17:43:51 +000076 REPORTER_ASSERT(reporter, gpu->stats()->numSubmitToGpus() == 21);
Greg Danield2073452018-12-07 11:20:33 -050077
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}