/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#[path = "common/boilerplate.rs"] mod boilerplate;
usecrate::boilerplate::{Example, HandyDandyRectBuilder}; use rayon::{ThreadPool, ThreadPoolBuilder}; use rayon::prelude::*; use std::collections::HashMap; use std::sync::Arc; use webrender::api::{self, DisplayListBuilder, DocumentId, PipelineId, PrimitiveFlags}; use webrender::api::{ColorF, CommonItemProperties, SpaceAndClipInfo, ImageDescriptorFlags}; use webrender::api::BlobTilePool; use webrender::api::units::*; use webrender::render_api::*; use webrender::euclid::size2;
// This example shows how to implement a very basic BlobImageHandler that can only render // a checkerboard pattern.
// The deserialized command list internally used by this example is just a color. type ImageRenderingCommands = api::ColorU;
// Serialize/deserialize the blob. // For real usecases you should probably use serde rather than doing it by hand.
// This is the function that applies the deserialized drawing commands and generates // actual image data. fn render_blob(
commands: Arc<ImageRenderingCommands>,
descriptor: &api::BlobImageDescriptor,
tile: TileOffset,
) -> api::BlobImageResult { let color = *commands;
// Note: This implementation ignores the dirty rect which isn't incorrect // but is a missed optimization.
// Allocate storage for the result. Right now the resource cache expects the // tiles to have have no stride or offset. let bpp = 4; letmut texels = Vec::with_capacity((descriptor.rect.area() * bpp) as usize);
// Generate a per-tile pattern to see it in the demo. For a real use case it would not // make sense for the rendered content to depend on its tile. let tile_checker = (tile.x % 2 == 0) != (tile.y % 2 == 0);
let [w, h] = descriptor.rect.size().to_array(); let offset = descriptor.rect.min;
for y in0..h { for x in0..w { // Apply the tile's offset. This is important: all drawing commands should be // translated by this offset to give correct results with tiled blob images. let x2 = x + offset.x; let y2 = y + offset.y;
// Render a simple checkerboard pattern let checker = if (x2 % 20 >= 10) != (y2 % 20 >= 10) { 1
} else { 0
}; // ..nested in the per-tile checkerboard pattern let tc = if tile_checker { 0 } else { (1 - checker) * 40 };
struct CheckerboardRenderer { // We are going to defer the rendering work to worker threads. // Using a pre-built Arc<ThreadPool> rather than creating our own threads // makes it possible to share the same thread pool as the glyph renderer (if we // want to).
workers: Arc<ThreadPool>,
// The deserialized drawing commands. // In this example we store them in Arcs. This isn't necessary since in this simplified // case the command list is a simple 32 bits value and would be cheap to clone before sending // to the workers. But in a more realistic scenario the commands would typically be bigger // and more expensive to clone, so let's pretend it is also the case here.
image_cmds: HashMap<api::BlobImageKey, Arc<ImageRenderingCommands>>,
}
fn update(&mutself, key: api::BlobImageKey, cmds: Arc<api::BlobImageData>,
_visible_rect: &DeviceIntRect, _dirty_rect: &BlobDirtyRect) { // Here, updating is just replacing the current version of the commands with // the new one (no incremental updates). self.image_cmds
.insert(key, Arc::new(deserialize_blob(&cmds[..]).unwrap()));
}
fn main() { let workers =
ThreadPoolBuilder::new().thread_name(|idx| format!("WebRender:Worker#{}", idx))
.build();
let workers = Arc::new(workers.unwrap());
let opts = webrender::WebRenderOptions {
workers: Some(Arc::clone(&workers)), // Register our blob renderer, so that WebRender integrates it in the resource cache.. // Share the same pool of worker threads between WebRender and our blob renderer.
blob_image_handler: Some(Box::new(CheckerboardRenderer::new(Arc::clone(&workers)))),
..Default::default()
};
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.