SkRasterPipeline.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Copyright 2016 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 "src/core/SkOpts.h"
  8. #include "src/core/SkRasterPipeline.h"
  9. #include <algorithm>
  10. SkRasterPipeline::SkRasterPipeline(SkArenaAlloc* alloc) : fAlloc(alloc) {
  11. this->reset();
  12. }
  13. void SkRasterPipeline::reset() {
  14. fStages = nullptr;
  15. fNumStages = 0;
  16. fSlotsNeeded = 1; // We always need one extra slot for just_return().
  17. }
  18. void SkRasterPipeline::append(StockStage stage, void* ctx) {
  19. SkASSERT(stage != uniform_color); // Please use append_constant_color().
  20. SkASSERT(stage != unbounded_uniform_color); // Please use append_constant_color().
  21. SkASSERT(stage != set_rgb); // Please use append_set_rgb().
  22. SkASSERT(stage != unbounded_set_rgb); // Please use append_set_rgb().
  23. SkASSERT(stage != clamp_gamut); // Please use append_gamut_clamp_if_normalized().
  24. this->unchecked_append(stage, ctx);
  25. }
  26. void SkRasterPipeline::unchecked_append(StockStage stage, void* ctx) {
  27. fStages = fAlloc->make<StageList>( StageList{fStages, (uint64_t) stage, ctx, false} );
  28. fNumStages += 1;
  29. fSlotsNeeded += ctx ? 2 : 1;
  30. }
  31. void SkRasterPipeline::append(StockStage stage, uintptr_t ctx) {
  32. void* ptrCtx;
  33. memcpy(&ptrCtx, &ctx, sizeof(ctx));
  34. this->append(stage, ptrCtx);
  35. }
  36. void SkRasterPipeline::append(void* fn, void* ctx) {
  37. fStages = fAlloc->make<StageList>( StageList{fStages, (uint64_t) fn, ctx, true} );
  38. fNumStages += 1;
  39. fSlotsNeeded += ctx ? 2 : 1;
  40. }
  41. void SkRasterPipeline::extend(const SkRasterPipeline& src) {
  42. if (src.empty()) {
  43. return;
  44. }
  45. auto stages = fAlloc->makeArrayDefault<StageList>(src.fNumStages);
  46. int n = src.fNumStages;
  47. const StageList* st = src.fStages;
  48. while (n --> 1) {
  49. stages[n] = *st;
  50. stages[n].prev = &stages[n-1];
  51. st = st->prev;
  52. }
  53. stages[0] = *st;
  54. stages[0].prev = fStages;
  55. fStages = &stages[src.fNumStages - 1];
  56. fNumStages += src.fNumStages;
  57. fSlotsNeeded += src.fSlotsNeeded - 1; // Don't double count just_returns().
  58. }
  59. void SkRasterPipeline::dump() const {
  60. SkDebugf("SkRasterPipeline, %d stages\n", fNumStages);
  61. std::vector<const char*> stages;
  62. for (auto st = fStages; st; st = st->prev) {
  63. const char* name = "";
  64. switch (st->stage) {
  65. #define M(x) case x: name = #x; break;
  66. SK_RASTER_PIPELINE_STAGES(M)
  67. #undef M
  68. }
  69. stages.push_back(name);
  70. }
  71. std::reverse(stages.begin(), stages.end());
  72. for (const char* name : stages) {
  73. SkDebugf("\t%s\n", name);
  74. }
  75. SkDebugf("\n");
  76. }
  77. void SkRasterPipeline::append_set_rgb(SkArenaAlloc* alloc, const float rgb[3]) {
  78. auto arg = alloc->makeArrayDefault<float>(3);
  79. arg[0] = rgb[0];
  80. arg[1] = rgb[1];
  81. arg[2] = rgb[2];
  82. auto stage = unbounded_set_rgb;
  83. if (0 <= rgb[0] && rgb[0] <= 1 &&
  84. 0 <= rgb[1] && rgb[1] <= 1 &&
  85. 0 <= rgb[2] && rgb[2] <= 1)
  86. {
  87. stage = set_rgb;
  88. }
  89. this->unchecked_append(stage, arg);
  90. }
  91. void SkRasterPipeline::append_constant_color(SkArenaAlloc* alloc, const float rgba[4]) {
  92. // r,g,b might be outside [0,1], but alpha should probably always be in [0,1].
  93. SkASSERT(0 <= rgba[3] && rgba[3] <= 1);
  94. if (rgba[0] == 0 && rgba[1] == 0 && rgba[2] == 0 && rgba[3] == 1) {
  95. this->append(black_color);
  96. } else if (rgba[0] == 1 && rgba[1] == 1 && rgba[2] == 1 && rgba[3] == 1) {
  97. this->append(white_color);
  98. } else {
  99. auto ctx = alloc->make<SkRasterPipeline_UniformColorCtx>();
  100. Sk4f color = Sk4f::Load(rgba);
  101. color.store(&ctx->r);
  102. // uniform_color requires colors in range and can go lowp,
  103. // while unbounded_uniform_color supports out-of-range colors too but not lowp.
  104. if (0 <= rgba[0] && rgba[0] <= rgba[3] &&
  105. 0 <= rgba[1] && rgba[1] <= rgba[3] &&
  106. 0 <= rgba[2] && rgba[2] <= rgba[3]) {
  107. // To make loads more direct, we store 8-bit values in 16-bit slots.
  108. color = color * 255.0f + 0.5f;
  109. ctx->rgba[0] = (uint16_t)color[0];
  110. ctx->rgba[1] = (uint16_t)color[1];
  111. ctx->rgba[2] = (uint16_t)color[2];
  112. ctx->rgba[3] = (uint16_t)color[3];
  113. this->unchecked_append(uniform_color, ctx);
  114. } else {
  115. this->unchecked_append(unbounded_uniform_color, ctx);
  116. }
  117. }
  118. }
  119. void SkRasterPipeline::append_matrix(SkArenaAlloc* alloc, const SkMatrix& matrix) {
  120. SkMatrix::TypeMask mt = matrix.getType();
  121. if (mt == SkMatrix::kIdentity_Mask) {
  122. return;
  123. }
  124. if (mt == SkMatrix::kTranslate_Mask) {
  125. float* trans = alloc->makeArrayDefault<float>(2);
  126. trans[0] = matrix.getTranslateX();
  127. trans[1] = matrix.getTranslateY();
  128. this->append(SkRasterPipeline::matrix_translate, trans);
  129. } else if ((mt | (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) ==
  130. (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) {
  131. float* scaleTrans = alloc->makeArrayDefault<float>(4);
  132. scaleTrans[0] = matrix.getScaleX();
  133. scaleTrans[1] = matrix.getScaleY();
  134. scaleTrans[2] = matrix.getTranslateX();
  135. scaleTrans[3] = matrix.getTranslateY();
  136. this->append(SkRasterPipeline::matrix_scale_translate, scaleTrans);
  137. } else {
  138. float* storage = alloc->makeArrayDefault<float>(9);
  139. if (matrix.asAffine(storage)) {
  140. // note: asAffine and the 2x3 stage really only need 6 entries
  141. this->append(SkRasterPipeline::matrix_2x3, storage);
  142. } else {
  143. matrix.get9(storage);
  144. this->append(SkRasterPipeline::matrix_perspective, storage);
  145. }
  146. }
  147. }
  148. void SkRasterPipeline::append_load(SkColorType ct, const SkRasterPipeline_MemoryCtx* ctx) {
  149. switch (ct) {
  150. case kUnknown_SkColorType: SkASSERT(false); break;
  151. case kAlpha_8_SkColorType: this->append(load_a8, ctx); break;
  152. case kRGB_565_SkColorType: this->append(load_565, ctx); break;
  153. case kARGB_4444_SkColorType: this->append(load_4444, ctx); break;
  154. case kRGBA_8888_SkColorType: this->append(load_8888, ctx); break;
  155. case kRGBA_1010102_SkColorType: this->append(load_1010102, ctx); break;
  156. case kRGBA_F16Norm_SkColorType:
  157. case kRGBA_F16_SkColorType: this->append(load_f16, ctx); break;
  158. case kRGBA_F32_SkColorType: this->append(load_f32, ctx); break;
  159. case kGray_8_SkColorType: this->append(load_a8, ctx);
  160. this->append(alpha_to_gray);
  161. break;
  162. case kRGB_888x_SkColorType: this->append(load_8888, ctx);
  163. this->append(force_opaque);
  164. break;
  165. case kRGB_101010x_SkColorType: this->append(load_1010102, ctx);
  166. this->append(force_opaque);
  167. break;
  168. case kBGRA_8888_SkColorType: this->append(load_8888, ctx);
  169. this->append(swap_rb);
  170. break;
  171. }
  172. }
  173. void SkRasterPipeline::append_load_dst(SkColorType ct, const SkRasterPipeline_MemoryCtx* ctx) {
  174. switch (ct) {
  175. case kUnknown_SkColorType: SkASSERT(false); break;
  176. case kAlpha_8_SkColorType: this->append(load_a8_dst, ctx); break;
  177. case kRGB_565_SkColorType: this->append(load_565_dst, ctx); break;
  178. case kARGB_4444_SkColorType: this->append(load_4444_dst, ctx); break;
  179. case kRGBA_8888_SkColorType: this->append(load_8888_dst, ctx); break;
  180. case kRGBA_1010102_SkColorType: this->append(load_1010102_dst, ctx); break;
  181. case kRGBA_F16Norm_SkColorType:
  182. case kRGBA_F16_SkColorType: this->append(load_f16_dst, ctx); break;
  183. case kRGBA_F32_SkColorType: this->append(load_f32_dst, ctx); break;
  184. case kGray_8_SkColorType: this->append(load_a8_dst, ctx);
  185. this->append(alpha_to_gray_dst);
  186. break;
  187. case kRGB_888x_SkColorType: this->append(load_8888_dst, ctx);
  188. this->append(force_opaque_dst);
  189. break;
  190. case kRGB_101010x_SkColorType: this->append(load_1010102_dst, ctx);
  191. this->append(force_opaque_dst);
  192. break;
  193. case kBGRA_8888_SkColorType: this->append(load_8888_dst, ctx);
  194. this->append(swap_rb_dst);
  195. break;
  196. }
  197. }
  198. void SkRasterPipeline::append_store(SkColorType ct, const SkRasterPipeline_MemoryCtx* ctx) {
  199. switch (ct) {
  200. case kUnknown_SkColorType: SkASSERT(false); break;
  201. case kAlpha_8_SkColorType: this->append(store_a8, ctx); break;
  202. case kRGB_565_SkColorType: this->append(store_565, ctx); break;
  203. case kARGB_4444_SkColorType: this->append(store_4444, ctx); break;
  204. case kRGBA_8888_SkColorType: this->append(store_8888, ctx); break;
  205. case kRGBA_1010102_SkColorType: this->append(store_1010102, ctx); break;
  206. case kRGBA_F16Norm_SkColorType:
  207. case kRGBA_F16_SkColorType: this->append(store_f16, ctx); break;
  208. case kRGBA_F32_SkColorType: this->append(store_f32, ctx); break;
  209. case kRGB_888x_SkColorType: this->append(force_opaque);
  210. this->append(store_8888, ctx);
  211. break;
  212. case kRGB_101010x_SkColorType: this->append(force_opaque);
  213. this->append(store_1010102, ctx);
  214. break;
  215. case kGray_8_SkColorType: this->append(bt709_luminance_or_luma_to_alpha);
  216. this->append(store_a8, ctx);
  217. break;
  218. case kBGRA_8888_SkColorType: this->append(swap_rb);
  219. this->append(store_8888, ctx);
  220. break;
  221. }
  222. }
  223. void SkRasterPipeline::append_gamut_clamp_if_normalized(const SkImageInfo& dstInfo) {
  224. // N.B. we _do_ clamp for kRGBA_F16Norm_SkColorType... because it's normalized.
  225. if (dstInfo.colorType() != kRGBA_F16_SkColorType &&
  226. dstInfo.colorType() != kRGBA_F32_SkColorType &&
  227. dstInfo.alphaType() == kPremul_SkAlphaType)
  228. {
  229. this->unchecked_append(SkRasterPipeline::clamp_gamut, nullptr);
  230. }
  231. }
  232. SkRasterPipeline::StartPipelineFn SkRasterPipeline::build_pipeline(void** ip) const {
  233. // We'll try to build a lowp pipeline, but if that fails fallback to a highp float pipeline.
  234. void** reset_point = ip;
  235. // Stages are stored backwards in fStages, so we reverse here, back to front.
  236. *--ip = (void*)SkOpts::just_return_lowp;
  237. for (const StageList* st = fStages; st; st = st->prev) {
  238. SkOpts::StageFn fn;
  239. if (!st->rawFunction && (fn = SkOpts::stages_lowp[st->stage])) {
  240. if (st->ctx) {
  241. *--ip = st->ctx;
  242. }
  243. *--ip = (void*)fn;
  244. } else {
  245. ip = reset_point;
  246. break;
  247. }
  248. }
  249. if (ip != reset_point) {
  250. return SkOpts::start_pipeline_lowp;
  251. }
  252. *--ip = (void*)SkOpts::just_return_highp;
  253. for (const StageList* st = fStages; st; st = st->prev) {
  254. if (st->ctx) {
  255. *--ip = st->ctx;
  256. }
  257. if (st->rawFunction) {
  258. *--ip = (void*)st->stage;
  259. } else {
  260. *--ip = (void*)SkOpts::stages_highp[st->stage];
  261. }
  262. }
  263. return SkOpts::start_pipeline_highp;
  264. }
  265. void SkRasterPipeline::run(size_t x, size_t y, size_t w, size_t h) const {
  266. if (this->empty()) {
  267. return;
  268. }
  269. // Best to not use fAlloc here... we can't bound how often run() will be called.
  270. SkAutoSTMalloc<64, void*> program(fSlotsNeeded);
  271. auto start_pipeline = this->build_pipeline(program.get() + fSlotsNeeded);
  272. start_pipeline(x,y,x+w,y+h, program.get());
  273. }
  274. std::function<void(size_t, size_t, size_t, size_t)> SkRasterPipeline::compile() const {
  275. if (this->empty()) {
  276. return [](size_t, size_t, size_t, size_t) {};
  277. }
  278. void** program = fAlloc->makeArray<void*>(fSlotsNeeded);
  279. auto start_pipeline = this->build_pipeline(program + fSlotsNeeded);
  280. return [=](size_t x, size_t y, size_t w, size_t h) {
  281. start_pipeline(x,y,x+w,y+h, program);
  282. };
  283. }