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/GrSwizzle.h"
9
10#include "src/core/SkRasterPipeline.h"
11
12void GrSwizzle::apply(SkRasterPipeline* pipeline) const {
13 SkASSERT(pipeline);
14 switch (fKey) {
15 case GrSwizzle("rgba").asKey():
16 return;
17 case GrSwizzle("bgra").asKey():
18 pipeline->append(SkRasterPipeline::swap_rb);
19 return;
20 case GrSwizzle("aaa1").asKey():
21 pipeline->append(SkRasterPipeline::alpha_to_gray);
22 return;
23 case GrSwizzle("rgb1").asKey():
24 pipeline->append(SkRasterPipeline::force_opaque);
25 return;
26 default: {
27 static_assert(sizeof(uintptr_t) >= 4 * sizeof(char));
28 // Rather than allocate the 4 control bytes on the heap somewhere, just jam them right
29 // into a uintptr_t context.
30 uintptr_t ctx;
31 memcpy(&ctx, this->asString().c_str(), 4 * sizeof(char));
32 pipeline->append(SkRasterPipeline::swizzle, ctx);
33 return;
34 }
35 }
36}
37
38SkString GrSwizzle::asString() const {
39 char swiz[5];
40 uint16_t key = fKey;
41 for (int i = 0; i < 4; ++i) {
42 swiz[i] = IToC(key & 0xfU);
43 key >>= 4;
44 }
45 swiz[4] = '\0';
46 return SkString(swiz);
47}
48