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
8 #include "GrContext.h"
9 #include "GrContextPriv.h"
10 #include "GrGpu.h"
11 #include "SkCanvas.h"
12 #include "SkSurface.h"
13 #include "Test.h"
14
check_read(skiatest::Reporter * reporter,const SkBitmap & bitmap)15 static bool check_read(skiatest::Reporter* reporter, const SkBitmap& bitmap) {
16 bool result = true;
17 for (int x = 0; x < 1000 && result; ++x) {
18 const uint32_t srcPixel = *bitmap.getAddr32(x, 0);
19 if (srcPixel != SK_ColorGREEN) {
20 ERRORF(reporter, "Expected color of Green, but got 0x%08x, at pixel (%d, 0).",
21 x, srcPixel);
22 result = false;
23 }
24 }
25 return result;
26 }
27
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrOpListFlushCount,reporter,ctxInfo)28 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrOpListFlushCount, reporter, ctxInfo) {
29 GrContext* context = ctxInfo.grContext();
30 GrGpu* gpu = context->contextPriv().getGpu();
31
32 SkImageInfo imageInfo = SkImageInfo::Make(1000, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
33
34 sk_sp<SkSurface> surface1 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, imageInfo);
35 if (!surface1) {
36 return;
37 }
38 sk_sp<SkSurface> surface2 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, imageInfo);
39 if (!surface2) {
40 return;
41 }
42
43 SkCanvas* canvas1 = surface1->getCanvas();
44 SkCanvas* canvas2 = surface2->getCanvas();
45
46 canvas1->clear(SK_ColorRED);
47 canvas2->clear(SK_ColorRED);
48
49 SkIRect srcRect = SkIRect::MakeWH(1, 1);
50 SkRect dstRect = SkRect::MakeWH(1, 1);
51 SkPaint paint;
52 paint.setColor(SK_ColorGREEN);
53 canvas1->drawRect(dstRect, paint);
54
55 for (int i = 0; i < 1000; ++i) {
56 srcRect.fLeft = i;
57 srcRect.fRight = srcRect.fLeft + 1;
58
59 sk_sp<SkImage> image = surface1->makeImageSnapshot();
60 canvas2->drawImageRect(image.get(), srcRect, dstRect, nullptr);
61 if (i != 999) {
62 dstRect.fLeft = i+1;
63 dstRect.fRight = dstRect.fLeft + 1;
64 image = surface2->makeImageSnapshot();
65 canvas1->drawImageRect(image.get(), srcRect, dstRect, nullptr);
66 }
67 }
68 context->flush();
69
70 // In total we make 2000 oplists. Our current limit on max oplists between flushes is 100, so we
71 // should do 20 flushes while executing oplists. Additionaly we always do 1 flush at the end of
72 // executing all oplists. So in total we should see 21 flushes here.
73 REPORTER_ASSERT(reporter, gpu->stats()->numFinishFlushes() == 21);
74
75 SkBitmap readbackBitmap;
76 readbackBitmap.allocN32Pixels(1000, 1);
77 REPORTER_ASSERT(reporter, surface1->readPixels(readbackBitmap, 0, 0));
78 REPORTER_ASSERT(reporter, check_read(reporter, readbackBitmap));
79
80 REPORTER_ASSERT(reporter, surface2->readPixels(readbackBitmap, 0, 0));
81 REPORTER_ASSERT(reporter, check_read(reporter, readbackBitmap));
82 }
83