You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by cx...@apache.org on 2017/04/10 05:53:55 UTC

[05/15] incubator-weex git commit: * [ios] use bitmap context to draw compositing

* [ios] use bitmap context to draw compositing


Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/3a714ac3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/3a714ac3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/3a714ac3

Branch: refs/heads/0.12-dev
Commit: 3a714ac333b2d230d11e6c8c2e94ab94988049da
Parents: c6d980d
Author: \u9690\u98ce <cx...@apache.org>
Authored: Wed Apr 5 20:17:20 2017 +0800
Committer: \u9690\u98ce <cx...@apache.org>
Committed: Wed Apr 5 20:17:20 2017 +0800

----------------------------------------------------------------------
 .../Sources/Component/WXImageComponent.m        |  2 +-
 .../WeexSDK/Sources/Component/WXTextComponent.m | 10 ++--
 .../Sources/Display/WXComponent+Display.m       | 49 +++++++++++++-------
 ios/sdk/WeexSDK/Sources/Model/WXComponent.h     |  4 +-
 4 files changed, 38 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/3a714ac3/ios/sdk/WeexSDK/Sources/Component/WXImageComponent.m
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Component/WXImageComponent.m b/ios/sdk/WeexSDK/Sources/Component/WXImageComponent.m
index 1e2594c..2527adc 100644
--- a/ios/sdk/WeexSDK/Sources/Component/WXImageComponent.m
+++ b/ios/sdk/WeexSDK/Sources/Component/WXImageComponent.m
@@ -167,7 +167,7 @@ static dispatch_queue_t WXImageUpdateQueue;
     }
 }
 
-- (UIImage *)drawRect:(CGRect)rect
+- (UIImage *)drawRect:(CGRect)rect withContext:(CGContextRef)context;
 {
     if (!self.image) {
         [self updateImage];

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/3a714ac3/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m b/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m
index 4ee3860..ff98d2d 100644
--- a/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m
+++ b/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m
@@ -201,13 +201,13 @@ do {\
     return YES;
 }
 
-- (UIImage *)drawRect:(CGRect)rect
+- (UIImage *)drawRect:(CGRect)rect withContext:(CGContextRef)context;
 {
     if (_isCompositingChild) {
-        [self drawTextWithBounds:rect padding:_padding view:nil];
+        [self drawTextWithContext:context bounds:rect padding:_padding view:nil];
     } else {
         WXText *textView = ((WXText *)self.view);
-        [self drawTextWithBounds:rect padding:_padding view:textView];
+        [self drawTextWithContext:context bounds:rect padding:_padding view:textView];
     }
     
     return nil;
@@ -413,14 +413,12 @@ do {\
     [self syncTextStorageForView];
 }
 
-- (void)drawTextWithBounds:(CGRect)bounds padding:(UIEdgeInsets)padding view:(WXText *)view
+- (void)drawTextWithContext:(CGContextRef)context bounds:(CGRect)bounds padding:(UIEdgeInsets)padding view:(WXText *)view
 {
     if (bounds.size.width <=0 || bounds.size.height <= 0) {
         return;
     }
     
-    CGContextRef context = UIGraphicsGetCurrentContext();
-    
     if ([self _needsDrawBorder]) {
         [self _drawBorderWithContext:context size:bounds.size];
     } else {

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/3a714ac3/ios/sdk/WeexSDK/Sources/Display/WXComponent+Display.m
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Display/WXComponent+Display.m b/ios/sdk/WeexSDK/Sources/Display/WXComponent+Display.m
index fdd02a1..b548663 100644
--- a/ios/sdk/WeexSDK/Sources/Display/WXComponent+Display.m
+++ b/ios/sdk/WeexSDK/Sources/Display/WXComponent+Display.m
@@ -58,7 +58,7 @@
     return YES;
 }
 
-- (UIImage *)drawRect:(CGRect)rect
+- (UIImage *)drawRect:(CGRect)rect withContext:(CGContextRef)context
 {
     CGSize size = rect.size;
     if (size.width <= 0 || size.height <= 0) {
@@ -66,7 +66,6 @@
         return nil;
     }
     
-    CGContextRef context = UIGraphicsGetCurrentContext();
     [self _drawBorderWithContext:context size:size];
 
     return nil;
@@ -80,7 +79,8 @@
         }
 
         UIGraphicsBeginImageContextWithOptions(bounds.size, [self _bitmapOpaqueWithSize:bounds.size] , 0.0);
-        UIImage *image = [self drawRect:bounds];
+        CGContextRef context = UIGraphicsGetCurrentContext();
+        UIImage *image = [self drawRect:bounds withContext:context];
         if (!image) {
             image = UIGraphicsGetImageFromCurrentImageContext();
         }
@@ -176,16 +176,32 @@
 
 - (CGContextRef)beginDrawContext:(CGRect)bounds
 {
-    UIGraphicsBeginImageContextWithOptions(bounds.size, [self _bitmapOpaqueWithSize:bounds.size], 0.0);
-    CGContextRef context = UIGraphicsGetCurrentContext();
+//    UIGraphicsBeginImageContextWithOptions(bounds.size, [self _bitmapOpaqueWithSize:bounds.size], 0.0);
+//    CGContextRef context = UIGraphicsGetCurrentContext();
+    
+    float scaleFactor = [[UIScreen mainScreen] scale];
+    CGColorSpaceRef	colorSpace = CGColorSpaceCreateDeviceRGB();
+    CGContextRef context = CGBitmapContextCreate(NULL, bounds.size.width * scaleFactor, bounds.size.height * scaleFactor, 8, 4 * bounds.size.width * scaleFactor, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
+    CGContextScaleCTM(context, scaleFactor, scaleFactor);
+
+    // Adjusts position and invert the image.
+    // The OpenGL uses the image data upside-down compared commom image files.
+    CGContextTranslateCTM(context, 0, bounds.size.height);
+    CGContextScaleCTM(context, 1.0, -1.0);
+    
+    CGColorSpaceRelease(colorSpace);
     
     return context;
 }
 
-- (UIImage *)endDrawContext
+- (UIImage *)endDrawContext:(CGContextRef)context
 {
-    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
-    UIGraphicsEndImageContext();
+//    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
+//    UIGraphicsEndImageContext();
+    
+    CGImageRef imageRef= CGBitmapContextCreateImage(context);
+    UIImage *image = [[UIImage alloc] initWithCGImage:imageRef];
+    CGContextRelease(context);
     
     return image;
 }
@@ -220,13 +236,7 @@
             block();
         }
         
-//        CGImageRef imageRef= CGBitmapContextCreateImage(context);
-//        UIImage *image = [[UIImage alloc] initWithCGImage:imageRef];
-        
-//        CGColorSpaceRelease(colorSpace);
-//        CGContextRelease(context);
-       
-        UIImage *image = [self endDrawContext];
+        UIImage *image = [self endDrawContext:context];
         return image;
     };
 }
@@ -249,7 +259,9 @@
     
     if (shouldDisplay) {
         dispatch_block_t displayBlockToPush = ^{
-            CGContextRef context = UIGraphicsGetCurrentContext();
+            if (!context) {
+                
+            }
             CGContextSaveGState(context);
             CGContextTranslateCTM(context, frame.origin.x, frame.origin.y);
             
@@ -261,9 +273,10 @@
                 [[UIBezierPath bezierPathWithRect:bounds] addClip];
             }
             
-            UIImage *image = [self drawRect:bounds];
+            UIImage *image = [self drawRect:bounds withContext:context];
             if (image) {
-                [image drawInRect:bounds];
+                CGContextDrawImage(context, bounds, image.CGImage);
+//                [image drawInRect:bounds];
             }
         };
         [displayBlocks addObject:[displayBlockToPush copy]];

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/3a714ac3/ios/sdk/WeexSDK/Sources/Model/WXComponent.h
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Model/WXComponent.h b/ios/sdk/WeexSDK/Sources/Model/WXComponent.h
index 6bd7cd3..d9e7070 100644
--- a/ios/sdk/WeexSDK/Sources/Model/WXComponent.h
+++ b/ios/sdk/WeexSDK/Sources/Model/WXComponent.h
@@ -333,7 +333,7 @@ NS_ASSUME_NONNULL_BEGIN
 
 - (CGContextRef)beginDrawContext:(CGRect)bounds;
 
-- (UIImage *)endDrawContext;
+- (UIImage *)endDrawContext:(CGContextRef)context;
 
 /**
  * @abstract Returns a Boolean indicating whether the component needs to be drawn by `drawRect:`
@@ -350,7 +350,7 @@ NS_ASSUME_NONNULL_BEGIN
  * If you already have an image that represents the content of the component, then you should just return the image and do no drawing, otherwise you should draw your content in the current context and return nil.
  * You should never call this method directly yourself. To invalidate part of your component's content, and thus cause that portion to be redrawn, call the `setNeedsDisplay` method instead.
  */
-- (UIImage *)drawRect:(CGRect)rect;
+- (UIImage *)drawRect:(CGRect)rect withContext:(CGContextRef)context;
 
 /**
  * @abstract Called when a component finishes rendering its content.