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