Skia
2D Graphics Library
SkSurface.h
Go to the documentation of this file.
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 "include/core/SkImage.h"
13 #include "include/core/SkPixmap.h"
14 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkScalar.h"
18 #include "include/core/SkTypes.h"
19 
20 #include <cstddef>
21 #include <cstdint>
22 #include <memory>
23 
24 class GrBackendSemaphore;
25 class GrBackendTexture;
26 class GrRecordingContext;
27 class GrSurfaceCharacterization;
28 enum GrSurfaceOrigin : int;
29 class SkBitmap;
30 class SkCanvas;
31 class SkCapabilities;
32 class SkColorSpace;
33 class SkPaint;
34 class SkSurface;
35 struct SkIRect;
36 struct SkISize;
37 
38 namespace skgpu::graphite {
39 class Recorder;
40 }
41 
42 namespace SkSurfaces {
43 
45  kNoAccess,
46  kPresent,
47 };
48 
58 SK_API sk_sp<SkSurface> Null(int width, int height);
59 
77 SK_API sk_sp<SkSurface> Raster(const SkImageInfo& imageInfo,
78  size_t rowBytes,
79  const SkSurfaceProps* surfaceProps);
80 inline sk_sp<SkSurface> Raster(const SkImageInfo& imageInfo,
81  const SkSurfaceProps* props = nullptr) {
82  return Raster(imageInfo, 0, props);
83 }
84 
108 SK_API sk_sp<SkSurface> WrapPixels(const SkImageInfo& imageInfo,
109  void* pixels,
110  size_t rowBytes,
111  const SkSurfaceProps* surfaceProps = nullptr);
112 inline sk_sp<SkSurface> WrapPixels(const SkPixmap& pm, const SkSurfaceProps* props = nullptr) {
113  return WrapPixels(pm.info(), pm.writable_addr(), pm.rowBytes(), props);
114 }
115 
116 using PixelsReleaseProc = void(void* pixels, void* context);
117 
142 SK_API sk_sp<SkSurface> WrapPixels(const SkImageInfo& imageInfo,
143  void* pixels,
144  size_t rowBytes,
146  void* context,
147  const SkSurfaceProps* surfaceProps = nullptr);
148 } // namespace SkSurfaces
149 
161 class SK_API SkSurface : public SkRefCnt {
162 public:
172  bool isCompatible(const GrSurfaceCharacterization& characterization) const;
173 
178  int width() const { return fWidth; }
179 
184  int height() const { return fHeight; }
185 
188  virtual SkImageInfo imageInfo() const { return SkImageInfo::MakeUnknown(fWidth, fHeight); }
189 
198  uint32_t generationID();
199 
206  };
207 
216 
222 
227  skgpu::graphite::Recorder* recorder() const;
228 
229  enum class BackendHandleAccess {
230  kFlushRead,
231  kFlushWrite,
232  kDiscardWrite,
233 
234  // Legacy names, remove when clients are migrated
235  kFlushRead_BackendHandleAccess = kFlushRead,
236  kFlushWrite_BackendHandleAccess = kFlushWrite,
237  kDiscardWrite_BackendHandleAccess = kDiscardWrite,
238  };
239 
240  // Legacy names, remove when clients are migrated
241  static constexpr BackendHandleAccess kFlushRead_BackendHandleAccess =
242  BackendHandleAccess::kFlushRead;
243  static constexpr BackendHandleAccess kFlushWrite_BackendHandleAccess =
244  BackendHandleAccess::kFlushWrite;
245  static constexpr BackendHandleAccess kDiscardWrite_BackendHandleAccess =
246  BackendHandleAccess::kDiscardWrite;
247 
249  using ReleaseContext = void*;
252 
268  virtual bool replaceBackendTexture(const GrBackendTexture& backendTexture,
269  GrSurfaceOrigin origin,
270  ContentChangeMode mode = kRetain_ContentChangeMode,
271  TextureReleaseProc = nullptr,
272  ReleaseContext = nullptr) = 0;
273 
283 
289 
304 
308  sk_sp<SkSurface> makeSurface(int width, int height);
309 
319 
332 
346  void draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkSamplingOptions& sampling,
347  const SkPaint* paint);
348 
349  void draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint = nullptr) {
350  this->draw(canvas, x, y, SkSamplingOptions(), paint);
351  }
352 
364  bool peekPixels(SkPixmap* pixmap);
365 
395  bool readPixels(const SkPixmap& dst, int srcX, int srcY);
396 
426  bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
427  int srcX, int srcY);
428 
459  bool readPixels(const SkBitmap& dst, int srcX, int srcY);
460 
462 
464  using ReadPixelsContext = void*;
465 
469  using ReadPixelsCallback = void(ReadPixelsContext, std::unique_ptr<const AsyncReadResult>);
470 
476 
508  const SkIRect& srcRect,
509  RescaleGamma rescaleGamma,
510  RescaleMode rescaleMode,
511  ReadPixelsCallback callback,
512  ReadPixelsContext context);
513 
544  sk_sp<SkColorSpace> dstColorSpace,
545  const SkIRect& srcRect,
546  const SkISize& dstSize,
547  RescaleGamma rescaleGamma,
548  RescaleMode rescaleMode,
549  ReadPixelsCallback callback,
550  ReadPixelsContext context);
551 
558  sk_sp<SkColorSpace> dstColorSpace,
559  const SkIRect& srcRect,
560  const SkISize& dstSize,
561  RescaleGamma rescaleGamma,
562  RescaleMode rescaleMode,
563  ReadPixelsCallback callback,
564  ReadPixelsContext context);
565 
581  void writePixels(const SkPixmap& src, int dstX, int dstY);
582 
598  void writePixels(const SkBitmap& src, int dstX, int dstY);
599 
604  const SkSurfaceProps& props() const { return fProps; }
605 
624  bool wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores,
625  bool deleteSemaphoresAfterWait = true);
626 
639  bool characterize(GrSurfaceCharacterization* characterization) const;
640 
641 protected:
642  SkSurface(int width, int height, const SkSurfaceProps* surfaceProps);
643  SkSurface(const SkImageInfo& imageInfo, const SkSurfaceProps* surfaceProps);
644 
645  // called by subclass if their contents have changed
647  fGenerationID = 0;
648  }
649 
650 private:
651  const SkSurfaceProps fProps;
652  const int fWidth;
653  const int fHeight;
654  uint32_t fGenerationID;
655 
656  using INHERITED = SkRefCnt;
657 };
658 
659 #endif
GrSurfaceOrigin
GPU SkImage and SkSurfaces can be stored such that (0, 0) in texture space may correspond to either t...
Definition: GrTypes.h:156
SkYUVColorSpace
Definition: SkImageInfo.h:68
float SkScalar
Definition: SkScalar.h:14
Wrapper class for passing into and receiving data from Ganesh about a backend semaphore object.
Definition: GrBackendSemaphore.h:34
Definition: GrBackendSurface.h:176
Definition: GrRecordingContext.h:42
SkBitmap describes a two-dimensional raster pixel array.
Definition: SkBitmap.h:59
SkCanvas provides an interface for drawing, and how the drawing is clipped and transformed.
Definition: SkCanvas.h:99
Definition: SkCapabilities.h:21
Definition: SkColorSpace.h:107
The result from asyncRescaleAndReadPixels() or asyncRescaleAndReadPixelsYUV420().
Definition: SkImage.h:552
RescaleMode
Definition: SkImage.h:578
RescaleGamma
Definition: SkImage.h:576
SkPaint controls options applied when drawing.
Definition: SkPaint.h:44
SkPixmap provides a utility to pair SkImageInfo with pixels and row bytes.
Definition: SkPixmap.h:40
size_t rowBytes() const
Returns row bytes, the interval from one pixel row to the next.
Definition: SkPixmap.h:145
void * writable_addr() const
Returns writable base pixel address.
Definition: SkPixmap.h:483
const SkImageInfo & info() const
Returns width, height, SkAlphaType, SkColorType, and SkColorSpace.
Definition: SkPixmap.h:135
Definition: SkRefCnt.h:119
Describes properties and constraints of a given SkSurface.
Definition: SkSurfaceProps.h:52
SkSurface is responsible for managing the pixels that a canvas draws into.
Definition: SkSurface.h:161
void dirtyGenerationID()
Definition: SkSurface.h:646
ContentChangeMode
Definition: SkSurface.h:203
@ kDiscard_ContentChangeMode
discards surface on change
Definition: SkSurface.h:204
@ kRetain_ContentChangeMode
preserves surface on change
Definition: SkSurface.h:205
const SkSurfaceProps & props() const
Returns SkSurfaceProps for surface.
Definition: SkSurface.h:604
bool readPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY)
Copies SkRect of pixels from SkCanvas into dstPixels.
void(ReadPixelsContext, std::unique_ptr< const AsyncReadResult >) ReadPixelsCallback
Client-provided callback to asyncRescaleAndReadPixels() or asyncRescaleAndReadPixelsYUV420() that is ...
Definition: SkSurface.h:469
int width() const
Returns pixel count in each row; may be zero or greater.
Definition: SkSurface.h:178
sk_sp< const SkCapabilities > capabilities()
Returns SkCapabilities that describes the capabilities of the SkSurface's device.
bool characterize(GrSurfaceCharacterization *characterization) const
Initializes GrSurfaceCharacterization that can be used to perform GPU back-end processing in a separa...
virtual bool replaceBackendTexture(const GrBackendTexture &backendTexture, GrSurfaceOrigin origin, ContentChangeMode mode=kRetain_ContentChangeMode, TextureReleaseProc=nullptr, ReleaseContext=nullptr)=0
If the surface was made via MakeFromBackendTexture then it's backing texture may be substituted with ...
sk_sp< SkSurface > makeSurface(const SkImageInfo &imageInfo)
Returns a compatible SkSurface, or nullptr.
void writePixels(const SkBitmap &src, int dstX, int dstY)
Copies SkRect of pixels from the src SkBitmap to the SkSurface.
void draw(SkCanvas *canvas, SkScalar x, SkScalar y, const SkSamplingOptions &sampling, const SkPaint *paint)
Draws SkSurface contents to canvas, with its top-left corner at (x, y).
GrRecordingContext * recordingContext() const
Returns the recording context being used by the SkSurface.
void asyncRescaleAndReadPixels(const SkImageInfo &info, const SkIRect &srcRect, RescaleGamma rescaleGamma, RescaleMode rescaleMode, ReadPixelsCallback callback, ReadPixelsContext context)
Makes surface pixel data available to caller, possibly asynchronously.
bool isCompatible(const GrSurfaceCharacterization &characterization) const
Is this surface compatible with the provided characterization?
virtual SkImageInfo imageInfo() const
Returns an ImageInfo describing the surface.
Definition: SkSurface.h:188
void draw(SkCanvas *canvas, SkScalar x, SkScalar y, const SkPaint *paint=nullptr)
Definition: SkSurface.h:349
bool peekPixels(SkPixmap *pixmap)
Copies SkSurface pixel address, row bytes, and SkImageInfo to SkPixmap, if address is available,...
sk_sp< SkSurface > makeSurface(int width, int height)
Calls makeSurface(ImageInfo) with the same ImageInfo as this surface, but with the specified width an...
SkSurface(const SkImageInfo &imageInfo, const SkSurfaceProps *surfaceProps)
sk_sp< SkImage > makeImageSnapshot()
Returns SkImage capturing SkSurface contents.
int height() const
Returns pixel row count; may be zero or greater.
Definition: SkSurface.h:184
void asyncRescaleAndReadPixelsYUVA420(SkYUVColorSpace yuvColorSpace, sk_sp< SkColorSpace > dstColorSpace, const SkIRect &srcRect, const SkISize &dstSize, RescaleGamma rescaleGamma, RescaleMode rescaleMode, ReadPixelsCallback callback, ReadPixelsContext context)
Identical to asyncRescaleAndReadPixelsYUV420 but a fourth plane is returned in the AsyncReadResult pa...
void(*)(ReleaseContext) TextureReleaseProc
User function called when supplied texture may be deleted.
Definition: SkSurface.h:251
void * ReleaseContext
Caller data passed to TextureReleaseProc; may be nullptr.
Definition: SkSurface.h:249
bool readPixels(const SkBitmap &dst, int srcX, int srcY)
Copies SkRect of pixels from SkSurface into bitmap.
void asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace, sk_sp< SkColorSpace > dstColorSpace, const SkIRect &srcRect, const SkISize &dstSize, RescaleGamma rescaleGamma, RescaleMode rescaleMode, ReadPixelsCallback callback, ReadPixelsContext context)
Similar to asyncRescaleAndReadPixels but performs an additional conversion to YUV.
SkSurface(int width, int height, const SkSurfaceProps *surfaceProps)
void writePixels(const SkPixmap &src, int dstX, int dstY)
Copies SkRect of pixels from the src SkPixmap to the SkSurface.
void notifyContentWillChange(ContentChangeMode mode)
Notifies that SkSurface contents will be changed by code outside of Skia.
BackendHandleAccess
Definition: SkSurface.h:229
bool wait(int numSemaphores, const GrBackendSemaphore *waitSemaphores, bool deleteSemaphoresAfterWait=true)
Inserts a list of GPU semaphores that the current GPU-backed API must wait on before executing any mo...
SkCanvas * getCanvas()
Returns SkCanvas that draws into SkSurface.
bool readPixels(const SkPixmap &dst, int srcX, int srcY)
Copies SkRect of pixels to dst.
void * ReadPixelsContext
Client-provided context that is passed to client-provided ReadPixelsContext.
Definition: SkSurface.h:464
skgpu::graphite::Recorder * recorder() const
Returns the recorder being used by the SkSurface.
sk_sp< SkImage > makeImageSnapshot(const SkIRect &bounds)
Like the no-parameter version, this returns an image of the current surface contents.
uint32_t generationID()
Returns unique value identifying the content of SkSurface.
Shared pointer class to wrap classes that support a ref()/unref() interface.
Definition: SkRefCnt.h:220
void * ReleaseContext
Caller data passed to RasterReleaseProc; may be nullptr.
Definition: SkImage.h:50
Definition: SkSurface.h:42
SK_API sk_sp< SkSurface > Null(int width, int height)
Returns SkSurface without backing pixels.
void(void *pixels, void *context) PixelsReleaseProc
Definition: SkSurface.h:116
BackendSurfaceAccess
Definition: SkSurface.h:44
@ kPresent
back-end surface will be used for presenting to screen
@ kNoAccess
back-end surface will not be used by client
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
Allocates raster SkSurface.
SK_API sk_sp< SkSurface > WrapPixels(const SkImageInfo &imageInfo, void *pixels, size_t rowBytes, const SkSurfaceProps *surfaceProps=nullptr)
Allocates raster SkSurface.
Definition: SkCanvas.h:73
SkIRect holds four 32-bit integer coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:32
Definition: SkSize.h:15
Describes pixel dimensions and encoding.
Definition: SkImageInfo.h:194
static SkImageInfo MakeUnknown()
Creates SkImageInfo from integral dimensions width and height set to zero, kUnknown_SkColorType,...
Definition: SkImageInfo.h:343
Definition: SkSamplingOptions.h:58