Skip to content

Commit c6af33e

Browse files
committed
[react-native] Open-source ReactART for native
1 parent 483077d commit c6af33e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2498
-0
lines changed

Libraries/ART/ART.xcodeproj/project.pbxproj

Lines changed: 371 additions & 0 deletions
Large diffs are not rendered by default.

Libraries/ART/ARTCGFloatArray.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
// A little helper to make sure we have the right memory allocation ready for use.
11+
// We assume that we will only this in one place so no reference counting is necessary.
12+
// Needs to be freed when dealloced.
13+
14+
// This is fragile since this relies on these values not getting reused. Consider
15+
// wrapping these in an Obj-C class or some ARC hackery to get refcounting.
16+
17+
typedef struct {
18+
size_t count;
19+
CGFloat *array;
20+
} ARTCGFloatArray;

Libraries/ART/ARTContainer.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import <Foundation/Foundation.h>
11+
12+
@protocol ARTContainer <NSObject>
13+
14+
// This is used as a hook for child to mark it's parent as dirty.
15+
// This bubbles up to the root which gets marked as dirty.
16+
- (void)invalidate;
17+
18+
@end

Libraries/ART/ARTGroup.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import <Foundation/Foundation.h>
11+
12+
#import "ARTContainer.h"
13+
#import "ARTNode.h"
14+
15+
@interface ARTGroup : ARTNode <ARTContainer>
16+
17+
@end

Libraries/ART/ARTGroup.m

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import "ARTGroup.h"
11+
12+
@implementation ARTGroup
13+
14+
- (void)renderLayerTo:(CGContextRef)context
15+
{
16+
// TO-DO: Clipping rectangle
17+
18+
for (ARTNode *node in self.subviews) {
19+
[node renderTo:context];
20+
}
21+
}
22+
23+
@end

Libraries/ART/ARTNode.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import <Foundation/Foundation.h>
11+
#import <UIKit/UIKit.h>
12+
13+
/**
14+
* ART nodes are implemented as empty UIViews but this is just an implementation detail to fit
15+
* into the existing view management. They should also be shadow views and painted on a background
16+
* thread.
17+
*/
18+
19+
@interface ARTNode : UIView
20+
21+
@property (nonatomic, assign) CGFloat opacity;
22+
23+
- (void)invalidate;
24+
- (void)renderTo:(CGContextRef)context;
25+
26+
/**
27+
* renderTo will take opacity into account and draw renderLayerTo off-screen if there is opacity
28+
* specified, then composite that onto the context. renderLayerTo always draws at opacity=1.
29+
* @abstract
30+
*/
31+
- (void)renderLayerTo:(CGContextRef)context;
32+
33+
@end

Libraries/ART/ARTNode.m

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import "ARTNode.h"
11+
12+
#import "ARTContainer.h"
13+
14+
@implementation ARTNode
15+
16+
- (void)insertSubview:(UIView *)subview atIndex:(NSInteger)index
17+
{
18+
[self invalidate];
19+
[super insertSubview:subview atIndex:index];
20+
}
21+
22+
- (void)removeFromSuperview
23+
{
24+
[self invalidate];
25+
[super removeFromSuperview];
26+
}
27+
28+
- (void)setOpacity:(CGFloat)opacity
29+
{
30+
[self invalidate];
31+
_opacity = opacity;
32+
}
33+
34+
- (void)setTransform:(CGAffineTransform)transform
35+
{
36+
[self invalidate];
37+
super.transform = transform;
38+
}
39+
40+
- (void)invalidate
41+
{
42+
id<ARTContainer> container = (id<ARTContainer>)self.superview;
43+
[container invalidate];
44+
}
45+
46+
- (void)renderTo:(CGContextRef)context
47+
{
48+
if (self.opacity <= 0) {
49+
// Nothing to paint
50+
return;
51+
}
52+
if (self.opacity >= 1) {
53+
// Just paint at full opacity
54+
CGContextSaveGState(context);
55+
CGContextConcatCTM(context, self.transform);
56+
CGContextSetAlpha(context, 1);
57+
[self renderLayerTo:context];
58+
CGContextRestoreGState(context);
59+
return;
60+
}
61+
// This needs to be painted on a layer before being composited.
62+
CGContextSaveGState(context);
63+
CGContextConcatCTM(context, self.transform);
64+
CGContextSetAlpha(context, self.opacity);
65+
CGContextBeginTransparencyLayer(context, NULL);
66+
[self renderLayerTo:context];
67+
CGContextEndTransparencyLayer(context);
68+
CGContextRestoreGState(context);
69+
}
70+
71+
- (void)renderLayerTo:(CGContextRef)context
72+
{
73+
// abstract
74+
}
75+
76+
@end

Libraries/ART/ARTRenderable.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import <Foundation/Foundation.h>
11+
12+
#import "ARTBrush.h"
13+
#import "ARTCGFloatArray.h"
14+
#import "ARTNode.h"
15+
16+
@interface ARTRenderable : ARTNode
17+
18+
@property (nonatomic, strong) ARTBrush *fill;
19+
@property (nonatomic, assign) CGColorRef stroke;
20+
@property (nonatomic, assign) CGFloat strokeWidth;
21+
@property (nonatomic, assign) CGLineCap strokeCap;
22+
@property (nonatomic, assign) CGLineJoin strokeJoin;
23+
@property (nonatomic, assign) ARTCGFloatArray strokeDash;
24+
25+
@end

Libraries/ART/ARTRenderable.m

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import "ARTRenderable.h"
11+
12+
@implementation ARTRenderable
13+
14+
- (void)setFill:(ARTBrush *)fill
15+
{
16+
[self invalidate];
17+
_fill = fill;
18+
}
19+
20+
- (void)setStroke:(CGColorRef)stroke
21+
{
22+
if (stroke == _stroke) {
23+
return;
24+
}
25+
[self invalidate];
26+
CGColorRelease(_stroke);
27+
_stroke = CGColorRetain(stroke);
28+
}
29+
30+
- (void)setStrokeWidth:(CGFloat)strokeWidth
31+
{
32+
[self invalidate];
33+
_strokeWidth = strokeWidth;
34+
}
35+
36+
- (void)setStrokeCap:(CGLineCap)strokeCap
37+
{
38+
[self invalidate];
39+
_strokeCap = strokeCap;
40+
}
41+
42+
- (void)setStrokeJoin:(CGLineJoin)strokeJoin
43+
{
44+
[self invalidate];
45+
_strokeJoin = strokeJoin;
46+
}
47+
48+
- (void)setStrokeDash:(ARTCGFloatArray)strokeDash
49+
{
50+
if (strokeDash.array == _strokeDash.array) {
51+
return;
52+
}
53+
if (_strokeDash.array) {
54+
free(_strokeDash.array);
55+
}
56+
[self invalidate];
57+
_strokeDash = strokeDash;
58+
}
59+
60+
- (void)dealloc
61+
{
62+
CGColorRelease(_stroke);
63+
if (_strokeDash.array) {
64+
free(_strokeDash.array);
65+
}
66+
}
67+
68+
- (void)renderTo:(CGContextRef)context
69+
{
70+
if (self.opacity <= 0 || self.opacity >= 1 || (self.fill && self.stroke)) {
71+
// If we have both fill and stroke, we will need to paint this using normal compositing
72+
[super renderTo: context];
73+
return;
74+
}
75+
// This is a terminal with only one painting. Therefore we don't need to paint this
76+
// off-screen. We can just composite it straight onto the buffer.
77+
CGContextSaveGState(context);
78+
CGContextConcatCTM(context, self.transform);
79+
CGContextSetAlpha(context, self.opacity);
80+
[self renderLayerTo:context];
81+
CGContextRestoreGState(context);
82+
}
83+
84+
- (void)renderLayerTo:(CGContextRef)context
85+
{
86+
// abstract
87+
}
88+
89+
@end
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule ARTSerializablePath
10+
*/
11+
12+
"use strict";
13+
14+
// TODO: Move this into an ART mode called "serialized" or something
15+
16+
var Class = require('art/core/class.js');
17+
var Path = require('art/core/path.js');
18+
19+
var MOVE_TO = 0;
20+
var CLOSE = 1;
21+
var LINE_TO = 2;
22+
var CURVE_TO = 3;
23+
var ARC = 4;
24+
25+
var SerializablePath = Class(Path, {
26+
27+
initialize: function(path) {
28+
this.reset();
29+
if (path instanceof SerializablePath) {
30+
this.path = path.path.slice(0);
31+
} else if (path) {
32+
if (path.applyToPath) {
33+
path.applyToPath(this);
34+
} else {
35+
this.push(path);
36+
}
37+
}
38+
},
39+
40+
onReset: function() {
41+
this.path = [];
42+
},
43+
44+
onMove: function(sx, sy, x, y) {
45+
this.path.push(MOVE_TO, x, y);
46+
},
47+
48+
onLine: function(sx, sy, x, y) {
49+
this.path.push(LINE_TO, x, y);
50+
},
51+
52+
onBezierCurve: function(sx, sy, p1x, p1y, p2x, p2y, x, y) {
53+
this.path.push(CURVE_TO, p1x, p1y, p2x, p2y, x, y);
54+
},
55+
56+
_arcToBezier: Path.prototype.onArc,
57+
58+
onArc: function(sx, sy, ex, ey, cx, cy, rx, ry, sa, ea, ccw, rotation) {
59+
if (rx !== ry || rotation) {
60+
return this._arcToBezier(
61+
sx, sy, ex, ey, cx, cy, rx, ry, sa, ea, ccw, rotation
62+
);
63+
}
64+
this.path.push(ARC, cx, cy, rx, sa, ea, ccw ? 0 : 1);
65+
},
66+
67+
onClose: function() {
68+
this.path.push(CLOSE);
69+
},
70+
71+
toJSON: function() {
72+
return this.path;
73+
}
74+
75+
});
76+
77+
module.exports = SerializablePath;

0 commit comments

Comments
 (0)