• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 Google LLC.
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 "gm/gm.h"
9 #include "include/core/SkPoint.h"
10 #include "include/core/SkRect.h"
11 #include "include/private/SkColorData.h"
12 #include "src/core/SkCanvasPriv.h"
13 #include "src/gpu/GrRecordingContextPriv.h"
14 #include "src/gpu/GrSwizzle.h"
15 #include "src/gpu/SurfaceFillContext.h"
16 
17 namespace skiagm {
18 
19 // Size of each clear
20 static constexpr int kSize = 64;
21 
22 DEF_SIMPLE_GPU_GM_CAN_FAIL(clear_swizzle, rContext, canvas, errorMsg, 6*kSize, 2*kSize) {
23     if (rContext->abandoned()) {
24         *errorMsg = GM::kErrorMsg_DrawSkippedGpuOnly;
25         return DrawResult::kSkip;
26     }
27 
28     auto sfc = SkCanvasPriv::TopDeviceSurfaceFillContext(canvas);
29     if (!sfc) {
30         *errorMsg = GM::kErrorMsg_DrawSkippedGpuOnly;
31         return DrawResult::kSkip;
32     }
33 
__anon2ce2630c0102(const SkISize dimensions) 34     auto make_offscreen = [&](const SkISize dimensions) {
35         GrSwizzle readSwizzle  = GrSwizzle::Concat(sfc->readSwizzle(), GrSwizzle{"bgra"});
36         GrSwizzle writeSwizzle = GrSwizzle::Concat(sfc->readSwizzle(), GrSwizzle{"bgra"});
37         return rContext->priv().makeSFC(kPremul_SkAlphaType,
38                                         sfc->colorInfo().refColorSpace(),
39                                         dimensions,
40                                         SkBackingFit::kExact,
41                                         sfc->asSurfaceProxy()->backendFormat(),
42                                         /* sample count*/ 1,
43                                         GrMipmapped::kNo,
44                                         sfc->asSurfaceProxy()->isProtected(),
45                                         readSwizzle,
46                                         writeSwizzle,
47                                         kTopLeft_GrSurfaceOrigin,
48                                         SkBudgeted::kYes);
49     };
50 
51     struct {
52         SkIRect rect;
53         SkPMColor4f color;
54     } clears[] {
55             {{    0,     0,   kSize,   kSize}, {1, 0, 0, 1}},
56             {{kSize,     0, 2*kSize,   kSize}, {0, 1, 0, 1}},
57             {{    0, kSize,   kSize, 2*kSize}, {0, 0, 1, 1}},
58             {{kSize, kSize, 2*kSize, 2*kSize}, {1, 0, 1, 1}},
59     };
60 
61     // onscreen for reference
62     for (const auto& c : clears) {
63         sfc->clear(c.rect, c.color);
64     }
65 
66     // partial clear offscreen
67     auto offscreen = make_offscreen({2*kSize, 2*kSize});
68     for (const auto& c : clears) {
69         offscreen->clear(c.rect, c.color);
70     }
71     sfc->blitTexture(offscreen->readSurfaceView(),
72                      SkIRect::MakeSize({2*kSize, 2*kSize}),
73                      SkIPoint{2*kSize, 0});
74 
75     // full offscreen clears
76     for (const auto& c : clears) {
77         offscreen = make_offscreen(c.rect.size());
78         offscreen->clear(SkIRect::MakeSize(c.rect.size()), c.color);
79         sfc->blitTexture(offscreen->readSurfaceView(),
80                          SkIRect::MakeSize(offscreen->dimensions()),
81                          c.rect.topLeft() + SkIPoint{4*kSize, 0});
82     }
83 
84     return DrawResult::kOk;
85 }
86 
87 } // namespace skiagm
88