SkTableColorFilter.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * Copyright 2015 Google Inc.
  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. #include "include/effects/SkTableColorFilter.h"
  8. #include "include/core/SkBitmap.h"
  9. #include "include/core/SkString.h"
  10. #include "include/core/SkUnPreMultiply.h"
  11. #include "include/private/SkColorData.h"
  12. #include "include/private/SkTo.h"
  13. #include "src/core/SkArenaAlloc.h"
  14. #include "src/core/SkEffectPriv.h"
  15. #include "src/core/SkRasterPipeline.h"
  16. #include "src/core/SkReadBuffer.h"
  17. #include "src/core/SkWriteBuffer.h"
  18. static const uint8_t gIdentityTable[] = {
  19. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  20. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  21. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  22. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
  23. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  24. 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
  25. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  26. 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
  27. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  28. 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
  29. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  30. 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
  31. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
  32. 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
  33. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  34. 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
  35. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  36. 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
  37. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  38. 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
  39. 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
  40. 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
  41. 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7,
  42. 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
  43. 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
  44. 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
  45. 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7,
  46. 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
  47. 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
  48. 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
  49. 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
  50. 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
  51. };
  52. class SkTable_ColorFilter : public SkColorFilter {
  53. public:
  54. SkTable_ColorFilter(const uint8_t tableA[], const uint8_t tableR[],
  55. const uint8_t tableG[], const uint8_t tableB[]) {
  56. fBitmap = nullptr;
  57. fFlags = 0;
  58. uint8_t* dst = fStorage;
  59. if (tableA) {
  60. memcpy(dst, tableA, 256);
  61. dst += 256;
  62. fFlags |= kA_Flag;
  63. }
  64. if (tableR) {
  65. memcpy(dst, tableR, 256);
  66. dst += 256;
  67. fFlags |= kR_Flag;
  68. }
  69. if (tableG) {
  70. memcpy(dst, tableG, 256);
  71. dst += 256;
  72. fFlags |= kG_Flag;
  73. }
  74. if (tableB) {
  75. memcpy(dst, tableB, 256);
  76. fFlags |= kB_Flag;
  77. }
  78. }
  79. ~SkTable_ColorFilter() override { delete fBitmap; }
  80. #if SK_SUPPORT_GPU
  81. std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(
  82. GrRecordingContext*, const GrColorSpaceInfo&) const override;
  83. #endif
  84. enum {
  85. kA_Flag = 1 << 0,
  86. kR_Flag = 1 << 1,
  87. kG_Flag = 1 << 2,
  88. kB_Flag = 1 << 3,
  89. };
  90. bool onAppendStages(const SkStageRec& rec, bool shaderIsOpaque) const override {
  91. const uint8_t *r = gIdentityTable,
  92. *g = gIdentityTable,
  93. *b = gIdentityTable,
  94. *a = gIdentityTable;
  95. const uint8_t* ptr = fStorage;
  96. if (fFlags & kA_Flag) { a = ptr; ptr += 256; }
  97. if (fFlags & kR_Flag) { r = ptr; ptr += 256; }
  98. if (fFlags & kG_Flag) { g = ptr; ptr += 256; }
  99. if (fFlags & kB_Flag) { b = ptr; }
  100. SkRasterPipeline* p = rec.fPipeline;
  101. if (!shaderIsOpaque) {
  102. p->append(SkRasterPipeline::unpremul);
  103. }
  104. struct Tables { const uint8_t *r, *g, *b, *a; };
  105. p->append(SkRasterPipeline::byte_tables, rec.fAlloc->make<Tables>(Tables{r,g,b,a}));
  106. bool definitelyOpaque = shaderIsOpaque && a[0xff] == 0xff;
  107. if (!definitelyOpaque) {
  108. p->append(SkRasterPipeline::premul);
  109. }
  110. return true;
  111. }
  112. protected:
  113. void flatten(SkWriteBuffer&) const override;
  114. private:
  115. SK_FLATTENABLE_HOOKS(SkTable_ColorFilter)
  116. void getTableAsBitmap(SkBitmap* table) const;
  117. mutable const SkBitmap* fBitmap; // lazily allocated
  118. uint8_t fStorage[256 * 4];
  119. unsigned fFlags;
  120. friend class SkTableColorFilter;
  121. typedef SkColorFilter INHERITED;
  122. };
  123. static const uint8_t gCountNibBits[] = {
  124. 0, 1, 1, 2,
  125. 1, 2, 2, 3,
  126. 1, 2, 2, 3,
  127. 2, 3, 3, 4
  128. };
  129. #include "src/effects/SkPackBits.h"
  130. void SkTable_ColorFilter::flatten(SkWriteBuffer& buffer) const {
  131. uint8_t storage[5*256];
  132. int count = gCountNibBits[fFlags & 0xF];
  133. size_t size = SkPackBits::Pack8(fStorage, count * 256, storage,
  134. sizeof(storage));
  135. buffer.write32(fFlags);
  136. buffer.writeByteArray(storage, size);
  137. }
  138. sk_sp<SkFlattenable> SkTable_ColorFilter::CreateProc(SkReadBuffer& buffer) {
  139. const int flags = buffer.read32();
  140. const size_t count = gCountNibBits[flags & 0xF];
  141. SkASSERT(count <= 4);
  142. uint8_t packedStorage[5*256];
  143. size_t packedSize = buffer.getArrayCount();
  144. if (!buffer.validate(packedSize <= sizeof(packedStorage))) {
  145. return nullptr;
  146. }
  147. if (!buffer.readByteArray(packedStorage, packedSize)) {
  148. return nullptr;
  149. }
  150. uint8_t unpackedStorage[4*256];
  151. size_t unpackedSize = SkPackBits::Unpack8(packedStorage, packedSize,
  152. unpackedStorage, sizeof(unpackedStorage));
  153. // now check that we got the size we expected
  154. if (!buffer.validate(unpackedSize == count*256)) {
  155. return nullptr;
  156. }
  157. const uint8_t* a = nullptr;
  158. const uint8_t* r = nullptr;
  159. const uint8_t* g = nullptr;
  160. const uint8_t* b = nullptr;
  161. const uint8_t* ptr = unpackedStorage;
  162. if (flags & kA_Flag) {
  163. a = ptr;
  164. ptr += 256;
  165. }
  166. if (flags & kR_Flag) {
  167. r = ptr;
  168. ptr += 256;
  169. }
  170. if (flags & kG_Flag) {
  171. g = ptr;
  172. ptr += 256;
  173. }
  174. if (flags & kB_Flag) {
  175. b = ptr;
  176. ptr += 256;
  177. }
  178. return SkTableColorFilter::MakeARGB(a, r, g, b);
  179. }
  180. void SkTable_ColorFilter::getTableAsBitmap(SkBitmap* table) const {
  181. if (table) {
  182. if (nullptr == fBitmap) {
  183. SkBitmap* bmp = new SkBitmap;
  184. bmp->allocPixels(SkImageInfo::MakeA8(256, 4));
  185. uint8_t* bitmapPixels = bmp->getAddr8(0, 0);
  186. int offset = 0;
  187. static const unsigned kFlags[] = { kA_Flag, kR_Flag, kG_Flag, kB_Flag };
  188. for (int x = 0; x < 4; ++x) {
  189. if (!(fFlags & kFlags[x])) {
  190. memcpy(bitmapPixels, gIdentityTable, sizeof(gIdentityTable));
  191. } else {
  192. memcpy(bitmapPixels, fStorage + offset, 256);
  193. offset += 256;
  194. }
  195. bitmapPixels += 256;
  196. }
  197. bmp->setImmutable();
  198. fBitmap = bmp;
  199. }
  200. *table = *fBitmap;
  201. }
  202. }
  203. #if SK_SUPPORT_GPU
  204. #include "include/private/GrRecordingContext.h"
  205. #include "src/gpu/GrColorSpaceInfo.h"
  206. #include "src/gpu/GrFragmentProcessor.h"
  207. #include "src/gpu/GrRecordingContextPriv.h"
  208. #include "src/gpu/SkGr.h"
  209. #include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
  210. #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
  211. #include "src/gpu/glsl/GrGLSLProgramDataManager.h"
  212. #include "src/gpu/glsl/GrGLSLUniformHandler.h"
  213. class ColorTableEffect : public GrFragmentProcessor {
  214. public:
  215. static std::unique_ptr<GrFragmentProcessor> Make(GrRecordingContext* context,
  216. const SkBitmap& bitmap);
  217. ~ColorTableEffect() override {}
  218. const char* name() const override { return "ColorTableEffect"; }
  219. std::unique_ptr<GrFragmentProcessor> clone() const override {
  220. return std::unique_ptr<GrFragmentProcessor>(
  221. new ColorTableEffect(sk_ref_sp(fTextureSampler.proxy())));
  222. }
  223. private:
  224. GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
  225. void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
  226. bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
  227. ColorTableEffect(sk_sp<GrTextureProxy> proxy)
  228. : INHERITED(kColorTableEffect_ClassID,
  229. kNone_OptimizationFlags) // Not bothering with table-specific optimizations.
  230. , fTextureSampler(std::move(proxy)) {
  231. this->setTextureSamplerCnt(1);
  232. }
  233. const TextureSampler& onTextureSampler(int) const override { return fTextureSampler; }
  234. GR_DECLARE_FRAGMENT_PROCESSOR_TEST
  235. TextureSampler fTextureSampler;
  236. typedef GrFragmentProcessor INHERITED;
  237. };
  238. class GLColorTableEffect : public GrGLSLFragmentProcessor {
  239. public:
  240. void emitCode(EmitArgs&) override;
  241. static void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*) {}
  242. private:
  243. typedef GrGLSLFragmentProcessor INHERITED;
  244. };
  245. void GLColorTableEffect::emitCode(EmitArgs& args) {
  246. static const float kColorScaleFactor = 255.0f / 256.0f;
  247. static const float kColorOffsetFactor = 1.0f / 512.0f;
  248. GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
  249. if (nullptr == args.fInputColor) {
  250. // the input color is solid white (all ones).
  251. static const float kMaxValue = kColorScaleFactor + kColorOffsetFactor;
  252. fragBuilder->codeAppendf("\t\thalf4 coord = half4(%f, %f, %f, %f);\n",
  253. kMaxValue, kMaxValue, kMaxValue, kMaxValue);
  254. } else {
  255. fragBuilder->codeAppendf("\t\thalf nonZeroAlpha = max(%s.a, .0001);\n", args.fInputColor);
  256. fragBuilder->codeAppendf("\t\thalf4 coord = half4(%s.rgb / nonZeroAlpha, nonZeroAlpha);\n",
  257. args.fInputColor);
  258. fragBuilder->codeAppendf("\t\tcoord = coord * %f + half4(%f, %f, %f, %f);\n",
  259. kColorScaleFactor,
  260. kColorOffsetFactor, kColorOffsetFactor,
  261. kColorOffsetFactor, kColorOffsetFactor);
  262. }
  263. SkString coord;
  264. fragBuilder->codeAppendf("\t\t%s.a = ", args.fOutputColor);
  265. coord.printf("half2(coord.a, 0.125)");
  266. fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str());
  267. fragBuilder->codeAppend(".a;\n");
  268. fragBuilder->codeAppendf("\t\t%s.r = ", args.fOutputColor);
  269. coord.printf("half2(coord.r, 0.375)");
  270. fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str());
  271. fragBuilder->codeAppend(".a;\n");
  272. fragBuilder->codeAppendf("\t\t%s.g = ", args.fOutputColor);
  273. coord.printf("half2(coord.g, 0.625)");
  274. fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str());
  275. fragBuilder->codeAppend(".a;\n");
  276. fragBuilder->codeAppendf("\t\t%s.b = ", args.fOutputColor);
  277. coord.printf("half2(coord.b, 0.875)");
  278. fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str());
  279. fragBuilder->codeAppend(".a;\n");
  280. fragBuilder->codeAppendf("\t\t%s.rgb *= %s.a;\n", args.fOutputColor, args.fOutputColor);
  281. }
  282. ///////////////////////////////////////////////////////////////////////////////
  283. std::unique_ptr<GrFragmentProcessor> ColorTableEffect::Make(GrRecordingContext* context,
  284. const SkBitmap& bitmap) {
  285. SkASSERT(kPremul_SkAlphaType == bitmap.alphaType());
  286. SkASSERT(bitmap.isImmutable());
  287. if (kUnknown_GrPixelConfig == SkColorType2GrPixelConfig(bitmap.colorType())) {
  288. return nullptr;
  289. }
  290. sk_sp<SkImage> srcImage = SkImage::MakeFromBitmap(bitmap);
  291. if (!srcImage) {
  292. return nullptr;
  293. }
  294. sk_sp<GrTextureProxy> proxy = GrMakeCachedImageProxy(context->priv().proxyProvider(),
  295. std::move(srcImage));
  296. if (!proxy) {
  297. return nullptr;
  298. }
  299. return std::unique_ptr<GrFragmentProcessor>(new ColorTableEffect(std::move(proxy)));
  300. }
  301. void ColorTableEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
  302. GrProcessorKeyBuilder* b) const {
  303. GLColorTableEffect::GenKey(*this, caps, b);
  304. }
  305. GrGLSLFragmentProcessor* ColorTableEffect::onCreateGLSLInstance() const {
  306. return new GLColorTableEffect;
  307. }
  308. ///////////////////////////////////////////////////////////////////////////////
  309. GR_DEFINE_FRAGMENT_PROCESSOR_TEST(ColorTableEffect);
  310. #if GR_TEST_UTILS
  311. #include "include/gpu/GrContext.h"
  312. std::unique_ptr<GrFragmentProcessor> ColorTableEffect::TestCreate(GrProcessorTestData* d) {
  313. int flags = 0;
  314. uint8_t luts[256][4];
  315. do {
  316. for (int i = 0; i < 4; ++i) {
  317. flags |= d->fRandom->nextBool() ? (1 << i): 0;
  318. }
  319. } while (!flags);
  320. for (int i = 0; i < 4; ++i) {
  321. if (flags & (1 << i)) {
  322. for (int j = 0; j < 256; ++j) {
  323. luts[j][i] = SkToU8(d->fRandom->nextBits(8));
  324. }
  325. }
  326. }
  327. auto filter(SkTableColorFilter::MakeARGB(
  328. (flags & (1 << 0)) ? luts[0] : nullptr,
  329. (flags & (1 << 1)) ? luts[1] : nullptr,
  330. (flags & (1 << 2)) ? luts[2] : nullptr,
  331. (flags & (1 << 3)) ? luts[3] : nullptr
  332. ));
  333. sk_sp<SkColorSpace> colorSpace = GrTest::TestColorSpace(d->fRandom);
  334. auto fp = filter->asFragmentProcessor(
  335. d->context(), GrColorSpaceInfo(GrColorType::kRGBA_8888, kUnknown_SkAlphaType,
  336. std::move(colorSpace)));
  337. SkASSERT(fp);
  338. return fp;
  339. }
  340. #endif
  341. std::unique_ptr<GrFragmentProcessor> SkTable_ColorFilter::asFragmentProcessor(
  342. GrRecordingContext* context, const GrColorSpaceInfo&) const {
  343. SkBitmap bitmap;
  344. this->getTableAsBitmap(&bitmap);
  345. return ColorTableEffect::Make(context, bitmap);
  346. }
  347. #endif // SK_SUPPORT_GPU
  348. ///////////////////////////////////////////////////////////////////////////////
  349. sk_sp<SkColorFilter> SkTableColorFilter::Make(const uint8_t table[256]) {
  350. return sk_make_sp<SkTable_ColorFilter>(table, table, table, table);
  351. }
  352. sk_sp<SkColorFilter> SkTableColorFilter::MakeARGB(const uint8_t tableA[256],
  353. const uint8_t tableR[256],
  354. const uint8_t tableG[256],
  355. const uint8_t tableB[256]) {
  356. return sk_make_sp<SkTable_ColorFilter>(tableA, tableR, tableG, tableB);
  357. }
  358. void SkTableColorFilter::RegisterFlattenables() { SK_REGISTER_FLATTENABLE(SkTable_ColorFilter); }