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 "SkImageInfo.h"
12 #include "SkRefCnt.h"
13 #include "SkSurfaceProps.h"
14 
15 #if SK_SUPPORT_GPU
16 #include "GrTypes.h"
17 #endif
18 
19 class GrContext;
20 class SkBitmap;
21 class SkCanvas;
22 class SkSpecialImage;
23 
24 /**
25  * SkSpecialSurface is a restricted form of SkSurface solely for internal use. It differs
26  * from SkSurface in that:
27  *     - it can be backed by GrTextures larger than [ fWidth, fHeight ]
28  *     - it can't be used for tiling
29  *     - it becomes inactive once a snapshot of it is taken (i.e., no copy-on-write)
30  *     - it has no generation ID
31  */
32 class SkSpecialSurface : public SkRefCnt {
33 public:
props()34     const SkSurfaceProps& props() const { return fProps; }
35 
width()36     int width() const { return fSubset.width(); }
height()37     int height() const { return fSubset.height(); }
38 
39     /**
40     *  Return a canvas that will draw into this surface. This will always
41     *  return the same canvas for a given surface, and is managed/owned by the
42     *  surface.
43     *
44     *  The canvas will be invalid after 'newImageSnapshot' is called.
45     */
46     SkCanvas* getCanvas();
47 
48     /**
49     *  Returns an image of the current state of the surface pixels up to this
50     *  point. The canvas returned by 'getCanvas' becomes invalidated by this
51     *  call and no more drawing to this surface is allowed.
52     *
53     *  Note: the caller inherits a ref from this call that must be balanced
54     */
55     sk_sp<SkSpecialImage> makeImageSnapshot();
56 
57 #if SK_SUPPORT_GPU
58     /**
59      *  Allocate a new GPU-backed SkSpecialSurface. If the requested surface cannot
60      *  be created, nullptr will be returned.
61      */
62     static sk_sp<SkSpecialSurface> MakeRenderTarget(GrContext*,
63                                                     int width, int height,
64                                                     GrPixelConfig config,
65                                                     sk_sp<SkColorSpace> colorSpace);
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     typedef SkRefCnt INHERITED;
96 };
97 
98 #endif
99