1/*
2 * Copyright 2017 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
8#ifndef SkSurfaceCharacterization_DEFINED
9#define SkSurfaceCharacterization_DEFINED
10
11#include "GrTypes.h"
12
13#include "SkColorSpace.h"
14#include "SkRefCnt.h"
15#include "SkSurfaceProps.h"
16
17class SkColorSpace;
18
19// This define can be used to swap between the default (raster) DDL implementation and the
20// gpu implementation.
21#define SK_RASTER_RECORDER_IMPLEMENTATION 1
22
23#if SK_SUPPORT_GPU
24#include "GrContext.h"
25
26/** \class SkSurfaceCharacterization
27    A surface characterization contains all the information Ganesh requires to makes its internal
28    rendering decisions. When passed into a SkDeferredDisplayListRecorder it will copy the
29    data and pass it on to the SkDeferredDisplayList if/when it is created. Note that both of
30    those objects (the Recorder and the DisplayList) will take a ref on the
31    GrContextThreadSafeProxy and SkColorSpace objects.
32*/
33class SkSurfaceCharacterization {
34public:
35    enum class Textureable : bool { kNo = false, kYes = true };
36    enum class MipMapped : bool { kNo = false, kYes = true };
37
38    SkSurfaceCharacterization()
39            : fCacheMaxResourceBytes(0)
40            , fOrigin(kBottomLeft_GrSurfaceOrigin)
41            , fWidth(0)
42            , fHeight(0)
43            , fConfig(kUnknown_GrPixelConfig)
44            , fFSAAType(GrFSAAType::kNone)
45            , fStencilCnt(0)
46            , fIsTextureable(Textureable::kYes)
47            , fIsMipMapped(MipMapped::kYes)
48            , fSurfaceProps(0, kUnknown_SkPixelGeometry) {
49    }
50
51    SkSurfaceCharacterization(SkSurfaceCharacterization&&) = default;
52    SkSurfaceCharacterization& operator=(SkSurfaceCharacterization&&) = default;
53
54    SkSurfaceCharacterization(const SkSurfaceCharacterization&) = default;
55    SkSurfaceCharacterization& operator=(const SkSurfaceCharacterization& other) = default;
56
57    GrContextThreadSafeProxy* contextInfo() const { return fContextInfo.get(); }
58    sk_sp<GrContextThreadSafeProxy> refContextInfo() const { return fContextInfo; }
59    size_t cacheMaxResourceBytes() const { return fCacheMaxResourceBytes; }
60
61    bool isValid() const { return kUnknown_GrPixelConfig != fConfig; }
62
63    GrSurfaceOrigin origin() const { return fOrigin; }
64    int width() const { return fWidth; }
65    int height() const { return fHeight; }
66    GrPixelConfig config() const { return fConfig; }
67    GrFSAAType fsaaType() const { return fFSAAType; }
68    int stencilCount() const { return fStencilCnt; }
69    bool isTextureable() const { return Textureable::kYes == fIsTextureable; }
70    bool isMipMapped() const { return MipMapped::kYes == fIsMipMapped; }
71    SkColorSpace* colorSpace() const { return fColorSpace.get(); }
72    sk_sp<SkColorSpace> refColorSpace() const { return fColorSpace; }
73    const SkSurfaceProps& surfaceProps()const { return fSurfaceProps; }
74
75private:
76    friend class SkSurface_Gpu; // for 'set'
77    friend class GrContextThreadSafeProxy; // for private ctor
78
79    SkSurfaceCharacterization(sk_sp<GrContextThreadSafeProxy> contextInfo,
80                              size_t cacheMaxResourceBytes,
81                              GrSurfaceOrigin origin, int width, int height,
82                              GrPixelConfig config, GrFSAAType FSAAType, int stencilCnt,
83                              Textureable isTextureable, MipMapped isMipMapped,
84                              sk_sp<SkColorSpace> colorSpace,
85                              const SkSurfaceProps& surfaceProps)
86            : fContextInfo(std::move(contextInfo))
87            , fCacheMaxResourceBytes(cacheMaxResourceBytes)
88            , fOrigin(origin)
89            , fWidth(width)
90            , fHeight(height)
91            , fConfig(config)
92            , fFSAAType(FSAAType)
93            , fStencilCnt(stencilCnt)
94            , fIsTextureable(isTextureable)
95            , fIsMipMapped(isMipMapped)
96            , fColorSpace(std::move(colorSpace))
97            , fSurfaceProps(surfaceProps) {
98    }
99
100    void set(sk_sp<GrContextThreadSafeProxy> contextInfo,
101             size_t cacheMaxResourceBytes,
102             GrSurfaceOrigin origin,
103             int width, int height,
104             GrPixelConfig config,
105             GrFSAAType fsaaType,
106             int stencilCnt,
107             Textureable isTextureable,
108             MipMapped isMipMapped,
109             sk_sp<SkColorSpace> colorSpace,
110             const SkSurfaceProps& surfaceProps) {
111        SkASSERT(MipMapped::kNo == isMipMapped || Textureable::kYes == isTextureable);
112
113        fContextInfo = contextInfo;
114        fCacheMaxResourceBytes = cacheMaxResourceBytes;
115
116        fOrigin = origin;
117        fWidth = width;
118        fHeight = height;
119        fConfig = config;
120        fFSAAType = fsaaType;
121        fStencilCnt = stencilCnt;
122        fIsTextureable = isTextureable;
123        fIsMipMapped = isMipMapped;
124        fColorSpace = std::move(colorSpace);
125        fSurfaceProps = surfaceProps;
126    }
127
128    sk_sp<GrContextThreadSafeProxy> fContextInfo;
129    size_t                          fCacheMaxResourceBytes;
130
131    GrSurfaceOrigin                 fOrigin;
132    int                             fWidth;
133    int                             fHeight;
134    GrPixelConfig                   fConfig;
135    GrFSAAType                      fFSAAType;
136    int                             fStencilCnt;
137    Textureable                     fIsTextureable;
138    MipMapped                       fIsMipMapped;
139    sk_sp<SkColorSpace>             fColorSpace;
140    SkSurfaceProps                  fSurfaceProps;
141};
142
143#else// !SK_SUPPORT_GPU
144
145class SkSurfaceCharacterization {
146public:
147    SkSurfaceCharacterization()
148            : fWidth(0)
149            , fHeight(0)
150            , fSurfaceProps(0, kUnknown_SkPixelGeometry) {
151    }
152
153    bool isValid() const { return false; }
154
155    int width() const { return fWidth; }
156    int height() const { return fHeight; }
157    SkColorSpace* colorSpace() const { return fColorSpace.get(); }
158    sk_sp<SkColorSpace> refColorSpace() const { return fColorSpace; }
159    const SkSurfaceProps& surfaceProps()const { return fSurfaceProps; }
160
161private:
162    int                             fWidth;
163    int                             fHeight;
164    sk_sp<SkColorSpace>             fColorSpace;
165    SkSurfaceProps                  fSurfaceProps;
166};
167
168#endif
169
170#endif
171