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