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/SkDevice.h" 10 #include "ui/gfx/rect.h" 11 #include "ui/gfx/skia_util.h" 12 13 namespace cc { 14 15 TestPixelRef::TestPixelRef(int width, int height) 16 : pixels_(new char[4 * width * height]) {} 17 18 TestPixelRef::~TestPixelRef() {} 19 20 SkFlattenable::Factory TestPixelRef::getFactory() { 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(int width, int height) 35 : pixels_(new char[4 * width * height]) {} 36 37 TestLazyPixelRef::~TestLazyPixelRef() {} 38 39 SkFlattenable::Factory TestLazyPixelRef::getFactory() { return NULL; } 40 41 void* TestLazyPixelRef::onLockPixels(SkColorTable** color_table) { 42 return pixels_.get(); 43 } 44 45 bool TestLazyPixelRef::PrepareToDecode(const PrepareParams& params) { 46 return true; 47 } 48 49 bool TestLazyPixelRef::MaybeDecoded() { 50 return true; 51 } 52 53 SkPixelRef* TestLazyPixelRef::deepCopy( 54 SkBitmap::Config config, 55 const SkIRect* subset) { 56 this->ref(); 57 return this; 58 } 59 60 void DrawPicture(unsigned char* buffer, 61 gfx::Rect layer_rect, 62 scoped_refptr<Picture> picture) { 63 SkBitmap bitmap; 64 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 65 layer_rect.width(), 66 layer_rect.height()); 67 bitmap.setPixels(buffer); 68 SkDevice device(bitmap); 69 SkCanvas canvas(&device); 70 canvas.clipRect(gfx::RectToSkRect(layer_rect)); 71 picture->Raster(&canvas, NULL, layer_rect, 1.0f); 72 } 73 74 void CreateBitmap(gfx::Size size, const char* uri, SkBitmap* bitmap) { 75 skia::RefPtr<TestLazyPixelRef> lazy_pixel_ref = 76 skia::AdoptRef(new TestLazyPixelRef(size.width(), size.height())); 77 lazy_pixel_ref->setURI(uri); 78 79 bitmap->setConfig(SkBitmap::kARGB_8888_Config, 80 size.width(), 81 size.height()); 82 bitmap->setPixelRef(lazy_pixel_ref.get()); 83 } 84 85 86 } // namespace cc 87