Home | History | Annotate | Download | only in gfx
      1 // Copyright (c) 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 "base/basictypes.h"
      6 #include "base/compiler_specific.h"
      7 #include "ui/gfx/canvas.h"
      8 #include "ui/gfx/canvas_skia_paint.h"
      9 #include "ui/gfx/rect.h"
     10 
     11 namespace gfx {
     12 
     13 CanvasSkiaPaint::CanvasSkiaPaint(HWND hwnd, HDC dc, const PAINTSTRUCT& ps)
     14     : hwnd_(hwnd),
     15       paint_dc_(dc) {
     16   memset(&ps_, 0, sizeof(ps_));
     17   ps_.rcPaint.left = ps.rcPaint.left;
     18   ps_.rcPaint.right = ps.rcPaint.right;
     19   ps_.rcPaint.top = ps.rcPaint.top;
     20   ps_.rcPaint.bottom = ps.rcPaint.bottom;
     21   Init(true);
     22 }
     23 
     24 CanvasSkiaPaint::CanvasSkiaPaint(HDC dc, bool opaque, int x, int y,
     25                                  int w, int h)
     26     : hwnd_(NULL),
     27       paint_dc_(dc) {
     28   memset(&ps_, 0, sizeof(ps_));
     29   ps_.rcPaint.left = x;
     30   ps_.rcPaint.right = x + w;
     31   ps_.rcPaint.top = y;
     32   ps_.rcPaint.bottom = y + h;
     33   Init(opaque);
     34 }
     35 
     36 CanvasSkiaPaint::~CanvasSkiaPaint() {
     37   if (!is_empty()) {
     38     skia::PlatformCanvas* canvas = platform_canvas();
     39     canvas->restoreToCount(1);
     40     // Commit the drawing to the screen
     41     skia::DrawToNativeContext(canvas, paint_dc_, ps_.rcPaint.left,
     42                               ps_.rcPaint.top, NULL);
     43   }
     44 }
     45 
     46 gfx::Rect CanvasSkiaPaint::GetInvalidRect() const {
     47   return gfx::Rect(paint_struct().rcPaint);
     48 }
     49 
     50 void CanvasSkiaPaint::Init(bool opaque) {
     51   // FIXME(brettw) for ClearType, we probably want to expand the bounds of
     52   // painting by one pixel so that the boundaries will be correct (ClearType
     53   // text can depend on the adjacent pixel). Then we would paint just the
     54   // inset pixels to the screen.
     55   const int width = ps_.rcPaint.right - ps_.rcPaint.left;
     56   const int height = ps_.rcPaint.bottom - ps_.rcPaint.top;
     57 
     58   RecreateBackingCanvas(gfx::Size(width, height),
     59       gfx::win::GetDeviceScaleFactor(),
     60       opaque);
     61   skia::PlatformCanvas* canvas = platform_canvas();
     62 
     63   canvas->clear(SkColorSetARGB(0, 0, 0, 0));
     64 
     65   // This will bring the canvas into the screen coordinate system for the
     66   // dirty rect
     67   canvas->translate(
     68       -ps_.rcPaint.left / gfx::win::GetDeviceScaleFactor(),
     69       -ps_.rcPaint.top / gfx::win::GetDeviceScaleFactor());
     70 }
     71 
     72 }  // namespace gfx
     73