`fabric/graphics` module: all about graphics

Summary: `fabric/graphics` provides graphics primitives; the implementation should be platform-specific eventually.

Reviewed By: fkgozali

Differential Revision: D7230675

fbshipit-source-id: ff05d2673072fad5ee6f331f0155561969e1a8b6
This commit is contained in:
Valentin Shergin 2018-03-18 19:04:13 -07:00 committed by Facebook Github Bot
parent c623455845
commit 608d1fb590
7 changed files with 251 additions and 0 deletions

View File

@ -48,5 +48,6 @@ rn_xplat_cxx_library(
"xplat//folly:molly",
"xplat//third-party/glog:glog",
react_native_xplat_target("fabric/debug:debug"),
react_native_xplat_target("fabric/graphics:graphics"),
],
)

View File

@ -0,0 +1,50 @@
load("//configurations/buck/apple:flag_defs.bzl", "get_debug_preprocessor_flags")
load("//ReactNative:DEFS.bzl", "IS_OSS_BUILD", "react_native_xplat_target", "rn_xplat_cxx_library", "APPLE_INSPECTOR_FLAGS")
APPLE_COMPILER_FLAGS = []
if not IS_OSS_BUILD:
load("@xplat//configurations/buck/apple:flag_defs.bzl", "get_static_library_ios_flags", "flags")
APPLE_COMPILER_FLAGS = flags.get_flag_value(get_static_library_ios_flags(), 'compiler_flags')
rn_xplat_cxx_library(
name = "graphics",
srcs = glob(
[
"**/*.cpp",
],
),
headers = glob(
[
"**/*.h",
],
),
header_namespace = "",
exported_headers = subdir_glob(
[
("", "*.h"),
],
prefix = "fabric/graphics",
),
compiler_flags = [
"-std=c++14",
"-Wall",
"-fexceptions",
"-frtti",
],
fbobjc_compiler_flags = APPLE_COMPILER_FLAGS,
fbobjc_preprocessor_flags = get_debug_preprocessor_flags() + APPLE_INSPECTOR_FLAGS,
force_static = True,
preprocessor_flags = [
"-DLOG_TAG=\"ReactNative\"",
"-DWITH_FBSYSTRACE=1",
],
visibility = ["PUBLIC"],
deps = [
"xplat//fbsystrace:fbsystrace",
"xplat//folly:headers_only",
"xplat//folly:memory",
"xplat//folly:molly",
"xplat//third-party/glog:glog",
],
)

View File

@ -0,0 +1,39 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "Color.h"
#include <CoreGraphics/CoreGraphics.h>
#include <CoreGraphics/CGColor.h>
#include <cassert>
namespace facebook {
namespace react {
SharedColor colorFromComponents(float red, float green, float blue, float alpha) {
const CGFloat components[] = {red, green, blue, alpha};
CGColorRef color = CGColorCreate(
CGColorSpaceCreateDeviceRGB(),
components
);
return SharedColor(color, CFRelease);
}
ColorComponents colorComponentsFromColor(SharedColor color) {
int numberOfComponents = CGColorGetNumberOfComponents(color.get());
assert(numberOfComponents == 4);
const CGFloat *components = CGColorGetComponents(color.get());
return ColorComponents {
(float)components[0],
(float)components[1],
(float)components[2],
(float)components[3]
};
}
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,30 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <CoreGraphics/CoreGraphics.h>
#include <memory>
namespace facebook {
namespace react {
using Color = CGColor;
using SharedColor = std::shared_ptr<Color>;
struct ColorComponents {
float red {0};
float green {0};
float blue {0};
float alpha {0};
};
SharedColor colorFromComponents(float red, float green, float blue, float alpha);
ColorComponents colorComponentsFromColor(SharedColor color);
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,60 @@
// Copyright 2004-present Facebook. All Rights Reserved.
#pragma once
namespace facebook {
namespace react {
/*
* Exact type of float numbers which ideally should match a type behing
* platform- and chip-architecture-specific float type (something like
* CGFloat on iOS).
*/
using Float = double;
/*
* Point
*/
struct Point {
Float x {0};
Float y {0};
Point& operator += (const Point& rhs) {
x += rhs.x;
y += rhs.y;
return *this;
}
friend Point operator + (Point lhs, const Point& rhs) {
return lhs += rhs;
}
};
/*
* Size
*/
struct Size {
Float width {0};
Float height {0};
};
/*
* Rect: Point and Size
*/
struct Rect {
Point origin {0, 0};
Size size {0, 0};
};
/*
* EdgeInsets
*/
struct EdgeInsets {
Float top {0};
Float left {0};
Float bottom {0};
Float right {0};
};
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,51 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "graphicValuesConversions.h"
namespace facebook {
namespace react {
SharedColor colorFromDynamic(folly::dynamic value) {
float red;
float green;
float blue;
float alpha;
if (value.isNumber()) {
auto argb = value.asInt();
float ratio = 256;
alpha = ((argb >> 24) & 0xFF) / ratio;
red = ((argb >> 16) & 0xFF) / ratio;
green = ((argb >> 8) & 0xFF) / ratio;
blue = (argb & 0xFF) / ratio;
} else if (value.isArray()) {
auto size = value.size();
assert(size == 3 || size == 4);
red = value[0].asDouble();
green = value[1].asDouble();
blue = value[2].asDouble();
alpha = size == 4 ? value[3].asDouble() : 1.0;
} else {
abort();
}
return colorFromComponents(red, green, blue, alpha);
}
std::string colorNameFromColor(SharedColor value) {
ColorComponents components = colorComponentsFromColor(value);
const float ratio = 256;
return "rgba(" +
folly::to<std::string>(round(components.red * ratio)) + ", " +
folly::to<std::string>(round(components.green * ratio)) + ", " +
folly::to<std::string>(round(components.blue * ratio)) + ", " +
folly::to<std::string>(round(components.alpha * ratio)) + ")";
}
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,20 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <folly/dynamic.h>
#include <fabric/graphics/Color.h>
namespace facebook {
namespace react {
SharedColor colorFromDynamic(folly::dynamic value);
std::string colorNameFromColor(SharedColor value);
} // namespace react
} // namespace facebook