thorvg/src/lib/tvgCanvasImpl.h
Hermet Park 8dca270a30 common: code refactoring for simplicity.
Introduce internal PaintMethod since there more derived paint classes are coming.

This PaintMethod is a sort of Strategy Pattern method.

Change-Id: I29c49f5d4ddbfb9e429d4976636b20b39914ee20
2020-07-30 14:51:31 +09:00

103 lines
No EOL
2.8 KiB
C

/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef _TVG_CANVAS_IMPL_H_
#define _TVG_CANVAS_IMPL_H_
#include "tvgCommon.h"
/************************************************************************/
/* Internal Class Implementation */
/************************************************************************/
struct Canvas::Impl
{
vector<Paint*> paints;
RenderMethod* renderer;
Impl(RenderMethod* pRenderer):renderer(pRenderer)
{
renderer->ref();
}
~Impl()
{
clear();
renderer->unref();
}
Result push(unique_ptr<Paint> paint)
{
auto p = paint.release();
if (!p) return Result::MemoryCorruption;
paints.push_back(p);
return update(p);
}
Result clear()
{
if (!renderer) return Result::InsufficientCondition;
//Clear render target before drawing
if (!renderer->clear()) return Result::InsufficientCondition;
for (auto paint : paints) {
paint->IMPL->method->dispose(*renderer);
delete(paint);
}
paints.clear();
return Result::Success;
}
Result update(Paint* paint)
{
if (!renderer) return Result::InsufficientCondition;
//Update single paint node
if (paint) {
if (!paint->IMPL->method->update(*renderer, nullptr, RenderUpdateFlag::None)) {
return Result::InsufficientCondition;
}
//Update retained all paint nodes
} else {
for(auto paint: paints) {
if (!paint->IMPL->method->update(*renderer, nullptr, RenderUpdateFlag::None)) {
return Result::InsufficientCondition;
}
}
}
return Result::Success;
}
Result draw()
{
if (!renderer) return Result::InsufficientCondition;
if (!renderer->preRender()) return Result::InsufficientCondition;
for(auto paint: paints) {
if(!paint->IMPL->method->render(*renderer)) return Result::InsufficientCondition;
}
if (!renderer->postRender()) return Result::InsufficientCondition;
return Result::Success;
}
};
#endif /* _TVG_CANVAS_IMPL_H_ */