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