Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(193)

Unified Diff: ui/gfx/color_transform.cc

Issue 2697253002: color: Add support for shader generation (Closed)
Patch Set: Rebase Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« cc/output/program_binding.cc ('K') | « ui/gfx/color_transform.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/color_transform.cc
diff --git a/ui/gfx/color_transform.cc b/ui/gfx/color_transform.cc
index 361a8ba4bb07727a193b9f0aed2ebe7101942ac0..96e73dcfbcb40fd9ab074953bcfa8d0d546d2b1a 100644
--- a/ui/gfx/color_transform.cc
+++ b/ui/gfx/color_transform.cc
@@ -11,6 +11,7 @@
#include "base/logging.h"
#include "base/memory/ptr_util.h"
+#include "base/strings/stringprintf.h"
#include "third_party/qcms/src/qcms.h"
#include "ui/gfx/color_space.h"
#include "ui/gfx/icc_profile.h"
@@ -250,8 +251,9 @@ class ColorTransformStep {
// Return true if this is a null transform.
virtual bool IsNull() { return false; }
-
virtual void Transform(ColorTransform::TriStim* color, size_t num) = 0;
+ virtual bool CanAppendShaderSource() { return false; }
+ virtual void AppendShaderSource(std::string* result) { NOTREACHED(); }
private:
DISALLOW_COPY_AND_ASSIGN(ColorTransformStep);
@@ -264,11 +266,13 @@ class ColorTransformInternal : public ColorTransform {
Intent intent);
~ColorTransformInternal() override;
- // Perform transformation of colors, |colors| is both input and output.
void Transform(TriStim* colors, size_t num) override {
for (const auto& step : steps_)
step->Transform(colors, num);
}
+ bool CanGetShaderSource() const override;
+ std::string GetShaderSource() const override;
+ bool IsIdentity() const override { return steps_.empty(); }
size_t NumberOfStepsForTesting() const override { return steps_.size(); }
private:
@@ -285,11 +289,19 @@ class ColorTransformInternal : public ColorTransform {
std::list<std::unique_ptr<ColorTransformStep>> steps_;
};
+#define SRC(...) \
+ do { \
+ *result += std::string(" ") + base::StringPrintf(__VA_ARGS__) + \
+ std::string("\n"); \
+ } while (0)
+
class ColorTransformNull : public ColorTransformStep {
public:
ColorTransformNull* GetNull() override { return this; }
bool IsNull() override { return true; }
void Transform(ColorTransform::TriStim* color, size_t num) override {}
+ bool CanAppendShaderSource() override { return true; }
+ void AppendShaderSource(std::string* result) override {}
};
class ColorTransformMatrix : public ColorTransformStep {
@@ -316,6 +328,17 @@ class ColorTransformMatrix : public ColorTransformStep {
matrix_.TransformPoint(colors + i);
}
+ bool CanAppendShaderSource() override { return true; }
+
+ void AppendShaderSource(std::string* result) override {
+ const SkMatrix44& m = matrix_.matrix();
+ SRC("color = mat3(%f, %f, %f, %f, %f, %f, %f, %f, %f) * color;",
+ m.get(0, 0), m.get(1, 0), m.get(2, 0), // column 1
+ m.get(0, 1), m.get(1, 1), m.get(2, 1), // column 2
+ m.get(0, 2), m.get(1, 2), m.get(2, 2)); // column 3
+ SRC("color += vec3(%f, %f, %f);", m.get(0, 3), m.get(1, 3), m.get(2, 3));
+ }
+
private:
class Transform matrix_;
};
@@ -591,6 +614,11 @@ void ColorTransformInternal::AppendColorSpaceToColorSpaceTransform(
steps_.push_back(
base::MakeUnique<ColorTransformMatrix>(Invert(GetTransferMatrix(from))));
+ // If the target color space is not defined, just apply the adjust and
+ // tranfer matrices.
+ if (!to.IsValid())
+ return;
+
SkColorSpaceTransferFn to_linear_fn;
bool to_linear_fn_valid = from.GetTransferFunction(&to_linear_fn);
steps_.push_back(base::MakeUnique<ColorTransformToLinear>(
@@ -723,6 +751,24 @@ ColorTransformInternal::ColorTransformInternal(const ColorSpace& from,
Simplify();
}
+std::string ColorTransformInternal::GetShaderSource() const {
+ std::string result;
+ result += "vec3 DoColorConversion(vec3 color) {\n";
+ for (const auto& step : steps_)
+ step->AppendShaderSource(&result);
+ result += " return color;\n";
+ result += "}\n";
+ return result;
+}
+
+bool ColorTransformInternal::CanGetShaderSource() const {
+ for (const auto& step : steps_) {
+ if (!step->CanAppendShaderSource())
+ return false;
+ }
+ return true;
+}
+
ColorTransformInternal::~ColorTransformInternal() {}
void ColorTransformInternal::Simplify() {
« cc/output/program_binding.cc ('K') | « ui/gfx/color_transform.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698