Skip to content

Commit

Permalink
Add RGBAAALayout.h
Browse files Browse the repository at this point in the history
  • Loading branch information
domchen committed Feb 13, 2022
1 parent 3dde04b commit 5393b79
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 72 deletions.
14 changes: 2 additions & 12 deletions src/rendering/graphics/Picture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,14 @@ static bool TryDrawDirectly(Canvas* canvas, const Texture* texture, const RGBAAA
if (!texture->isYUV() && layout == nullptr) {
// RGBA 纹理始终可以直接上屏。
canvas->drawTexture(texture);
// 防止临时纹理析构
canvas->flush();
return true;
}
auto totalMatrix = canvas->getMatrix();
auto scaleFactor = GetMaxScaleFactor(totalMatrix);
if (scaleFactor <= 1.0f) {
auto width = layout ? layout->width : texture->width();
auto height = layout ? layout->height : texture->height();
// 纹理格式为 YUV 或含有 RGBAAALayout 时在缩放值小于等于 1.0f 时才直接上屏会有更好的性能。
auto bounds = Rect::MakeWH(static_cast<float>(width), static_cast<float>(height));
auto result = canvas->hasComplexPaint(bounds);
if (!(result & PaintKind::Blend || result & PaintKind::Clip)) {
canvas->drawTexture(texture, layout);
// 防止临时纹理析构
canvas->flush();
return true;
}
canvas->drawTexture(texture, layout);
return true;
}
return false;
}
Expand Down
1 change: 1 addition & 0 deletions src/rendering/graphics/Picture.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "Graphic.h"
#include "core/Bitmap.h"
#include "core/Image.h"
#include "core/RGBAAALayout.h"
#include "rendering/graphics/Snapshot.h"

namespace pag {
Expand Down
43 changes: 43 additions & 0 deletions tgfx/include/core/RGBAAALayout.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making libpag available.
//
// Copyright (C) 2021 THL A29 Limited, a Tencent company. 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.
//
/////////////////////////////////////////////////////////////////////////////////////////////////

#pragma once

namespace pag {
/**
* Defines the layout of a RGBAAA format image, which is half RGB, half AAA.
*/
struct RGBAAALayout {
/**
* The display width of the image.
*/
int width;
/**
* The display height of the image.
*/
int height;
/**
* The x position of where alpha area begins.
*/
int alphaStartX;
/**
* The y position of where alpha area begins.
*/
int alphaStartY;
};
} // namespace pag
6 changes: 6 additions & 0 deletions tgfx/include/gpu/Canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "core/Blend.h"
#include "core/Font.h"
#include "core/Path.h"
#include "core/RGBAAALayout.h"
#include "gpu/Paint.h"
#include "gpu/Texture.h"

Expand Down Expand Up @@ -119,6 +120,11 @@ class Canvas {
*/
virtual void clear() = 0;

/**
* Draws a rectangle using current clip, matrix, and specified paint.
*/
void drawRect(const Rect& rect, const Paint& paint);

/**
* Draws a Texture, with its top-left corner at (0, 0), using a mask texture and current alpha,
* blend mode, clip and matrix. The mask texture has the same position and size with the texture.
Expand Down
29 changes: 0 additions & 29 deletions tgfx/include/gpu/Paint.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,6 @@
#include "pag/types.h"

namespace pag {
/**
* Defines the layout of a RGBAAA format image, which is half RGB, half AAA.
*/
class RGBAAALayout {
public:
RGBAAALayout() = default;

RGBAAALayout(int width, int height, int alphaStartX, int alphaStartY)
: width(width), height(height), alphaStartX(alphaStartX), alphaStartY(alphaStartY) {
}

/**
* The display width of the image.
*/
int width = 0;
/**
* The display height of the image.
*/
int height = 0;
/**
* The x position of where alpha area begins.
*/
int alphaStartX = 0;
/**
* The y position of where alpha area begins.
*/
int alphaStartY = 0;
};

/**
* Defines enumerations for Paint.setStyle().
*/
Expand Down
6 changes: 6 additions & 0 deletions tgfx/src/gpu/Canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ void Canvas::clipPath(const Path& path) {
globalPaint.clip.addPath(clipPath, PathOp::Intersect);
}

void Canvas::drawRect(const Rect& rect, const Paint& paint) {
Path path = {};
path.addRect(rect);
drawPath(path, paint);
}

void Canvas::drawTexture(const Texture* texture) {
drawTexture(texture, nullptr);
}
Expand Down
23 changes: 21 additions & 2 deletions tgfx/src/gpu/TextureFragmentProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,31 @@
/////////////////////////////////////////////////////////////////////////////////////////////////

#include "TextureFragmentProcessor.h"
#include "base/utils/Log.h"
#include "base/utils/UniqueID.h"
#include "gpu/YUVTextureFragmentProcessor.h"
#include "opengl/GLTextureFragmentProcessor.h"

namespace pag {
std::unique_ptr<TextureFragmentProcessor> TextureFragmentProcessor::Make(
const Texture* texture, const RGBAAALayout* layout, const Matrix& localMatrix) {
std::unique_ptr<FragmentProcessor> TextureFragmentProcessor::Make(const Texture* texture,
const RGBAAALayout* layout,
const Matrix& localMatrix) {
if (texture == nullptr) {
return nullptr;
}
if (layout != nullptr) {
if (layout->width <= 0 || layout->height <= 0 ||
(layout->alphaStartX <= 0 && layout->alphaStartY <= 0) ||
layout->width + layout->alphaStartX > texture->width() ||
layout->height + layout->alphaStartY > texture->height()) {
LOGE("TextureFragmentProcessor::Make(): Invalid RGBAAALayout specified!");
return nullptr;
}
}
if (texture->isYUV()) {
return std::unique_ptr<YUVTextureFragmentProcessor>(new YUVTextureFragmentProcessor(
static_cast<const YUVTexture*>(texture), layout, localMatrix));
}
return std::unique_ptr<TextureFragmentProcessor>(
new TextureFragmentProcessor(texture, layout, localMatrix));
}
Expand Down
7 changes: 3 additions & 4 deletions tgfx/src/gpu/TextureFragmentProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@

#pragma once

#include "core/RGBAAALayout.h"
#include "gpu/FragmentProcessor.h"
#include "gpu/Paint.h"

namespace pag {
class TextureFragmentProcessor : public FragmentProcessor {
public:
static std::unique_ptr<TextureFragmentProcessor> Make(const Texture* texture,
const RGBAAALayout* layout,
const Matrix& localMatrix);
static std::unique_ptr<FragmentProcessor> Make(const Texture* texture, const RGBAAALayout* layout,
const Matrix& localMatrix);

std::string name() const override {
return "TextureFragmentProcessor";
Expand Down
6 changes: 0 additions & 6 deletions tgfx/src/gpu/YUVTextureFragmentProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@
#include "opengl/GLYUVTextureFragmentProcessor.h"

namespace pag {
std::unique_ptr<YUVTextureFragmentProcessor> YUVTextureFragmentProcessor::Make(
const YUVTexture* texture, const RGBAAALayout* layout, const Matrix& localMatrix) {
return std::unique_ptr<YUVTextureFragmentProcessor>(
new YUVTextureFragmentProcessor(texture, layout, localMatrix));
}

YUVTextureFragmentProcessor::YUVTextureFragmentProcessor(const YUVTexture* texture,
const RGBAAALayout* layout,
const Matrix& localMatrix)
Expand Down
8 changes: 3 additions & 5 deletions tgfx/src/gpu/YUVTextureFragmentProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@
#pragma once

#include "FragmentProcessor.h"
#include "gpu/Paint.h"
#include "core/RGBAAALayout.h"
#include "gpu/YUVTexture.h"

namespace pag {
class YUVTextureFragmentProcessor : public FragmentProcessor {
public:
static std::unique_ptr<YUVTextureFragmentProcessor> Make(const YUVTexture* texture,
const RGBAAALayout* layout,
const Matrix& localMatrix);

std::string name() const override {
return "YUVTextureFragmentProcessor";
}
Expand All @@ -49,6 +45,8 @@ class YUVTextureFragmentProcessor : public FragmentProcessor {
const RGBAAALayout* layout;
CoordTransform coordTransform;

friend class TextureFragmentProcessor;

friend class GLYUVTextureFragmentProcessor;
};
} // namespace pag
22 changes: 9 additions & 13 deletions tgfx/src/gpu/opengl/GLCanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "gpu/ColorShader.h"
#include "gpu/TextureFragmentProcessor.h"
#include "gpu/TextureMaskFragmentProcessor.h"
#include "gpu/YUVTextureFragmentProcessor.h"
#include "pag/file.h"

namespace pag {
Expand Down Expand Up @@ -132,14 +131,11 @@ void GLCanvas::drawTexture(const Texture* texture, const RGBAAALayout* layout, c
localMatrix.postScale(scale.x, scale.y);
auto translate = texture->getTextureCoord(clippedLocalQuad.x(), clippedLocalQuad.y());
localMatrix.postTranslate(translate.x, translate.y);
std::unique_ptr<FragmentProcessor> color;
if (texture->isYUV()) {
color = YUVTextureFragmentProcessor::Make(static_cast<const YUVTexture*>(texture), layout,
localMatrix);
} else {
color = TextureFragmentProcessor::Make(texture, layout, localMatrix);
auto processor = TextureFragmentProcessor::Make(texture, layout, localMatrix);
if (processor == nullptr) {
return;
}
draw(clippedLocalQuad, clippedDeviceQuad, GLFillRectOp::Make(), std::move(color),
draw(clippedLocalQuad, clippedDeviceQuad, GLFillRectOp::Make(), std::move(processor),
TextureMaskFragmentProcessor::MakeUseLocalCoord(mask, Matrix::I(), inverted), true);
}

Expand All @@ -149,15 +145,15 @@ void GLCanvas::drawPath(const Path& path, const Paint& paint) {
shader = Shader::MakeColorShader(paint.getColor(), paint.getAlpha());
}
if (paint.getStyle() == PaintStyle::Fill) {
drawPath(path, shader.get());
fillPath(path, shader.get());
return;
}
auto strokePath = path;
auto strokeEffect = PathEffect::MakeStroke(*paint.getStroke());
if (strokeEffect) {
strokeEffect->applyTo(&strokePath);
}
drawPath(strokePath, shader.get());
fillPath(strokePath, shader.get());
}

static std::unique_ptr<GLDrawOp> MakeSimplePathOp(const Path& path) {
Expand All @@ -171,7 +167,7 @@ static std::unique_ptr<GLDrawOp> MakeSimplePathOp(const Path& path) {
return nullptr;
}

void GLCanvas::drawPath(const Path& path, const Shader* shader) {
void GLCanvas::fillPath(const Path& path, const Shader* shader) {
if (path.isEmpty()) {
return;
}
Expand Down Expand Up @@ -239,7 +235,7 @@ void GLCanvas::drawGlyphs(const GlyphID glyphIDs[], const Point positions[], siz
auto stroke = paint.getStyle() == PaintStyle::Stroke ? paint.getStroke() : nullptr;
if (textBlob->getPath(&path, stroke)) {
auto shader = Shader::MakeColorShader(paint.getColor(), paint.getAlpha());
drawPath(path, shader.get());
fillPath(path, shader.get());
return;
}
drawMaskGlyphs(textBlob.get(), paint);
Expand All @@ -266,7 +262,7 @@ void GLCanvas::drawColorGlyphs(const GlyphID glyphIDs[], const Point positions[]
concat(glyphMatrix);
concatAlpha(paint.getAlpha());
auto texture = glyphBuffer->makeTexture(getContext());
drawTexture(texture.get(), nullptr);
drawTexture(texture.get(), nullptr, false);
restore();
}
}
Expand Down
3 changes: 2 additions & 1 deletion tgfx/src/gpu/opengl/GLCanvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class GLCanvas : public Canvas {
void drawGlyphs(const GlyphID glyphIDs[], const Point positions[], size_t glyphCount,
const Font& font, const Paint& paint) override;
Enum hasComplexPaint(const Rect& drawingBounds) const override;
void drawPath(const Path& path, const Shader* shader);

protected:
void onSave() override {
Expand Down Expand Up @@ -71,6 +70,8 @@ class GLCanvas : public Canvas {

void drawMaskGlyphs(TextBlob* textBlob, const Paint& paint);

void fillPath(const Path& path, const Shader* shader);

void draw(const Rect& localQuad, const Rect& deviceQuad, std::unique_ptr<GLDrawOp> op,
std::unique_ptr<FragmentProcessor> color,
std::unique_ptr<FragmentProcessor> mask = nullptr, bool aa = false);
Expand Down

0 comments on commit 5393b79

Please sign in to comment.