Home | History | Annotate | Download | only in aura
      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.
      4 
      5 #include "content/browser/aura/software_output_device_win.h"
      6 
      7 #include "content/public/browser/browser_thread.h"
      8 #include "third_party/skia/include/core/SkBitmap.h"
      9 #include "third_party/skia/include/core/SkDevice.h"
     10 #include "ui/compositor/compositor.h"
     11 #include "ui/gfx/canvas.h"
     12 #include "ui/gfx/canvas_skia_paint.h"
     13 #include "ui/gfx/gdi_util.h"
     14 #include "ui/gfx/skia_util.h"
     15 
     16 namespace content {
     17 
     18 SoftwareOutputDeviceWin::SoftwareOutputDeviceWin(ui::Compositor* compositor)
     19     : hwnd_(compositor->widget()),
     20       is_hwnd_composited_(false) {
     21   // TODO(skaslev) Remove this when crbug.com/180702 is fixed.
     22   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     23 
     24   LONG style = GetWindowLong(hwnd_, GWL_EXSTYLE);
     25   is_hwnd_composited_ = !!(style & WS_EX_COMPOSITED);
     26 }
     27 
     28 SoftwareOutputDeviceWin::~SoftwareOutputDeviceWin() {
     29   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     30 }
     31 
     32 void SoftwareOutputDeviceWin::Resize(gfx::Size viewport_size) {
     33   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     34 
     35   if (viewport_size_ == viewport_size)
     36     return;
     37 
     38   viewport_size_ = viewport_size;
     39   contents_.reset(new gfx::Canvas(viewport_size, 1.0f, true));
     40   memset(&bitmap_info_, 0, sizeof(bitmap_info_));
     41   gfx::CreateBitmapHeader(viewport_size_.width(), viewport_size_.height(),
     42                           &bitmap_info_.bmiHeader);
     43 }
     44 
     45 SkCanvas* SoftwareOutputDeviceWin::BeginPaint(gfx::Rect damage_rect) {
     46   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     47   DCHECK(contents_);
     48 
     49   damage_rect_ = damage_rect;
     50   return contents_ ? contents_->sk_canvas() : NULL;
     51 }
     52 
     53 void SoftwareOutputDeviceWin::EndPaint(cc::SoftwareFrameData* frame_data) {
     54   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     55   DCHECK(contents_);
     56   DCHECK(frame_data);
     57 
     58   if (!contents_)
     59     return;
     60 
     61   SoftwareOutputDevice::EndPaint(frame_data);
     62 
     63   gfx::Rect rect = damage_rect_;
     64   rect.Intersect(gfx::Rect(viewport_size_));
     65   if (rect.IsEmpty())
     66     return;
     67 
     68   SkCanvas* canvas = contents_->sk_canvas();
     69   DCHECK(canvas);
     70   if (is_hwnd_composited_) {
     71     RECT wr;
     72     GetWindowRect(hwnd_, &wr);
     73     SIZE size = {wr.right - wr.left, wr.bottom - wr.top};
     74     POINT position = {wr.left, wr.top};
     75     POINT zero = {0, 0};
     76     BLENDFUNCTION blend = {AC_SRC_OVER, 0x00, 0xFF, AC_SRC_ALPHA};
     77 
     78     DWORD style = GetWindowLong(hwnd_, GWL_EXSTYLE);
     79     style &= ~WS_EX_COMPOSITED;
     80     style |= WS_EX_LAYERED;
     81     SetWindowLong(hwnd_, GWL_EXSTYLE, style);
     82 
     83     HDC dib_dc = skia::BeginPlatformPaint(canvas);
     84     ::UpdateLayeredWindow(hwnd_, NULL, &position, &size, dib_dc, &zero,
     85                           RGB(0xFF, 0xFF, 0xFF), &blend, ULW_ALPHA);
     86     skia::EndPlatformPaint(canvas);
     87   } else {
     88     HDC hdc = ::GetDC(hwnd_);
     89     RECT src_rect = rect.ToRECT();
     90     skia::DrawToNativeContext(canvas, hdc, rect.x(), rect.y(), &src_rect);
     91     ::ReleaseDC(hwnd_, hdc);
     92   }
     93 }
     94 
     95 void SoftwareOutputDeviceWin::CopyToBitmap(
     96     gfx::Rect rect, SkBitmap* output) {
     97   DCHECK(contents_);
     98   SkBaseDevice* device = contents_->sk_canvas()->getDevice();
     99   const SkBitmap& bitmap = device->accessBitmap(false);
    100   bitmap.extractSubset(output, gfx::RectToSkIRect(rect));
    101 }
    102 
    103 }  // namespace content
    104