aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/effects/SkColorCubeFilter.h
blob: 8d63667c3232c295bf8c93b855cac0adbda716a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * Copyright 2014 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkColorCubeFilter_DEFINED
#define SkColorCubeFilter_DEFINED

#include "SkColorFilter.h"
#include "SkData.h"
#include "../private/SkMutex.h"
#include "../private/SkTemplates.h"

class SK_API SkColorCubeFilter : public SkColorFilter {
public:
    /** cubeData must containt a 3D data in the form of cube of the size:
     *  cubeDimension * cubeDimension * cubeDimension * sizeof(SkColor)
     *  This cube contains a transform where (x,y,z) maps to the (r,g,b).
     *  The alpha components of the colors must be 0xFF.
     */
    static SkColorFilter* Create(SkData* cubeData, int cubeDimension);

    void filterSpan(const SkPMColor src[], int count, SkPMColor[]) const override;
    uint32_t getFlags() const override;

#if SK_SUPPORT_GPU
    const GrFragmentProcessor* asFragmentProcessor(GrContext*) const override;
#endif

    SK_TO_STRING_OVERRIDE()
    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkColorCubeFilter)

protected:
    SkColorCubeFilter(SkData* cubeData, int cubeDimension);
    void flatten(SkWriteBuffer&) const override;

private:
    /** The cache is initialized on-demand when getProcessingLuts is called.
     */
    class ColorCubeProcesingCache {
    public:
        ColorCubeProcesingCache(int cubeDimension);

        void getProcessingLuts(const int* (*colorToIndex)[2],
                               const SkScalar* (*colorToFactors)[2],
                               const SkScalar** colorToScalar);

        int cubeDimension() const { return fCubeDimension; }

    private:
        // Working pointers. If any of these is NULL,
        // we need to recompute the corresponding cache values.
        int* fColorToIndex[2];
        SkScalar* fColorToFactors[2];
        SkScalar* fColorToScalar;

        SkAutoTMalloc<uint8_t> fLutStorage;

        const int fCubeDimension;

        // Make sure we only initialize the caches once.
        SkMutex fLutsMutex;
        bool fLutsInited;

        static void initProcessingLuts(ColorCubeProcesingCache* cache);
    };

    sk_sp<SkData> fCubeData;
    int32_t fUniqueID;

    mutable ColorCubeProcesingCache fCache;

    typedef SkColorFilter INHERITED;
};

#endif