2 * Copyright (c) 2020 - 2022 Samsung Electronics Co., Ltd. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 #ifndef _TVG_PICTURE_IMPL_H_
23 #define _TVG_PICTURE_IMPL_H_
27 #include "tvgLoader.h"
29 /************************************************************************/
30 /* Internal Class Implementation */
31 /************************************************************************/
33 struct PictureIterator : Iterator
35 Paint* paint = nullptr;
38 PictureIterator(Paint* p) : paint(p) {}
40 const Paint* next() override
42 if (!ptr) ptr = paint;
47 uint32_t count() override
62 shared_ptr<LoadModule> loader = nullptr;
64 Paint* paint = nullptr; //vector picture uses
65 Surface* surface = nullptr; //bitmap picture uses
66 void* rdata = nullptr; //engine data
68 bool resizing = false;
72 if (paint) delete(paint);
76 bool dispose(RenderMethod& renderer)
80 ret = paint->pImpl->dispose(renderer);
82 ret = renderer.dispose(rdata);
92 if (auto p = loader->paint()) {
95 if (w != loader->w || h != loader->h) {
96 loader->resize(paint, w, h);
99 if (paint) return RenderUpdateFlag::None;
103 if ((surface = loader->bitmap().release())) {
105 return RenderUpdateFlag::Image;
108 return RenderUpdateFlag::None;
111 RenderTransform resizeTransform(const RenderTransform* pTransform)
113 //Overriding Transformation by the desired image size
114 auto sx = w / loader->w;
115 auto sy = h / loader->h;
116 auto scale = sx < sy ? sx : sy;
119 tmp.m = {scale, 0, 0, 0, scale, 0, 0, 0, 1};
121 if (!pTransform) return tmp;
122 else return RenderTransform(pTransform, &tmp);
125 void* update(RenderMethod &renderer, const RenderTransform* pTransform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag pFlag)
127 auto flag = reload();
130 auto transform = resizeTransform(pTransform);
131 rdata = renderer.prepare(surface, rdata, &transform, opacity, clips, static_cast<RenderUpdateFlag>(pFlag | flag));
134 loader->resize(paint, w, h);
137 rdata = paint->pImpl->update(renderer, pTransform, opacity, clips, static_cast<RenderUpdateFlag>(pFlag | flag));
142 bool render(RenderMethod &renderer)
144 if (surface) return renderer.renderImage(rdata);
145 else if (paint) return paint->pImpl->render(renderer);
149 bool viewbox(float* x, float* y, float* w, float* h) const
151 if (!loader) return false;
152 if (x) *x = loader->vx;
153 if (y) *y = loader->vy;
154 if (w) *w = loader->vw;
155 if (h) *h = loader->vh;
159 bool size(float w, float h)
167 bool bounds(float* x, float* y, float* w, float* h)
177 RenderRegion bounds(RenderMethod& renderer)
179 if (rdata) return renderer.region(rdata);
180 if (paint) return paint->pImpl->bounds(renderer);
184 Result load(const string& path)
186 if (paint || surface) return Result::InsufficientCondition;
187 if (loader) loader->close();
188 bool invalid; //Invalid Path
189 loader = LoaderMgr::loader(path, &invalid);
191 if (invalid) return Result::InvalidArguments;
192 return Result::NonSupport;
194 if (!loader->read()) return Result::Unknown;
197 return Result::Success;
200 Result load(const char* data, uint32_t size, const string& mimeType, bool copy)
202 if (paint || surface) return Result::InsufficientCondition;
203 if (loader) loader->close();
204 loader = LoaderMgr::loader(data, size, mimeType, copy);
205 if (!loader) return Result::NonSupport;
206 if (!loader->read()) return Result::Unknown;
209 return Result::Success;
212 Result load(uint32_t* data, uint32_t w, uint32_t h, bool copy)
214 if (paint || surface) return Result::InsufficientCondition;
215 if (loader) loader->close();
216 loader = LoaderMgr::loader(data, w, h, copy);
217 if (!loader) return Result::NonSupport;
220 return Result::Success;
227 auto ret = Picture::gen();
229 auto dup = ret.get()->pImpl;
230 if (paint) dup->paint = paint->duplicate();
232 dup->loader = loader;
234 dup->surface = static_cast<Surface*>(malloc(sizeof(Surface)));
235 *dup->surface = *surface;
239 dup->resizing = resizing;
241 return ret.release();
247 return new PictureIterator(paint);
251 #endif //_TVG_PICTURE_IMPL_H_