• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 
8 #ifndef SkVMBlitter_DEFINED
9 #define SkVMBlitter_DEFINED
10 
11 #include "src/core/SkArenaAlloc.h"
12 #include "src/core/SkBlitter.h"
13 #include "src/core/SkLRUCache.h"
14 #include "src/core/SkVM.h"
15 
16 class SkVMBlitter final : public SkBlitter {
17 public:
18     static SkVMBlitter* Make(const SkPixmap& dst,
19                              const SkPaint&,
20                              const SkMatrixProvider&,
21                              SkArenaAlloc*,
22                              sk_sp<SkShader> clipShader);
23 
24     static SkVMBlitter* Make(const SkPixmap& dst,
25                              const SkPaint&,
26                              const SkPixmap& sprite,
27                              int left, int top,
28                              SkArenaAlloc*,
29                              sk_sp<SkShader> clipShader);
30 
31     SkVMBlitter(const SkPixmap& device,
32                 const SkPaint& paint,
33                 const SkPixmap* sprite,
34                 SkIPoint spriteOffset,
35                 const SkMatrixProvider& matrices,
36                 sk_sp<SkShader> clip,
37                 bool* ok);
38 
39     ~SkVMBlitter() override;
40 
41 private:
42     enum class Coverage { Full, UniformF, MaskA8, MaskLCD16, Mask3D };
43     struct Key {
44         uint64_t shader,
45                  clip,
46                  blender,
47                  colorSpace;
48         uint8_t  colorType,
49                  alphaType,
50                  coverage;
51         uint8_t  padding8{0};
52         uint32_t padding{0};
53         // Params::{paint,quality,matrices} are only passed to {shader,clip}->program(),
54         // not used here by the blitter itself.  No need to include them in the key;
55         // they'll be folded into the shader key if used.
56 
57         bool operator==(const Key& that) const;
58         Key withCoverage(Coverage c) const;
59     };
60 
61     struct Params {
62         sk_sp<SkShader>         shader;
63         sk_sp<SkShader>         clip;
64         sk_sp<SkBlender>        blender;    // never null
65         SkColorInfo             dst;
66         Coverage                coverage;
67         SkColor4f               paint;
68         const SkMatrixProvider& matrices;
69 
70         Params withCoverage(Coverage c) const;
71     };
72 
73     static Params EffectiveParams(const SkPixmap& device,
74                                   const SkPixmap* sprite,
75                                   SkPaint paint,
76                                   const SkMatrixProvider& matrices,
77                                   sk_sp<SkShader> clip);
78     static skvm::Color DstColor(skvm::Builder* p, const Params& params);
79     static void BuildProgram(skvm::Builder* p, const Params& params,
80                              skvm::Uniforms* uniforms, SkArenaAlloc* alloc);
81     static Key CacheKey(const Params& params,
82                         skvm::Uniforms* uniforms, SkArenaAlloc* alloc, bool* ok);
83     static SkLRUCache<Key, skvm::Program>* TryAcquireProgramCache();
84     static SkString DebugName(const Key& key);
85     static void ReleaseProgramCache();
86 
87     skvm::Program buildProgram(Coverage coverage);
88     void updateUniforms(int right, int y);
89     const void* isSprite(int x, int y) const;
90 
91     void blitH(int x, int y, int w) override;
92     void blitAntiH(int x, int y, const SkAlpha cov[], const int16_t runs[]) override;
93     void blitMask(const SkMask& mask, const SkIRect& clip) override;
94 
95     SkPixmap        fDevice;
96     const SkPixmap  fSprite;                  // See isSprite().
97     const SkIPoint  fSpriteOffset;
98     skvm::Uniforms  fUniforms;                // Most data is copied directly into fUniforms,
99     SkArenaAlloc    fAlloc{2*sizeof(void*)};  // but a few effects need to ref large content.
100     const Params    fParams;
101     const Key       fKey;
102     skvm::Program   fBlitH,
103                     fBlitAntiH,
104                     fBlitMaskA8,
105                     fBlitMask3D,
106                     fBlitMaskLCD16;
107 
108     friend class Viewer;
109 };
110 #endif  // SkVMBlitter_DEFINED
111