2012-07-27 21:10:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkSurface_DEFINED
|
|
|
|
#define SkSurface_DEFINED
|
|
|
|
|
|
|
|
#include "SkRefCnt.h"
|
|
|
|
#include "SkImage.h"
|
2014-09-22 14:29:03 +00:00
|
|
|
#include "SkSurfaceProps.h"
|
2012-07-27 21:10:42 +00:00
|
|
|
|
|
|
|
class SkCanvas;
|
|
|
|
class SkPaint;
|
2012-07-31 15:45:27 +00:00
|
|
|
class GrContext;
|
|
|
|
class GrRenderTarget;
|
2012-07-27 21:10:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SkSurface represents the backend/results of drawing to a canvas. For raster
|
|
|
|
* drawing, the surface will be pixels, but (for example) when drawing into
|
|
|
|
* a PDF or Picture canvas, the surface stores the recorded commands.
|
|
|
|
*
|
|
|
|
* To draw into a canvas, first create the appropriate type of Surface, and
|
|
|
|
* then request the canvas from the surface.
|
|
|
|
*/
|
2013-04-18 13:28:19 +00:00
|
|
|
class SK_API SkSurface : public SkRefCnt {
|
2012-07-27 21:10:42 +00:00
|
|
|
public:
|
2012-08-28 12:43:54 +00:00
|
|
|
SK_DECLARE_INST_COUNT(SkSurface)
|
2012-08-16 14:58:06 +00:00
|
|
|
|
2012-07-27 21:10:42 +00:00
|
|
|
/**
|
|
|
|
* Create a new surface, using the specified pixels/rowbytes as its
|
|
|
|
* backend.
|
|
|
|
*
|
|
|
|
* If the requested surface cannot be created, or the request is not a
|
|
|
|
* supported configuration, NULL will be returned.
|
|
|
|
*/
|
2014-09-22 14:29:03 +00:00
|
|
|
static SkSurface* NewRasterDirect(const SkImageInfo&, void* pixels, size_t rowBytes,
|
|
|
|
const SkSurfaceProps* = NULL);
|
2012-07-27 21:10:42 +00:00
|
|
|
|
2014-06-27 13:48:14 +00:00
|
|
|
/**
|
|
|
|
* The same as NewRasterDirect, but also accepts a call-back routine, which is invoked
|
|
|
|
* when the surface is deleted, and is passed the pixel memory and the specified context.
|
|
|
|
*/
|
|
|
|
static SkSurface* NewRasterDirectReleaseProc(const SkImageInfo&, void* pixels, size_t rowBytes,
|
|
|
|
void (*releaseProc)(void* pixels, void* context),
|
2014-09-22 14:29:03 +00:00
|
|
|
void* context, const SkSurfaceProps* = NULL);
|
2014-06-27 13:48:14 +00:00
|
|
|
|
2012-07-27 21:10:42 +00:00
|
|
|
/**
|
|
|
|
* Return a new surface, with the memory for the pixels automatically
|
|
|
|
* allocated.
|
|
|
|
*
|
|
|
|
* If the requested surface cannot be created, or the request is not a
|
|
|
|
* supported configuration, NULL will be returned.
|
|
|
|
*/
|
2014-09-22 14:29:03 +00:00
|
|
|
static SkSurface* NewRaster(const SkImageInfo&, const SkSurfaceProps* = NULL);
|
2012-07-27 21:10:42 +00:00
|
|
|
|
2013-09-17 20:03:43 +00:00
|
|
|
/**
|
2013-11-01 13:46:54 +00:00
|
|
|
* Helper version of NewRaster. It creates a SkImageInfo with the
|
2013-09-17 20:03:43 +00:00
|
|
|
* specified width and height, and populates the rest of info to match
|
|
|
|
* pixels in SkPMColor format.
|
|
|
|
*/
|
2014-09-22 14:29:03 +00:00
|
|
|
static SkSurface* NewRasterPMColor(int width, int height, const SkSurfaceProps* props = NULL) {
|
|
|
|
return NewRaster(SkImageInfo::MakeN32Premul(width, height), props);
|
2013-09-17 20:03:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-04 14:58:30 +00:00
|
|
|
/**
|
|
|
|
* Return a new surface using the specified render target.
|
|
|
|
*/
|
2014-09-22 14:29:03 +00:00
|
|
|
static SkSurface* NewRenderTargetDirect(GrRenderTarget*, const SkSurfaceProps*);
|
|
|
|
|
|
|
|
static SkSurface* NewRenderTargetDirect(GrRenderTarget* target) {
|
|
|
|
return NewRenderTargetDirect(target, NULL);
|
|
|
|
}
|
|
|
|
|
2012-07-31 15:45:27 +00:00
|
|
|
/**
|
|
|
|
* Return a new surface whose contents will be drawn to an offscreen
|
|
|
|
* render target, allocated by the surface.
|
|
|
|
*/
|
2014-09-22 14:29:03 +00:00
|
|
|
static SkSurface* NewRenderTarget(GrContext*, const SkImageInfo&, int sampleCount,
|
|
|
|
const SkSurfaceProps* = NULL);
|
|
|
|
|
|
|
|
static SkSurface* NewRenderTarget(GrContext* gr, const SkImageInfo& info) {
|
|
|
|
return NewRenderTarget(gr, info, 0, NULL);
|
|
|
|
}
|
2012-07-31 15:45:27 +00:00
|
|
|
|
2014-03-19 21:19:16 +00:00
|
|
|
/**
|
|
|
|
* Return a new surface whose contents will be drawn to an offscreen
|
|
|
|
* render target, allocated by the surface from the scratch texture pool
|
|
|
|
* managed by the GrContext. The scratch texture pool serves the purpose
|
|
|
|
* of retaining textures after they are no longer in use in order to
|
|
|
|
* re-use them later without having to re-allocate. Scratch textures
|
|
|
|
* should be used in cases where high turnover is expected. This allows,
|
|
|
|
* for example, the copy on write to recycle a texture from a recently
|
|
|
|
* released SkImage snapshot of the surface.
|
|
|
|
* Note: Scratch textures count against the GrContext's cached resource
|
|
|
|
* budget.
|
|
|
|
*/
|
2014-09-22 14:29:03 +00:00
|
|
|
static SkSurface* NewScratchRenderTarget(GrContext*, const SkImageInfo&, int sampleCount,
|
|
|
|
const SkSurfaceProps* = NULL);
|
|
|
|
|
|
|
|
static SkSurface* NewScratchRenderTarget(GrContext* gr, const SkImageInfo& info) {
|
|
|
|
return NewScratchRenderTarget(gr, info, 0, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SK_SUPPORT_LEGACY_TEXTRENDERMODE
|
|
|
|
/**
|
|
|
|
* Text rendering modes that can be passed to NewRenderTarget*
|
|
|
|
*/
|
|
|
|
enum TextRenderMode {
|
|
|
|
/**
|
|
|
|
* This will use the standard text rendering method
|
|
|
|
*/
|
|
|
|
kStandard_TextRenderMode,
|
|
|
|
/**
|
|
|
|
* This will use signed distance fields for text rendering when possible
|
|
|
|
*/
|
|
|
|
kDistanceField_TextRenderMode,
|
|
|
|
};
|
|
|
|
static SkSurface* NewRenderTargetDirect(GrRenderTarget*, TextRenderMode);
|
|
|
|
static SkSurface* NewRenderTarget(GrContext*, const SkImageInfo&, int sampleCount,
|
|
|
|
TextRenderMode);
|
|
|
|
static SkSurface* NewScratchRenderTarget(GrContext*, const SkImageInfo&, int sampleCount,
|
|
|
|
TextRenderMode);
|
|
|
|
#endif
|
2014-03-19 21:19:16 +00:00
|
|
|
|
2012-07-27 21:10:42 +00:00
|
|
|
int width() const { return fWidth; }
|
|
|
|
int height() const { return fHeight; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a unique non-zero, unique value identifying the content of this
|
|
|
|
* surface. Each time the content is changed changed, either by drawing
|
|
|
|
* into this surface, or explicitly calling notifyContentChanged()) this
|
|
|
|
* method will return a new value.
|
|
|
|
*
|
|
|
|
* If this surface is empty (i.e. has a zero-dimention), this will return
|
|
|
|
* 0.
|
|
|
|
*/
|
2012-08-28 12:19:02 +00:00
|
|
|
uint32_t generationID();
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-07-27 21:10:42 +00:00
|
|
|
/**
|
2013-04-22 14:28:01 +00:00
|
|
|
* Modes that can be passed to notifyContentWillChange
|
|
|
|
*/
|
|
|
|
enum ContentChangeMode {
|
2013-04-23 07:01:29 +00:00
|
|
|
/**
|
2013-04-22 14:28:01 +00:00
|
|
|
* Use this mode if it is known that the upcoming content changes will
|
|
|
|
* clear or overwrite prior contents, thus making them discardable.
|
|
|
|
*/
|
|
|
|
kDiscard_ContentChangeMode,
|
2013-04-23 07:01:29 +00:00
|
|
|
/**
|
2013-04-22 14:28:01 +00:00
|
|
|
* Use this mode if prior surface contents need to be preserved or
|
|
|
|
* if in doubt.
|
|
|
|
*/
|
|
|
|
kRetain_ContentChangeMode,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call this if the contents are about to change. This will (lazily) force a new
|
2012-07-27 21:10:42 +00:00
|
|
|
* value to be returned from generationID() when it is called next.
|
|
|
|
*/
|
2013-04-22 14:28:01 +00:00
|
|
|
void notifyContentWillChange(ContentChangeMode mode);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-07-27 21:10:42 +00:00
|
|
|
/**
|
2012-07-30 21:03:46 +00:00
|
|
|
* Return a canvas that will draw into this surface. This will always
|
|
|
|
* return the same canvas for a given surface, and is manged/owned by the
|
|
|
|
* surface. It should not be used when its parent surface has gone out of
|
|
|
|
* scope.
|
2012-07-27 21:10:42 +00:00
|
|
|
*/
|
2012-07-30 21:03:46 +00:00
|
|
|
SkCanvas* getCanvas();
|
2012-07-27 21:10:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a new surface that is "compatible" with this one, in that it will
|
|
|
|
* efficiently be able to be drawn into this surface. Typical calling
|
|
|
|
* pattern:
|
|
|
|
*
|
|
|
|
* SkSurface* A = SkSurface::New...();
|
|
|
|
* SkCanvas* canvasA = surfaceA->newCanvas();
|
|
|
|
* ...
|
|
|
|
* SkSurface* surfaceB = surfaceA->newSurface(...);
|
|
|
|
* SkCanvas* canvasB = surfaceB->newCanvas();
|
|
|
|
* ... // draw using canvasB
|
|
|
|
* canvasA->drawSurface(surfaceB); // <--- this will always be optimal!
|
|
|
|
*/
|
2013-11-01 13:46:54 +00:00
|
|
|
SkSurface* newSurface(const SkImageInfo&);
|
2012-07-27 21:10:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an image of the current state of the surface pixels up to this
|
|
|
|
* point. Subsequent changes to the surface (by drawing into its canvas)
|
|
|
|
* will not be reflected in this image.
|
|
|
|
*/
|
2013-04-12 20:20:50 +00:00
|
|
|
SkImage* newImageSnapshot();
|
2012-07-27 21:10:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Thought the caller could get a snapshot image explicitly, and draw that,
|
|
|
|
* it seems that directly drawing a surface into another canvas might be
|
|
|
|
* a common pattern, and that we could possibly be more efficient, since
|
|
|
|
* we'd know that the "snapshot" need only live until we've handed it off
|
|
|
|
* to the canvas.
|
|
|
|
*/
|
|
|
|
void draw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*);
|
|
|
|
|
2014-02-13 17:14:46 +00:00
|
|
|
/**
|
|
|
|
* If the surface has direct access to its pixels (i.e. they are in local
|
|
|
|
* RAM) return the const-address of those pixels, and if not null, return
|
|
|
|
* the ImageInfo and rowBytes. The returned address is only valid while
|
|
|
|
* the surface object is in scope, and no API call is made on the surface
|
|
|
|
* or its canvas.
|
|
|
|
*
|
|
|
|
* On failure, returns NULL and the info and rowBytes parameters are
|
|
|
|
* ignored.
|
|
|
|
*/
|
|
|
|
const void* peekPixels(SkImageInfo* info, size_t* rowBytes);
|
|
|
|
|
2014-09-22 14:29:03 +00:00
|
|
|
const SkSurfaceProps& props() const { return fProps; }
|
|
|
|
|
2012-07-27 21:10:42 +00:00
|
|
|
protected:
|
2014-09-22 14:29:03 +00:00
|
|
|
SkSurface(int width, int height, const SkSurfaceProps*);
|
|
|
|
SkSurface(const SkImageInfo&, const SkSurfaceProps*);
|
2012-07-27 21:10:42 +00:00
|
|
|
|
2012-08-28 12:19:02 +00:00
|
|
|
// called by subclass if their contents have changed
|
|
|
|
void dirtyGenerationID() {
|
|
|
|
fGenerationID = 0;
|
|
|
|
}
|
|
|
|
|
2012-07-27 21:10:42 +00:00
|
|
|
private:
|
2014-09-22 14:29:03 +00:00
|
|
|
const SkSurfaceProps fProps;
|
|
|
|
const int fWidth;
|
|
|
|
const int fHeight;
|
|
|
|
uint32_t fGenerationID;
|
2012-08-30 14:38:00 +00:00
|
|
|
|
2012-08-28 12:48:35 +00:00
|
|
|
typedef SkRefCnt INHERITED;
|
2012-07-27 21:10:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|