Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Side by Side Diff: experimental/SkiaExamples/BaseExample.cpp

Issue 16337012: Smallest possible desktop application that uses Skia to render stuff. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Initial support for OSX (not working yet). Code review fixes. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 /*
2 * Copyright 2013 Google Inc.
3 *
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 *
8 */
9
10 #include "BaseExample.h"
11
12 #include "gl/GrGLUtil.h"
13 #include "gl/GrGLDefines.h"
14 #include "gl/GrGLInterface.h"
15 #include "SkApplication.h"
16 #include "SkGpuDevice.h"
17 #include "SkGraphics.h"
18
19 void application_init() {
20 SkGraphics::Init();
21 SkEvent::Init();
22 }
23
24 void application_term() {
25 SkEvent::Term();
26 SkGraphics::Term();
27 }
28
29 BaseExample::BaseExample(void* hWnd, int argc, char** argv)
30 : INHERITED(hWnd)
31 {
32 this->setConfig(SkBitmap::kARGB_8888_Config);
33 this->setVisibleP(true);
34 this->setClipToBounds(false);
35 }
36
37 void BaseExample::tearDownBackend() {
38 if (kGPU_DeviceType == fType) {
39 SkSafeUnref(fContext);
40 fContext = NULL;
41
42 SkSafeUnref(fInterface);
43 fInterface = NULL;
44
45 SkSafeUnref(fRenderTarget);
46 fRenderTarget = NULL;
47
48 detach();
49 }
50 }
51
52 bool BaseExample::setupBackend(DeviceType type) {
53 fType = type;
54
55 if(fType != kGPU_DeviceType) {
56 return true;
57 }
58
59 AttachmentInfo attachmentInfo;
60
61 bool result = attach(kNativeGL_BackEndType, 0 /*msaa*/, &attachmentInfo);
62
63 fInterface = GrGLCreateNativeInterface();
64
65 SkASSERT(NULL != fInterface);
66
67 fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fInterface );
68 SkASSERT(NULL != fContext);
69
70 if (false == result) {
71 SkDebugf("Not possible to create GL backend. Defaulting to raster.");
72 fType = kRaster_DeviceType;
73 detach();
74 return false;
75 }
76
77 GrBackendRenderTargetDesc desc;
78 desc.fWidth = SkScalarRound(width());
79 desc.fHeight = SkScalarRound(height());
80 desc.fConfig = kSkia8888_GrPixelConfig;
81 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
82 desc.fSampleCnt = attachmentInfo.fSampleCount;
83 desc.fStencilBits = attachmentInfo.fStencilBits;
84
85 GrGLint buffer;
86 GR_GL_GetIntegerv(fInterface, GR_GL_FRAMEBUFFER_BINDING, &buffer);
87 desc.fRenderTargetHandle = buffer;
88
89 fRenderTarget = fContext->wrapBackendRenderTarget(desc);
90
91 fContext->setRenderTarget(fRenderTarget);
92
93 return true;
94 }
95
96 SkCanvas* BaseExample::createCanvas() {
97 if (fType == kGPU_DeviceType &&
98 NULL != fContext && NULL != fRenderTarget) {
99 SkAutoTUnref<SkDevice> device(new SkGpuDevice(fContext, fRenderTarget));
100 return new SkCanvas(device);
101 } else {
102 if (fType == kGPU_DeviceType) {
103 tearDownBackend();
104 setupBackend(kRaster_DeviceType);
105 }
106 return INHERITED::createCanvas();
107 }
108 }
109
110 void BaseExample::draw(SkCanvas* canvas) {
111 if (getDeviceType() == kGPU_DeviceType) {
112 SkASSERT(NULL != fContext);
113 fContext->flush();
114 }
115 if (kRaster_DeviceType == getDeviceType()) {
116 // need to send the raster bits to the (gpu) window
117 fContext->setRenderTarget(fRenderTarget);
118 const SkBitmap& bm = getBitmap();
119 fRenderTarget->writePixels(0, 0, bm.width(), bm.height(),
120 kSkia8888_GrPixelConfig,
121 bm.getPixels(),
122 bm.rowBytes());
123 }
124 present();
125 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698