xref: /aosp_15_r20/external/skia/src/gpu/Swizzle.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2019 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 "src/gpu/Swizzle.h"
9 
10 #include "src/core/SkRasterPipeline.h"
11 #include "src/core/SkRasterPipelineOpList.h"
12 
13 #include <cstring>
14 
15 namespace skgpu {
16 
apply(SkRasterPipeline * pipeline) const17 void Swizzle::apply(SkRasterPipeline* pipeline) const {
18     SkASSERT(pipeline);
19     switch (fKey) {
20         case Swizzle("rgba").asKey():
21             return;
22         case Swizzle("bgra").asKey():
23             pipeline->append(SkRasterPipelineOp::swap_rb);
24             return;
25         case Swizzle("aaa1").asKey():
26             pipeline->append(SkRasterPipelineOp::alpha_to_gray);
27             return;
28         case Swizzle("rgb1").asKey():
29             pipeline->append(SkRasterPipelineOp::force_opaque);
30             return;
31         case Swizzle("a001").asKey():
32             pipeline->append(SkRasterPipelineOp::alpha_to_red);
33             return;
34         default: {
35             static_assert(sizeof(uintptr_t) >= 4 * sizeof(char));
36             // Rather than allocate the 4 control bytes on the heap somewhere, just jam them right
37             // into a uintptr_t context.
38             uintptr_t ctx = {};
39             memcpy(&ctx, this->asString().c_str(), 4 * sizeof(char));
40             pipeline->append(SkRasterPipelineOp::swizzle, ctx);
41             return;
42         }
43     }
44 }
45 
asString() const46 SkString Swizzle::asString() const {
47     char swiz[5];
48     uint16_t key = fKey;
49     for (int i = 0; i < 4; ++i) {
50         swiz[i] = IToC(key & 0xfU);
51         key >>= 4;
52     }
53     swiz[4] = '\0';
54     return SkString(swiz);
55 }
56 
57 } // namespace skgpu
58