Best way to draw text in CAOpenGLLayer

  • HI All,
      I am trying to scroll text in CAOpenGLLayer. Every thing is working
    fine. But the scrolling is not smooth and the CPU usage was 60 % -
    80%. I think application is taking time to convert NSString into a
    CGImage. Is there a better way of displaying text in CAOpenGLLayer.

    This is how i am creating the CGBitmap context

    - (void)setUpContext
    {
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    size_t  width, height, bytesPerRow;
    CGDataProviderRef bitmapContextDataProvider;
    width = floor(self.bounds.size.width);
    height = floor(self.bounds.size.height);
    bytesPerRow = (width * 4 + 63) & ~63;
    bitmapData = calloc(bytesPerRow * height, 1);
    bitmapContext = CGBitmapContextCreate(bitmapData, width, height,
    8 ,bytesPerRow, colorspace, kCGImageAlphaPremultipliedLast);
    graphicsContext = [[NSGraphicsContext
    graphicsContextWithGraphicsPort:bitmapContext flipped:NO] retain];
    bitmapContextDataProvider = CGDataProviderCreateWithData(nil,
    bitmapData, bytesPerRow * height, nil);
    bitmapImage = CGImageCreate(width,
            height,
            8,
            32,
            bytesPerRow,
            colorspace,
            kCGImageAlphaPremultipliedLast,
            bitmapContextDataProvider,
            nil,
            false,
            kCGRenderingIntentDefault);
    CGDataProviderRelease(bitmapContextDataProvider);
    CGColorSpaceRelease(colorspace);
    }

    this is how i am converting it to image

    NSGraphicsContext    *previousContext = [NSGraphicsContext currentContext];
    CGContextClearRect(bitmapContext,self.bounds);
    [NSGraphicsContext setCurrentContext:graphicsContext];
    NSRect rect = NSRectFromCGRect(self.bounds);
    if (startCrawler) {
      NSRect frameRect;
      frameRect.size = NSMakeSize(0, self.frame.size.height - 10);
      NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
      [attributes setObject:font forKey:NSFontAttributeName];
      [attributes setObject:textColor
    forKey:NSForegroundColorAttributeName];
      for (int i =  0 ; i < [dispalyTitleArray count]  ; i++ ) {
      RSSDisplayItem *currentDisplayItem = [dispalyTitleArray
    objectAtIndex:i];
      frameRect.size.width = [currentDisplayItem size].width +
    kTextPadding;
      frameRect.origin.x  = [currentDisplayItem xLocation];
      [[currentDisplayItem title] drawInRect:frameRect
    withAttributes:attributes];
      }
    }
    [NSGraphicsContext setCurrentContext:previousContext];

    Regards,
    Anshul jain

    Regards,
    Anshul jain
    Software Engineer

    Prithvi Information Solutions Ltd
    10Q3A1, Cyber Towers
    HITEC City, Madhapur
    Hyderabad - 500 081
  • >
    > But the scrolling is not smooth and the CPU usage was 60 % -  80%. I
    > think application is taking time to convert NSString into a CGImage.

    Do you know that is the case? If you have any performance issue, you
    should use sample and Shark. Then you can try to figure out what is
    wrong.

    -corbin
  • On May 21, 2009, at 3:23 AM, Anshul jain wrote:

    > I am trying to scroll text in CAOpenGLLayer

    Don't. Use a CATextLayer (which can render text for you) or a CALayer
    (to display image content) instead. Unless you want to get outlines of
    your text, the CAOpenGLLayer is really not a good way to do this.
    --
    David Duncan
    Apple DTS Animation and Printing
  • On May 21, 2009, at 10:12 AM, David Duncan wrote:

    > Don't. Use a CATextLayer (which can render text for you) or a
    > CALayer (to display image content) instead. Unless you want to get
    > outlines of your text, the CAOpenGLLayer is really not a good way to
    > do this.

    Clarifying a bit - unless you want to render your text as vector
    graphics, then you don't want to use an OpenGL layer to do this.
    --
    David Duncan
    Apple DTS Animation and Printing
  • HI
    thanks for suggestion. Since i want to scroll the text so i have to
    update it regularly so i have taken a CAOPenGLLayer and set is
    asynchronous and  i am using

      - (void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:
    (CGLPixelFormatObj)pixelFormat forLayerTime:
    (CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp

    this fuction to update text.

    Is this the right approach to do it.

    On 21-May-09, at 10:52 PM, David Duncan wrote:

    > On May 21, 2009, at 10:12 AM, David Duncan wrote:
    >
    >> Don't. Use a CATextLayer (which can render text for you) or a
    >> CALayer (to display image content) instead. Unless you want to get
    >> outlines of your text, the CAOpenGLLayer is really not a good way
    >> to do this.
    >
    >
    > Clarifying a bit - unless you want to render your text as vector
    > graphics, then you don't want to use an OpenGL layer to do this.
    > --
    > David Duncan
    > Apple DTS Animation and Printing
    >
    >

    Regards,
    Anshul jain
  • HI
    thanks for suggestion. Since i want to scroll the text so i have to
    update it regularly so i have taken a CAOPenGLLayer and set is
    asynchronous and  i am using

      - (void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:
    (CGLPixelFormatObj)pixelFormat forLayerTime:
    (CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp

    this fuction to update text.

    Is this the right approach to do it.

    On 21-May-09, at 10:52 PM, David Duncan wrote:

    > On May 21, 2009, at 10:12 AM, David Duncan wrote:
    >
    >> Don't. Use a CATextLayer (which can render text for you) or a
    >> CALayer (to display image content) instead. Unless you want to get
    >> outlines of your text, the CAOpenGLLayer is really not a good way
    >> to do this.
    >
    >
    > Clarifying a bit - unless you want to render your text as vector
    > graphics, then you don't want to use an OpenGL layer to do this.
    > --
    > David Duncan
    > Apple DTS Animation and Printing
    >
    >

    Regards,
    Anshul jain
    Software Engineer

    Prithvi Information Solutions Ltd
    10Q3A1, Cyber Towers
    HITEC City, Madhapur
    Hyderabad - 500 081
  • On May 21, 2009, at 11:25 PM, Anshul jain wrote:

    > Hi
    > thanks for suggestion. Since i want to scroll the text so i have to
    > update it regularly so i have taken a CAOPenGLLayer and set is
    > asynchronous and  i am using
    >
    > Is this the right approach to do it.

    In Core Animation if you want to scroll something, then you move its
    layer. There is no need to continuously update the content of the
    layer, which is what you are proposing with the CAOpenGLLayer
    solution. Additionally, your basically just using a GL layer to draw a
    bitmap that is continuously updating without any other reason to use
    GL, its a waste of resources.

    I would highly recommend that you read the Core Animation Programming
    Guide before you continue much further, as it may give you some bigger
    insights into what is available and how you might use it. You can find
    it here: <http://developer.apple.com/documentation/Cocoa/Conceptual/CoreAnimation_gui
    de/Introduction/Introduction.html
    >
    --
    David Duncan
    Apple DTS Animation and Printing
  • Ya i also thought that way but the problem is that between two rss
    feed there should be a separator image. So if i use scroll layer then
    it can be a problem of drawing the image.
    On 22-May-09, at 11:16 PM, David Duncan wrote:

    > On May 21, 2009, at 11:25 PM, Anshul jain wrote:
    >
    >> Hi
    >> thanks for suggestion. Since i want to scroll the text so i have
    >> to update it regularly so i have taken a CAOPenGLLayer and set is
    >> asynchronous and  i am using
    >>
    >> Is this the right approach to do it.
    >
    >
    > In Core Animation if you want to scroll something, then you move its
    > layer. There is no need to continuously update the content of the
    > layer, which is what you are proposing with the CAOpenGLLayer
    > solution. Additionally, your basically just using a GL layer to draw
    > a bitmap that is continuously updating without any other reason to
    > use GL, its a waste of resources.
    >
    > I would highly recommend that you read the Core Animation
    > Programming Guide before you continue much further, as it may give
    > you some bigger insights into what is available and how you might
    > use it. You can find it here: <http://developer.apple.com/documentation/Cocoa/Conceptual/CoreAnimation_gui
    de/Introduction/Introduction.html
    > >
    > --
    > David Duncan
    > Apple DTS Animation and Printing
    >
    >

    Regards,
    Anshul jain
    Software Engineer

    Prithvi Information Solutions Ltd
    10Q3A1, Cyber Towers
    HITEC City, Madhapur
    Hyderabad - 500 081