gfx/skia/trunk/src/effects/SkEmbossMask.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1
michael@0 2 /*
michael@0 3 * Copyright 2006 The Android Open Source Project
michael@0 4 *
michael@0 5 * Use of this source code is governed by a BSD-style license that can be
michael@0 6 * found in the LICENSE file.
michael@0 7 */
michael@0 8
michael@0 9
michael@0 10 #include "SkEmbossMask.h"
michael@0 11 #include "SkMath.h"
michael@0 12
michael@0 13 static inline int nonzero_to_one(int x) {
michael@0 14 #if 0
michael@0 15 return x != 0;
michael@0 16 #else
michael@0 17 return ((unsigned)(x | -x)) >> 31;
michael@0 18 #endif
michael@0 19 }
michael@0 20
michael@0 21 static inline int neq_to_one(int x, int max) {
michael@0 22 #if 0
michael@0 23 return x != max;
michael@0 24 #else
michael@0 25 SkASSERT(x >= 0 && x <= max);
michael@0 26 return ((unsigned)(x - max)) >> 31;
michael@0 27 #endif
michael@0 28 }
michael@0 29
michael@0 30 static inline int neq_to_mask(int x, int max) {
michael@0 31 #if 0
michael@0 32 return -(x != max);
michael@0 33 #else
michael@0 34 SkASSERT(x >= 0 && x <= max);
michael@0 35 return (x - max) >> 31;
michael@0 36 #endif
michael@0 37 }
michael@0 38
michael@0 39 static inline unsigned div255(unsigned x) {
michael@0 40 SkASSERT(x <= (255*255));
michael@0 41 return x * ((1 << 24) / 255) >> 24;
michael@0 42 }
michael@0 43
michael@0 44 #define kDelta 32 // small enough to show off angle differences
michael@0 45
michael@0 46 #include "SkEmbossMask_Table.h"
michael@0 47
michael@0 48 #if defined(SK_BUILD_FOR_WIN32) && defined(SK_DEBUG)
michael@0 49
michael@0 50 #include <stdio.h>
michael@0 51
michael@0 52 void SkEmbossMask_BuildTable() {
michael@0 53 // build it 0..127 x 0..127, so we use 2^15 - 1 in the numerator for our "fixed" table
michael@0 54
michael@0 55 FILE* file = ::fopen("SkEmbossMask_Table.h", "w");
michael@0 56 SkASSERT(file);
michael@0 57 ::fprintf(file, "#include \"SkTypes.h\"\n\n");
michael@0 58 ::fprintf(file, "static const U16 gInvSqrtTable[128 * 128] = {\n");
michael@0 59 for (int dx = 0; dx <= 255/2; dx++) {
michael@0 60 for (int dy = 0; dy <= 255/2; dy++) {
michael@0 61 if ((dy & 15) == 0)
michael@0 62 ::fprintf(file, "\t");
michael@0 63
michael@0 64 uint16_t value = SkToU16((1 << 15) / SkSqrt32(dx * dx + dy * dy + kDelta*kDelta/4));
michael@0 65
michael@0 66 ::fprintf(file, "0x%04X", value);
michael@0 67 if (dx * 128 + dy < 128*128-1) {
michael@0 68 ::fprintf(file, ", ");
michael@0 69 }
michael@0 70 if ((dy & 15) == 15) {
michael@0 71 ::fprintf(file, "\n");
michael@0 72 }
michael@0 73 }
michael@0 74 }
michael@0 75 ::fprintf(file, "};\n#define kDeltaUsedToBuildTable\t%d\n", kDelta);
michael@0 76 ::fclose(file);
michael@0 77 }
michael@0 78
michael@0 79 #endif
michael@0 80
michael@0 81 void SkEmbossMask::Emboss(SkMask* mask, const SkEmbossMaskFilter::Light& light) {
michael@0 82 SkASSERT(kDelta == kDeltaUsedToBuildTable);
michael@0 83
michael@0 84 SkASSERT(mask->fFormat == SkMask::k3D_Format);
michael@0 85
michael@0 86 int specular = light.fSpecular;
michael@0 87 int ambient = light.fAmbient;
michael@0 88 SkFixed lx = SkScalarToFixed(light.fDirection[0]);
michael@0 89 SkFixed ly = SkScalarToFixed(light.fDirection[1]);
michael@0 90 SkFixed lz = SkScalarToFixed(light.fDirection[2]);
michael@0 91 SkFixed lz_dot_nz = lz * kDelta;
michael@0 92 int lz_dot8 = lz >> 8;
michael@0 93
michael@0 94 size_t planeSize = mask->computeImageSize();
michael@0 95 uint8_t* alpha = mask->fImage;
michael@0 96 uint8_t* multiply = (uint8_t*)alpha + planeSize;
michael@0 97 uint8_t* additive = multiply + planeSize;
michael@0 98
michael@0 99 int rowBytes = mask->fRowBytes;
michael@0 100 int maxy = mask->fBounds.height() - 1;
michael@0 101 int maxx = mask->fBounds.width() - 1;
michael@0 102
michael@0 103 int prev_row = 0;
michael@0 104 for (int y = 0; y <= maxy; y++) {
michael@0 105 int next_row = neq_to_mask(y, maxy) & rowBytes;
michael@0 106
michael@0 107 for (int x = 0; x <= maxx; x++) {
michael@0 108 if (alpha[x]) {
michael@0 109 int nx = alpha[x + neq_to_one(x, maxx)] - alpha[x - nonzero_to_one(x)];
michael@0 110 int ny = alpha[x + next_row] - alpha[x - prev_row];
michael@0 111
michael@0 112 SkFixed numer = lx * nx + ly * ny + lz_dot_nz;
michael@0 113 int mul = ambient;
michael@0 114 int add = 0;
michael@0 115
michael@0 116 if (numer > 0) { // preflight when numer/denom will be <= 0
michael@0 117 #if 0
michael@0 118 int denom = SkSqrt32(nx * nx + ny * ny + kDelta*kDelta);
michael@0 119 SkFixed dot = numer / denom;
michael@0 120 dot >>= 8; // now dot is 2^8 instead of 2^16
michael@0 121 #else
michael@0 122 // can use full numer, but then we need to call SkFixedMul, since
michael@0 123 // numer is 24 bits, and our table is 12 bits
michael@0 124
michael@0 125 // SkFixed dot = SkFixedMul(numer, gTable[]) >> 8
michael@0 126 SkFixed dot = (unsigned)(numer >> 4) * gInvSqrtTable[(SkAbs32(nx) >> 1 << 7) | (SkAbs32(ny) >> 1)] >> 20;
michael@0 127 #endif
michael@0 128 mul = SkFastMin32(mul + dot, 255);
michael@0 129
michael@0 130 // now for the reflection
michael@0 131
michael@0 132 // R = 2 (Light * Normal) Normal - Light
michael@0 133 // hilite = R * Eye(0, 0, 1)
michael@0 134
michael@0 135 int hilite = (2 * dot - lz_dot8) * lz_dot8 >> 8;
michael@0 136 if (hilite > 0) {
michael@0 137 // pin hilite to 255, since our fast math is also a little sloppy
michael@0 138 hilite = SkClampMax(hilite, 255);
michael@0 139
michael@0 140 // specular is 4.4
michael@0 141 // would really like to compute the fractional part of this
michael@0 142 // and then possibly cache a 256 table for a given specular
michael@0 143 // value in the light, and just pass that in to this function.
michael@0 144 add = hilite;
michael@0 145 for (int i = specular >> 4; i > 0; --i) {
michael@0 146 add = div255(add * hilite);
michael@0 147 }
michael@0 148 }
michael@0 149 }
michael@0 150 multiply[x] = SkToU8(mul);
michael@0 151 additive[x] = SkToU8(add);
michael@0 152
michael@0 153 // multiply[x] = 0xFF;
michael@0 154 // additive[x] = 0;
michael@0 155 // ((uint8_t*)alpha)[x] = alpha[x] * multiply[x] >> 8;
michael@0 156 }
michael@0 157 }
michael@0 158 alpha += rowBytes;
michael@0 159 multiply += rowBytes;
michael@0 160 additive += rowBytes;
michael@0 161 prev_row = rowBytes;
michael@0 162 }
michael@0 163 }

mercurial