Skia
2DGraphicsLibrary
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SkSurface.h
1 /*
2  * Copyright 2012 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 SkSurface_DEFINED
9 #define SkSurface_DEFINED
10 
11 #include "SkRefCnt.h"
12 #include "SkImage.h"
13 #include "SkSurfaceProps.h"
14 
15 class SkCanvas;
16 class SkPaint;
17 class GrContext;
18 class GrRenderTarget;
19 
31 class SK_API SkSurface : public SkRefCnt {
32 public:
42  static sk_sp<SkSurface> MakeRasterDirect(const SkImageInfo&, void* pixels, size_t rowBytes,
43  const SkSurfaceProps* = nullptr);
44 
49  static sk_sp<SkSurface> MakeRasterDirectReleaseProc(const SkImageInfo&, void* pixels, size_t rowBytes,
50  void (*releaseProc)(void* pixels, void* context),
51  void* context, const SkSurfaceProps* = nullptr);
52 
62  static sk_sp<SkSurface> MakeRaster(const SkImageInfo&, size_t rowBytes, const SkSurfaceProps*);
63 
68  const SkSurfaceProps* props = nullptr) {
69  return MakeRaster(info, 0, props);
70  }
71 
77  static sk_sp<SkSurface> MakeRasterN32Premul(int width, int height,
78  const SkSurfaceProps* props = nullptr) {
79  return MakeRaster(SkImageInfo::MakeN32Premul(width, height), props);
80  }
81 
88  static sk_sp<SkSurface> MakeFromBackendTexture(GrContext*, const GrBackendTextureDesc&,
90 
96  static sk_sp<SkSurface> MakeFromBackendRenderTarget(GrContext*,
99  const SkSurfaceProps*);
100 
109  static sk_sp<SkSurface> MakeFromBackendTextureAsRenderTarget(
111 
117  const SkSurfaceProps* props) {
118  return MakeFromBackendTexture(ctx, desc, nullptr, props);
119  }
120 
121  static sk_sp<SkSurface> MakeFromBackendRenderTarget(GrContext* ctx,
122  const GrBackendRenderTargetDesc& desc,
123  const SkSurfaceProps* props) {
124  return MakeFromBackendRenderTarget(ctx, desc, nullptr, props);
125  }
126 
127  static sk_sp<SkSurface> MakeFromBackendTextureAsRenderTarget(
128  GrContext* ctx, const GrBackendTextureDesc& desc, const SkSurfaceProps* props) {
129  return MakeFromBackendTextureAsRenderTarget(ctx, desc, nullptr, props);
130  }
131 
132 
137  static sk_sp<SkSurface> MakeRenderTarget(GrContext*, SkBudgeted, const SkImageInfo&,
138  int sampleCount, GrSurfaceOrigin,
139  const SkSurfaceProps*);
140 
141  static sk_sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
142  const SkImageInfo& info, int sampleCount,
143  const SkSurfaceProps* props) {
144  return MakeRenderTarget(context, budgeted, info, sampleCount,
145  kBottomLeft_GrSurfaceOrigin, props);
146  }
147 
148  static sk_sp<SkSurface> MakeRenderTarget(GrContext* gr, SkBudgeted b, const SkImageInfo& info) {
149  return MakeRenderTarget(gr, b, info, 0, kBottomLeft_GrSurfaceOrigin, nullptr);
150  }
151 
152 #ifdef SK_SUPPORT_LEGACY_NEW_SURFACE_API
153  static SkSurface* NewRasterDirect(const SkImageInfo& info, void* pixels, size_t rowBytes,
154  const SkSurfaceProps* props = NULL) {
155  return MakeRasterDirect(info, pixels, rowBytes, props).release();
156  }
157  static SkSurface* NewRasterDirectReleaseProc(const SkImageInfo& info, void* pixels,
158  size_t rowBytes,
159  void (*releaseProc)(void* pixels, void* context),
160  void* context, const SkSurfaceProps* props = NULL){
161  return MakeRasterDirectReleaseProc(info, pixels, rowBytes, releaseProc, context,
162  props).release();
163  }
164  static SkSurface* NewRaster(const SkImageInfo& info, size_t rowBytes,
165  const SkSurfaceProps* props) {
166  return MakeRaster(info, rowBytes, props).release();
167  }
168  static SkSurface* NewRaster(const SkImageInfo& info, const SkSurfaceProps* props = NULL) {
169  return MakeRaster(info, props).release();
170  }
171  static SkSurface* NewRasterN32Premul(int width, int height,
172  const SkSurfaceProps* props = NULL) {
173  return NewRaster(SkImageInfo::MakeN32Premul(width, height), props);
174  }
175  static SkSurface* NewFromBackendTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
176  const SkSurfaceProps* props) {
177  return MakeFromBackendTexture(ctx, desc, props).release();
178  }
179  // Legacy alias
180  static SkSurface* NewWrappedRenderTarget(GrContext* ctx, const GrBackendTextureDesc& desc,
181  const SkSurfaceProps* props) {
182  return NewFromBackendTexture(ctx, desc, props);
183  }
184  static SkSurface* NewFromBackendRenderTarget(GrContext* ctx, const GrBackendRenderTargetDesc& d,
185  const SkSurfaceProps* props) {
186  return MakeFromBackendRenderTarget(ctx, d, props).release();
187  }
188  static SkSurface* NewFromBackendTextureAsRenderTarget(GrContext* ctx,
189  const GrBackendTextureDesc& desc,
190  const SkSurfaceProps* props) {
191  return MakeFromBackendTextureAsRenderTarget(ctx, desc, props).release();
192  }
193  static SkSurface* NewRenderTarget(GrContext* ctx, SkBudgeted b, const SkImageInfo& info,
194  int sampleCount, const SkSurfaceProps* props = NULL) {
195  return MakeRenderTarget(ctx, b, info, sampleCount, props).release();
196  }
197  static SkSurface* NewRenderTarget(GrContext* gr, SkBudgeted b, const SkImageInfo& info) {
198  return NewRenderTarget(gr, b, info, 0);
199  }
200  SkSurface* newSurface(const SkImageInfo& info) { return this->makeSurface(info).release(); }
201 #endif
202 
203  int width() const { return fWidth; }
204  int height() const { return fHeight; }
205 
215  uint32_t generationID();
216 
231  };
232 
239  void notifyContentWillChange(ContentChangeMode mode);
240 
245  };
246 
247  /*
248  * These are legacy aliases which will be removed soon
249  */
250  static const BackendHandleAccess kFlushRead_TextureHandleAccess =
251  kFlushRead_BackendHandleAccess;
252  static const BackendHandleAccess kFlushWrite_TextureHandleAccess =
253  kFlushWrite_BackendHandleAccess;
254  static const BackendHandleAccess kDiscardWrite_TextureHandleAccess =
255  kDiscardWrite_BackendHandleAccess;
256 
257 
265  GrBackendObject getTextureHandle(BackendHandleAccess);
266 
273  bool getRenderTargetHandle(GrBackendObject*, BackendHandleAccess);
274 
281  SkCanvas* getCanvas();
282 
296  sk_sp<SkSurface> makeSurface(const SkImageInfo&);
297 
305  sk_sp<SkImage> makeImageSnapshot(SkBudgeted = SkBudgeted::kYes);
306 
313  enum ForceUnique {
314  kNo_ForceUnique,
315  kYes_ForceUnique
316  };
317  sk_sp<SkImage> makeImageSnapshot(SkBudgeted, ForceUnique);
318 
319 #ifdef SK_SUPPORT_LEGACY_IMAGEFACTORY
320  SkImage* newImageSnapshot(SkBudgeted budgeted = SkBudgeted::kYes) {
321  return this->makeImageSnapshot(budgeted).release();
322  }
323  SkImage* newImageSnapshot(SkBudgeted budgeted, ForceUnique force) {
324  return this->makeImageSnapshot(budgeted, force).release();
325  }
326 #endif
327 
335  void draw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*);
336 
346  bool peekPixels(SkPixmap*);
347 
348 #ifdef SK_SUPPORT_LEGACY_PEEKPIXELS_PARMS
349  const void* peekPixels(SkImageInfo* info, size_t* rowBytes);
350 #endif
351 
370  bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
371  int srcX, int srcY);
372 
373  const SkSurfaceProps& props() const { return fProps; }
374 
378  void prepareForExternalIO();
379 
380 protected:
381  SkSurface(int width, int height, const SkSurfaceProps*);
382  SkSurface(const SkImageInfo&, const SkSurfaceProps*);
383 
384  // called by subclass if their contents have changed
385  void dirtyGenerationID() {
386  fGenerationID = 0;
387  }
388 
389 private:
390  const SkSurfaceProps fProps;
391  const int fWidth;
392  const int fHeight;
393  uint32_t fGenerationID;
394 
395  typedef SkRefCnt INHERITED;
396 };
397 
398 #endif
T *SK_WARN_UNUSED_RESULT release()
Return the bare pointer, and set the internal object pointer to nullptr.
Definition: SkRefCnt.h:363
A Canvas encapsulates all of the state about drawing into a device (bitmap).
Definition: SkCanvas.h:58
Definition: GrTypes.h:557
Describe an image's dimensions and pixel type.
Definition: SkImageInfo.h:181
Describes properties and constraints of a given SkSurface.
Definition: SkSurfaceProps.h:51
Pairs SkImageInfo with actual pixels and rowbytes.
Definition: SkPixmap.h:23
SkSurface represents the backend/results of drawing to a canvas.
Definition: SkSurface.h:31
static sk_sp< SkSurface > MakeRaster(const SkImageInfo &info, const SkSurfaceProps *props=nullptr)
Allocate a new surface, automatically computing the rowBytes.
Definition: SkSurface.h:67
Definition: SkRefCnt.h:135
Gr can wrap an existing render target created by the client in the 3D API with a GrRenderTarget objec...
Definition: GrTypes.h:588
The SkPaint class holds the style and color information about how to draw geometries, text and bitmaps.
Definition: SkPaint.h:46
caller must over-write the entire backend object
Definition: SkSurface.h:244
static SkImageInfo MakeN32Premul(int width, int height, sk_sp< SkColorSpace > cs=nullptr)
Sets colortype to the native ARGB32 type, and the alphatype to premul.
Definition: SkImageInfo.h:212
static sk_sp< SkSurface > MakeFromBackendTexture(GrContext *ctx, const GrBackendTextureDesc &desc, const SkSurfaceProps *props)
Legacy versions of the above factories, without color space support.
Definition: SkSurface.h:116
static sk_sp< SkSurface > MakeRasterN32Premul(int width, int height, const SkSurfaceProps *props=nullptr)
Helper version of NewRaster.
Definition: SkSurface.h:77
Shared pointer class to wrap classes that support a ref()/unref() interface.
Definition: SkRefCnt.h:258
ContentChangeMode
Modes that can be passed to notifyContentWillChange.
Definition: SkSurface.h:220
Definition: GrContext.h:48
Use this mode if prior surface contents need to be preserved or if in doubt.
Definition: SkSurface.h:230
caller may read from the backend object
Definition: SkSurface.h:242
Use this mode if it is known that the upcoming content changes will clear or overwrite prior contents...
Definition: SkSurface.h:225
ForceUnique
In rare instances a client may want a unique copy of the SkSurface's contents in an image snapshot...
Definition: SkSurface.h:313
GrRenderTarget represents a 2D buffer of pixels that can be rendered to.
Definition: GrRenderTarget.h:26
caller may write to the backend object
Definition: SkSurface.h:243
SkImage is an abstraction for drawing a rectagle of pixels, though the particular type of image could...
Definition: SkImage.h:45
BackendHandleAccess
Definition: SkSurface.h:241