blob: 96b74a4dacc4ea781a1e12be31315dccc7777932 [file] [log] [blame]
robertphillipsb6c65e92016-02-04 10:52:42 -08001/*
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
Hal Canary6b20a552017-02-07 14:09:38 -050011#include "SkImageInfo.h"
robertphillipsb6c65e92016-02-04 10:52:42 -080012#include "SkRefCnt.h"
13#include "SkSurfaceProps.h"
14
Hal Canary6b20a552017-02-07 14:09:38 -050015#if SK_SUPPORT_GPU
16#include "GrTypes.h"
17#endif
18
robertphillipsb6c65e92016-02-04 10:52:42 -080019class GrContext;
Hal Canary6b20a552017-02-07 14:09:38 -050020class SkBitmap;
robertphillipsb6c65e92016-02-04 10:52:42 -080021class SkCanvas;
robertphillipsb6c65e92016-02-04 10:52:42 -080022class 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 */
32class SkSpecialSurface : public SkRefCnt {
33public:
34 const SkSurfaceProps& props() const { return fProps; }
35
36 int width() const { return fSubset.width(); }
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
halcanary9d524f22016-03-29 09:03:52 -070042 * surface.
robertphillipsb6c65e92016-02-04 10:52:42 -080043 *
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.
robertphillipse8c34972016-02-16 12:09:36 -080052 *
53 * Note: the caller inherits a ref from this call that must be balanced
robertphillipsb6c65e92016-02-04 10:52:42 -080054 */
robertphillips37bd7c32016-03-17 14:31:39 -070055 sk_sp<SkSpecialImage> makeImageSnapshot();
robertphillipsb6c65e92016-02-04 10:52:42 -080056
robertphillipsc91fd342016-04-25 12:32:54 -070057#if SK_SUPPORT_GPU
robertphillipsb6c65e92016-02-04 10:52:42 -080058 /**
robertphillipsb6c65e92016-02-04 10:52:42 -080059 * Allocate a new GPU-backed SkSpecialSurface. If the requested surface cannot
60 * be created, nullptr will be returned.
61 */
robertphillips4df16562016-04-28 15:09:34 -070062 static sk_sp<SkSpecialSurface> MakeRenderTarget(GrContext*,
63 int width, int height,
brianosmandfe4f2e2016-07-21 13:28:36 -070064 GrPixelConfig config,
65 sk_sp<SkColorSpace> colorSpace);
robertphillipsc91fd342016-04-25 12:32:54 -070066#endif
robertphillipsb6c65e92016-02-04 10:52:42 -080067
68 /**
69 * Use and existing SkBitmap as the backing store.
70 */
robertphillips3e302272016-04-20 11:48:36 -070071 static sk_sp<SkSpecialSurface> MakeFromBitmap(const SkIRect& subset, SkBitmap& bm,
robertphillips37bd7c32016-03-17 14:31:39 -070072 const SkSurfaceProps* = nullptr);
robertphillipsb6c65e92016-02-04 10:52:42 -080073
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 */
robertphillips3e302272016-04-20 11:48:36 -070081 static sk_sp<SkSpecialSurface> MakeRaster(const SkImageInfo&,
robertphillips37bd7c32016-03-17 14:31:39 -070082 const SkSurfaceProps* = nullptr);
robertphillipsb6c65e92016-02-04 10:52:42 -080083
84protected:
robertphillips3e302272016-04-20 11:48:36 -070085 SkSpecialSurface(const SkIRect& subset, const SkSurfaceProps*);
robertphillipsb6c65e92016-02-04 10:52:42 -080086
87 // For testing only
88 friend class TestingSpecialSurfaceAccess;
89 const SkIRect& subset() const { return fSubset; }
90
91private:
92 const SkSurfaceProps fProps;
93 const SkIRect fSubset;
94
robertphillipsb6c65e92016-02-04 10:52:42 -080095 typedef SkRefCnt INHERITED;
96};
97
98#endif