1 #include "rive_tizen.hpp"
3 #include <skia_factory.hpp>
4 #include <skia_renderer.hpp>
7 #include <SkColorSpace.h>
13 #include <GLFW/glfw3.h>
15 #include "GrBackendSurface.h"
16 #include "GrDirectContext.h"
17 #include "gl/GrGLInterface.h"
20 #include <rive/node.hpp>
21 #include <rive/shapes/paint/color.hpp>
22 #include <rive/shapes/paint/solid_color.hpp>
23 #include <rive/shapes/paint/stroke.hpp>
24 #include <rive/shapes/paint/fill.hpp>
25 #include <rive/artboard.hpp>
30 #define LOG_TAG "RIVE_TIZEN"
34 #define LOGE(fmt, ...) dlog_print(DLOG_ERROR, LOG_TAG, "[%s]" fmt, __func__, ##__VA_ARGS__)
36 #define C_RED "\e[31m"
38 #define LOGE(fmt, ...) printf(C_RED "[" LOG_TAG "]" C_END "[%s]" fmt "\n", __func__, ##__VA_ARGS__)
41 static sk_sp<SkSurface> mSkiaSurface;
42 static SkCanvas *mSkiaCanvas = nullptr;
43 static rive::SkiaFactory mSkiaFactory;
44 static std::unique_ptr<rive::File> mFile;
45 static std::unique_ptr<rive::ArtboardInstance> mArtboardInstance;
48 static sk_sp<GrDirectContext> mContext;
51 RiveTizen::RiveTizen()
55 RiveTizen::~RiveTizen()
59 rive::Artboard *RiveTizen::getArtboard()
61 return mArtboardInstance.get();
64 void RiveTizen::animationAdvanceApply(rive::LinearAnimationInstance *animation, double elapsed)
68 LOGE("[Invalid parameter][animation == nullptr]");
71 animation->advanceAndApply(elapsed);
75 void RiveTizen::animationApply(rive::LinearAnimationInstance *animation, double elapsed)
79 LOGE("[Invalid parameter][animation == nullptr]");
82 animation->time(elapsed);
86 rive::LinearAnimationInstance *RiveTizen::createLinearAnimationInstance(size_t index)
88 auto animation = mArtboardInstance->animation(index);
92 LOGE("[Invalid parameter][animation == nullptr]");
96 rive::LinearAnimationInstance *instance = new rive::LinearAnimationInstance(animation, mArtboardInstance.get());
100 bool RiveTizen::loadRiveResource(uint8_t *bytes, size_t size)
103 mArtboardInstance = nullptr;
105 const rive::Span<const uint8_t> span(bytes, size);
106 auto file = rive::File::import(span, &mSkiaFactory);
112 mFile = std::move(file);
114 mArtboardInstance = mFile->artboardDefault();
115 mArtboardInstance->advance(0.0f);
120 bool RiveTizen::createCanvas(unsigned char *buffer, unsigned int width, unsigned int height, unsigned int stride)
123 // Buffer is unused for gpu-based version
124 GrContextOptions options;
125 mContext = GrDirectContext::MakeGL(nullptr, options);
127 GrGLFramebufferInfo framebufferInfo;
128 framebufferInfo.fFBOID = 0;
129 framebufferInfo.fFormat = GL_RGBA8;
130 GrBackendRenderTarget backendRenderTarget(width, height, 0 /* sample count */, 0 /* stencil bits */, framebufferInfo);
132 mSkiaSurface = SkSurface::MakeFromBackendRenderTarget(mContext.get(), backendRenderTarget, kBottomLeft_GrSurfaceOrigin,
133 kRGBA_8888_SkColorType, nullptr, nullptr);
135 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
136 mSkiaSurface = SkSurface::MakeRasterDirect(info, buffer, stride * 4);
142 #define USE_GL_VALUE "true"
144 #define USE_GL_VALUE "false"
146 LOGE("[Failed to create Skia surface][USE_GL = " USE_GL_VALUE "]");
150 mSkiaCanvas = mSkiaSurface->getCanvas();
151 mSkiaCanvas->clear(SK_ColorTRANSPARENT);
155 bool RiveTizen::render(double elapsed, unsigned int width, unsigned int height)
157 rive::SkiaRenderer renderer(mSkiaCanvas);
159 auto viewTransform = rive::computeAlignment(rive::Fit::contain, rive::Alignment::center,
160 rive::AABB(0, 0, width, height), mArtboardInstance->bounds());
162 renderer.transform(viewTransform);
163 mArtboardInstance->draw(&renderer);
173 bool RiveTizen::setShapeFillColor(const std::string &name, int a, int r, int g, int b)
175 auto instance = mArtboardInstance->find<rive::Fill>(name.c_str());
176 if (!instance || !instance->paint()->is<rive::SolidColor>())
179 instance->paint()->as<rive::SolidColor>()->colorValue(rive::colorARGB(a, r, g, b));
183 bool RiveTizen::setShapeStrokeColor(const std::string &name, int a, int r, int g, int b)
185 auto instance = mArtboardInstance->find<rive::Stroke>(name.c_str());
186 if (!instance || !instance->paint()->is<rive::SolidColor>())
189 instance->paint()->as<rive::SolidColor>()->colorValue(rive::colorARGB(a, r, g, b));
193 bool RiveTizen::setNodeOpacity(const std::string &name, float opacity)
195 auto node = mArtboardInstance->find(name.c_str());
199 node->as<rive::Node>()->opacity(opacity);
203 bool RiveTizen::setNodeScale(const std::string &name, float scale_x, float scale_y)
205 auto node = mArtboardInstance->find(name.c_str());
209 auto nodeInstance = node->as<rive::Node>();
210 nodeInstance->scaleX(scale_x);
211 nodeInstance->scaleY(scale_y);
215 bool RiveTizen::setNodeRotation(const std::string &name, float degree)
217 auto node = mArtboardInstance->find(name.c_str());
221 node->as<rive::Node>()->rotation(degree);
225 bool RiveTizen::setNodePosition(const std::string &name, float position_x, float position_y)
227 auto node = mArtboardInstance->find(name.c_str());
231 auto nodeInstance = node->as<rive::Node>();
232 nodeInstance->x(position_x);
233 nodeInstance->y(position_y);