static CGLayerRef createCGLayerForDrawing(CGContextRef c) { CGRect rect = { 0, 0, 50, 50 }; CGSize layerSize; CGLayerRef layer; // Make the layer the size of the rectangle that // this code draws into the layer. layerSize.width = rect.size.width; layerSize.height = rect.size.height; // Create the layer to draw into. layer = CGLayerCreateWithContext(c, layerSize, NULL); if(layer == NULL) return NULL; // Get the context corresponding to the layer. Note // that this is a 'Get' function so the code must // not release the context. CGContextRef layerContext = CGLayerGetContext(layer); if(layerContext == NULL){ CGLayerRelease(layer); return NULL; } // Set the fill color to opaque black. CGContextSetFillColorWithColor(layerContext, getRGBOpaqueBlackColor()); // Draw the content into the layer. CGContextFillRect(layerContext, rect); // Now the layer has the contents needed. return layer; }
void GraphicsContext::fillRect(const FloatRect& rect) { if (paintingDisabled()) return; CGContextRef context = platformContext(); if (m_state.fillGradient) { CGContextSaveGState(context); CGContextConcatCTM(context, m_state.fillGradient->gradientSpaceTransform()); if (hasShadow()) { CGLayerRef layer = CGLayerCreateWithContext(context, CGSizeMake(rect.width(), rect.height()), 0); CGContextRef layerContext = CGLayerGetContext(layer); m_state.fillGradient->paint(layerContext); CGContextDrawLayerAtPoint(context, CGPointMake(rect.left(), rect.top()), layer); CGLayerRelease(layer); } else { CGContextClipToRect(context, rect); m_state.fillGradient->paint(this); } CGContextRestoreGState(context); return; } if (m_state.fillPattern) applyFillPattern(); CGContextFillRect(context, rect); }
// // Stage 4 - Draw a 2K by 2K source image to an offscreen 512 x 512 CG Layer, // effectively caching the original image. The test measures how fast drawing // the cached layer takes. The cache alleviates the time taken to color match the // image and also time required to downsample the image. // This cache technique is useful when the image has to be repeatedly draw at the // same scale. The difference between this test the previous is that the previous // performed the caching using a bitmap context. This one caches using a CGLayerRef. // Notice that we don't need to know the colorspace or destination context's device // specific information when creating a CG Layer. // The source image is created programattically as a gradient between colors. // Returns: the number of operations per second for this stage // float drawStage4(CGContextRef context, CGRect rect) { size_t numOperations; CGImageRef image; double delta; int i; unsigned char *data; CGLayerRef lyr; CGContextRef lyrContext; data = createGradientARGBBuffer(W, H); if (data == NULL) return 0; // Create the source image from the data provider image = createImage(W, H, data); // Create a layer from the destination context. The layer is the best representation // for the layer. We have to specify the size of the layer we want to create // in default userspace units. lyr = CGLayerCreateWithContext(context,CGSizeMake(ScaledToWidth,ScaledToHeight),NULL); // Now we want to draw in to layer, so get the CGContext for the layer to draw to lyrContext = CGLayerGetContext(lyr); // Draw the 3K x 3K source image once to the layer context CGContextDrawImage(lyrContext, CGRectMake(0,0,ScaledToWidth,ScaledToHeight), image); CFRelease(image); // Done with the image - now cached in the layer ref free(data); // free the source data // Perform the drawing operation once to get an rough idea of how long it will take delta = currentTime(); // Draw the contents of the layer to the destination CGContextDrawLayerAtPoint(context,CGPointMake(0,0),lyr); delta = currentTime() - delta; // Calculate the approximage number of operations needed in one second numOperations = SecsPerTest / delta; // Now run the test again repeatedly delta = currentTime(); for (i = 0 ; i < numOperations; i++) { // Draw the contents of the layer to the destination CGContextDrawLayerAtPoint(context,CGPointMake(0,0),lyr); } delta = currentTime() - delta; CFRelease(lyr); // done with the layer ref return (numOperations / delta); }
//----------------------------------------------------------------------------- CGLayerRef CGBitmap::createCGLayer (CGContextRef context) { if (layer && !dirty) return layer; CGImageRef image = getCGImage (); layer = image ? CGLayerCreateWithContext (context, CGSizeMake (size.x, size.y), 0) : 0; if (layer) { CGContextRef layerContext = CGLayerGetContext (layer); CGContextDrawImage (layerContext, CGRectMake (0, 0, size.x, size.y), image); } return layer; }
//----------------------------------------------------------------------------- CGLayerRef CGBitmap::createCGLayer (CGContextRef context) { if (layer && !dirty) return layer; CGImageRef image = getCGImage (); layer = image ? CGLayerCreateWithContext (context, CGSizeFromCPoint (size), 0) : 0; if (layer) { CGContextRef layerContext = CGLayerGetContext (layer); CGContextDrawImage (layerContext, CGRectMake (0, 0, static_cast<CGFloat> (size.x), static_cast<CGFloat> (size.y)), image); } return layer; }
static CGLayerRef createLayerWithImageForContext(CGContextRef c, CFURLRef url) { CGSize layerSize; CGLayerRef layer; CGPDFDocumentRef pdfDoc = getThePDFDoc(url, &layerSize.width, &layerSize.height); if(pdfDoc == NULL){ return NULL; } #if DOSCALING // Make the layer 1/3 the size of the PDF document. layerSize.width /= 3; layerSize.height /= 3; #endif // Create the layer to draw into. layer = CGLayerCreateWithContext(c, layerSize, NULL); if(layer == NULL) return NULL; // Get the context corresponding to the layer. Note // that this is a 'Get' function so the code must // not release the context. CGContextRef layerContext = CGLayerGetContext(layer); if(layerContext == NULL){ CGLayerRelease(layer); return NULL; } // Draw the PDF document into the layer. CGContextDrawPDFDocument(layerContext, CGRectMake(0, 0, layerSize.width, layerSize.height), pdfDoc, 1); // Now the layer has the contents needed. return layer; }
CGLayer* CGLayerCreateWithContext_wrap(CGContextRef context, float w, float h) { return CGLayerCreateWithContext( context, CGSizeMake(w, h), NULL); }