Flutter Engine
The Flutter Engine
SkEmbossMask.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2006 The Android Open Source Project
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
9
10#include "include/core/SkRect.h"
15#include "src/base/SkMathPriv.h"
16#include "src/core/SkMask.h"
17
18#include <algorithm>
19#include <cstddef>
20#include <cstdint>
21
22static inline int nonzero_to_one(int x) {
23#if 0
24 return x != 0;
25#else
26 return ((unsigned)(x | -x)) >> 31;
27#endif
28}
29
30static inline int neq_to_one(int x, int max) {
31#if 0
32 return x != max;
33#else
34 SkASSERT(x >= 0 && x <= max);
35 return ((unsigned)(x - max)) >> 31;
36#endif
37}
38
39static inline int neq_to_mask(int x, int max) {
40#if 0
41 return -(x != max);
42#else
43 SkASSERT(x >= 0 && x <= max);
44 return (x - max) >> 31;
45#endif
46}
47
48static inline unsigned div255(unsigned x) {
49 SkASSERT(x <= (255*255));
50 return x * ((1 << 24) / 255) >> 24;
51}
52
53#define kDelta 32 // small enough to show off angle differences
54
57
58 int specular = light.fSpecular;
59 int ambient = light.fAmbient;
60 SkFixed lx = SkScalarToFixed(light.fDirection[0]);
61 SkFixed ly = SkScalarToFixed(light.fDirection[1]);
62 SkFixed lz = SkScalarToFixed(light.fDirection[2]);
63 SkFixed lz_dot_nz = lz * kDelta;
64 int lz_dot8 = lz >> 8;
65
66 size_t planeSize = mask->computeImageSize();
67 uint8_t* alpha = mask->image();
68 uint8_t* multiply = (uint8_t*)alpha + planeSize;
69 uint8_t* additive = multiply + planeSize;
70
71 int rowBytes = mask->fRowBytes;
72 int maxy = mask->fBounds.height() - 1;
73 int maxx = mask->fBounds.width() - 1;
74
75 int prev_row = 0;
76 for (int y = 0; y <= maxy; y++) {
77 int next_row = neq_to_mask(y, maxy) & rowBytes;
78
79 for (int x = 0; x <= maxx; x++) {
80 int nx = alpha[x + neq_to_one(x, maxx)] - alpha[x - nonzero_to_one(x)];
81 int ny = alpha[x + next_row] - alpha[x - prev_row];
82
83 SkFixed numer = lx * nx + ly * ny + lz_dot_nz;
84 int mul = ambient;
85 int add = 0;
86
87 if (numer > 0) { // preflight when numer/denom will be <= 0
88 int denom = SkSqrt32(nx * nx + ny * ny + kDelta*kDelta);
89 SkFixed dot = numer / denom;
90 dot >>= 8; // now dot is 2^8 instead of 2^16
91 mul = std::min(mul + dot, 255);
92
93 // now for the reflection
94
95 // R = 2 (Light * Normal) Normal - Light
96 // hilite = R * Eye(0, 0, 1)
97
98 int hilite = (2 * dot - lz_dot8) * lz_dot8 >> 8;
99 if (hilite > 0) {
100 // pin hilite to 255, since our fast math is also a little sloppy
101 hilite = std::min(hilite, 255);
102
103 // specular is 4.4
104 // would really like to compute the fractional part of this
105 // and then possibly cache a 256 table for a given specular
106 // value in the light, and just pass that in to this function.
107 add = hilite;
108 for (int i = specular >> 4; i > 0; --i) {
109 add = div255(add * hilite);
110 }
111 }
112 }
113 multiply[x] = SkToU8(mul);
114 additive[x] = SkToU8(add);
115 }
116 alpha += rowBytes;
117 multiply += rowBytes;
118 additive += rowBytes;
119 prev_row = rowBytes;
120 }
121}
#define SkASSERT(cond)
Definition: SkAssert.h:116
static int neq_to_one(int x, int max)
static int nonzero_to_one(int x)
static int neq_to_mask(int x, int max)
static unsigned div255(unsigned x)
#define kDelta
int32_t SkFixed
Definition: SkFixed.h:25
#define SkScalarToFixed(x)
Definition: SkFixed.h:125
static int32_t SkSqrt32(int32_t n)
Definition: SkMathPriv.h:25
constexpr uint8_t SkToU8(S x)
Definition: SkTo.h:22
static void Emboss(SkMaskBuilder *mask, const SkEmbossMaskFilter::Light &)
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float min(float r, float g, float b)
Definition: hsl.cpp:48
double y
double x
Int96 multiply(int64_t a, int32_t b)
Definition: Int96.cpp:41
SINT T dot(const Vec< N, T > &a, const Vec< N, T > &b)
Definition: SkVx.h:964
static void next_row(SkCanvas *canvas)
constexpr int32_t height() const
Definition: SkRect.h:165
constexpr int32_t width() const
Definition: SkRect.h:158
uint8_t *& image()
Definition: SkMask.h:236
const uint32_t fRowBytes
Definition: SkMask.h:43
@ k3D_Format
3 8bit per pixl planes: alpha, mul, add
Definition: SkMask.h:29
const SkIRect fBounds
Definition: SkMask.h:42
size_t computeImageSize() const
Definition: SkMask.cpp:30
const Format fFormat
Definition: SkMask.h:44