Home | History | Annotate | Download | only in test
      1 // Copyright 2012 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/test/skia_common.h"
      6 
      7 #include "cc/resources/picture.h"
      8 #include "skia/ext/refptr.h"
      9 #include "third_party/skia/include/core/SkBitmapDevice.h"
     10 #include "ui/gfx/rect.h"
     11 #include "ui/gfx/skia_util.h"
     12 
     13 namespace cc {
     14 
     15 TestPixelRef::TestPixelRef(const SkImageInfo& info)
     16     : SkPixelRef(info), pixels_(new char[4 * info.fWidth * info.fHeight]) {}
     17 
     18 TestPixelRef::~TestPixelRef() {}
     19 
     20 SkFlattenable::Factory TestPixelRef::getFactory() const { return NULL; }
     21 
     22 void* TestPixelRef::onLockPixels(SkColorTable** color_table) {
     23   return pixels_.get();
     24 }
     25 
     26 SkPixelRef* TestPixelRef::deepCopy(
     27     SkBitmap::Config config,
     28     const SkIRect* subset) {
     29   this->ref();
     30   return this;
     31 }
     32 
     33 
     34 TestLazyPixelRef::TestLazyPixelRef(const SkImageInfo& info)
     35     : skia::LazyPixelRef(info),
     36       pixels_(new char[4 * info.fWidth * info.fHeight]) {}
     37 
     38 TestLazyPixelRef::~TestLazyPixelRef() {}
     39 
     40 SkFlattenable::Factory TestLazyPixelRef::getFactory() const { return NULL; }
     41 
     42 void* TestLazyPixelRef::onLockPixels(SkColorTable** color_table) {
     43   return pixels_.get();
     44 }
     45 
     46 bool TestLazyPixelRef::PrepareToDecode(const PrepareParams& params) {
     47   return true;
     48 }
     49 
     50 bool TestLazyPixelRef::MaybeDecoded() {
     51   return true;
     52 }
     53 
     54 SkPixelRef* TestLazyPixelRef::deepCopy(
     55     SkBitmap::Config config,
     56     const SkIRect* subset) {
     57   this->ref();
     58   return this;
     59 }
     60 
     61 void DrawPicture(unsigned char* buffer,
     62                  gfx::Rect layer_rect,
     63                  scoped_refptr<Picture> picture) {
     64   SkBitmap bitmap;
     65   bitmap.setConfig(SkBitmap::kARGB_8888_Config,
     66                    layer_rect.width(),
     67                    layer_rect.height());
     68   bitmap.setPixels(buffer);
     69   SkBitmapDevice device(bitmap);
     70   SkCanvas canvas(&device);
     71   canvas.clipRect(gfx::RectToSkRect(layer_rect));
     72   picture->Raster(&canvas, NULL, layer_rect, 1.0f);
     73 }
     74 
     75 void CreateBitmap(gfx::Size size, const char* uri, SkBitmap* bitmap) {
     76   SkImageInfo info = {
     77     size.width(),
     78     size.height(),
     79     kPMColor_SkColorType,
     80     kPremul_SkAlphaType
     81   };
     82 
     83   skia::RefPtr<TestLazyPixelRef> lazy_pixel_ref =
     84       skia::AdoptRef(new TestLazyPixelRef(info));
     85   lazy_pixel_ref->setURI(uri);
     86 
     87   bitmap->setConfig(info);
     88   bitmap->setPixelRef(lazy_pixel_ref.get());
     89 }
     90 
     91 
     92 }  // namespace cc
     93