• 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/SkAlphaType.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorType.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkSamplingOptions.h"
19 #include "include/core/SkSurface.h"
20 #include "include/core/SkTypes.h"
21 #include "include/gpu/GpuTypes.h"
22 #include "include/gpu/GrDirectContext.h"
23 #include "src/gpu/ganesh/GrDirectContextPriv.h"
24 #include "src/gpu/ganesh/GrGpu.h"
25 #include "tests/CtsEnforcement.h"
26 #include "tests/Test.h"
27 
28 #include <cstdint>
29 
30 struct GrContextOptions;
31 
check_read(skiatest::Reporter * reporter,const SkBitmap & bitmap)32 static bool check_read(skiatest::Reporter* reporter, const SkBitmap& bitmap) {
33     bool result = true;
34     for (int x = 0; x < 1000 && result; ++x) {
35         const uint32_t srcPixel = *bitmap.getAddr32(x, 0);
36         if (srcPixel != SK_ColorGREEN) {
37             ERRORF(reporter, "Expected color of Green, but got 0x%08x, at pixel (%d, 0).",
38                    x, srcPixel);
39             result = false;
40         }
41     }
42     return result;
43 }
44 
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(OpsTaskFlushCount,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)45 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(OpsTaskFlushCount,
46                                        reporter,
47                                        ctxInfo,
48                                        CtsEnforcement::kApiLevel_T) {
49     auto context = ctxInfo.directContext();
50     GrGpu* gpu = context->priv().getGpu();
51 
52     SkImageInfo imageInfo = SkImageInfo::Make(1000, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
53 
54     sk_sp<SkSurface> surface1 =
55             SkSurface::MakeRenderTarget(context, skgpu::Budgeted::kYes, imageInfo);
56     if (!surface1) {
57         return;
58     }
59     sk_sp<SkSurface> surface2 =
60             SkSurface::MakeRenderTarget(context, skgpu::Budgeted::kYes, imageInfo);
61     if (!surface2) {
62         return;
63     }
64 
65     SkCanvas* canvas1 = surface1->getCanvas();
66     SkCanvas* canvas2 = surface2->getCanvas();
67 
68     canvas1->clear(SK_ColorRED);
69     canvas2->clear(SK_ColorRED);
70 
71     SkRect srcRect = SkRect::MakeWH(1, 1);
72     SkRect dstRect = SkRect::MakeWH(1, 1);
73     SkPaint paint;
74     paint.setColor(SK_ColorGREEN);
75     canvas1->drawRect(dstRect, paint);
76 
77     for (int i = 0; i < 1000; ++i) {
78         srcRect.fLeft = i;
79         srcRect.fRight = srcRect.fLeft + 1;
80 
81         sk_sp<SkImage> image = surface1->makeImageSnapshot();
82         canvas2->drawImageRect(image.get(), srcRect, dstRect, SkSamplingOptions(), nullptr,
83                                SkCanvas::kStrict_SrcRectConstraint);
84         if (i != 999) {
85             dstRect.fLeft = i+1;
86             dstRect.fRight = dstRect.fLeft + 1;
87             image = surface2->makeImageSnapshot();
88             canvas1->drawImageRect(image.get(), srcRect, dstRect, SkSamplingOptions(), nullptr,
89                                    SkCanvas::kStrict_SrcRectConstraint);
90         }
91     }
92     context->flushAndSubmit();
93 
94     // In total we make 2000 oplists. Our current limit on max oplists between flushes is 100, so we
95     // should do 20 flushes while executing oplists. Additionaly we always do 1 flush at the end of
96     // executing all oplists. So in total we should see 21 flushes here.
97     REPORTER_ASSERT(reporter, gpu->stats()->numSubmitToGpus() == 21);
98 
99     SkBitmap readbackBitmap;
100     readbackBitmap.allocN32Pixels(1000, 1);
101     REPORTER_ASSERT(reporter, surface1->readPixels(readbackBitmap, 0, 0));
102     REPORTER_ASSERT(reporter, check_read(reporter, readbackBitmap));
103 
104     REPORTER_ASSERT(reporter, surface2->readPixels(readbackBitmap, 0, 0));
105     REPORTER_ASSERT(reporter, check_read(reporter, readbackBitmap));
106 }
107