blob: 3bef983b11f5db00c6dfadc4ccd8786dd78401f3 [file] [log] [blame]
Brian Salomon217522c2019-06-11 15:55:30 -04001/*
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
Greg Danielf91aeb22019-06-18 09:58:02 -04008#include "src/gpu/GrSwizzle.h"
Brian Salomon217522c2019-06-11 15:55:30 -04009
Greg Daniel2c19e7f2019-06-18 13:29:21 -040010#include "src/core/SkRasterPipeline.h"
11
Brian Salomon217522c2019-06-11 15:55:30 -040012void GrSwizzle::apply(SkRasterPipeline* pipeline) const {
13 SkASSERT(pipeline);
14 switch (fKey) {
Brian Salomonf30b1c12019-06-20 12:25:02 -040015 case GrSwizzle("rgba").asKey():
Brian Salomon217522c2019-06-11 15:55:30 -040016 return;
Brian Salomonf30b1c12019-06-20 12:25:02 -040017 case GrSwizzle("bgra").asKey():
Brian Salomon217522c2019-06-11 15:55:30 -040018 pipeline->append(SkRasterPipeline::swap_rb);
19 return;
Brian Salomonf30b1c12019-06-20 12:25:02 -040020 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;
Brian Salomon217522c2019-06-11 15:55:30 -040026 default: {
Brian Salomon4dea72a2019-12-18 10:43:10 -050027 static_assert(sizeof(uintptr_t) >= 4 * sizeof(char));
Brian Salomon217522c2019-06-11 15:55:30 -040028 // 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;
Greg Daniel369ee6b2019-12-02 15:30:02 -050031 memcpy(&ctx, this->asString().c_str(), 4 * sizeof(char));
Brian Salomon217522c2019-06-11 15:55:30 -040032 pipeline->append(SkRasterPipeline::swizzle, ctx);
33 return;
34 }
35 }
36}
Greg Daniel369ee6b2019-12-02 15:30:02 -050037
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}