Convert cocoa to use bitmap render API

This commit is contained in:
Vincent Sanders 2015-04-24 15:46:40 +01:00
parent 2df03e2d85
commit 596a62e20e
3 changed files with 84 additions and 101 deletions

View File

@ -87,7 +87,6 @@ S_COCOA := \
plotter.m \ plotter.m \
schedule.m \ schedule.m \
selection.m \ selection.m \
thumbnail.m \
utils.m \ utils.m \
ArrowBox.m \ ArrowBox.m \
ArrowWindow.m \ ArrowWindow.m \

View File

@ -16,11 +16,21 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
/**
* \file
* Cocoa implementation of bitmap operations.
*/
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#import "cocoa/bitmap.h" #import "desktop/browser.h"
#import "desktop/plotters.h"
#import "image/bitmap.h" #import "image/bitmap.h"
#import "content/urldb.h"
#import "content/content.h"
#import "cocoa/plotter.h"
#import "cocoa/bitmap.h"
#define BITS_PER_SAMPLE (8) #define BITS_PER_SAMPLE (8)
#define SAMPLES_PER_PIXEL (4) #define SAMPLES_PER_PIXEL (4)
@ -72,7 +82,8 @@ void bitmap_destroy(void *bitmap)
void *bitmap_create(int width, int height, unsigned int state) void *bitmap_create(int width, int height, unsigned int state)
{ {
NSBitmapImageRep *bmp = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL NSBitmapImageRep *bmp = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes: NULL
pixelsWide: width pixelsWide: width
pixelsHigh: height pixelsHigh: height
bitsPerSample: BITS_PER_SAMPLE bitsPerSample: BITS_PER_SAMPLE
@ -215,6 +226,43 @@ static CGImageRef cocoa_prepare_bitmap( void *bitmap )
return result; return result;
} }
static nserror bitmap_render(struct bitmap *bitmap, struct hlcache_handle *content)
{
int bwidth = bitmap_get_width( bitmap );
int bheight = bitmap_get_height( bitmap );
struct redraw_context ctx = {
.interactive = false,
.background_images = true,
.plot = &cocoa_plotters
};
CGColorSpaceRef cspace = CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB );
CGContextRef bitmapContext = CGBitmapContextCreate( bitmap_get_buffer( bitmap ),
bwidth, bheight,
bitmap_get_bpp( bitmap ) * 8 / 4,
bitmap_get_rowstride( bitmap ),
cspace, kCGImageAlphaNoneSkipLast );
CGColorSpaceRelease( cspace );
size_t width = MIN( content_get_width( content ), 1024 );
size_t height = ((width * bheight) + bwidth / 2) / bwidth;
CGContextTranslateCTM( bitmapContext, 0, bheight );
CGContextScaleCTM( bitmapContext, (CGFloat)bwidth / width, -(CGFloat)bheight / height );
[NSGraphicsContext setCurrentContext: [NSGraphicsContext graphicsContextWithGraphicsPort: bitmapContext flipped: YES]];
content_scaled_redraw( content, width, height, &ctx );
[NSGraphicsContext setCurrentContext: nil];
CGContextRelease( bitmapContext );
bitmap_modified( bitmap );
return true;
}
static struct gui_bitmap_table bitmap_table = { static struct gui_bitmap_table bitmap_table = {
.create = bitmap_create, .create = bitmap_create,
.destroy = bitmap_destroy, .destroy = bitmap_destroy,
@ -228,6 +276,7 @@ static struct gui_bitmap_table bitmap_table = {
.get_bpp = bitmap_get_bpp, .get_bpp = bitmap_get_bpp,
.save = bitmap_save, .save = bitmap_save,
.modified = bitmap_modified, .modified = bitmap_modified,
.render = bitmap_render,
}; };
struct gui_bitmap_table *cocoa_bitmap_table = &bitmap_table; struct gui_bitmap_table *cocoa_bitmap_table = &bitmap_table;

View File

@ -1,65 +0,0 @@
/*
* Copyright 2011 Sven Weidauer <sven.weidauer@gmail.com>
*
* This file is part of NetSurf, http://www.netsurf-browser.org/
*
* NetSurf is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* NetSurf 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#import <Cocoa/Cocoa.h>
#import "desktop/browser.h"
#import "desktop/plotters.h"
#import "desktop/thumbnail.h"
#import "content/urldb.h"
#import "cocoa/plotter.h"
#import "image/bitmap.h"
/* In platform specific thumbnail.c. */
bool thumbnail_create(struct hlcache_handle *content, struct bitmap *bitmap)
{
int bwidth = bitmap_get_width( bitmap );
int bheight = bitmap_get_height( bitmap );
struct redraw_context ctx = {
.interactive = false,
.background_images = true,
.plot = &cocoa_plotters
};
CGColorSpaceRef cspace = CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB );
CGContextRef bitmapContext = CGBitmapContextCreate( bitmap_get_buffer( bitmap ),
bwidth, bheight,
bitmap_get_bpp( bitmap ) * 8 / 4,
bitmap_get_rowstride( bitmap ),
cspace, kCGImageAlphaNoneSkipLast );
CGColorSpaceRelease( cspace );
size_t width = MIN( content_get_width( content ), 1024 );
size_t height = ((width * bheight) + bwidth / 2) / bwidth;
CGContextTranslateCTM( bitmapContext, 0, bheight );
CGContextScaleCTM( bitmapContext, (CGFloat)bwidth / width, -(CGFloat)bheight / height );
[NSGraphicsContext setCurrentContext: [NSGraphicsContext graphicsContextWithGraphicsPort: bitmapContext flipped: YES]];
thumbnail_redraw( content, width, height, &ctx );
[NSGraphicsContext setCurrentContext: nil];
CGContextRelease( bitmapContext );
bitmap_modified( bitmap );
return true;
}