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 
8 #ifndef SkSpecialSurface_DEFINED
9 #define SkSpecialSurface_DEFINED
10 
11 #include "include/core/SkImageInfo.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkSurfaceProps.h"
14 
15 #if SK_SUPPORT_GPU
16 #include "include/private/GrTypesPriv.h"
17 #endif
18 
19 class GrBackendFormat;
20 class GrRecordingContext;
21 class SkBitmap;
22 class SkCanvas;
23 class SkSpecialImage;
24 
25 /**
26  * SkSpecialSurface is a restricted form of SkSurface solely for internal use. It differs
27  * from SkSurface in that:
28  *     - it can be backed by GrTextures larger than [ fWidth, fHeight ]
29  *     - it can't be used for tiling
30  *     - it becomes inactive once a snapshot of it is taken (i.e., no copy-on-write)
31  *     - it has no generation ID
32  */
33 class SkSpecialSurface : public SkRefCnt {
34 public:
props()35     const SkSurfaceProps& props() const { return fProps; }
36 
width()37     int width() const { return fSubset.width(); }
height()38     int height() const { return fSubset.height(); }
39 
40     /**
41     *  Return a canvas that will draw into this surface. This will always
42     *  return the same canvas for a given surface, and is managed/owned by the
43     *  surface.
44     *
45     *  The canvas will be invalid after 'newImageSnapshot' is called.
46     */
47     SkCanvas* getCanvas();
48 
49     /**
50     *  Returns an image of the current state of the surface pixels up to this
51     *  point. The canvas returned by 'getCanvas' becomes invalidated by this
52     *  call and no more drawing to this surface is allowed.
53     *
54     *  Note: the caller inherits a ref from this call that must be balanced
55     */
56     sk_sp<SkSpecialImage> makeImageSnapshot();
57 
58 #if SK_SUPPORT_GPU
59     /**
60      *  Allocate a new GPU-backed SkSpecialSurface. If the requested surface cannot
61      *  be created, nullptr will be returned.
62      */
63     static sk_sp<SkSpecialSurface> MakeRenderTarget(GrRecordingContext*, int width, int height,
64                                                     GrColorType, sk_sp<SkColorSpace> colorSpace,
65                                                     const SkSurfaceProps* = nullptr);
66 #endif
67 
68     /**
69      * Use and existing SkBitmap as the backing store.
70      */
71     static sk_sp<SkSpecialSurface> MakeFromBitmap(const SkIRect& subset, SkBitmap& bm,
72                                                   const SkSurfaceProps* = nullptr);
73 
74     /**
75      *  Return a new CPU-backed surface, with the memory for the pixels automatically
76      *  allocated.
77      *
78      *  If the requested surface cannot be created, or the request is not a
79      *  supported configuration, nullptr will be returned.
80      */
81     static sk_sp<SkSpecialSurface> MakeRaster(const SkImageInfo&,
82                                               const SkSurfaceProps* = nullptr);
83 
84 protected:
85     SkSpecialSurface(const SkIRect& subset, const SkSurfaceProps*);
86 
87     // For testing only
88     friend class TestingSpecialSurfaceAccess;
subset()89     const SkIRect& subset() const { return fSubset; }
90 
91 private:
92     const SkSurfaceProps fProps;
93     const SkIRect        fSubset;
94 
95     using INHERITED = SkRefCnt;
96 };
97 
98 #endif
99