1 // Copyright 2013 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.
5 #include "ui/gfx/ozone/impl/software_surface_ozone.h"
12 #include "base/logging.h"
13 #include "third_party/skia/include/core/SkBitmap.h"
14 #include "third_party/skia/include/core/SkBitmapDevice.h"
15 #include "third_party/skia/include/core/SkCanvas.h"
16 #include "ui/gfx/ozone/impl/drm_skbitmap_ozone.h"
17 #include "ui/gfx/ozone/impl/hardware_display_controller_ozone.h"
18 #include "ui/gfx/skia_util.h"
24 // Extends the SkBitmapDevice to allow setting the SkPixelRef. We use the setter
25 // to change the SkPixelRef such that the device always points to the
27 class CustomSkBitmapDevice : public SkBitmapDevice {
29 CustomSkBitmapDevice(const SkBitmap& bitmap) : SkBitmapDevice(bitmap) {}
30 virtual ~CustomSkBitmapDevice() {}
32 void SetPixelRef(SkPixelRef* pixel_ref) { setPixelRef(pixel_ref, 0); }
35 DISALLOW_COPY_AND_ASSIGN(CustomSkBitmapDevice);
40 ////////////////////////////////////////////////////////////////////////////////
41 // SoftwareSurfaceOzone implementation
43 SoftwareSurfaceOzone::SoftwareSurfaceOzone(
44 HardwareDisplayControllerOzone* controller)
45 : controller_(controller),
50 SoftwareSurfaceOzone::~SoftwareSurfaceOzone() {
53 bool SoftwareSurfaceOzone::Initialize() {
54 for (int i = 0; i < 2; ++i) {
55 bitmaps_[i].reset(CreateBuffer());
56 // TODO(dnicoara) Should select the configuration based on what the
57 // underlying system supports.
58 bitmaps_[i]->setConfig(SkBitmap::kARGB_8888_Config,
59 controller_->get_mode().hdisplay,
60 controller_->get_mode().vdisplay);
62 if (!bitmaps_[i]->Initialize()) {
67 skia_device_ = skia::AdoptRef(
68 new CustomSkBitmapDevice(*bitmaps_[front_buffer_ ^ 1].get()));
69 skia_canvas_ = skia::AdoptRef(new SkCanvas(skia_device_.get()));
74 uint32_t SoftwareSurfaceOzone::GetFramebufferId() const {
75 CHECK(bitmaps_[0].get() && bitmaps_[1].get());
76 return bitmaps_[front_buffer_ ^ 1]->get_framebuffer();
79 // This call is made after the hardware just started displaying our back buffer.
80 // We need to update our pointer reference and synchronize the two buffers.
81 void SoftwareSurfaceOzone::SwapBuffers() {
82 CHECK(bitmaps_[0].get() && bitmaps_[1].get());
84 // Update our front buffer pointer.
87 // Unlocking will unset the pixel pointer, so it won't be pointing to the old
89 skia_device_->accessBitmap(false).unlockPixels();
90 // Update the backing pixels for the bitmap device.
91 static_cast<CustomSkBitmapDevice*>(skia_device_.get())->SetPixelRef(
92 bitmaps_[front_buffer_ ^ 1]->pixelRef());
93 // Locking the pixels will set the pixel pointer based on the PixelRef value.
94 skia_device_->accessBitmap(false).lockPixels();
96 SkIRect device_damage;
97 skia_canvas_->getClipDeviceBounds(&device_damage);
98 SkRect damage = SkRect::Make(device_damage);
100 skia_canvas_->drawBitmapRectToRect(*bitmaps_[front_buffer_].get(),
105 SkCanvas* SoftwareSurfaceOzone::GetDrawableForWidget() {
106 return skia_canvas_.get();
109 DrmSkBitmapOzone* SoftwareSurfaceOzone::CreateBuffer() {
110 return new DrmSkBitmapOzone(controller_->get_fd());