2014-11-11 12:56:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2015-02-06 16:36:15 +00:00
|
|
|
#ifndef SkDrawable_DEFINED
|
|
|
|
#define SkDrawable_DEFINED
|
2014-11-11 12:56:05 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkFlattenable.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
2014-11-11 12:56:05 +00:00
|
|
|
|
2018-10-18 16:43:25 +00:00
|
|
|
class GrBackendDrawableInfo;
|
2014-11-11 12:56:05 +00:00
|
|
|
class SkCanvas;
|
2016-04-25 13:40:26 +00:00
|
|
|
class SkMatrix;
|
2014-11-24 22:41:51 +00:00
|
|
|
class SkPicture;
|
2018-10-18 16:43:25 +00:00
|
|
|
enum class GrBackendApi : unsigned;
|
2014-11-11 12:56:05 +00:00
|
|
|
struct SkRect;
|
|
|
|
|
|
|
|
/**
|
2014-11-11 15:54:10 +00:00
|
|
|
* Base-class for objects that draw into SkCanvas.
|
|
|
|
*
|
|
|
|
* The object has a generation ID, which is guaranteed to be unique across all drawables. To
|
|
|
|
* allow for clients of the drawable that may want to cache the results, the drawable must
|
|
|
|
* change its generation ID whenever its internal state changes such that it will draw differently.
|
2014-11-11 12:56:05 +00:00
|
|
|
*/
|
2017-09-20 19:51:08 +00:00
|
|
|
class SK_API SkDrawable : public SkFlattenable {
|
2014-11-11 12:56:05 +00:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Draws into the specified content. The drawing sequence will be balanced upon return
|
|
|
|
* (i.e. the saveLevel() on the canvas will match what it was when draw() was called,
|
|
|
|
* and the current matrix and clip settings will not be changed.
|
|
|
|
*/
|
2017-08-28 14:34:05 +00:00
|
|
|
void draw(SkCanvas*, const SkMatrix* = nullptr);
|
2015-07-07 17:22:31 +00:00
|
|
|
void draw(SkCanvas*, SkScalar x, SkScalar y);
|
2014-11-11 12:56:05 +00:00
|
|
|
|
2018-10-18 16:43:25 +00:00
|
|
|
/**
|
|
|
|
* When using the GPU backend it is possible for a drawable to execute using the underlying 3D
|
|
|
|
* API rather than the SkCanvas API. It does so by creating a GpuDrawHandler. The GPU backend
|
|
|
|
* is deferred so the handler will be given access to the 3D API at the correct point in the
|
|
|
|
* drawing stream as the GPU backend flushes. Since the drawable may mutate, each time it is
|
|
|
|
* drawn to a GPU-backed canvas a new handler is snapped, representing the drawable's state at
|
|
|
|
* the time of the snap.
|
|
|
|
*
|
|
|
|
* When the GPU backend flushes to the 3D API it will call the draw method on the
|
|
|
|
* GpuDrawHandler. At this time the drawable may add commands to the stream of GPU commands for
|
|
|
|
* the unerlying 3D API. The draw function takes a GrBackendDrawableInfo which contains
|
|
|
|
* information about the current state of 3D API which the caller must respect. See
|
|
|
|
* GrBackendDrawableInfo for more specific details on what information is sent and the
|
|
|
|
* requirements for different 3D APIs.
|
|
|
|
*
|
|
|
|
* Additionaly there may be a slight delay from when the drawable adds its commands to when
|
|
|
|
* those commands are actually submitted to the GPU. Thus the drawable or GpuDrawHandler is
|
|
|
|
* required to keep any resources that are used by its added commands alive and valid until
|
|
|
|
* those commands are submitted to the GPU. The GpuDrawHandler will be kept alive and then
|
|
|
|
* deleted once the commands are submitted to the GPU. The dtor of the GpuDrawHandler is the
|
|
|
|
* signal to the drawable that the commands have all been submitted. Different 3D APIs may have
|
|
|
|
* additional requirements for certain resources which require waiting for the GPU to finish
|
|
|
|
* all work on those resources before reusing or deleting them. In this case, the drawable can
|
|
|
|
* use the dtor call of the GpuDrawHandler to add a fence to the GPU to track when the GPU work
|
|
|
|
* has completed.
|
|
|
|
*
|
|
|
|
* Currently this is only supported for the GPU Vulkan backend.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class GpuDrawHandler {
|
|
|
|
public:
|
|
|
|
virtual ~GpuDrawHandler() {}
|
|
|
|
|
|
|
|
virtual void draw(const GrBackendDrawableInfo&) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Snaps off a GpuDrawHandler to represent the state of the SkDrawable at the time the snap is
|
|
|
|
* called. This is used for executing GPU backend specific draws intermixed with normal Skia GPU
|
2019-01-10 18:19:06 +00:00
|
|
|
* draws. The GPU API, which will be used for the draw, as well as the full matrix, device clip
|
|
|
|
* bounds and imageInfo of the target buffer are passed in as inputs.
|
2018-10-18 16:43:25 +00:00
|
|
|
*/
|
|
|
|
std::unique_ptr<GpuDrawHandler> snapGpuDrawHandler(GrBackendApi backendApi,
|
2018-12-17 15:28:47 +00:00
|
|
|
const SkMatrix& matrix,
|
2019-01-10 18:19:06 +00:00
|
|
|
const SkIRect& clipBounds,
|
|
|
|
const SkImageInfo& bufferInfo) {
|
|
|
|
return this->onSnapGpuDrawHandler(backendApi, matrix, clipBounds, bufferInfo);
|
2018-10-18 16:43:25 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 22:41:51 +00:00
|
|
|
SkPicture* newPictureSnapshot();
|
2014-11-18 19:08:05 +00:00
|
|
|
|
2014-11-11 12:56:05 +00:00
|
|
|
/**
|
|
|
|
* Return a unique value for this instance. If two calls to this return the same value,
|
|
|
|
* it is presumed that calling the draw() method will render the same thing as well.
|
|
|
|
*
|
|
|
|
* Subclasses that change their state should call notifyDrawingChanged() to ensure that
|
|
|
|
* a new value will be returned the next time it is called.
|
|
|
|
*/
|
|
|
|
uint32_t getGenerationID();
|
|
|
|
|
|
|
|
/**
|
2014-11-18 19:08:05 +00:00
|
|
|
* Return the (conservative) bounds of what the drawable will draw. If the drawable can
|
|
|
|
* change what it draws (e.g. animation or in response to some external change), then this
|
|
|
|
* must return a bounds that is always valid for all possible states.
|
2014-11-11 12:56:05 +00:00
|
|
|
*/
|
2014-11-18 19:08:05 +00:00
|
|
|
SkRect getBounds();
|
2014-11-11 12:56:05 +00:00
|
|
|
|
2014-11-11 15:54:10 +00:00
|
|
|
/**
|
|
|
|
* Calling this invalidates the previous generation ID, and causes a new one to be computed
|
|
|
|
* the next time getGenerationID() is called. Typically this is called by the object itself,
|
|
|
|
* in response to its internal state changing.
|
|
|
|
*/
|
2014-11-11 12:56:05 +00:00
|
|
|
void notifyDrawingChanged();
|
|
|
|
|
2018-05-21 15:56:57 +00:00
|
|
|
static SkFlattenable::Type GetFlattenableType() {
|
|
|
|
return kSkDrawable_Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkFlattenable::Type getFlattenableType() const override {
|
|
|
|
return kSkDrawable_Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sk_sp<SkDrawable> Deserialize(const void* data, size_t size,
|
|
|
|
const SkDeserialProcs* procs = nullptr) {
|
|
|
|
return sk_sp<SkDrawable>(static_cast<SkDrawable*>(
|
|
|
|
SkFlattenable::Deserialize(
|
|
|
|
kSkDrawable_Type, data, size, procs).release()));
|
|
|
|
}
|
|
|
|
|
2016-04-25 13:40:26 +00:00
|
|
|
Factory getFactory() const override { return nullptr; }
|
2018-10-18 21:27:16 +00:00
|
|
|
const char* getTypeName() const override { return nullptr; }
|
2016-04-25 13:40:26 +00:00
|
|
|
|
2014-11-11 12:56:05 +00:00
|
|
|
protected:
|
2018-10-18 16:43:25 +00:00
|
|
|
SkDrawable();
|
|
|
|
|
2014-11-18 19:08:05 +00:00
|
|
|
virtual SkRect onGetBounds() = 0;
|
2014-11-11 12:56:05 +00:00
|
|
|
virtual void onDraw(SkCanvas*) = 0;
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2018-12-17 15:28:47 +00:00
|
|
|
virtual std::unique_ptr<GpuDrawHandler> onSnapGpuDrawHandler(GrBackendApi, const SkMatrix&,
|
2019-01-10 18:19:06 +00:00
|
|
|
const SkIRect& /*clipBounds*/,
|
|
|
|
const SkImageInfo&) {
|
2018-12-17 15:28:47 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Delete this once Android gets updated to take the clipBounds version above.
|
2018-10-18 16:43:25 +00:00
|
|
|
virtual std::unique_ptr<GpuDrawHandler> onSnapGpuDrawHandler(GrBackendApi, const SkMatrix&) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-02-06 16:36:15 +00:00
|
|
|
/**
|
|
|
|
* Default implementation calls onDraw() with a canvas that records into a picture. Subclasses
|
|
|
|
* may override if they have a more efficient way to return a picture for the current state
|
|
|
|
* of their drawable. Note: this picture must draw the same as what would be drawn from
|
|
|
|
* onDraw().
|
|
|
|
*/
|
2014-11-24 22:41:51 +00:00
|
|
|
virtual SkPicture* onNewPictureSnapshot();
|
2014-11-11 12:56:05 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
int32_t fGenerationID;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|