| /* | |
| * Copyright 2022 Rive | |
| */ | |
| #ifndef _RIVE_FACTORY_HPP_ | |
| #define _RIVE_FACTORY_HPP_ | |
| #include "rive/renderer.hpp" | |
| #include "rive/text_engine.hpp" | |
| #include "rive/audio/audio_source.hpp" | |
| #include "rive/refcnt.hpp" | |
| #include "rive/span.hpp" | |
| #include "rive/math/aabb.hpp" | |
| #include <stdio.h> | |
| #include <cstdint> | |
| #include <vector> | |
| namespace rive | |
| { | |
| class RawPath; | |
| namespace ore | |
| { | |
| class Context; | |
| } | |
| namespace cmd | |
| { | |
| class DeferredCanvasHost; | |
| } | |
| class Factory | |
| { | |
| public: | |
| Factory() {} | |
| virtual ~Factory() {} | |
| virtual rcp<RenderBuffer> makeRenderBuffer( | |
| RenderBufferType, | |
| RenderBufferFlags, | |
| size_t sizeInBytes) = 0; | |
| virtual rcp<ImageMeshInstances> makeImageMeshInstances(size_t count) | |
| { | |
| return make_rcp<ImageMeshInstances>(count); | |
| } | |
| virtual rcp<RenderShader> makeLinearGradient( | |
| float sx, | |
| float sy, | |
| float ex, | |
| float ey, | |
| const ColorInt colors[], // [count] | |
| const float stops[], // [count] | |
| size_t count) = 0; | |
| virtual rcp<RenderShader> makeRadialGradient( | |
| float cx, | |
| float cy, | |
| float radius, | |
| const ColorInt colors[], // [count] | |
| const float stops[], // [count] | |
| size_t count) = 0; | |
| virtual rcp<RenderShader> makeAngularGradient( | |
| const ColorInt colors[], // [count] | |
| const float stops[], // [count] | |
| size_t count) | |
| { | |
| // TODO: This is defaulted to make a linear gradient with a single stop | |
| // (i.e. it's solid color) for platforms where angular gradients are not | |
| // yet implemented, mostly to have it do *something*. This defaulting | |
| // should get removed once there's an implementation everywhere (which | |
| // may still be doing something like this if, as with the SVG renderer, | |
| // this isn't possible to implement properly). | |
| return makeLinearGradient( | |
| 0.0f, | |
| 0.0f, | |
| 1.0f, | |
| 0.0f, | |
| colors, | |
| stops, | |
| std::min(size_t(1), count)); | |
| } | |
| // Returns a full-formed RenderPath -- can be treated as immutable | |
| // This call might swap out the arrays backing the points and verbs in the | |
| // given RawPath, so the caller can expect it to be in an undefined state | |
| // upon return. | |
| virtual rcp<RenderPath> makeRenderPath(RawPath&, FillRule) = 0; | |
| // Deprecated -- working to make RenderPath's immutable | |
| virtual rcp<RenderPath> makeEmptyRenderPath() = 0; | |
| virtual rcp<RenderPaint> makeRenderPaint() = 0; | |
| virtual rcp<RenderImage> decodeImage(Span<const uint8_t>) = 0; | |
| // An import is about to pass at most this many bytes of embedded assets | |
| // through decodeImage; a factory that stores them can make room first. | |
| virtual void reserveForImport(size_t fileBytes) {} | |
| // GPU ore context, when this factory is backed by a RenderContext. | |
| // Null for non-GPU factories. Kept last in the virtual section to avoid | |
| // shifting existing vtable slots. | |
| virtual ore::Context* ore() { return nullptr; } | |
| // The GPU render context an import through this factory should give its | |
| // scripts, as a Factory so this header stays free of gpu types. A render | |
| // context answers with itself; a recording session answers with the one it | |
| // records for, which on web is null until a render texture attaches, so | |
| // callers that deferred an allocation ask again rather than caching the | |
| // null they saw at import. Null means the importer cannot route GPU | |
| // scripting. | |
| virtual Factory* renderContext() { return nullptr; } | |
| // Set when script canvas work must record rather than issue. Null means | |
| // scripts draw straight to the driver. | |
| virtual cmd::DeferredCanvasHost* deferredCanvasHost() { return nullptr; } | |
| // A host that can hand out an offscreen frame to draw into, used by | |
| // features that rasterize into a texture rather than to the screen (e.g. | |
| // an artboard caching itself as a bitmap). A recording session doubles as | |
| // one, which is the default. | |
| // | |
| // Kept separate from deferredCanvasHost() on purpose: that one answers | |
| // "content is being recorded for a later replay", and the scripting layer | |
| // keys canvas allocation and frame handling off it. A renderer that draws | |
| // immediately can serve this hook, but must not answer that one. | |
| virtual cmd::DeferredCanvasHost* canvasContentHost() | |
| { | |
| return deferredCanvasHost(); | |
| } | |
| // An image from width * height premultiplied RGBA8 pixels, mipmapped like | |
| // a decoded image. Null when the pixels do not match the size, or where | |
| // the factory cannot make images from pixels. A size past the device's max | |
| // texture size is null only from an immediate RenderContext: a factory that | |
| // creates the texture later (DeferredFactory at replay, WebGL2 at upload) | |
| // returns an image that draws nothing, as decodeImage does there. | |
| rcp<RenderImage> makeImageFromPixels( | |
| uint32_t width, | |
| uint32_t height, | |
| Span<const uint8_t> rgbaPremul) | |
| { | |
| return validImagePixels(width, height, rgbaPremul) | |
| ? onMakeImageFromPixels(width, height, rgbaPremul) | |
| : nullptr; | |
| } | |
| // The largest side makeImageFromPixels takes, so every caller refuses the | |
| // same sizes. | |
| static constexpr uint32_t maxImageSize = 65535; | |
| // True when rgbaPremul is exactly width * height RGBA8 pixels and no side | |
| // passes maxImageSize, without the multiplication that wraps in 32 bits. | |
| static bool validImagePixels( | |
| uint32_t width, | |
| uint32_t height, | |
| Span<const uint8_t> rgbaPremul) | |
| { | |
| return width != 0 && height != 0 && width <= maxImageSize && | |
| height <= maxImageSize && rgbaPremul.size() % 4 == 0 && | |
| static_cast<uint64_t>(width) * height == | |
| static_cast<uint64_t>(rgbaPremul.size() / 4); | |
| } | |
| rcp<Font> decodeFont(Span<const uint8_t>); | |
| rcp<Font> decodeFont(std::vector<uint8_t>&&); | |
| rcp<AudioSource> decodeAudio(Span<const uint8_t>); | |
| // Non-virtual helpers | |
| rcp<RenderPath> makeRenderPath(const AABB&); | |
| protected: | |
| // makeImageFromPixels, for pixels that match the size. | |
| virtual rcp<RenderImage> onMakeImageFromPixels( | |
| uint32_t /*width*/, | |
| uint32_t /*height*/, | |
| Span<const uint8_t> /*rgbaPremul*/) | |
| { | |
| return nullptr; | |
| } | |
| }; | |
| } // namespace rive | |
| #endif |