This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathcontext.cpp
More file actions
2542 lines (2040 loc) · 74.6 KB
/
context.cpp
File metadata and controls
2542 lines (2040 loc) · 74.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "graphics.h"
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "graphics-internal.h"
#include <SkCanvas.h>
#include <SkDevice.h>
#include <SkPaint.h>
#include <SkBitmap.h>
#include <SkShader.h>
#include <SkLayerDrawLooper.h>
#include <SkBlurMaskFilter.h>
#include <SkColorFilter.h>
#include <SkDevice.h>
#include <SkImageFilter.h>
#include <SkOffsetImageFilter.h>
#include <SkBlurImageFilter.h>
#include <SkTypeface.h>
#include <SkColorPriv.h>
#include <SkSurface.h>
#include <time.h>
#include "SkStippleMaskFilter.h"
////////////////////////////////////////////////////////////////////////////////
struct MCGIRectangle
{
int32_t left;
int32_t top;
int32_t right;
int32_t bottom;
};
MCGIRectangle MCGIRectangleMake(int32_t left, int32_t top, int32_t right, int32_t bottom)
{
MCGIRectangle r;
r . left = left;
r . top = top;
r . right = right;
r . bottom = bottom;
return r;
}
MCGIRectangle MCGRectangleComputeHull(const MCGRectangle& self)
{
return MCGIRectangleMake((int)floor(self . origin . x), (int)floor(self . origin . y), (int)ceil(self . origin . x + self . size . width), (int)ceil(self . origin . y + self . size . height));
}
MCGIRectangle MCGIRectangleUnion(const MCGIRectangle& left, const MCGIRectangle& right)
{
return MCGIRectangleMake(SkMin32(left . left, right . left), SkMin32(left . top, right . top), SkMax32(left . right, right . right), SkMax32(left . bottom, right . bottom));
}
MCGIRectangle MCGIRectangleIntersect(const MCGIRectangle& left, const MCGIRectangle& right)
{
return MCGIRectangleMake(SkMax32(left . left, right . left), SkMax32(left . top, right . top), SkMin32(left . right, right . right), SkMin32(left . bottom, right . bottom));
}
MCGIRectangle MCGIRectangleOffset(const MCGIRectangle& self, int32_t dx, int32_t dy)
{
return MCGIRectangleMake(self . left + dx, self . top + dy, self . right + dx, self . bottom + dy);
}
MCGIRectangle MCGIRectangleExpand(const MCGIRectangle& self, int32_t dx, int32_t dy)
{
return MCGIRectangleMake(self . left - dx, self . top - dy, self . right + dx, self . bottom + dy);
}
////////////////////////////////////////////////////////////////////////////////
static void MCGContextStateDestroy(MCGContextStateRef self)
{
if (self != NULL)
{
MCGRelease(self->fill_paint);
MCGRelease(self->stroke_paint);
MCGDashesRelease(self -> stroke_attr . dashes);
}
MCMemoryDelete(self);
}
static bool MCGContextStateCreate(MCGContextStateRef& r_state)
{
bool t_success;
t_success = true;
__MCGContextState *t_state;
t_state = NULL;
if (t_success)
t_success = MCMemoryNew(t_state);
if (t_success)
{
t_state -> base_transform = MCGAffineTransformMakeIdentity();
t_state -> transform = MCGAffineTransformMakeIdentity();
t_state -> opacity = 1.0f;
t_state -> blend_mode = kMCGBlendModeSourceOver;
t_state -> flatness = 0.0f;
t_state -> should_antialias = false;
t_state -> fill_paint = MCGRetain(kMCGBlackSolidColor);
t_state -> fill_opacity = 1.0f;
t_state -> fill_rule = kMCGFillRuleNonZero;
t_state -> fill_style = kMCGPaintStyleOpaque;
t_state -> stroke_paint = nullptr;
t_state -> stroke_opacity = 1.0f;
t_state -> stroke_attr . width = 0.0f;
t_state -> stroke_attr . join_style = kMCGJoinStyleBevel;
t_state -> stroke_attr . cap_style = kMCGCapStyleButt;
t_state -> stroke_attr . miter_limit = 0.0f;
t_state -> stroke_attr . dashes = NULL;
t_state -> stroke_style = kMCGPaintStyleOpaque;
t_state -> is_layer_begin_pt = false;
t_state -> parent = NULL;
}
if (t_success)
r_state = t_state;
else
MCGContextStateDestroy(t_state);
return t_success;
}
static bool MCGContextStateCopy(MCGContextStateRef p_state, MCGContextStateRef& r_new_state)
{
bool t_success;
t_success = true;
__MCGContextState *t_state;
t_state = NULL;
if (t_success)
t_success = MCMemoryNew(t_state);
if (t_success)
{
t_state->base_transform = MCGAffineTransformMakeIdentity();
t_state->transform = MCGAffineTransformMakeIdentity();
t_state -> opacity = p_state -> opacity;
t_state -> blend_mode = p_state -> blend_mode;
t_state -> flatness = p_state -> flatness;
t_state -> should_antialias = p_state -> should_antialias;
t_state -> fill_paint = MCGRetain(p_state->fill_paint);
t_state -> fill_opacity = p_state -> fill_opacity;
t_state -> fill_rule = p_state -> fill_rule;
t_state -> fill_style = p_state -> fill_style;
t_state -> stroke_paint = MCGRetain(p_state->stroke_paint);
t_state -> stroke_opacity = p_state -> stroke_opacity;
t_state -> stroke_attr . width = p_state -> stroke_attr . width;
t_state -> stroke_attr . join_style = p_state -> stroke_attr . join_style;
t_state -> stroke_attr . cap_style = p_state -> stroke_attr . cap_style;
t_state -> stroke_attr . miter_limit = p_state -> stroke_attr . miter_limit;
t_state -> stroke_attr . dashes = MCGDashesRetain(p_state -> stroke_attr . dashes);
t_state -> stroke_style = p_state -> stroke_style;
t_state -> is_layer_begin_pt = false;
t_state -> parent = NULL;
}
if (t_success)
r_new_state = t_state;
else
MCGContextStateDestroy(t_state);
return t_success;
}
////////////////////////////////////////////////////////////////////////////////
static void MCGContextLayerDestroy(MCGContextLayerRef self)
{
if (self == nil)
return;
// If there is a surface, it manages the canvas' lifetime
if (self->m_surface == nullptr)
self -> canvas -> unref();
self->m_surface.reset();
MCMemoryDelete(self);
}
static bool MCGContextLayerCreateUnbound(MCGContextLayerRef& r_layer)
{
bool t_success;
t_success = true;
__MCGContextLayer *t_layer;
t_layer = NULL;
if (t_success)
t_success = MCMemoryNew(t_layer);
if (t_success)
{
t_layer->nesting = 0;
t_layer->parent = nil;
}
if (t_success)
r_layer = t_layer;
else
MCGContextLayerDestroy(t_layer);
return t_success;
}
static bool MCGContextLayerCreateWithCanvas(SkCanvas *p_canvas, MCGContextLayerRef& r_layer)
{
if (!MCGContextLayerCreateUnbound(r_layer))
return false;
r_layer->canvas = p_canvas;
r_layer->canvas->ref();
return true;
}
static bool MCGContextLayerCreateWithSurface(sk_sp<SkSurface> p_surface, MCGContextLayerRef& r_layer)
{
if (!MCGContextLayerCreateUnbound(r_layer))
return false;
// Lifetime of the canvas is controlled by the surface
r_layer->m_surface = p_surface;
r_layer->canvas = p_surface->getCanvas();
return true;
}
static bool MCGContextLayerCreateSoftware(uint32_t p_width, uint32_t p_height, bool p_alpha, MCGContextLayerRef& r_layer)
{
// Create the image information for native-endian RGB with or without alpha
SkImageInfo t_info = SkImageInfo::MakeN32(p_width, p_height, p_alpha ? kPremul_SkAlphaType : kOpaque_SkAlphaType);
// Create the bitmap and set its info
SkBitmap t_bitmap;
t_bitmap.setInfo(t_info);
// Allocate the pixels for the bitmap
if (t_bitmap.tryAllocPixels())
{
t_bitmap.eraseARGB(0, 0, 0, 0);
sk_sp<SkCanvas> t_canvas(new (nothrow) SkCanvas(t_bitmap));
return MCGContextLayerCreateWithCanvas(t_canvas.get(), r_layer);
}
else
return false;
}
static bool MCGContextLayerCreate(uint32_t p_width, uint32_t p_height, bool p_alpha, MCGContextLayerRef& r_layer)
{
// This function contains hooks for adding OpenGL-accelerated drawing support
//if (!MCGPrepareOpenGL())
return MCGContextLayerCreateSoftware(p_width, p_height, p_alpha, r_layer);
// Get the GPU context
//GrContext* t_context = MCGGetGrContext();
// Create a GPU-backed surface as the render target
//SkImageInfo t_info = SkImageInfo::MakeN32(p_width, p_height, p_alpha ? kPremul_SkAlphaType : kOpaque_SkAlphaType);
//sk_sp<SkSurface> t_surface(SkSurface::MakeRenderTarget(t_context, SkBudgeted::kNo, t_info));
//if (t_surface == nullptr)
// return false;
// Create a context from the surface
//return MCGContextLayerCreateWithSurface(t_surface, r_layer);
}
////////////////////////////////////////////////////////////////////////////////
static bool MCGContextPushState(MCGContextRef self)
{
bool t_success;
t_success = true;
MCGContextStateRef t_state;
if (t_success)
t_success = MCGContextStateCopy(self -> state, t_state);
if (t_success)
{
t_state -> parent = self -> state;
t_state->base_transform = MCGContextGetDeviceTransform(self);
self -> state = t_state;
}
return t_success;
}
static bool MCGContextPopState(MCGContextRef self)
{
bool t_success;
t_success = true;
if (t_success)
{
MCGContextStateRef t_parent_state;
t_parent_state = self -> state -> parent;
MCGContextStateDestroy(self -> state);
self -> state = t_parent_state;
}
return t_success;
}
////////////////////////////////////////////////////////////////////////////////
static void MCGContextDestroy(MCGContextRef self)
{
if (self != NULL)
{
while (self -> state != NULL)
{
MCGContextStateRef t_tmp_state;
t_tmp_state = self -> state -> parent;
MCGContextStateDestroy(self -> state);
self -> state = t_tmp_state;
}
while(self -> layer != NULL)
{
MCGContextLayerRef t_tmp_layer;
t_tmp_layer = self -> layer -> parent;
MCGContextLayerDestroy(self -> layer);
self -> layer = t_tmp_layer;
}
if (self -> path != NULL)
MCGPathRelease(self -> path);
}
MCMemoryDelete(self);
}
static bool MCGContextCreateUnbound(MCGContextRef& r_context)
{
bool t_success;
t_success = true;
__MCGContext *t_context;
t_context = NULL;
if (t_success)
t_success = MCMemoryNew(t_context);
if (t_success)
t_success = MCGContextStateCreate(t_context->state);
if (t_success)
{
t_context->references = 1;
t_context->path = NULL;
t_context->is_valid = true;
}
if (t_success)
{
r_context = t_context;
}
else
MCGContextDestroy(t_context);
return t_success;
}
static bool MCGContextCreateWithBitmap(SkBitmap& p_bitmap, MCGContextRef& r_context)
{
bool t_success = true;
MCGContextRef t_context = nullptr;
t_success = MCGContextCreateUnbound(t_context);
SkCanvas *t_canvas = nil;
if (t_success)
{
t_canvas = new (nothrow) SkCanvas(p_bitmap);
t_success = t_canvas != nil;
}
if (t_success)
t_success = MCGContextLayerCreateWithCanvas(t_canvas, t_context->layer);
if (t_success)
r_context = t_context;
else
MCGContextDestroy(t_context);
if (t_canvas != nil)
t_canvas -> unref();
return t_success;
}
static bool MCGContextCreateWithSurface(sk_sp<SkSurface> p_surface, MCGContextRef& r_context)
{
bool t_success = true;
MCGContextRef t_context;
t_success = MCGContextCreateUnbound(t_context);
if (t_success)
t_success = MCGContextLayerCreateWithSurface(p_surface, t_context->layer);
if (t_success)
r_context = t_context;
else
MCGContextDestroy(t_context);
return t_success;
}
static bool MCGContextCreateSoftware(uint32_t p_width, uint32_t p_height, bool p_alpha, MCGContextRef& r_context)
{
// Create the image information for native-endian RGB with or without alpha
SkImageInfo t_info = SkImageInfo::MakeN32(p_width, p_height, p_alpha ? kPremul_SkAlphaType : kOpaque_SkAlphaType);
// Create the bitmap and set its info
SkBitmap t_bitmap;
t_bitmap.setInfo(t_info);
// Allocate the pixels for the bitmap
if (t_bitmap.tryAllocPixels())
{
t_bitmap.eraseARGB(0, 0, 0, 0);
return MCGContextCreateWithBitmap(t_bitmap, r_context);
}
else
return false;
}
bool MCGContextCreate(uint32_t p_width, uint32_t p_height, bool p_alpha, MCGContextRef& r_context)
{
// This function contains hooks for adding OpenGL-accelerated drawing support
//if (!MCGPrepareOpenGL())
return MCGContextCreateSoftware(p_width, p_height, p_alpha, r_context);
// Get the GPU context
//GrContext* t_context = MCGGetGrContext();
// Create a GPU-backed surface as the render target
//SkImageInfo t_info = SkImageInfo::MakeN32(p_width, p_height, p_alpha ? kPremul_SkAlphaType : kOpaque_SkAlphaType);
//sk_sp<SkSurface> t_surface(SkSurface::MakeRenderTarget(t_context, SkBudgeted::kNo, t_info));
//if (t_surface == nullptr)
// return false;
// Create a context from the surface
//return MCGContextCreateWithSurface(t_surface, r_context);
}
bool MCGContextCreateWithRaster(const MCGRaster& p_raster, MCGContextRef& r_context)
{
SkBitmap t_bitmap;
if (MCGRasterToSkBitmap(p_raster, kMCGPixelOwnershipTypeBorrow, t_bitmap))
return MCGContextCreateWithBitmap(t_bitmap, r_context);
else
return false;
}
bool MCGContextCreateWithPixels(uint32_t p_width, uint32_t p_height, uint32_t p_stride, void *p_pixels, bool p_alpha, MCGContextRef& r_context)
{
// Create the image information for native-endian RGB with or without alpha
SkImageInfo t_info = SkImageInfo::MakeN32(p_width, p_height, p_alpha ? kPremul_SkAlphaType : kOpaque_SkAlphaType);
// Create the bitmap and set its info
SkBitmap t_bitmap;
t_bitmap.setInfo(t_info);
// Tell the bitmap to use the given pixels
t_bitmap.installPixels(t_info, p_pixels, p_stride);
return MCGContextCreateWithBitmap(t_bitmap, r_context);
}
void *MCGContextGetPixelPtr(MCGContextRef context)
{
MCGContextLayerRef t_layer;
t_layer = context -> layer;
while(t_layer -> parent != nil)
t_layer = t_layer -> parent;
const SkBitmap& t_bitmap = t_layer -> canvas -> getTopDevice() -> accessBitmap(false);
return t_bitmap . getPixels();
}
uint32_t MCGContextGetWidth(MCGContextRef context)
{
MCGContextLayerRef t_layer;
t_layer = context -> layer;
while(t_layer -> parent != nil)
t_layer = t_layer -> parent;
const SkBitmap& t_bitmap = t_layer -> canvas -> getTopDevice() -> accessBitmap(false);
return t_bitmap . width();
}
uint32_t MCGContextGetHeight(MCGContextRef context)
{
MCGContextLayerRef t_layer;
t_layer = context -> layer;
while(t_layer -> parent != nil)
t_layer = t_layer -> parent;
const SkBitmap& t_bitmap = t_layer -> canvas -> getTopDevice() -> accessBitmap(false);
return t_bitmap . height();
}
MCGContextRef MCGContextRetain(MCGContextRef self)
{
if (self != NULL)
self -> references++;
return self;
}
void MCGContextRelease(MCGContextRef self)
{
if (self != NULL)
{
self -> references--;
if (self -> references <= 0)
MCGContextDestroy(self);
}
}
////////////////////////////////////////////////////////////////////////////////
// Returns whether the current context context is valid. If an error
// occurs when calling any method on a context context, it will become
// invalid and all further operations will be no-ops.
bool MCGContextIsValid(MCGContextRef self)
{
return self != NULL && self -> is_valid;
}
bool MCGContextIsLayerOpaque(MCGContextRef self)
{
return SkAlphaTypeIsOpaque(self->layer->canvas->imageInfo().alphaType());
}
////////////////////////////////////////////////////////////////////////////////
// Graphics state operations
void MCGContextSave(MCGContextRef self)
{
if (!MCGContextIsValid(self))
return;
bool t_success;
t_success = true;
if (t_success)
{
// we use skia to manage the clip and matrix between states, everything else is held in the state directly
self -> layer -> canvas -> save();
t_success = MCGContextPushState(self);
}
self -> is_valid = t_success;
}
void MCGContextRestore(MCGContextRef self)
{
if (!MCGContextIsValid(self))
return;
bool t_success;
t_success = true;
if (t_success)
t_success = self -> state -> parent != NULL;
// make sure we don't restore to a state before the current layer was begun
if (t_success)
if (!self -> state -> parent -> is_layer_begin_pt)
t_success = MCGContextPopState(self);
// we use skia to maintain a state's clip and transform, so calling restore ensures that the parent state's clip and CTM are restored properly
if (t_success)
self -> layer -> canvas -> restore();
self -> is_valid = t_success;
}
////////////////////////////////////////////////////////////////////////////////
// General attributes
void MCGContextSetFlatness(MCGContextRef self, MCGFloat p_flatness)
{
if (!MCGContextIsValid(self))
return;
self -> state -> flatness = p_flatness;
}
void MCGContextSetShouldAntialias(MCGContextRef self, bool p_should_antialias)
{
if (!MCGContextIsValid(self))
return;
self -> state -> should_antialias = p_should_antialias;
}
////////////////////////////////////////////////////////////////////////////////
// Layer attributes and manipulation - bitmap effect options would be added here also.
// Now replay the clip from the parent layer.
class CanvasClipVisitor: public SkCanvas::ClipVisitor
{
public:
CanvasClipVisitor(SkCanvas *p_target_canvas)
{
m_target_canvas = p_target_canvas;
}
void clipRect(const SkRect& p_rect, SkRegion::Op p_op, bool p_antialias) override
{
m_target_canvas -> clipRect(p_rect, p_op, false);
}
void clipRRect(const SkRRect& p_rrect, SkRegion::Op p_op, bool p_antialias) override
{
// Todo: support rounded rect clip regions
clipRect(p_rrect.getBounds(), p_op, p_antialias);
}
void clipPath(const SkPath& p_path, SkRegion::Op p_op, bool p_antialias) override
{
m_target_canvas -> clipPath(p_path, p_op, false);
}
private:
SkCanvas *m_target_canvas;
};
void MCGContextBegin(MCGContextRef self, bool p_need_layer)
{
if (!MCGContextIsValid(self))
return;
// If we are blending sourceOver
if (!p_need_layer && self -> state -> blend_mode == kMCGBlendModeSourceOver && self -> state -> opacity == 1.0)
{
//MCGContextSave(self);
self -> layer -> nesting += 1;
return;
}
// Fetch the bounds of the current clip in device co-ords and if the
// clip is empty, then just increase the nesting level.
SkIRect t_device_clip;
if (!self -> layer -> canvas -> getClipDeviceBounds(&t_device_clip))
{
//MCGContextSave(self);
self -> layer -> nesting += 1;
return;
}
// Fetch the total matrix of the canvas.
SkMatrix t_device_matrix;
t_device_matrix = self -> layer -> canvas -> getTotalMatrix();
// Create a new layer the same size as the device clip
MCGContextLayerRef t_new_layer;
SkIRect t_clip_rect;
self->layer->canvas->getClipDeviceBounds(&t_clip_rect);
if (!MCGContextLayerCreate(t_clip_rect.width(), t_clip_rect.height(), true, t_new_layer))
{
self->is_valid = false;
return;
}
// Get the canvas from the new layer
SkCanvas* t_new_canvas = t_new_layer->canvas;
// Next translate the canvas by the translation factor of the matrix.
t_new_canvas -> translate(-t_device_clip . x(), -t_device_clip . y());
// Replay the clip.
CanvasClipVisitor t_clip_visitor(t_new_canvas);
self -> layer -> canvas -> replayClips(&t_clip_visitor);
// Set the matrix.
t_new_canvas -> concat(t_device_matrix);
// Make a save point in the new canvas.
t_new_canvas -> save();
// Set the current state as the layer being pt.
self -> state -> is_layer_begin_pt = true;
// Push the current state onto the attribute stack.
MCGContextPushState(self);
self -> state -> opacity = 1.0;
self -> state -> blend_mode = kMCGBlendModeSourceOver;
t_new_layer -> parent = self -> layer;
t_new_layer -> origin_x = t_device_clip . x();
t_new_layer -> origin_y = t_device_clip . y();
t_new_layer -> has_effects = false;
self -> layer = t_new_layer;
}
// The 'shape' parameter is the rectangle in user-space of the area to which the effect
// is to be applied.
//
// This shape is unrelated to the current clip, and the resulting rect of the layer which
// is to be rendered into must be big enough to ensure that any pixels rendered as a result
// of the bitmap effects have source pixels to operate on.
//
void MCGContextBeginWithEffects(MCGContextRef self, MCGRectangle p_shape, const MCGBitmapEffects& p_effects)
{
if (!MCGContextIsValid(self))
return;
MCGAffineTransform t_device_transform;
t_device_transform = MCGContextGetDeviceTransform(self);
MCGIRectangle t_device_clip;
t_device_clip = MCGRectangleComputeHull(MCGContextGetDeviceClipBounds(self));
// First transform the shape rect by the total transform to get it's rectangle in device space.
MCGIRectangle t_device_shape;
t_device_shape = MCGRectangleComputeHull(MCGRectangleApplyAffineTransform(p_shape, t_device_transform));
// This is the rectangle of pixels not related to bitmap effects which are needed.
MCGIRectangle t_layer_clip;
t_layer_clip = MCGIRectangleIntersect(t_device_clip, t_device_shape);
// This calculates the rect of pixels needed from the layer to draw the drop shadow.
// We offset the shape by the drop shadow x/y and intersect with the clip.
// We then offset it back and expand by the blur radius.
// We then intersect with the shape.
// Finally add this rectangle to the layer clip (union).
if (p_effects . has_drop_shadow)
{
MCGSize t_radii, t_offset;
t_radii = MCGSizeApplyAffineTransform(MCGSizeMake(p_effects . drop_shadow . size, p_effects . drop_shadow . size), t_device_transform);
t_offset = MCGSizeApplyAffineTransform(MCGSizeMake(p_effects . drop_shadow . x_offset, p_effects . drop_shadow . y_offset), t_device_transform);
t_layer_clip = MCGIRectangleUnion(
t_layer_clip,
MCGIRectangleIntersect(
t_device_shape,
MCGIRectangleExpand(
MCGIRectangleUnion(
MCGIRectangleOffset(
MCGIRectangleIntersect(
t_device_clip,
MCGIRectangleOffset(
t_device_shape,
floor(t_offset . width), floor(t_offset . height))),
-floor(t_offset . width), -floor(t_offset . height)),
MCGIRectangleOffset(
MCGIRectangleIntersect(
t_device_clip,
MCGIRectangleOffset(
t_device_shape,
ceil(t_offset . width), ceil(t_offset . height))),
-ceil(t_offset . width), -ceil(t_offset . height))),
ceil(t_radii . width), ceil(t_radii . height))));
}
// Next process the inner shadow.
// We intersect the shape with the clip to determine the visible pixels (as inner shadow only works internally).
// We then offset it by the inner shadow x/y (to work out what source pixels are needed)
// We then expand by the blur radius.
// We then intersect with the shape.
// Finally add this rectangle to the layer clip (union).
if (p_effects . has_inner_shadow)
{
MCGSize t_radii, t_offset;
t_radii = MCGSizeApplyAffineTransform(MCGSizeMake(p_effects . inner_shadow . size, p_effects . inner_shadow . size), t_device_transform);
t_offset = MCGSizeApplyAffineTransform(MCGSizeMake(p_effects . inner_shadow . x_offset, p_effects . inner_shadow . y_offset), t_device_transform);
// MW-2013-10-31: [[ Bug 11359 ]] Offset by -t_offset, not t_offset.
t_layer_clip = MCGIRectangleUnion(
t_layer_clip,
MCGIRectangleIntersect(
t_device_shape,
MCGIRectangleExpand(
MCGIRectangleUnion(
MCGIRectangleOffset(
MCGIRectangleIntersect(t_device_shape, t_device_clip),
-floor(t_offset . width), -floor(t_offset . height)),
MCGIRectangleOffset(
MCGIRectangleIntersect(t_device_shape, t_device_clip),
-ceil(t_offset . width), -ceil(t_offset . height))),
ceil(t_radii . width), ceil(t_radii . height))));
}
// Next process outer glow.
// We intersect the shape with the clip to determine visible pixels
// We then expand by the radii.
// We then intersect with the device shape to restrict to renderable pixels.
// Finally we add this rectangle to the layer clip (union).
if (p_effects . has_outer_glow)
{
MCGSize t_radii;
t_radii = MCGSizeApplyAffineTransform(MCGSizeMake(p_effects . outer_glow . size, p_effects . outer_glow . size), t_device_transform);
t_layer_clip = MCGIRectangleUnion(
t_layer_clip,
MCGIRectangleIntersect(
t_device_shape,
MCGIRectangleExpand(
MCGIRectangleIntersect(t_device_shape, t_device_clip),
ceil(t_radii . width), ceil(t_radii . height))));
}
// Next process inner glow.
// We intersect the shape with the clip to determine visible pixels (inner glow only works internally).
// We then expand by the radii.
// We then intersect with the shape
// Finally we add this rectangle to the layer clip (union).
if (p_effects . has_inner_glow)
{
// MW-2013-10-31: [[ Bug 11359 ]] Make sure we use the inner_glow size size field, not the outer_glow one.
MCGSize t_radii;
t_radii = MCGSizeApplyAffineTransform(MCGSizeMake(p_effects . inner_glow . size, p_effects . inner_glow . size), t_device_transform);
t_layer_clip = MCGIRectangleUnion(
t_layer_clip,
MCGIRectangleIntersect(
t_device_shape,
MCGIRectangleExpand(
MCGIRectangleIntersect(t_device_clip, t_device_shape),
ceil(t_radii . width), ceil(t_radii . height))));
}
t_layer_clip = MCGIRectangleIntersect(t_layer_clip, t_device_shape);
// IM-2014-06-24: [[ GraphicsPerformance ]] If the clip is empty then don't try to create a new layer
if ((t_layer_clip.left >= t_layer_clip.right) || (t_layer_clip.top >= t_layer_clip.bottom))
{
self -> layer -> nesting += 1;
return;
}
// Allocate a new layer the same size as the device clip
MCGContextLayerRef t_new_layer;
if (!MCGContextLayerCreate(t_layer_clip.right - t_layer_clip.left, t_layer_clip.bottom - t_layer_clip.top, true, t_new_layer))
{
self->is_valid = false;
return;
}
// Get the canvas from the new layer
SkCanvas* t_new_canvas = t_new_layer->canvas;
// Next translate the canvas by the translation factor of the matrix.
t_new_canvas -> translate(-t_layer_clip . left, -t_layer_clip . top);
// Set the matrix.
t_new_canvas -> concat(self -> layer -> canvas -> getTotalMatrix());
// Make a save point in the new canvas.
t_new_canvas -> save();
// Set the current state as the layer being pt.
self -> state -> is_layer_begin_pt = true;
// Push the current state onto the attribute stack.
MCGContextPushState(self);
self -> state -> opacity = 1.0;
self -> state -> blend_mode = kMCGBlendModeSourceOver;
t_new_layer -> parent = self -> layer;
t_new_layer -> origin_x = t_layer_clip . left;
t_new_layer -> origin_y = t_layer_clip . top;
t_new_layer -> has_effects = true;
t_new_layer -> effects = p_effects;
self -> layer = t_new_layer;
}
// Utility methods for drawing against a raster mask
static void MCGMaskedDeviceFill(SkCanvas& p_canvas, const SkMask& p_mask, const SkPaint& p_paint)
{
SkRect t_bounds = SkRect::Make(p_mask.fBounds);
SkImageInfo t_info = SkImageInfo::MakeA8(p_mask.fBounds.width(), p_mask.fBounds.height());
SkBitmap t_mask_bitmap;
t_mask_bitmap.setInfo(t_info);
t_mask_bitmap.installMaskPixels(p_mask);
p_canvas.save();
p_canvas.resetMatrix();
p_canvas.drawBitmap(t_mask_bitmap, t_bounds.x(), t_bounds.y(), &p_paint);
p_canvas.restore();
}
static void MCGContextRenderEffect(MCGContextRef self, const SkMask& p_mask, MCGSize p_radii, MCGSize p_offset, MCGFloat p_spread, MCGBlurType p_attenuation, MCGColor p_color, MCGBlendMode p_blend)
{
// Get the device transform.
MCGAffineTransform t_transform;
t_transform = MCGContextGetDeviceTransform(self);
// Compute the transforms of the radii and offset.
MCGSize t_transformed_radii, t_transformed_offset;
t_transformed_radii = MCGSizeApplyAffineTransform(p_radii, t_transform);
t_transformed_offset = MCGSizeApplyAffineTransform(p_offset, t_transform);
// Now blur the mask.
SkMask t_blurred_mask;
if (!MCGBlurBox(p_mask, t_transformed_radii . width, t_transformed_radii . height, p_spread, p_spread, t_blurred_mask))
return;
// Offset the blur mask appropriately.
// TODO: Handle sub-pixel case!
t_blurred_mask . fBounds . offset(t_transformed_offset . width, t_transformed_offset . height);
// Compute the intersection.
SkIRect t_inside;
bool t_overlap;
t_overlap = t_inside . intersect(p_mask . fBounds, t_blurred_mask . fBounds);
// MW-2013-10-31: [[ Bug 11325 ]] An outer blur with no intersection is just a normal
// blur.
if (!t_overlap && (p_attenuation == kMCGBlurTypeOuter))
p_attenuation = kMCGBlurTypeNormal;
// Now process the mask according to the attenuation.
uint8_t *t_old_blurred_mask_fImage;
t_old_blurred_mask_fImage = t_blurred_mask . fImage;
switch(p_attenuation)
{
case kMCGBlurTypeNormal:
{
uint8_t *t_blur_ptr;
t_blur_ptr = t_blurred_mask . fImage;
// MW-2013-10-31: [[ Bug 11325 ]] Attenuate the mask appropriately, including
// applying the opacity.
for(int y = 0; y < t_blurred_mask . fBounds . height(); y++)
{
for(int x = 0; x < t_blurred_mask . fBounds . width(); x++)
t_blur_ptr[x] = SkAlphaMul(t_blur_ptr[x], SkAlpha255To256(p_color >> 24));
t_blur_ptr += t_blurred_mask . fRowBytes;
}
}
break;
case kMCGBlurTypeInner:
{
// We process the generated mask so that the mask consists only of pixels
// in the intersection of blur-mask and mask.
if (t_overlap)
{
uint8_t *t_blur_ptr, *t_mask_ptr;
t_blur_ptr = t_blurred_mask . getAddr8(t_inside . x(), t_inside . y());
t_mask_ptr = p_mask . getAddr8(t_inside . x(), t_inside . y());
// MW-2013-10-31: [[ Bug 11325 ]] Attenuate the mask appropriately, including
// applying the opacity.
for(int y = 0; y < t_inside . height(); y++)
{
for(int x = 0; x < t_inside . width(); x++)
{
t_blur_ptr[x] = SkAlphaMul(t_blur_ptr[x], SkAlpha255To256(t_mask_ptr[x]));
t_blur_ptr[x] = SkAlphaMul(t_blur_ptr[x], SkAlpha255To256(p_color >> 24));
}
t_blur_ptr += t_blurred_mask . fRowBytes;
t_mask_ptr += p_mask . fRowBytes;
}
t_blurred_mask . fImage = t_blurred_mask . getAddr8(t_inside . x(), t_inside . y());
t_blurred_mask . fBounds = t_inside;
}
}
break;
case kMCGBlurTypeInvertedInner: