blob: d97164820228a41794f99addc6a021c2a5393493 [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
11#include "SkRefCnt.h"
12#include "SkSurfaceProps.h"
13
14class GrContext;
15struct GrSurfaceDesc;
16class SkCanvas;
17struct SkImageInfo;
18class 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 */
28class SkSpecialSurface : public SkRefCnt {
29public:
30 const SkSurfaceProps& props() const { return fProps; }
31
32 int width() const { return fSubset.width(); }
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
halcanary9d524f22016-03-29 09:03:52 -070038 * surface.
robertphillipsb6c65e92016-02-04 10:52:42 -080039 *
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.
robertphillipse8c34972016-02-16 12:09:36 -080048 *
49 * Note: the caller inherits a ref from this call that must be balanced
robertphillipsb6c65e92016-02-04 10:52:42 -080050 */
robertphillips37bd7c32016-03-17 14:31:39 -070051 sk_sp<SkSpecialImage> makeImageSnapshot();
robertphillipsb6c65e92016-02-04 10:52:42 -080052
robertphillipsc91fd342016-04-25 12:32:54 -070053#if SK_SUPPORT_GPU
robertphillipsb6c65e92016-02-04 10:52:42 -080054 /**
robertphillipsb6c65e92016-02-04 10:52:42 -080055 * Allocate a new GPU-backed SkSpecialSurface. If the requested surface cannot
56 * be created, nullptr will be returned.
57 */
robertphillips4df16562016-04-28 15:09:34 -070058 static sk_sp<SkSpecialSurface> MakeRenderTarget(GrContext*,
59 int width, int height,
60 GrPixelConfig config);
robertphillipsc91fd342016-04-25 12:32:54 -070061#endif
robertphillipsb6c65e92016-02-04 10:52:42 -080062
63 /**
64 * Use and existing SkBitmap as the backing store.
65 */
robertphillips3e302272016-04-20 11:48:36 -070066 static sk_sp<SkSpecialSurface> MakeFromBitmap(const SkIRect& subset, SkBitmap& bm,
robertphillips37bd7c32016-03-17 14:31:39 -070067 const SkSurfaceProps* = nullptr);
robertphillipsb6c65e92016-02-04 10:52:42 -080068
69 /**
70 * Return a new CPU-backed surface, with the memory for the pixels automatically
71 * allocated.
72 *
73 * If the requested surface cannot be created, or the request is not a
74 * supported configuration, nullptr will be returned.
75 */
robertphillips3e302272016-04-20 11:48:36 -070076 static sk_sp<SkSpecialSurface> MakeRaster(const SkImageInfo&,
robertphillips37bd7c32016-03-17 14:31:39 -070077 const SkSurfaceProps* = nullptr);
robertphillipsb6c65e92016-02-04 10:52:42 -080078
79protected:
robertphillips3e302272016-04-20 11:48:36 -070080 SkSpecialSurface(const SkIRect& subset, const SkSurfaceProps*);
robertphillipsb6c65e92016-02-04 10:52:42 -080081
82 // For testing only
83 friend class TestingSpecialSurfaceAccess;
84 const SkIRect& subset() const { return fSubset; }
85
86private:
87 const SkSurfaceProps fProps;
88 const SkIRect fSubset;
89
robertphillipsb6c65e92016-02-04 10:52:42 -080090 typedef SkRefCnt INHERITED;
91};
92
93#endif