• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/core/SkBitmap.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkSurface.h"
11 #include "include/gpu/GrDirectContext.h"
12 #include "src/gpu/GrDirectContextPriv.h"
13 #include "src/gpu/GrGpu.h"
14 #include "tests/Test.h"
15 
check_read(skiatest::Reporter * reporter,const SkBitmap & bitmap)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 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OpsTaskFlushCount,reporter,ctxInfo)29 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OpsTaskFlushCount, reporter, ctxInfo) {
30     auto context = ctxInfo.directContext();
31     GrGpu* gpu = context->priv().getGpu();
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 
50     SkRect srcRect = SkRect::MakeWH(1, 1);
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();
61         canvas2->drawImageRect(image.get(), srcRect, dstRect, SkSamplingOptions(), nullptr,
62                                SkCanvas::kStrict_SrcRectConstraint);
63         if (i != 999) {
64             dstRect.fLeft = i+1;
65             dstRect.fRight = dstRect.fLeft + 1;
66             image = surface2->makeImageSnapshot();
67             canvas1->drawImageRect(image.get(), srcRect, dstRect, SkSamplingOptions(), nullptr,
68                                    SkCanvas::kStrict_SrcRectConstraint);
69         }
70     }
71     context->flushAndSubmit();
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.
76     REPORTER_ASSERT(reporter, gpu->stats()->numSubmitToGpus() == 21);
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 }
86