Home | History | Annotate | Download | only in resources
      1 // Copyright 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "cc/resources/bitmap_skpicture_content_layer_updater.h"
      6 
      7 #include "base/time/time.h"
      8 #include "cc/debug/rendering_stats_instrumentation.h"
      9 #include "cc/resources/layer_painter.h"
     10 #include "cc/resources/prioritized_resource.h"
     11 #include "cc/resources/resource_update_queue.h"
     12 #include "third_party/skia/include/core/SkCanvas.h"
     13 #include "third_party/skia/include/core/SkDevice.h"
     14 
     15 namespace cc {
     16 
     17 BitmapSkPictureContentLayerUpdater::Resource::Resource(
     18     BitmapSkPictureContentLayerUpdater* updater,
     19     scoped_ptr<PrioritizedResource> texture)
     20     : ContentLayerUpdater::Resource(texture.Pass()), updater_(updater) {}
     21 
     22 void BitmapSkPictureContentLayerUpdater::Resource::Update(
     23     ResourceUpdateQueue* queue,
     24     gfx::Rect source_rect,
     25     gfx::Vector2d dest_offset,
     26     bool partial_update) {
     27   bitmap_.setConfig(
     28       SkBitmap::kARGB_8888_Config, source_rect.width(), source_rect.height());
     29   bitmap_.allocPixels();
     30   bitmap_.setIsOpaque(updater_->layer_is_opaque());
     31   SkDevice device(bitmap_);
     32   SkCanvas canvas(&device);
     33   updater_->PaintContentsRect(&canvas, source_rect);
     34 
     35   ResourceUpdate upload = ResourceUpdate::Create(
     36       texture(), &bitmap_, source_rect, source_rect, dest_offset);
     37   if (partial_update)
     38     queue->AppendPartialUpload(upload);
     39   else
     40     queue->AppendFullUpload(upload);
     41 }
     42 
     43 scoped_refptr<BitmapSkPictureContentLayerUpdater>
     44 BitmapSkPictureContentLayerUpdater::Create(
     45     scoped_ptr<LayerPainter> painter,
     46     RenderingStatsInstrumentation* stats_instrumentation,
     47     int layer_id) {
     48   return make_scoped_refptr(
     49       new BitmapSkPictureContentLayerUpdater(painter.Pass(),
     50                                              stats_instrumentation,
     51                                              layer_id));
     52 }
     53 
     54 BitmapSkPictureContentLayerUpdater::BitmapSkPictureContentLayerUpdater(
     55     scoped_ptr<LayerPainter> painter,
     56     RenderingStatsInstrumentation* stats_instrumentation,
     57     int layer_id)
     58     : SkPictureContentLayerUpdater(painter.Pass(),
     59                                    stats_instrumentation,
     60                                    layer_id) {}
     61 
     62 BitmapSkPictureContentLayerUpdater::~BitmapSkPictureContentLayerUpdater() {}
     63 
     64 scoped_ptr<LayerUpdater::Resource>
     65 BitmapSkPictureContentLayerUpdater::CreateResource(
     66     PrioritizedResourceManager* manager) {
     67   return scoped_ptr<LayerUpdater::Resource>(
     68       new Resource(this, PrioritizedResource::Create(manager)));
     69 }
     70 
     71 void BitmapSkPictureContentLayerUpdater::PaintContentsRect(
     72     SkCanvas* canvas,
     73     gfx::Rect source_rect) {
     74   // Translate the origin of content_rect to that of source_rect.
     75   canvas->translate(content_rect().x() - source_rect.x(),
     76                     content_rect().y() - source_rect.y());
     77   base::TimeTicks start_time =
     78       rendering_stats_instrumentation_->StartRecording();
     79   DrawPicture(canvas);
     80   base::TimeDelta duration =
     81       rendering_stats_instrumentation_->EndRecording(start_time);
     82   rendering_stats_instrumentation_->AddRaster(
     83       duration,
     84       duration,
     85       source_rect.width() * source_rect.height(),
     86       false);
     87 }
     88 
     89 }  // namespace cc
     90